From ab2076893b8df3c14f1a126d47e9eee132a38f4b Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期二, 24 三月 2026 10:31:51 +0800
Subject: [PATCH] Implement protocol template monitoring and simulator management UI

---
 Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzNet/QuartzNetExtension.cs |  143 ++++++++++++++++++++++++++---------------------
 1 files changed, 79 insertions(+), 64 deletions(-)

diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzNet/QuartzNetExtension.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzNet/QuartzNetExtension.cs
index 95605e7..50b4e4e 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzNet/QuartzNetExtension.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzNet/QuartzNetExtension.cs
@@ -1,14 +1,9 @@
-锘縰sing System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Reflection;
-using System.Text;
-using System.Threading.Tasks;
-using WIDESEAWCS_Core.Helper;
+锘縰sing System.Reflection;
+using WIDESEAWCS_Common;
 using WIDESEAWCS_Core;
+using WIDESEAWCS_Core.Caches;
+using WIDESEAWCS_Core.Helper;
 using WIDESEAWCS_QuartzJob.DTO;
-using Microsoft.Extensions.Logging;
-using WIDESEAWCS_QuartzJob.QuartzExtensions;
 using WIDESEAWCS_QuartzJob.Service;
 
 namespace WIDESEAWCS_QuartzJob.QuartzNet
@@ -22,19 +17,21 @@
         private readonly IDeviceInfoService _deviceInfoService;
         private readonly IDispatchInfoService _dispatchInfoService;
         private readonly IDeviceProtocolDetailService _deviceProtocolDetailService;
+        private readonly ICacheService _cacheService;
         private readonly Storage _storage;
 
         /// <summary>
         /// 鍚姩绋嬪簭鑷姩寮�鍚皟搴︽湇鍔�
         /// </summary>
         /// <returns></returns>
-        public QuartzNetExtension(IDeviceInfoService deviceInfoService, IDispatchInfoService dispatchInfoService, ISchedulerCenter schedulerCenter, IDeviceProtocolDetailService deviceProtocolDetailService, Storage storage)
+        public QuartzNetExtension(IDeviceInfoService deviceInfoService, IDispatchInfoService dispatchInfoService, ISchedulerCenter schedulerCenter, IDeviceProtocolDetailService deviceProtocolDetailService, Storage storage, ICacheService cacheService)
         {
             _deviceInfoService = deviceInfoService;
             _dispatchInfoService = dispatchInfoService;
             _schedulerCenter = schedulerCenter;
             _deviceProtocolDetailService = deviceProtocolDetailService;
             _storage = storage;
+            _cacheService = cacheService;
         }
 
         /// <summary>
@@ -47,6 +44,7 @@
             {
                 List<DispatchInfoDTO> dispatches = _dispatchInfoService.QueryDispatchInfos();
                 List<DeviceInfoDTO> deviceInfos = await _deviceInfoService.QueryDeviceProInfos();
+                _cacheService.RemoveByPrefix($"{RedisPrefix.System}");
 
                 deviceInfos.ForEach(x =>
                 {
@@ -54,62 +52,69 @@
                     {
                         try
                         {
-                            #region 杩炴帴PLC
-                            // 鍔犺浇绋嬪簭闆�
-                            Assembly assembly = Assembly.Load($"WIDESEAWCS_Communicator");
-                            // 鑾峰彇绫诲瀷
-                            Type? type = assembly.GetType($"WIDESEAWCS_Communicator.{x.DevicePlcType}");
-                            // 鍒涘缓瀹炰緥
-                            object? obj = Activator.CreateInstance(type, new object[] { x.DeviceIp, x.DevicePort, x.DeviceName });
-                            // 璋冪敤杩炴帴鏂规硶
-                            bool? connectResult = (bool)type.InvokeMember("Connect", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj, new object[] { });
-                            // 鍒ゆ柇杩炴帴缁撴灉
-                            if (connectResult ?? false) ConsoleHelper.WriteSuccessLine(type.Name + x.DeviceCode + "杩炴帴鎴愬姛"); else ConsoleHelper.WriteErrorLine(type.Name + x.DeviceCode + "杩炴帴澶辫触");
-                            #endregion
-
-
-
-                            #region 瀹炰緥鍖栬澶囧璞�
-                            List<DeviceProDTO> devicePros = x.ProtocolList.Select(d => new DeviceProDTO
+                            if (!x.DevicePlcType.Contains("Socket"))
                             {
-                                // 璁惧瀛愮紪鐮�
-                                DeviceChildCode = d.DeviceChildCode,
-                                // 璁惧鏁版嵁绫诲瀷
-                                DeviceDataType = d.DeviceProDataType,
-                                // 璁惧ID
-                                DeviceId = d.DeviceId,
-                                // 璁惧鍗忚ID
-                                DeviceProId = d.Id,
-                                // 璁惧鍗忚鏁版嵁鍧�
-                                DeviceProDataBlock = d.DeviceProDataBlock,
-                                // 璁惧鍗忚鏁版嵁闀垮害
-                                DeviceProDataLength = d.DeviceProDataLength,
-                                // 璁惧鍗忚鍋忕Щ閲�
-                                DeviceProOffset = d.DeviceProOffset,
-                                // 璁惧鍗忚鍙傛暟鎻忚堪
-                                DeviceProParamDes = d.DeviceProParamDes,
-                                // 璁惧鍗忚鍙傛暟鍚嶇О
-                                DeviceProParamName = d.DeviceProParamName,
-                                // 璁惧鍗忚鍙傛暟绫诲瀷
-                                DeviceProParamType = d.DeviceProParamType,
-                                // 璁惧PLC绫诲瀷
-                                DevicePlcType = x.DevicePlcType
-                            }).ToList();
+                                #region 杩炴帴PLC
 
-                            // 鏍规嵁璁惧绫诲瀷鑾峰彇璁惧鍗忚璇︽儏
-                            List<DeviceProtocolDetailDTO> deviceProtocolDetails = _deviceProtocolDetailService.GetDeviceProtocolDetailsByDeviceType(x.DeviceType);
+                                // 鍔犺浇绋嬪簭闆�
+                                Assembly assembly = Assembly.Load($"WIDESEAWCS_Communicator");
+                                // 鑾峰彇绫诲瀷
+                                Type? type = assembly.GetType($"WIDESEAWCS_Communicator.{x.DevicePlcType}");
+                                // 鍒涘缓瀹炰緥
+                                object? obj = Activator.CreateInstance(type, new object[] { x.DeviceIp, x.DevicePort, x.DeviceName });
+                                // 璋冪敤杩炴帴鏂规硶
+                                bool? connectResult = (bool)type.InvokeMember("Connect", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj, new object[] { });
+                                // 鍒ゆ柇杩炴帴缁撴灉
+                                if (connectResult ?? false) ConsoleHelper.WriteSuccessLine(type.Name + x.DeviceCode + "杩炴帴鎴愬姛"); else ConsoleHelper.WriteErrorLine(type.Name + x.DeviceCode + "杩炴帴澶辫触");
 
-                            // 鍔犺浇璁惧绋嬪簭闆�
-                            Assembly assemblyDevice = Assembly.Load($"WIDESEAWCS_QuartzJob");
-                            // 鑾峰彇璁惧绫诲瀷瀵瑰簲鐨勭被鍨�
-                            Type typeDevice = assemblyDevice.GetType($"WIDESEAWCS_QuartzJob.{x.DeviceType}");
-                            // 鍒涘缓璁惧瀹炰緥
-                            object deviceInstance = Activator.CreateInstance(typeDevice, new object[] { obj, devicePros, deviceProtocolDetails, x.DeviceCode, x.DeviceName });
-                            #endregion
+                                #endregion 杩炴帴PLC
 
-                            x.Device = (IDevice)deviceInstance;
+                                #region 瀹炰緥鍖栬澶囧璞�
 
-                            Storage.Devices.Add((IDevice)deviceInstance);
+                                List<DeviceProDTO> devicePros = x.ProtocolList.Select(d => new DeviceProDTO
+                                {
+                                    // 璁惧瀛愮紪鐮�
+                                    DeviceChildCode = d.DeviceChildCode,
+                                    // 璁惧鏁版嵁绫诲瀷
+                                    DeviceDataType = d.DeviceProDataType,
+                                    // 璁惧ID
+                                    DeviceId = d.DeviceId,
+                                    // 璁惧鍗忚ID
+                                    DeviceProId = d.Id,
+                                    // 璁惧鍗忚鏁版嵁鍧�
+                                    DeviceProDataBlock = d.DeviceProDataBlock,
+                                    // 璁惧鍗忚鏁版嵁闀垮害
+                                    DeviceProDataLength = d.DeviceProDataLength,
+                                    // 璁惧鍗忚鍋忕Щ閲�
+                                    DeviceProOffset = d.DeviceProOffset,
+                                    // 璁惧鍗忚鍙傛暟鎻忚堪
+                                    DeviceProParamDes = d.DeviceProParamDes,
+                                    // 璁惧鍗忚鍙傛暟鍚嶇О
+                                    DeviceProParamName = d.DeviceProParamName,
+                                    // 璁惧鍗忚鍙傛暟绫诲瀷
+                                    DeviceProParamType = d.DeviceProParamType,
+                                    // 璁惧PLC绫诲瀷
+                                    DevicePlcType = x.DevicePlcType
+                                }).ToList();
+
+                                // 鏍规嵁璁惧绫诲瀷鑾峰彇璁惧鍗忚璇︽儏
+                                List<DeviceProtocolDetailDTO> deviceProtocolDetails = _deviceProtocolDetailService.GetDeviceProtocolDetailsByDeviceId(x.Id);
+
+                                // 鍔犺浇璁惧绋嬪簭闆�
+                                Assembly assemblyDevice = Assembly.Load($"WIDESEAWCS_QuartzJob");
+                                // 鑾峰彇璁惧绫诲瀷瀵瑰簲鐨勭被鍨�
+                                Type typeDevice = assemblyDevice.GetType($"WIDESEAWCS_QuartzJob.{x.DeviceType}");
+                                // 鍒涘缓璁惧瀹炰緥
+                                object deviceInstance = Activator.CreateInstance(typeDevice, new object[] { obj, devicePros, deviceProtocolDetails, x.DeviceCode, x.DeviceName });
+
+                                #endregion 瀹炰緥鍖栬澶囧璞�
+
+                                x.Device = (IDevice)deviceInstance;
+
+                                Storage.Devices.Add((IDevice)deviceInstance);
+
+                                _cacheService.AddObject($"{RedisPrefix.System}:{RedisName.IDevice}:{x.DeviceName}", (IDevice)deviceInstance);
+                            }
                         }
                         catch (Exception ex)
                         {
@@ -123,8 +128,18 @@
                 });
                 for (int i = 0; i < dispatches.Count; i++)
                 {
-                    DeviceInfoDTO? deviceProInfo = deviceInfos.FirstOrDefault(x => x.Id == dispatches[i].Id);
-                    dispatches[i].JobParams = deviceProInfo?.Device;
+                    var targetDevice = deviceInfos.FirstOrDefault(x => x.DispatchId == dispatches[i].Id);
+
+                    if (targetDevice is null) continue;
+
+
+                    // 浣跨敤妯″紡鍖归厤
+                    dispatches[i].JobParams = targetDevice switch
+                    {
+                        { DevicePlcType: var type } when type.Contains("Socket")
+                            => new RobotCraneDevice { DeviceCode = targetDevice.DeviceCode, DeviceName = targetDevice.DeviceName, IPAddress = targetDevice.DeviceIp + ":" + targetDevice.DevicePort },
+                        _ => targetDevice.Device
+                    };
                     WebResponseContent responseContent = await _schedulerCenter.AddScheduleJobAsync(dispatches[i]);
                     if (responseContent.Status) ConsoleHelper.WriteSuccessLine(dispatches[i].Name + "璋冨害鏈嶅姟娣诲姞鎴愬姛"); else ConsoleHelper.WriteErrorLine(dispatches[i].Name + "璋冨害鏈嶅姟娣诲姞澶辫触");
                 }
@@ -137,4 +152,4 @@
             }
         }
     }
-}
+}
\ No newline at end of file

--
Gitblit v1.9.3