ConfigService.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.Json;
  6. using APS7100TestTool.Models;
  7. using MiniExcelLibs;
  8. namespace APS7100TestTool.Services
  9. {
  10. public class ConfigService
  11. {
  12. /// <summary>
  13. /// Config 文件夹路径
  14. /// </summary>
  15. public static readonly string ConfigFolder = Path.Combine(
  16. AppDomain.CurrentDomain.BaseDirectory, "Config");
  17. private static readonly string AppSettingsPath = Path.Combine(
  18. ConfigFolder, "appsettings.json");
  19. /// <summary>
  20. /// 默认的 Modbus 配置文件路径
  21. /// </summary>
  22. public static readonly string DefaultModbusConfigPath = Path.Combine(
  23. ConfigFolder, "ModbusConfig.xlsx");
  24. /// <summary>
  25. /// 静态构造函数,确保 Config 文件夹存在
  26. /// </summary>
  27. static ConfigService()
  28. {
  29. if (!Directory.Exists(ConfigFolder))
  30. {
  31. Directory.CreateDirectory(ConfigFolder);
  32. }
  33. }
  34. #region Modbus Config
  35. public List<ModbusRegisterMapping> LoadModbusConfig(string filePath)
  36. {
  37. try
  38. {
  39. var rows = MiniExcel.Query<ModbusRegisterMapping>(filePath).ToList();
  40. return rows;
  41. }
  42. catch (Exception ex)
  43. {
  44. throw new Exception($"加载配置文件失败: {ex.Message}", ex);
  45. }
  46. }
  47. /// <summary>
  48. /// 保存 Modbus 配置到文件
  49. /// </summary>
  50. public void SaveModbusConfig(string filePath, List<ModbusRegisterMapping> mappings)
  51. {
  52. try
  53. {
  54. // 如果文件已存在,先删除(MiniExcel 不支持覆盖)
  55. if (File.Exists(filePath))
  56. {
  57. File.Delete(filePath);
  58. }
  59. MiniExcel.SaveAs(filePath, mappings);
  60. }
  61. catch (Exception ex)
  62. {
  63. throw new Exception($"保存配置文件失败: {ex.Message}", ex);
  64. }
  65. }
  66. public void CreateTemplate(string filePath)
  67. {
  68. // 模板示例:区分 APS7100 和 PSW250 的命令格式
  69. var data = new[]
  70. {
  71. // ========== APS7100 示例 ==========
  72. new ModbusRegisterMapping
  73. {
  74. Address = 1,
  75. RegisterType = "Holding",
  76. OperationType = "Write",
  77. DataType = "Float",
  78. DeviceTarget = "APS7100",
  79. ScpiCommand = "SOUR:VOLT {0}",
  80. ScaleFactor = 1.0,
  81. Description = "[APS] 设置电压 (V)"
  82. },
  83. new ModbusRegisterMapping
  84. {
  85. Address = 10,
  86. RegisterType = "Holding",
  87. OperationType = "Read",
  88. DataType = "Float",
  89. DeviceTarget = "APS7100",
  90. // ⚠ APS7100 必须使用 MEAS:SCAL:VOLT?
  91. ScpiCommand = "MEAS:SCAL:VOLT?",
  92. ScaleFactor = 1.0,
  93. Description = "[APS] 测量电压 (V)"
  94. },
  95. new ModbusRegisterMapping
  96. {
  97. Address = 20,
  98. RegisterType = "Holding",
  99. OperationType = "Write",
  100. DataType = "Int16",
  101. DeviceTarget = "APS7100",
  102. TriggerValue = 1,
  103. // ⚠ APS7100 必须使用 OUTP:STAT
  104. ScpiCommand = "OUTP:STAT ON",
  105. ScaleFactor = 1.0,
  106. Description = "[APS] 开启输出"
  107. },
  108. // ========== PSW250 示例 ==========
  109. new ModbusRegisterMapping
  110. {
  111. Address = 100,
  112. RegisterType = "Holding",
  113. OperationType = "Write",
  114. DataType = "Float",
  115. DeviceTarget = "PSW250",
  116. ScpiCommand = "SOUR:VOLT {0}",
  117. ScaleFactor = 1.0,
  118. Description = "[PSW] 设置电压 (V)"
  119. },
  120. new ModbusRegisterMapping
  121. {
  122. Address = 110,
  123. RegisterType = "Holding",
  124. OperationType = "Read",
  125. DataType = "Float",
  126. DeviceTarget = "PSW250",
  127. // PSW250 支持简写命令
  128. ScpiCommand = "MEAS:VOLT?",
  129. ScaleFactor = 1.0,
  130. Description = "[PSW] 测量电压 (V)"
  131. },
  132. };
  133. MiniExcel.SaveAs(filePath, data);
  134. }
  135. #endregion
  136. #region App Settings
  137. /// <summary>
  138. /// 加载应用程序设置
  139. /// </summary>
  140. public AppSettings LoadAppSettings()
  141. {
  142. try
  143. {
  144. if (File.Exists(AppSettingsPath))
  145. {
  146. string json = File.ReadAllText(AppSettingsPath);
  147. var settings = JsonSerializer.Deserialize<AppSettings>(json);
  148. return settings ?? new AppSettings();
  149. }
  150. }
  151. catch
  152. {
  153. // 如果加载失败,返回默认设置
  154. }
  155. return new AppSettings();
  156. }
  157. /// <summary>
  158. /// 保存应用程序设置
  159. /// </summary>
  160. public void SaveAppSettings(AppSettings settings)
  161. {
  162. try
  163. {
  164. settings.LastUpdated = DateTime.Now;
  165. var options = new JsonSerializerOptions
  166. {
  167. WriteIndented = true
  168. };
  169. string json = JsonSerializer.Serialize(settings, options);
  170. File.WriteAllText(AppSettingsPath, json);
  171. }
  172. catch (Exception ex)
  173. {
  174. throw new Exception($"保存设置失败: {ex.Message}", ex);
  175. }
  176. }
  177. #endregion
  178. }
  179. }