Program.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using ConfigOperator;
  2. using GeneralData;
  3. using NPOI.SS.UserModel;
  4. using NPOI.XSSF.UserModel;
  5. using TemperatureConfigFile;
  6. using Universal;
  7. namespace ToMcFile;
  8. internal class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. Start:
  13. string? path;
  14. Console.WriteLine("Please drag file into console and press <Enter>");
  15. path = Console.ReadLine();
  16. path = path?.Trim('"');
  17. if (string.IsNullOrEmpty(path) || !File.Exists(path))
  18. {
  19. Console.WriteLine("File Not Exist");
  20. goto Start;
  21. }
  22. Console.WriteLine("Input Numerator:");
  23. string? numeratorString = Console.ReadLine();
  24. if (string.IsNullOrEmpty(numeratorString) || !int.TryParse(numeratorString, out int numerator) || numerator < 0)
  25. {
  26. Console.WriteLine("numerator Error");
  27. goto Start;
  28. }
  29. Console.WriteLine("Input Denominator:");
  30. string? denominatorString = Console.ReadLine();
  31. if (string.IsNullOrEmpty(denominatorString) || !int.TryParse(denominatorString, out int denominator) || denominator < 0)
  32. {
  33. Console.WriteLine("denominator Error");
  34. goto Start;
  35. }
  36. Dictionary<string, Dictionary<int, Dictionary<int, string>>> sheet_row_colum = ExcelReader.Read(path, 1);
  37. foreach (var row_colum in sheet_row_colum)
  38. {
  39. ConfigSaveXML configSaveXML = new()
  40. {
  41. EditTime = DateTime.Now,
  42. Editor = "Undefined",
  43. Description = "Undefined",
  44. Mini8Configs = []
  45. };
  46. Dictionary<byte, Mini8ConfigXML> mini8Configs = [];
  47. foreach (var colum in row_colum.Value)
  48. {
  49. if (!byte.TryParse(colum.Value[0], out byte mini8Index))
  50. continue;
  51. if (!mini8Configs.TryGetValue(mini8Index, out Mini8ConfigXML? mini8Config))
  52. {
  53. mini8Config = new()
  54. {
  55. Index = mini8Index,
  56. ChannelConfig = []
  57. };
  58. mini8Configs[mini8Index] = mini8Config;
  59. }
  60. ChannelConfig channel = GetChannelConfig(colum.Value, numerator, denominator);
  61. channel.CapsWarning = (channel.Caps + channel.SetPoint) / 2;
  62. channel.FloorWarning = (channel.Floor + channel.SetPoint) / 2;
  63. mini8Config.ChannelConfig!.Add(channel);
  64. }
  65. configSaveXML.Mini8Configs.AddRange(mini8Configs.Values);
  66. string outputPath = Path.Combine(Path.GetDirectoryName(path)!, $"{row_colum.Key}.mc");
  67. XmlFileHelper.WriteFile(outputPath, configSaveXML);
  68. }
  69. goto Start;
  70. }
  71. private static ChannelConfig GetChannelConfig(Dictionary<int, string> channel, int numerator, int denominator)
  72. {
  73. string[] content = [.. channel.Values];
  74. ChannelConfig config = new()
  75. {
  76. Index = byte.Parse(content[1])
  77. };
  78. if (float.TryParse(content[2], out float setPoint))
  79. {
  80. Console.WriteLine(setPoint);
  81. if (setPoint > 60)
  82. config.SetPoint = setPoint / denominator * numerator;
  83. else
  84. config.SetPoint = setPoint;
  85. }
  86. if (numerator != 1 || denominator != 1)
  87. {
  88. config.Floor = config.SetPoint - 10;
  89. config.Caps = config.SetPoint + 10;
  90. }
  91. else
  92. {
  93. if (float.TryParse(content[4], out float floor))
  94. config.Floor = floor;
  95. if (float.TryParse(content[5], out float caps))
  96. config.Caps = caps;
  97. }
  98. config.Caps = config.Caps < 0 ? 0 : config.Caps;
  99. config.Floor = config.Floor < 0 ? 0 : config.Floor;
  100. config.CapsWarning = config.CapsWarning < 0 ? 0 : config.CapsWarning;
  101. config.FloorWarning = config.FloorWarning < 0 ? 0 : config.FloorWarning;
  102. config.Caps = (float)Math.Round(config.Caps, 0);
  103. config.Floor = (float)Math.Round(config.Floor, 0);
  104. config.CapsWarning = (float)Math.Round(config.CapsWarning, 0);
  105. config.FloorWarning = (float)Math.Round(config.FloorWarning, 0);
  106. if (float.TryParse(content[6], out float p))
  107. config.Running_P = p;
  108. if (float.TryParse(content[7], out float i))
  109. config.Running_I = i;
  110. if (float.TryParse(content[8], out float d))
  111. config.Running_D = d;
  112. config.ActiveTuneSet = ActiveTuneSet.Running;
  113. config.Inhibit = content[9] switch
  114. {
  115. "Enable" => Inhibit.Enable,
  116. _ => Inhibit.Disable
  117. };
  118. config.ChannelMode = content[2] switch
  119. {
  120. "moni" => ChannelMode.Monitor,
  121. _ => ChannelMode.Control,
  122. };
  123. if (config.Inhibit == Inhibit.Disable)
  124. config.ChannelMode = ChannelMode.UnUsed;
  125. if (content.Length < 11)
  126. return config;
  127. if (float.TryParse(content[10], out float upRate))
  128. config.SetpointUpRate = upRate;
  129. if (float.TryParse(content[11], out float downRate))
  130. config.SetpointDownRate = downRate;
  131. return config;
  132. }
  133. private static ChannelConfig GetChannelConfig(Dictionary<int, string> channel)
  134. {
  135. string[] content = [.. channel.Values];
  136. ChannelConfig config = new()
  137. {
  138. Index = byte.Parse(content[1])
  139. };
  140. if (float.TryParse(content[2], out float setPoint))
  141. config.SetPoint = setPoint;
  142. if (float.TryParse(content[4], out float floor))
  143. config.Floor = floor;
  144. if (float.TryParse(content[5], out float caps))
  145. config.Caps = caps;
  146. if (float.TryParse(content[6], out float p))
  147. config.Running_P = p;
  148. if (float.TryParse(content[7], out float i))
  149. config.Running_I = i;
  150. if (float.TryParse(content[8], out float d))
  151. config.Running_D = d;
  152. config.ActiveTuneSet = ActiveTuneSet.Running;
  153. config.Inhibit = content[9] switch
  154. {
  155. "Enable" => Inhibit.Enable,
  156. _ => Inhibit.Disable
  157. };
  158. config.ChannelMode = content[2] switch
  159. {
  160. "moni" => ChannelMode.Monitor,
  161. _ => ChannelMode.Control,
  162. };
  163. if (config.Inhibit == Inhibit.Disable)
  164. config.ChannelMode = ChannelMode.UnUsed;
  165. if (content.Length < 11)
  166. return config;
  167. if (float.TryParse(content[10], out float upRate))
  168. config.SetpointUpRate = upRate;
  169. if (float.TryParse(content[11], out float downRate))
  170. config.SetpointDownRate = downRate;
  171. return config;
  172. }
  173. }
  174. public class ExcelReader
  175. {
  176. public static Dictionary<string, Dictionary<int, Dictionary<int, string>>> Read(string filePath, int startRow)
  177. {
  178. Dictionary<string, Dictionary<int, Dictionary<int, string>>> sheets = [];
  179. using FileStream file = new(filePath, FileMode.Open, FileAccess.Read);
  180. for (int i = 0; ; i++)
  181. {
  182. ISheet sheet;
  183. try
  184. {
  185. sheet = new XSSFWorkbook(file).GetSheetAt(i);
  186. if (sheet is null)
  187. break;
  188. }
  189. catch
  190. {
  191. break;
  192. }
  193. Dictionary<int, Dictionary<int, string>> contents = [];
  194. sheets[sheet.SheetName] = contents;
  195. for (int row = startRow; row <= sheet.LastRowNum; row++)
  196. {
  197. if (sheet.GetRow(row) is not IRow currentRow)
  198. continue;
  199. contents[row] = [];
  200. for (int colum = 0; colum < currentRow.LastCellNum; colum++)
  201. {
  202. ICell cell = currentRow.GetCell(colum);
  203. contents[row][colum] = cell?.ToString() ?? string.Empty;
  204. }
  205. }
  206. }
  207. return sheets;
  208. }
  209. }