Program.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 (!colum.Value.TryGetValue(0, out string? key) || string.IsNullOrEmpty(key))
  50. continue;
  51. if (!byte.TryParse(key, out byte mini8Index))
  52. continue;
  53. if (!mini8Configs.TryGetValue(mini8Index, out Mini8ConfigXML? mini8Config))
  54. {
  55. mini8Config = new()
  56. {
  57. Index = mini8Index,
  58. ChannelConfig = []
  59. };
  60. mini8Configs[mini8Index] = mini8Config;
  61. }
  62. ChannelConfig channel = GetChannelConfig(colum.Value, numerator, denominator);
  63. channel.CapsWarning = (channel.Caps + channel.SetPoint) / 2;
  64. channel.FloorWarning = (channel.Floor + channel.SetPoint) / 2;
  65. mini8Config.ChannelConfig!.Add(channel);
  66. }
  67. configSaveXML.Mini8Configs.AddRange(mini8Configs.Values);
  68. string outputPath = Path.Combine(Path.GetDirectoryName(path)!, $"{row_colum.Key}.mc");
  69. XmlFileHelper.WriteFile(outputPath, configSaveXML);
  70. }
  71. goto Start;
  72. }
  73. private static ChannelConfig GetChannelConfig(Dictionary<int, string> channel, int numerator, int denominator)
  74. {
  75. string[] content = [.. channel.Values];
  76. ChannelConfig config = new()
  77. {
  78. Index = byte.Parse(content[1])
  79. };
  80. if (float.TryParse(content[2], out float setPoint))
  81. {
  82. Console.WriteLine(setPoint);
  83. if (setPoint > 60)
  84. config.SetPoint = setPoint / denominator * numerator;
  85. else
  86. config.SetPoint = setPoint;
  87. }
  88. if (numerator != 1 || denominator != 1)
  89. {
  90. config.Floor = config.SetPoint - 10;
  91. config.Caps = config.SetPoint + 10;
  92. }
  93. else
  94. {
  95. if (float.TryParse(content[4], out float floor))
  96. config.Floor = floor;
  97. if (float.TryParse(content[5], out float caps))
  98. config.Caps = caps;
  99. }
  100. config.Caps = config.Caps < 0 ? 0 : config.Caps;
  101. config.Floor = config.Floor < 0 ? 0 : config.Floor;
  102. config.CapsWarning = config.CapsWarning < 0 ? 0 : config.CapsWarning;
  103. config.FloorWarning = config.FloorWarning < 0 ? 0 : config.FloorWarning;
  104. config.Caps = (float)Math.Round(config.Caps, 0);
  105. config.Floor = (float)Math.Round(config.Floor, 0);
  106. config.CapsWarning = (float)Math.Round(config.CapsWarning, 0);
  107. config.FloorWarning = (float)Math.Round(config.FloorWarning, 0);
  108. if (float.TryParse(content[6], out float p))
  109. config.Running_P = p;
  110. if (float.TryParse(content[7], out float i))
  111. config.Running_I = i;
  112. if (float.TryParse(content[8], out float d))
  113. config.Running_D = d;
  114. config.ActiveTuneSet = ActiveTuneSet.Running;
  115. config.Inhibit = content[9] switch
  116. {
  117. "Enable" => Inhibit.Enable,
  118. _ => Inhibit.Disable
  119. };
  120. config.ChannelMode = content[2] switch
  121. {
  122. "moni" => ChannelMode.Monitor,
  123. _ => ChannelMode.Control,
  124. };
  125. if (config.Inhibit == Inhibit.Disable)
  126. config.ChannelMode = ChannelMode.UnUsed;
  127. if (content.Length < 11)
  128. return config;
  129. if (float.TryParse(content[10], out float upRate))
  130. config.SetpointUpRate = upRate;
  131. if (float.TryParse(content[11], out float downRate))
  132. config.SetpointDownRate = downRate;
  133. return config;
  134. }
  135. private static ChannelConfig GetChannelConfig(Dictionary<int, string> channel)
  136. {
  137. string[] content = [.. channel.Values];
  138. ChannelConfig config = new()
  139. {
  140. Index = byte.Parse(content[1])
  141. };
  142. if (float.TryParse(content[2], out float setPoint))
  143. config.SetPoint = setPoint;
  144. if (float.TryParse(content[4], out float floor))
  145. config.Floor = floor;
  146. if (float.TryParse(content[5], out float caps))
  147. config.Caps = caps;
  148. if (float.TryParse(content[6], out float p))
  149. config.Running_P = p;
  150. if (float.TryParse(content[7], out float i))
  151. config.Running_I = i;
  152. if (float.TryParse(content[8], out float d))
  153. config.Running_D = d;
  154. config.ActiveTuneSet = ActiveTuneSet.Running;
  155. config.Inhibit = content[9] switch
  156. {
  157. "Enable" => Inhibit.Enable,
  158. _ => Inhibit.Disable
  159. };
  160. config.ChannelMode = content[2] switch
  161. {
  162. "moni" => ChannelMode.Monitor,
  163. _ => ChannelMode.Control,
  164. };
  165. if (config.Inhibit == Inhibit.Disable)
  166. config.ChannelMode = ChannelMode.UnUsed;
  167. if (content.Length < 11)
  168. return config;
  169. if (float.TryParse(content[10], out float upRate))
  170. config.SetpointUpRate = upRate;
  171. if (float.TryParse(content[11], out float downRate))
  172. config.SetpointDownRate = downRate;
  173. return config;
  174. }
  175. }
  176. public class ExcelReader
  177. {
  178. public static Dictionary<string, Dictionary<int, Dictionary<int, string>>> Read(string filePath, int startRow)
  179. {
  180. Dictionary<string, Dictionary<int, Dictionary<int, string>>> sheets = [];
  181. using FileStream file = new(filePath, FileMode.Open, FileAccess.Read);
  182. for (int i = 0; ; i++)
  183. {
  184. ISheet sheet;
  185. try
  186. {
  187. sheet = new XSSFWorkbook(file).GetSheetAt(i);
  188. if (sheet is null)
  189. break;
  190. }
  191. catch
  192. {
  193. break;
  194. }
  195. Dictionary<int, Dictionary<int, string>> contents = [];
  196. sheets[sheet.SheetName] = contents;
  197. for (int row = startRow; row <= sheet.LastRowNum; row++)
  198. {
  199. if (sheet.GetRow(row) is not IRow currentRow)
  200. continue;
  201. contents[row] = [];
  202. for (int colum = 0; colum < currentRow.LastCellNum; colum++)
  203. {
  204. ICell cell = currentRow.GetCell(colum);
  205. contents[row][colum] = cell?.ToString() ?? string.Empty;
  206. }
  207. }
  208. }
  209. return sheets;
  210. }
  211. }