BOB/BOB/Singleton/Devices.cs
2025-11-20 14:49:32 +08:00

231 lines
11 KiB
C#

using Castle.DynamicProxy;
using DeviceCommand.Base;
using DeviceCommand.Device;
using Logger;
using Model;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
namespace BOB.Singleton
{
public class Devices
{
private CancellationTokenSource _appCancellationTokenSource = new();
public CancellationToken AppCancellationToken => _appCancellationTokenSource.Token;
public Dictionary<string, object> DeviceDic { get; private set; } = new Dictionary<string, object>();
private readonly ProxyGenerator _proxyGen = new ProxyGenerator();
private readonly IInterceptor _loggingInterceptor = new LoggingInterceptor();
private ITcp E36233ADevice { get; set; }
private ISerialPort IT6724CDevice { get; set; }
private ISerialPort IT6724CReverseDevice{ get; set; }
private IModbusDevice EAEL9080Device { get; set; }
private IModbusDevice IOBoardevice { get; set; }
private IModbusDevice LQ7500_DDevice { get; set; }
private IModbusDevice PSB11000Device { get; set; }
private IModbusDevice WS_68030_380TDevice { get; set; }
private ITcp SQ0030G1DTDevice { get; set; }
private IModbusDevice ZXKSTDevice { get; set; }
public Devices(List<DeviceConfigModel> deviceList)
{
foreach (var device in deviceList)
{
if (!device.IsEnabled) continue;
switch (device.DeviceType)
{
case "DeviceCommand.Device.E36233A":
if (device.CommunicationConfig is TcpConfig tcp1)
{
E36233ADevice = _proxyGen.CreateInterfaceProxyWithTarget<ITcp>(
new E36233A(tcp1.IPAddress, tcp1.Port, tcp1.ReadTimeout, tcp1.WriteTimeout),
_loggingInterceptor
);
DeviceDic["E36233A"] = E36233ADevice;
}
else throw new InvalidOperationException("E36233A 必须使用 TcpConfig");
break;
case "DeviceCommand.Device.IT6724C":
if (device.CommunicationConfig is SerialPortConfig sp1)
{
IT6724CDevice = _proxyGen.CreateInterfaceProxyWithTarget<ISerialPort>(
new IT6724C(sp1.COMPort, sp1.BaudRate, sp1.DataBit, sp1.StopBit, sp1.ParityBit, sp1.ReadTimeout, sp1.WriteTimeout),
_loggingInterceptor
);
DeviceDic["IT6724C"] = IT6724CDevice;
}
else throw new InvalidOperationException("IT6724C 必须使用 SerialPortConfig");
break;
case "DeviceCommand.Device.IT6724CReverse":
if (device.CommunicationConfig is SerialPortConfig sp3)
{
IT6724CReverseDevice = _proxyGen.CreateInterfaceProxyWithTarget<ISerialPort>(
new IT6724C(sp3.COMPort, sp3.BaudRate, sp3.DataBit, sp3.StopBit, sp3.ParityBit, sp3.ReadTimeout, sp3.WriteTimeout),
_loggingInterceptor
);
DeviceDic["IT6724CReverse"] = IT6724CReverseDevice;
}
else throw new InvalidOperationException("IT6724C 必须使用 SerialPortConfig");
break;
case "DeviceCommand.Device.LQ7500_D":
if (device.CommunicationConfig is SerialPortConfig sp2)
{
LQ7500_DDevice = _proxyGen.CreateInterfaceProxyWithTarget<IModbusDevice>(
new LQ7500_D(sp2.COMPort, sp2.BaudRate, sp2.DataBit, sp2.StopBit, sp2.ParityBit, sp2.ReadTimeout, sp2.WriteTimeout),
_loggingInterceptor
);
DeviceDic["LQ7500_D"] = LQ7500_DDevice;
}
else throw new InvalidOperationException("LQ7500D 必须使用 SerialPortConfig");
break;
case "DeviceCommand.Device.EAEL9080":
if (device.CommunicationConfig is TcpConfig tcp2)
{
EAEL9080Device = _proxyGen.CreateInterfaceProxyWithTarget<IModbusDevice>(
new EAEL9080(tcp2.IPAddress, tcp2.Port, tcp2.ReadTimeout, tcp2.WriteTimeout),
_loggingInterceptor
);
DeviceDic["EAEL9080"] = EAEL9080Device;
}
else throw new InvalidOperationException("EAEL9080 必须使用 TcpConfig");
break;
case "DeviceCommand.Device.IOBoard":
if (device.CommunicationConfig is TcpConfig tcp3)
{
IOBoardevice = _proxyGen.CreateInterfaceProxyWithTarget<IModbusDevice>(
new IOBoard(tcp3.IPAddress, tcp3.Port, tcp3.ReadTimeout, tcp3.WriteTimeout),
_loggingInterceptor
);
DeviceDic["IOBoard"] = IOBoardevice;
}
else throw new InvalidOperationException("IOBoard 必须使用 TcpConfig");
break;
case "DeviceCommand.Device.PSB11000":
if (device.CommunicationConfig is TcpConfig tcp4)
{
PSB11000Device = _proxyGen.CreateInterfaceProxyWithTarget<IModbusDevice>(
new PSB11000(tcp4.IPAddress, tcp4.Port, tcp4.ReadTimeout, tcp4.WriteTimeout),
_loggingInterceptor
);
DeviceDic["PSB11000"] = PSB11000Device;
}
else throw new InvalidOperationException("PSB11000 必须使用 TcpConfig");
break;
case "DeviceCommand.Device.WS_68030_380T":
if (device.CommunicationConfig is TcpConfig tcp5)
{
WS_68030_380TDevice = _proxyGen.CreateInterfaceProxyWithTarget<IModbusDevice>(
new WS_68030_380T(tcp5.IPAddress, tcp5.Port, tcp5.ReadTimeout, tcp5.WriteTimeout),
_loggingInterceptor
);
DeviceDic["WS_68030_380T"] = WS_68030_380TDevice;
}
else throw new InvalidOperationException("WS_68030_380T 必须使用 TcpConfig");
break;
case "DeviceCommand.Device.SQ0030G1D":
if (device.CommunicationConfig is TcpConfig tcp7)
{
SQ0030G1DTDevice = _proxyGen.CreateInterfaceProxyWithTarget<ITcp>(
new SQ0030G1D(tcp7.IPAddress, tcp7.Port, tcp7.ReadTimeout, tcp7.WriteTimeout),
_loggingInterceptor
);
DeviceDic["SQ0030G1D"] = SQ0030G1DTDevice;
}
else throw new InvalidOperationException("SQ0030G1D 必须使用 TcpConfig");
break;
case "DeviceCommand.Device.ZXKS":
if (device.CommunicationConfig is TcpConfig tcp6)
{
ZXKSTDevice = _proxyGen.CreateInterfaceProxyWithTarget<IModbusDevice>(
new ZXKS(tcp6.IPAddress, tcp6.Port, tcp6.ReadTimeout, tcp6.WriteTimeout),
_loggingInterceptor
);
DeviceDic["ZXKS"] = ZXKSTDevice;
}
else throw new InvalidOperationException("ZXKS 必须使用 TcpConfig");
break;
default:
throw new NotSupportedException($"未知设备类型:{device.DeviceType}");
}
}
}
public async Task ConnectAllDevicesAsync()
{
if (DeviceDic.Count == 0)
{
await Task.CompletedTask;
}
foreach (var kvp in DeviceDic)
{
var name = kvp.Key;
var device = kvp.Value;
if (device == null)
{
LoggerHelper.WarnWithNotify($"{name} 实例为空,跳过连接");
continue;
}
await Task.Delay(10);
try
{
// 检查并处理 ITcp 接口
if (device is ITcp itcpDevice)
{
bool result = await itcpDevice.ConnectAsync(CancellationToken.None);
LoggerHelper.SuccessWithNotify($"{name} 连接成功 (ITcp)");
}
// 检查并处理 IModbusDevice 接口
else if (device is IModbusDevice imodbusDevice)
{
bool result = await imodbusDevice.ConnectAsync(CancellationToken.None);
LoggerHelper.SuccessWithNotify($"{name} 连接成功 (IModbusDevice)");
}
// 检查并处理 ISerialPort 接口
else if (device is ISerialPort iserialPortDevice)
{
bool result = await iserialPortDevice.ConnectAsync(CancellationToken.None);
LoggerHelper.SuccessWithNotify($"{name} 连接成功 (ISerialPort)");
}
else
{
LoggerHelper.WarnWithNotify($"{name} 不是有效的设备类型");
}
}
catch(InvalidCastException ex)
{
}
catch (Exception ex)
{
LoggerHelper.ErrorWithNotify($"{name} 连接失败: {ex.Message}", ex.StackTrace);
}
}
}
public void Dispose()
{
_appCancellationTokenSource.Cancel();
_appCancellationTokenSource.Dispose();
}
}
}