| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- namespace YZWater.Core.Services;
- using YZWater.Core.Services.PlcDrivers;
- /// <summary>
- /// PLC 地址配置 - 定义所有 PLC 读写地址映射,支持从配置文件覆盖
- /// 当前地址格式为西门子 S7 格式(VD/M/Q/I)。
- /// 多协议支持:各驱动负责将 S7 地址转换为目标协议地址。
- /// </summary>
- public static class PlcConfig
- {
- // 地址覆盖表(从配置文件加载)
- private static Dictionary<string, string> _overrides = new();
- /// <summary>
- /// 当前 PLC 协议类型
- /// </summary>
- public static PlcProtocol CurrentProtocol
- {
- get
- {
- var protocol = ConfigService.GetConfig()?.PlcProtocol ?? "SiemensS7";
- return Enum.TryParse<PlcProtocol>(protocol, out var result) ? result : PlcProtocol.SiemensS7;
- }
- }
- /// <summary>
- /// 从配置文件加载地址覆盖
- /// </summary>
- public static void LoadFromConfig()
- {
- var config = ConfigService.GetConfig();
- if (config?.PlcAddresses != null)
- {
- _overrides = config.PlcAddresses;
- }
- }
- /// <summary>
- /// 获取地址(优先使用配置覆盖值)
- /// </summary>
- private static string Addr(string tagName, string defaultAddr)
- {
- return _overrides.TryGetValue(tagName, out var addr) ? addr : defaultAddr;
- }
- // ═══════════════════════════════════════════════════════════════
- // 读取地址 (Read)
- // ═══════════════════════════════════════════════════════════════
- // ─── 水箱液位 (float) ───
- public static string Tank1Level => Addr(nameof(Tank1Level), "VD100");
- public static string Tank2Level => Addr(nameof(Tank2Level), "VD104");
- public static string Tank3Level => Addr(nameof(Tank3Level), "VD108");
- public static string Tank4Level => Addr(nameof(Tank4Level), "VD112");
- // ─── 流量 (float) ───
- public static string InflowRate => Addr(nameof(InflowRate), "VD200");
- public static string OutflowRate => Addr(nameof(OutflowRate), "VD204");
- // ─── 泵状态 (bool) ───
- public const string Pump1Run = "Q0.0";
- public const string Pump2Run = "Q0.1";
- public const string Pump3Run = "Q0.2";
- public const string Pump4Run = "Q0.3";
- public const string Pump5Run = "Q0.4";
- // ─── 泵故障 (bool) ───
- public const string Pump1Fault = "I0.0";
- public const string Pump2Fault = "I0.1";
- public const string Pump3Fault = "I0.2";
- public const string Pump4Fault = "I0.3";
- public const string Pump5Fault = "I0.4";
- // ─── 泵频率反馈 (float) ───
- public static string Pump1Freq => Addr(nameof(Pump1Freq), "VD300");
- public static string Pump2Freq => Addr(nameof(Pump2Freq), "VD304");
- public static string Pump3Freq => Addr(nameof(Pump3Freq), "VD308");
- public static string Pump4Freq => Addr(nameof(Pump4Freq), "VD312");
- public static string Pump5Freq => Addr(nameof(Pump5Freq), "VD316");
- // ─── 泵电流 (float) ───
- public static string Pump1Current => Addr(nameof(Pump1Current), "VD320");
- public static string Pump2Current => Addr(nameof(Pump2Current), "VD324");
- public static string Pump3Current => Addr(nameof(Pump3Current), "VD328");
- public static string Pump4Current => Addr(nameof(Pump4Current), "VD332");
- public static string Pump5Current => Addr(nameof(Pump5Current), "VD336");
- // ─── 风机状态 (bool) ───
- public const string Fan1Run = "Q0.5";
- public const string Fan2Run = "Q0.6";
- // ─── 风机故障 (bool) ───
- public const string Fan1Fault = "I0.5";
- public const string Fan2Fault = "I0.6";
- // ─── 阀门位置反馈 (int) ───
- public static string Valve1Position => Addr(nameof(Valve1Position), "VD500");
- public static string Valve2Position => Addr(nameof(Valve2Position), "VD504");
- public static string Valve3Position => Addr(nameof(Valve3Position), "VD508");
- public static string Valve4Position => Addr(nameof(Valve4Position), "VD512");
- // ─── 系统状态 ───
- public const string SystemMode = "M0.0"; // true=自动, false=手动
- // ═══════════════════════════════════════════════════════════════
- // 写入地址 (Write)
- // ═══════════════════════════════════════════════════════════════
- // ─── 泵启停控制 (bool) ───
- public const string Pump1Start = "Q0.0";
- public const string Pump2Start = "Q0.1";
- public const string Pump3Start = "Q0.2";
- public const string Pump4Start = "Q0.3";
- public const string Pump5Start = "Q0.4";
- // ─── 风机启停控制 (bool) ───
- public const string Fan1Start = "Q0.5";
- public const string Fan2Start = "Q0.6";
- // ─── 阀门控制 (bool) ───
- public const string Valve1Control = "Q1.0";
- public const string Valve2Control = "Q1.1";
- public const string Valve3Control = "Q1.2";
- public const string Valve4Control = "Q1.3";
- // ─── 泵频率设定 (float) ───
- public const string PumpFreqSetpoint = "VD600";
- // ═══════════════════════════════════════════════════════════════
- // 辅助方法
- // ═══════════════════════════════════════════════════════════════
- /// <summary>
- /// 获取泵运行地址
- /// </summary>
- public static string GetPumpRunAddress(int pumpIndex) => pumpIndex switch
- {
- 0 => Pump1Run,
- 1 => Pump2Run,
- 2 => Pump3Run,
- 3 => Pump4Run,
- 4 => Pump5Run,
- _ => throw new ArgumentOutOfRangeException(nameof(pumpIndex))
- };
- /// <summary>
- /// 获取泵故障地址
- /// </summary>
- public static string GetPumpFaultAddress(int pumpIndex) => pumpIndex switch
- {
- 0 => Pump1Fault,
- 1 => Pump2Fault,
- 2 => Pump3Fault,
- 3 => Pump4Fault,
- 4 => Pump5Fault,
- _ => throw new ArgumentOutOfRangeException(nameof(pumpIndex))
- };
- /// <summary>
- /// 获取泵频率地址
- /// </summary>
- public static string GetPumpFreqAddress(int pumpIndex) => pumpIndex switch
- {
- 0 => Pump1Freq,
- 1 => Pump2Freq,
- 2 => Pump3Freq,
- 3 => Pump4Freq,
- 4 => Pump5Freq,
- _ => throw new ArgumentOutOfRangeException(nameof(pumpIndex))
- };
- /// <summary>
- /// 获取泵电流地址
- /// </summary>
- public static string GetPumpCurrentAddress(int pumpIndex) => pumpIndex switch
- {
- 0 => Pump1Current,
- 1 => Pump2Current,
- 2 => Pump3Current,
- 3 => Pump4Current,
- 4 => Pump5Current,
- _ => throw new ArgumentOutOfRangeException(nameof(pumpIndex))
- };
- /// <summary>
- /// 获取阀门控制地址
- /// </summary>
- public static string GetValveControlAddress(int valveIndex) => valveIndex switch
- {
- 0 => Valve1Control,
- 1 => Valve2Control,
- 2 => Valve3Control,
- 3 => Valve4Control,
- _ => throw new ArgumentOutOfRangeException(nameof(valveIndex))
- };
- /// <summary>
- /// 获取阀门位置反馈地址
- /// </summary>
- public static string GetValvePositionAddress(int valveIndex) => valveIndex switch
- {
- 0 => Valve1Position,
- 1 => Valve2Position,
- 2 => Valve3Position,
- 3 => Valve4Position,
- _ => throw new ArgumentOutOfRangeException(nameof(valveIndex))
- };
- }
|