From 9d1660270272180cd71147d6ddd53d1382643d2e Mon Sep 17 00:00:00 2001
From: wenxiangchao <wenxiangchao@ihengxin.com.cn>
Date: 星期一, 02 十一月 2026 18:00:14 +0800
Subject: [PATCH] merge: 并入远程 Initial commit 作为历史根节点
---
Aging.Infrastructure/Drivers/DriverFactory.cs | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 110 insertions(+), 0 deletions(-)
diff --git a/Aging.Infrastructure/Drivers/DriverFactory.cs b/Aging.Infrastructure/Drivers/DriverFactory.cs
new file mode 100644
index 0000000..dd852ce
--- /dev/null
+++ b/Aging.Infrastructure/Drivers/DriverFactory.cs
@@ -0,0 +1,110 @@
+using System;
+using System.Collections.Generic;
+using Aging.Infrastructure.Contracts;
+using log4net;
+
+namespace Aging.Infrastructure.Drivers
+{
+ /// <summary>
+ /// 璁惧椹卞姩宸ュ巶銆�
+ /// 鏍规嵁浼犺緭鍗忚绫诲瀷鍜岀鐐逛俊鎭垱寤哄搴旂殑椹卞姩閫傞厤鍣ㄥ疄渚嬨��
+ /// </summary>
+ public static class DriverFactory
+ {
+ private static readonly ILog Log = LogManager.GetLogger(typeof(DriverFactory));
+
+ /// <summary>
+ /// 鏍规嵁浼犺緭鍗忚绫诲瀷鍜岀鐐瑰垱寤洪┍鍔ㄩ�傞厤鍣�
+ /// </summary>
+ /// <param name="deviceId">璁惧鍞竴鏍囪瘑绗�</param>
+ /// <param name="transport">浼犺緭鍗忚绫诲瀷: "Tcp", "SCPI", "ModBus", "S7", "Serial"</param>
+ /// <param name="endpoint">绔偣淇℃伅锛屾牸寮�: "ip:port" 鎴� "ip:port:extra"</param>
+ /// <returns>IDeviceDriver 瀹炰緥</returns>
+ /// <exception cref="NotSupportedException">涓嶆敮鎸佺殑浼犺緭鍗忚绫诲瀷</exception>
+ /// <exception cref="ArgumentException">绔偣鏍煎紡閿欒</exception>
+ public static IDeviceDriver CreateDriver(string deviceId, string transport, string endpoint)
+ {
+ return CreateDriver(deviceId, transport, endpoint, null);
+ }
+
+ /// <summary>
+ /// 鏍规嵁浼犺緭鍗忚绫诲瀷銆佺鐐瑰拰棰濆鍙傛暟鍒涘缓椹卞姩閫傞厤鍣�
+ /// </summary>
+ /// <param name="deviceId">璁惧鍞竴鏍囪瘑绗�</param>
+ /// <param name="transport">浼犺緭鍗忚绫诲瀷: "Tcp", "SCPI", "ModBus", "S7", "Serial"</param>
+ /// <param name="endpoint">绔偣淇℃伅锛屾牸寮�: "ip:port" 鎴� "ip:port:extra"</param>
+ /// <param name="extraParams">棰濆鍙傛暟锛堝彲閫夛級</param>
+ /// <returns>IDeviceDriver 瀹炰緥</returns>
+ /// <exception cref="NotSupportedException">涓嶆敮鎸佺殑浼犺緭鍗忚绫诲瀷</exception>
+ /// <exception cref="ArgumentException">绔偣鏍煎紡閿欒</exception>
+ public static IDeviceDriver CreateDriver(string deviceId, string transport, string endpoint, Dictionary<string, string>? extraParams)
+ {
+ if (string.IsNullOrWhiteSpace(deviceId))
+ throw new ArgumentNullException(nameof(deviceId));
+ if (string.IsNullOrWhiteSpace(transport))
+ throw new ArgumentNullException(nameof(transport));
+ if (string.IsNullOrWhiteSpace(endpoint))
+ throw new ArgumentNullException(nameof(endpoint));
+
+ Log.Info($"[DriverFactory] 鍒涘缓璁惧椹卞姩: DeviceId={deviceId}, Transport={transport}, Endpoint={endpoint}");
+
+ string[] parts = endpoint.Split(':');
+ if (parts.Length < 2)
+ {
+ throw new ArgumentException($"绔偣鏍煎紡閿欒锛屽簲涓� 'ip:port' 鎴� 'ip:port:extra'锛屽疄闄�: {endpoint}");
+ }
+
+ string ip = parts[0];
+ if (!int.TryParse(parts[1], out int port))
+ {
+ throw new ArgumentException($"绔彛鍙锋牸寮忛敊璇�: {parts[1]}");
+ }
+
+ switch (transport.ToUpper())
+ {
+ case "TCP":
+ Log.Info($"[DriverFactory] 鍒涘缓 TCP 鍘熷閫氫俊椹卞姩: {ip}:{port}");
+ return new TcpRawDriverAdapter(deviceId, ip, port);
+
+ case "SCPI":
+ Log.Info($"[DriverFactory] 鍒涘缓 SCPI 鍗忚椹卞姩: {ip}:{port}");
+ return new ScpiDriverAdapter(deviceId, ip, port);
+
+ case "MODBUS":
+ byte slaveId = 1;
+ if (extraParams != null && extraParams.TryGetValue("slaveId", out string slaveIdStr))
+ {
+ if (!byte.TryParse(slaveIdStr, out slaveId))
+ {
+ slaveId = 1;
+ }
+ }
+ // 濡傛灉 endpoint 鏈夌涓夐儴鍒嗭紝浣滀负 slaveId
+ else if (parts.Length >= 3 && byte.TryParse(parts[2], out byte parsedSlaveId))
+ {
+ slaveId = parsedSlaveId;
+ }
+ Log.Info($"[DriverFactory] 鍒涘缓 ModBus TCP 椹卞姩: {ip}:{port} (浠庣珯: {slaveId})");
+ return new ModBusDriverAdapter(deviceId, ip, port, slaveId);
+
+ case "S7":
+ Log.Info($"[DriverFactory] 鍒涘缓 S7 PLC 椹卞姩: {ip}:{port}");
+ return new S7DriverAdapter(deviceId, ip, port);
+
+ case "SERIAL":
+ // TODO: 瀹炵幇涓插彛椹卞姩閫傞厤鍣�
+ throw new NotSupportedException("涓插彛椹卞姩閫傞厤鍣ㄥ皻鏈疄鐜�");
+
+ case "CAN":
+ // CAN 椹卞姩: endpoint 鏍煎紡涓� "busName:nodeIndex" 濡� "CAN_BUS_1:0"
+ string busName = ip; // ip 瀛楁鐢ㄤ綔鎬荤嚎鍚嶇О
+ int nodeIndex = port; // port 瀛楁鐢ㄤ綔鑺傜偣缂栧彿
+ Log.Info($"[DriverFactory] 鍒涘缓 CAN 铏氭嫙椹卞姩: Bus={busName}, Node={nodeIndex}");
+ return new CanDriverAdapter(deviceId, busName, nodeIndex);
+
+ default:
+ throw new NotSupportedException($"涓嶆敮鎸佺殑浼犺緭鍗忚绫诲瀷: {transport}銆傛敮鎸佺殑绫诲瀷: Tcp, SCPI, ModBus, S7, Serial, CAN");
+ }
+ }
+ }
+}
--
Gitblit v1.9.1