Files

56 lines
1.8 KiB
C#
Raw Permalink Normal View History

2026-09-02 16:31:50 +08:00
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
{
/// <summary>
/// 进程内 PLC 引擎注册表,按提升机编号管理通信实例。
/// </summary>
public static class PLCManager
{
private static readonly ConcurrentDictionary<int, IModbusEngine> PLC_Dic = new();
/// <summary>
/// 添加或替换指定提升机的 PLC 引擎。
/// </summary>
/// <param name="elevatorNo">提升机编号。</param>
/// <param name="modbusEngine">对应的 Modbus 引擎。</param>
public static void AddPLC(int elevatorNo, IModbusEngine modbusEngine)
{
if (PLC_Dic.ContainsKey(elevatorNo))
{
PLC_Dic[elevatorNo] = modbusEngine;
}
else
{
PLC_Dic.TryAdd(elevatorNo, modbusEngine);
}
}
/// <summary>
/// 获取指定提升机的 PLC 引擎;未注册时记录警告并返回空值。
/// </summary>
/// <param name="elevatorNo">提升机编号。</param>
/// <returns>已注册的通信引擎,未连接时返回空值。</returns>
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];
}
}
}