Program.cs 8.1 KB

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