using JSMachine.WMS.Common; using JSMachine.WMS.Infrastructure.AppConfiguration; using JSMachine.WMS.Infrastructure.Helper; using JSMachine.WMS.PLC.ModbusImpl.Elevator; using System.Collections.Concurrent; namespace JSMachine.WMS.PLC.ModbusImpl { /// /// 进程内 PLC 引擎注册表,按提升机编号管理通信实例。 /// public static class PLCManager { private static readonly ConcurrentDictionary PLC_Dic = new(); /// /// 添加或替换指定提升机的 PLC 引擎。 /// /// 提升机编号。 /// 对应的 Modbus 引擎。 public static void AddPLC(int elevatorNo, IModbusEngine modbusEngine) { if (PLC_Dic.ContainsKey(elevatorNo)) { PLC_Dic[elevatorNo] = modbusEngine; } else { PLC_Dic.TryAdd(elevatorNo, modbusEngine); } } /// /// 获取指定提升机的 PLC 引擎;未注册时记录警告并返回空值。 /// /// 提升机编号。 /// 已注册的通信引擎,未连接时返回空值。 public static IModbusEngine GetPLC(int elevatorNo) { if (!PLC_Dic.ContainsKey(elevatorNo)) { LogHelper.Warn($"{elevatorNo} 号提升机PLC尚未连接"); //ElevatorConfig elevatorConfig = Global.AppSettings.ElevatorConfig.First(p => p.ElevatorNo == elevatorNo); //new ElevatorPlc().Init(elevatorConfig.IP, elevatorConfig.Port); return null; } return PLC_Dic[elevatorNo]; } } }