PlcConfig.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. namespace YZWater.Core.Services;
  2. using YZWater.Core.Services.PlcDrivers;
  3. /// <summary>
  4. /// PLC 地址配置 - 定义所有 PLC 读写地址映射,支持从配置文件覆盖
  5. /// 当前地址格式为西门子 S7 格式(VD/M/Q/I)。
  6. /// 多协议支持:各驱动负责将 S7 地址转换为目标协议地址。
  7. /// </summary>
  8. public static class PlcConfig
  9. {
  10. // 地址覆盖表(从配置文件加载)
  11. private static Dictionary<string, string> _overrides = new();
  12. /// <summary>
  13. /// 当前 PLC 协议类型
  14. /// </summary>
  15. public static PlcProtocol CurrentProtocol
  16. {
  17. get
  18. {
  19. var protocol = ConfigService.GetConfig()?.PlcProtocol ?? "SiemensS7";
  20. return Enum.TryParse<PlcProtocol>(protocol, out var result) ? result : PlcProtocol.SiemensS7;
  21. }
  22. }
  23. /// <summary>
  24. /// 从配置文件加载地址覆盖
  25. /// </summary>
  26. public static void LoadFromConfig()
  27. {
  28. var config = ConfigService.GetConfig();
  29. if (config?.PlcAddresses != null)
  30. {
  31. _overrides = config.PlcAddresses;
  32. }
  33. }
  34. /// <summary>
  35. /// 获取地址(优先使用配置覆盖值)
  36. /// </summary>
  37. private static string Addr(string tagName, string defaultAddr)
  38. {
  39. return _overrides.TryGetValue(tagName, out var addr) ? addr : defaultAddr;
  40. }
  41. // ═══════════════════════════════════════════════════════════════
  42. // 读取地址 (Read)
  43. // ═══════════════════════════════════════════════════════════════
  44. // ─── 水箱液位 (float) ───
  45. public static string Tank1Level => Addr(nameof(Tank1Level), "VD100");
  46. public static string Tank2Level => Addr(nameof(Tank2Level), "VD104");
  47. public static string Tank3Level => Addr(nameof(Tank3Level), "VD108");
  48. public static string Tank4Level => Addr(nameof(Tank4Level), "VD112");
  49. // ─── 流量 (float) ───
  50. public static string InflowRate => Addr(nameof(InflowRate), "VD200");
  51. public static string OutflowRate => Addr(nameof(OutflowRate), "VD204");
  52. // ─── 泵状态 (bool) ───
  53. public const string Pump1Run = "Q0.0";
  54. public const string Pump2Run = "Q0.1";
  55. public const string Pump3Run = "Q0.2";
  56. public const string Pump4Run = "Q0.3";
  57. public const string Pump5Run = "Q0.4";
  58. // ─── 泵故障 (bool) ───
  59. public const string Pump1Fault = "I0.0";
  60. public const string Pump2Fault = "I0.1";
  61. public const string Pump3Fault = "I0.2";
  62. public const string Pump4Fault = "I0.3";
  63. public const string Pump5Fault = "I0.4";
  64. // ─── 泵频率反馈 (float) ───
  65. public static string Pump1Freq => Addr(nameof(Pump1Freq), "VD300");
  66. public static string Pump2Freq => Addr(nameof(Pump2Freq), "VD304");
  67. public static string Pump3Freq => Addr(nameof(Pump3Freq), "VD308");
  68. public static string Pump4Freq => Addr(nameof(Pump4Freq), "VD312");
  69. public static string Pump5Freq => Addr(nameof(Pump5Freq), "VD316");
  70. // ─── 泵电流 (float) ───
  71. public static string Pump1Current => Addr(nameof(Pump1Current), "VD320");
  72. public static string Pump2Current => Addr(nameof(Pump2Current), "VD324");
  73. public static string Pump3Current => Addr(nameof(Pump3Current), "VD328");
  74. public static string Pump4Current => Addr(nameof(Pump4Current), "VD332");
  75. public static string Pump5Current => Addr(nameof(Pump5Current), "VD336");
  76. // ─── 风机状态 (bool) ───
  77. public const string Fan1Run = "Q0.5";
  78. public const string Fan2Run = "Q0.6";
  79. // ─── 风机故障 (bool) ───
  80. public const string Fan1Fault = "I0.5";
  81. public const string Fan2Fault = "I0.6";
  82. // ─── 阀门位置反馈 (int) ───
  83. public static string Valve1Position => Addr(nameof(Valve1Position), "VD500");
  84. public static string Valve2Position => Addr(nameof(Valve2Position), "VD504");
  85. public static string Valve3Position => Addr(nameof(Valve3Position), "VD508");
  86. public static string Valve4Position => Addr(nameof(Valve4Position), "VD512");
  87. // ─── 系统状态 ───
  88. public const string SystemMode = "M0.0"; // true=自动, false=手动
  89. // ═══════════════════════════════════════════════════════════════
  90. // 写入地址 (Write)
  91. // ═══════════════════════════════════════════════════════════════
  92. // ─── 泵启停控制 (bool) ───
  93. public const string Pump1Start = "Q0.0";
  94. public const string Pump2Start = "Q0.1";
  95. public const string Pump3Start = "Q0.2";
  96. public const string Pump4Start = "Q0.3";
  97. public const string Pump5Start = "Q0.4";
  98. // ─── 风机启停控制 (bool) ───
  99. public const string Fan1Start = "Q0.5";
  100. public const string Fan2Start = "Q0.6";
  101. // ─── 阀门控制 (bool) ───
  102. public const string Valve1Control = "Q1.0";
  103. public const string Valve2Control = "Q1.1";
  104. public const string Valve3Control = "Q1.2";
  105. public const string Valve4Control = "Q1.3";
  106. // ─── 泵频率设定 (float) ───
  107. public const string PumpFreqSetpoint = "VD600";
  108. // ═══════════════════════════════════════════════════════════════
  109. // 辅助方法
  110. // ═══════════════════════════════════════════════════════════════
  111. /// <summary>
  112. /// 获取泵运行地址
  113. /// </summary>
  114. public static string GetPumpRunAddress(int pumpIndex) => pumpIndex switch
  115. {
  116. 0 => Pump1Run,
  117. 1 => Pump2Run,
  118. 2 => Pump3Run,
  119. 3 => Pump4Run,
  120. 4 => Pump5Run,
  121. _ => throw new ArgumentOutOfRangeException(nameof(pumpIndex))
  122. };
  123. /// <summary>
  124. /// 获取泵故障地址
  125. /// </summary>
  126. public static string GetPumpFaultAddress(int pumpIndex) => pumpIndex switch
  127. {
  128. 0 => Pump1Fault,
  129. 1 => Pump2Fault,
  130. 2 => Pump3Fault,
  131. 3 => Pump4Fault,
  132. 4 => Pump5Fault,
  133. _ => throw new ArgumentOutOfRangeException(nameof(pumpIndex))
  134. };
  135. /// <summary>
  136. /// 获取泵频率地址
  137. /// </summary>
  138. public static string GetPumpFreqAddress(int pumpIndex) => pumpIndex switch
  139. {
  140. 0 => Pump1Freq,
  141. 1 => Pump2Freq,
  142. 2 => Pump3Freq,
  143. 3 => Pump4Freq,
  144. 4 => Pump5Freq,
  145. _ => throw new ArgumentOutOfRangeException(nameof(pumpIndex))
  146. };
  147. /// <summary>
  148. /// 获取泵电流地址
  149. /// </summary>
  150. public static string GetPumpCurrentAddress(int pumpIndex) => pumpIndex switch
  151. {
  152. 0 => Pump1Current,
  153. 1 => Pump2Current,
  154. 2 => Pump3Current,
  155. 3 => Pump4Current,
  156. 4 => Pump5Current,
  157. _ => throw new ArgumentOutOfRangeException(nameof(pumpIndex))
  158. };
  159. /// <summary>
  160. /// 获取阀门控制地址
  161. /// </summary>
  162. public static string GetValveControlAddress(int valveIndex) => valveIndex switch
  163. {
  164. 0 => Valve1Control,
  165. 1 => Valve2Control,
  166. 2 => Valve3Control,
  167. 3 => Valve4Control,
  168. _ => throw new ArgumentOutOfRangeException(nameof(valveIndex))
  169. };
  170. /// <summary>
  171. /// 获取阀门位置反馈地址
  172. /// </summary>
  173. public static string GetValvePositionAddress(int valveIndex) => valveIndex switch
  174. {
  175. 0 => Valve1Position,
  176. 1 => Valve2Position,
  177. 2 => Valve3Position,
  178. 3 => Valve4Position,
  179. _ => throw new ArgumentOutOfRangeException(nameof(valveIndex))
  180. };
  181. }