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/InfrastructureBootstrapper.cs |  152 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 152 insertions(+), 0 deletions(-)

diff --git a/Aging.Infrastructure/InfrastructureBootstrapper.cs b/Aging.Infrastructure/InfrastructureBootstrapper.cs
new file mode 100644
index 0000000..aebb5b0
--- /dev/null
+++ b/Aging.Infrastructure/InfrastructureBootstrapper.cs
@@ -0,0 +1,152 @@
+using log4net;
+using HMI.Core.Util;
+using Aging.Infrastructure.Contracts;
+using Aging.Infrastructure.Core;
+using Aging.Infrastructure.Services;
+using Aging.Infrastructure.Drivers;
+
+namespace Aging.Infrastructure
+{
+    /// <summary>
+    /// 鍩虹璁炬柦鍚姩寮曞 - 鍒濆鍖栧苟娉ㄥ唽鎵�鏈夋牳蹇冩湇鍔″埌 UtilContainer
+    /// </summary>
+    public static class InfrastructureBootstrapper
+    {
+        private static readonly ILog log = LogManager.GetLogger(typeof(InfrastructureBootstrapper));
+        private static bool _initialized = false;
+
+        /// <summary>
+        /// 鍒濆鍖栧熀纭�璁炬柦鏈嶅姟
+        /// 搴斿湪 WPF 涓荤▼搴忓惎鍔ㄦ椂鎴栨彃浠� InitFinish 涓皟鐢�
+        /// </summary>
+        /// <param name="deviceConfigDbPath">DeviceConfig 鎻掍欢鐨� SQLite 鏁版嵁搴撹矾寰�</param>
+        public static void Initialize(string deviceConfigDbPath)
+        {
+            if (_initialized)
+            {
+                log.Warn("鍩虹璁炬柦宸插垵濮嬪寲锛岃烦杩囬噸澶嶅垵濮嬪寲");
+                return;
+            }
+
+            try
+            {
+                log.Info("寮�濮嬪垵濮嬪寲鍩虹璁炬柦鏈嶅姟...");
+
+                // 1. 鍒涘缓 ResourceManager 鍗曚緥
+                var resourceManager = new ResourceManager();
+                UtilContainer.RegisterUtil("ResourceManager", resourceManager);
+                log.Info("ResourceManager 宸叉敞鍐�");
+
+                // 2. 鍒涘缓 DeviceManager 鍗曚緥
+                var deviceManager = new DeviceManager();
+                UtilContainer.RegisterUtil("DeviceManager", deviceManager);
+                log.Info("DeviceManager 宸叉敞鍐�");
+
+                // 3. 鍒涘缓 SlotDeviceMappingResolver锛堥渶瑕� DeviceConfig DB 璺緞锛�
+                var mappingResolver = new SlotDeviceMappingResolver(deviceConfigDbPath);
+                UtilContainer.RegisterUtil("SlotDeviceMappingResolver", mappingResolver);
+                log.Info("SlotDeviceMappingResolver 宸叉敞鍐�");
+
+                // 4. 鍒涘缓 DeviceService
+                var deviceService = new DeviceService(resourceManager, deviceManager, mappingResolver);
+                UtilContainer.RegisterUtil("DeviceService", deviceService);
+                log.Info("DeviceService 宸叉敞鍐�");
+
+                // 5. 鍒涘缓 PowerSupplyService
+                var powerSupplyService = new PowerSupplyService(resourceManager, deviceManager, mappingResolver);
+                UtilContainer.RegisterUtil("PowerSupplyService", powerSupplyService);
+                log.Info("PowerSupplyService 宸叉敞鍐�");
+
+                // 6. 浠庢暟鎹簱鍔犺浇璁惧瀹氫箟骞舵敞鍐岄┍鍔�
+                LoadDevicesFromConfig(deviceManager, mappingResolver);
+
+                _initialized = true;
+                log.Info("鍩虹璁炬柦鏈嶅姟鍒濆鍖栧畬鎴�");
+            }
+            catch (Exception ex)
+            {
+                log.Error("鍩虹璁炬柦鍒濆鍖栧け璐�", ex);
+                throw;
+            }
+        }
+
+        /// <summary>
+        /// 浠� DeviceConfig 鏁版嵁搴撳姞杞借澶囧畾涔夊苟娉ㄥ唽椹卞姩
+        /// </summary>
+        private static void LoadDevicesFromConfig(IDeviceManager deviceManager, SlotDeviceMappingResolver mappingResolver)
+        {
+            try
+            {
+                // 璇诲彇鎵�鏈夎澶囨槧灏�
+                var mappings = mappingResolver.GetAllMappings();
+                // 浠庢槧灏勪腑鎻愬彇鍞竴鐨勮澶嘔D
+                var deviceIds = mappings
+                    .Select(m => m.DeviceId)
+                    .Distinct()
+                    .ToList();
+
+                int loaded = 0;
+                foreach (var deviceId in deviceIds)
+                {
+                    try
+                    {
+                        // 杩欓噷鏆傛椂浣跨敤閫氱敤 TCP 椹卞姩浣滀负鍗犱綅
+                        // 瀹為檯搴旇浠� device_definition 琛ㄨ鍙� transport 鍜� endpoint
+                        // 鐒跺悗閫氳繃 DriverFactory.CreateDriver 鍒涘缓瀵瑰簲椹卞姩
+                        //
+                        // TODO: 浠庢暟鎹簱璇诲彇璁惧璇︾粏淇℃伅锛坱ransport, endpoint锛�
+                        // var device = GetDeviceDefinition(deviceId);
+                        // var driver = DriverFactory.CreateDriver(deviceId, device.Transport, device.Endpoint);
+
+                        log.Debug($"璁惧 {deviceId} 椹卞姩娉ㄥ唽寰呴厤缃缁嗕俊鎭�");
+                        loaded++;
+                    }
+                    catch (Exception ex)
+                    {
+                        log.Error($"娉ㄥ唽璁惧 {deviceId} 椹卞姩澶辫触: {ex.Message}", ex);
+                    }
+                }
+
+                log.Info($"浠庤澶囬厤缃姞杞戒簡 {loaded} 涓澶囬┍鍔�");
+            }
+            catch (Exception ex)
+            {
+                log.Error("鍔犺浇璁惧閰嶇疆澶辫触", ex);
+            }
+        }
+
+        /// <summary>
+        /// 娉ㄥ唽鍗曚釜璁惧椹卞姩锛堜緵澶栭儴璋冪敤锛岀敤浜庡姩鎬佹坊鍔犺澶囷級
+        /// </summary>
+        public static void RegisterDevice(string deviceId, string transport, string endpoint)
+        {
+            var container = new UtilContainer();
+            var deviceManager = container.GetUtil<IDeviceManager>("DeviceManager");
+            if (deviceManager == null)
+            {
+                log.Error("DeviceManager 鏈垵濮嬪寲");
+                return;
+            }
+
+            try
+            {
+                var driver = DriverFactory.CreateDriver(deviceId, transport, endpoint);
+                deviceManager.RegisterDriver(deviceId, driver);
+                log.Info($"璁惧 {deviceId} ({transport}) 椹卞姩宸叉敞鍐�");
+            }
+            catch (Exception ex)
+            {
+                log.Error($"娉ㄥ唽璁惧 {deviceId} 澶辫触: {ex.Message}", ex);
+            }
+        }
+
+        /// <summary>
+        /// 閲嶇疆鍩虹璁炬柦锛堢敤浜庢祴璇曟垨閲嶆柊鍒濆鍖栵級
+        /// </summary>
+        public static void Reset()
+        {
+            _initialized = false;
+            log.Info("鍩虹璁炬柦宸查噸缃�");
+        }
+    }
+}

--
Gitblit v1.9.1