namespace YZWater.Core.Services; using YZWater.Core.Services.PlcDrivers; /// /// PLC 地址配置 - 定义所有 PLC 读写地址映射,支持从配置文件覆盖 /// 当前地址格式为西门子 S7 格式(VD/M/Q/I)。 /// 多协议支持:各驱动负责将 S7 地址转换为目标协议地址。 /// public static class PlcConfig { // 地址覆盖表(从配置文件加载) private static Dictionary _overrides = new(); /// /// 当前 PLC 协议类型 /// public static PlcProtocol CurrentProtocol { get { var protocol = ConfigService.GetConfig()?.PlcProtocol ?? "SiemensS7"; return Enum.TryParse(protocol, out var result) ? result : PlcProtocol.SiemensS7; } } /// /// 从配置文件加载地址覆盖 /// public static void LoadFromConfig() { var config = ConfigService.GetConfig(); if (config?.PlcAddresses != null) { _overrides = config.PlcAddresses; } } /// /// 获取地址(优先使用配置覆盖值) /// 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"; // ═══════════════════════════════════════════════════════════════ // 辅助方法 // ═══════════════════════════════════════════════════════════════ /// /// 获取泵运行地址 /// public static string GetPumpRunAddress(int pumpIndex) => pumpIndex switch { 0 => Pump1Run, 1 => Pump2Run, 2 => Pump3Run, 3 => Pump4Run, 4 => Pump5Run, _ => throw new ArgumentOutOfRangeException(nameof(pumpIndex)) }; /// /// 获取泵故障地址 /// public static string GetPumpFaultAddress(int pumpIndex) => pumpIndex switch { 0 => Pump1Fault, 1 => Pump2Fault, 2 => Pump3Fault, 3 => Pump4Fault, 4 => Pump5Fault, _ => throw new ArgumentOutOfRangeException(nameof(pumpIndex)) }; /// /// 获取泵频率地址 /// public static string GetPumpFreqAddress(int pumpIndex) => pumpIndex switch { 0 => Pump1Freq, 1 => Pump2Freq, 2 => Pump3Freq, 3 => Pump4Freq, 4 => Pump5Freq, _ => throw new ArgumentOutOfRangeException(nameof(pumpIndex)) }; /// /// 获取泵电流地址 /// public static string GetPumpCurrentAddress(int pumpIndex) => pumpIndex switch { 0 => Pump1Current, 1 => Pump2Current, 2 => Pump3Current, 3 => Pump4Current, 4 => Pump5Current, _ => throw new ArgumentOutOfRangeException(nameof(pumpIndex)) }; /// /// 获取阀门控制地址 /// public static string GetValveControlAddress(int valveIndex) => valveIndex switch { 0 => Valve1Control, 1 => Valve2Control, 2 => Valve3Control, 3 => Valve4Control, _ => throw new ArgumentOutOfRangeException(nameof(valveIndex)) }; /// /// 获取阀门位置反馈地址 /// public static string GetValvePositionAddress(int valveIndex) => valveIndex switch { 0 => Valve1Position, 1 => Valve2Position, 2 => Valve3Position, 3 => Valve4Position, _ => throw new ArgumentOutOfRangeException(nameof(valveIndex)) }; }