Program.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. if (float.TryParse(content[6], out float p))
  99. config.Running_P = p;
  100. if (float.TryParse(content[7], out float i))
  101. config.Running_I = i;
  102. if (float.TryParse(content[8], out float d))
  103. config.Running_D = d;
  104. config.ActiveTuneSet = ActiveTuneSet.Running;
  105. config.Inhibit = content[9] switch
  106. {
  107. "Enable" => Inhibit.Enable,
  108. _ => Inhibit.Disable
  109. };
  110. config.ChannelMode = content[2] switch
  111. {
  112. "moni" => ChannelMode.Monitor,
  113. _ => ChannelMode.Control,
  114. };
  115. if (config.Inhibit == Inhibit.Disable)
  116. config.ChannelMode = ChannelMode.UnUsed;
  117. if (content.Length < 11)
  118. return config;
  119. if (float.TryParse(content[10], out float upRate))
  120. config.SetpointUpRate = upRate;
  121. if (float.TryParse(content[11], out float downRate))
  122. config.SetpointDownRate = downRate;
  123. return config;
  124. }
  125. private static ChannelConfig GetChannelConfig(Dictionary<int, string> channel)
  126. {
  127. string[] content = [.. channel.Values];
  128. ChannelConfig config = new()
  129. {
  130. Index = byte.Parse(content[1])
  131. };
  132. if (float.TryParse(content[2], out float setPoint))
  133. config.SetPoint = setPoint;
  134. if (float.TryParse(content[4], out float floor))
  135. config.Floor = floor;
  136. if (float.TryParse(content[5], out float caps))
  137. config.Caps = caps;
  138. if (float.TryParse(content[6], out float p))
  139. config.Running_P = p;
  140. if (float.TryParse(content[7], out float i))
  141. config.Running_I = i;
  142. if (float.TryParse(content[8], out float d))
  143. config.Running_D = d;
  144. config.ActiveTuneSet = ActiveTuneSet.Running;
  145. config.Inhibit = content[9] switch
  146. {
  147. "Enable" => Inhibit.Enable,
  148. _ => Inhibit.Disable
  149. };
  150. config.ChannelMode = content[2] switch
  151. {
  152. "moni" => ChannelMode.Monitor,
  153. _ => ChannelMode.Control,
  154. };
  155. if (config.Inhibit == Inhibit.Disable)
  156. config.ChannelMode = ChannelMode.UnUsed;
  157. if (content.Length < 11)
  158. return config;
  159. if (float.TryParse(content[10], out float upRate))
  160. config.SetpointUpRate = upRate;
  161. if (float.TryParse(content[11], out float downRate))
  162. config.SetpointDownRate = downRate;
  163. return config;
  164. }
  165. }
  166. public class ExcelReader
  167. {
  168. public static Dictionary<string, Dictionary<int, Dictionary<int, string>>> Read(string filePath, int startRow)
  169. {
  170. Dictionary<string, Dictionary<int, Dictionary<int, string>>> sheets = [];
  171. using FileStream file = new(filePath, FileMode.Open, FileAccess.Read);
  172. for (int i = 0; ; i++)
  173. {
  174. ISheet sheet;
  175. try
  176. {
  177. sheet = new XSSFWorkbook(file).GetSheetAt(i);
  178. if (sheet is null)
  179. break;
  180. }
  181. catch
  182. {
  183. break;
  184. }
  185. Dictionary<int, Dictionary<int, string>> contents = [];
  186. sheets[sheet.SheetName] = contents;
  187. for (int row = startRow; row <= sheet.LastRowNum; row++)
  188. {
  189. if (sheet.GetRow(row) is not IRow currentRow)
  190. continue;
  191. contents[row] = [];
  192. for (int colum = 0; colum < currentRow.LastCellNum; colum++)
  193. {
  194. ICell cell = currentRow.GetCell(colum);
  195. contents[row][colum] = cell?.ToString() ?? string.Empty;
  196. }
  197. }
  198. }
  199. return sheets;
  200. }
  201. }