From 5270308151082506e0e6df2c72d278d2976ec860 Mon Sep 17 00:00:00 2001
From: liulijun <liulijun@hnkhzn.com>
Date: 星期三, 29 四月 2026 10:28:58 +0800
Subject: [PATCH] WCS设备监控

---
 项目代码/WCS/WCSServices/WIDESEAWCS_Tasks/成品仓/ConveyorLineJob_CPD.cs |  250 +++++++++++++++++++++++++++++++------------------
 1 files changed, 156 insertions(+), 94 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WCSServices/WIDESEAWCS_Tasks/\346\210\220\345\223\201\344\273\223/ConveyorLineJob_CPD.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WCSServices/WIDESEAWCS_Tasks/\346\210\220\345\223\201\344\273\223/ConveyorLineJob_CPD.cs"
index 775af1f..b80f145 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WCSServices/WIDESEAWCS_Tasks/\346\210\220\345\223\201\344\273\223/ConveyorLineJob_CPD.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WCSServices/WIDESEAWCS_Tasks/\346\210\220\345\223\201\344\273\223/ConveyorLineJob_CPD.cs"
@@ -1,4 +1,4 @@
-锘縰sing Microsoft.AspNetCore.Components.Routing;
+using Microsoft.AspNetCore.Components.Routing;
 using Newtonsoft.Json;
 using Quartz;
 using SqlSugar.Extensions;
@@ -20,6 +20,7 @@
 using WIDESEAWCS_QuartzJob.Repository;
 using WIDESEAWCS_QuartzJob.Service;
 using WIDESEAWCS_Tasks.ConveyorLineJob;
+using HslCommunication.WebSocket;
 using ICacheService = WIDESEAWCS_Core.Caches.ICacheService;
 
 namespace WIDESEAWCS_Tasks
@@ -35,9 +36,10 @@
         private readonly IRouterRepository _routerRepository;
         private readonly IRouterService _routerService;
         private readonly IRouterExtension _routerExtension;
+        private readonly WebSocketServer _webSocketServer;
         private readonly List<Dt_WarehouseDevice> warehouseDevices;
 
-        public ConveyorLineJob_CPD(ICacheService cacheService, ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IStationMangerRepository stationMangerRepository, IRouterRepository routerRepository, IRouterService routerService, IRouterExtension routerExtension)
+        public ConveyorLineJob_CPD(ICacheService cacheService, ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IStationMangerRepository stationMangerRepository, IRouterRepository routerRepository, IRouterService routerService, IRouterExtension routerExtension, WebSocketServer webSocketServer)
         {
             _cacheService = cacheService;
             _taskService = taskService;
@@ -47,6 +49,7 @@
             _routerRepository = routerRepository;
             _routerService = routerService;
             _routerExtension = routerExtension;
+            _webSocketServer = webSocketServer;
 
             string? warehouseDevicesStr = _cacheService.Get<string>(nameof(Dt_WarehouseDevice));
             if (!string.IsNullOrEmpty(warehouseDevicesStr))
@@ -61,111 +64,170 @@
 
         public Task Execute(IJobExecutionContext context)
         {
-            bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value);
-            if (flag && value != null)
+            try
             {
-                OtherDevice device = (OtherDevice)value;
-                List<string> deviceStations = device.DeviceProDTOs.Select(x => x.DeviceChildCode).Distinct().ToList();
-                List<Dt_StationManger> stationMangers = _stationMangerRepository.QueryData(x => x.StationDeviceCode == device.DeviceCode);
-                foreach (var item in stationMangers.Where(x => deviceStations.Contains(x.StationCode)))
+                bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value);
+                if (flag && value != null)
                 {
-                    DeviceProDTO? deviceProRead = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(R_ConveyorLineCPDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
-                    DeviceProDTO? deviceProWrite = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(W_ConveyorLineCPDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
-                    if (item.StationType == StationTypeEnum.StationType_InStartAndOutEnd.ObjToInt() && deviceProRead != null && deviceProWrite != null)
-                    {
-                        R_ConveyorLineCPInfo conveyorLineInfoRead = device.Communicator.ReadCustomer<R_ConveyorLineCPInfo>(deviceProRead.DeviceProAddress);
-                        if (conveyorLineInfoRead!=null && conveyorLineInfoRead.Command == 4 && conveyorLineInfoRead.TaskNo <= 0)
-                        {
-                            Dt_Task task = _taskRepository.QueryFirst(x => x.CurrentAddress == item.StationCode && _taskService.TaskInboundTypes.Contains(x.TaskType) && x.TaskState == TaskStatusEnum.New.ObjToInt() && x.DeviceCode == device.DeviceCode);
-                            if (task != null)
-                            {
-                                //鍒嗛厤宸烽亾 瀛樺叆鎵樼洏鏁版嵁
-                                List<Dt_Router> routers = _routerService.QueryNextRoutes(item.StationCode, "SC02_CP", task.TaskType);
-                                Dt_Router? router = routers.FirstOrDefault();
-                                if (routers == null || routers.Count == 0 || router == null)
-                                {
-                                    WriteError(item.StationName, $"鏈壘鍒板搴旇矾鐢变俊鎭�,璁惧缂栧彿:{item.StationCode},浠诲姟鍙�:{task.TaskNum}");
-                                    continue;
-                                }
-                                if (routers.Count > 1)
-                                {
-                                    WriteError(item.StationName, $"璺敱淇℃伅閰嶇疆閿欒,璁惧缂栧彿:{item.StationCode},浠诲姟鍙�:{task.TaskNum}");
-                                    continue;
-                                }
-                                //鍐欏叆鍏ュ簱绾夸綋鍦板潃
-                                device.SetValue(W_ConveyorLineCPDB.W_TargetAddress, router.NextPosi, item.StationCode);
-                                device.SetValue(W_ConveyorLineCPDB.W_TaskNo, task.TaskNum, item.StationCode);
-                                device.SetValue(W_ConveyorLineCPDB.W_Command, 1, item.StationCode);
-                                //鏇存柊浠诲姟淇℃伅
-                                task.CurrentAddress = item.StationCode;
-                                task.NextAddress = router.NextPosi;
-                                task.PalletType = 1;
-                                task.PalletCode = conveyorLineInfoRead.PalletCode;
-                                task.Roadway = "SC02_CP";
-                                _taskService.UpdateTask(task, TaskStatusEnum.Line_Executing);
-                                WriteError(item.StationName, $"浠诲姟鍙穥conveyorLineInfoRead.TaskNo}涓嬩竴姝�");
-                            }
-                        }
-                    }
-                    else if (item.StationType == StationTypeEnum.StationType_OnlyInbound.ObjToInt() && deviceProRead != null)
-                    {
-                        R_ConveyorLineCPInfo conveyorLineInfoRead = device.Communicator.ReadCustomer<R_ConveyorLineCPInfo>(deviceProRead.DeviceProAddress);
-                        if (conveyorLineInfoRead != null && conveyorLineInfoRead.Command == 2 && conveyorLineInfoRead.TaskNo > 0)
-                        {
-                            Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == conveyorLineInfoRead.TaskNo && x.NextAddress == item.StationCode && _taskService.TaskInboundTypes.Contains(x.TaskType) && x.TaskState == TaskStatusEnum.Line_Executing.ObjToInt() && x.DeviceCode == device.DeviceCode);
-                            if (task != null)
-                            {
-                                //鍒嗛厤璐т綅
-                                string local = "SC02_CP-002-064-001-01";
-                                task.CurrentAddress = item.StackerCraneStationCode;
-                                task.TargetAddress = local;
-                                task.NextAddress = local;
-                                task.DeviceCode = item.StackerCraneCode;
-                                _taskService.UpdateTask(task, TaskStatusEnum.SC_Execute);
-                                WriteError(item.StationName, $"浠诲姟鍙�:{task.TaskNum}杩涜鍫嗗灈鏈哄叆搴�");
-                                //string? locationCode = _taskService.RequestAssignLocation(task.TaskNum, "AGV_CP");
-                                //if (string.IsNullOrEmpty(locationCode) && task.TaskType != TaskTypeEnum.EmptyProductBack.ObjToInt())
-                                //{
-                                //    WriteError(item.StationName, $"璇锋眰鍒嗛厤璐т綅杩斿洖淇℃伅閿欒,璁惧缂栧彿:{item.StationCode},浠诲姟鍙�:{task.TaskNum}");
-                                //    continue;
-                                //}
+                    OtherDevice device = (OtherDevice)value;
+                    //鑾峰彇鎵�鏈夊崗璁殑杈撻�佺嚎绔欏彴
+                    List<string> deviceStations = device.DeviceProDTOs.Select(x => x.DeviceChildCode).Distinct().ToList();
+                    List<Dt_StationManger> stationMangers = _stationMangerRepository.QueryData(x => x.StationDeviceCode == device.DeviceCode);
 
-                            }
-                        }
-                    }
-                    else if (item.StationType == StationTypeEnum.StationType_OnlyOutbound.ObjToInt() && deviceProRead != null && deviceProWrite != null)
+                    // 鍒涘缓杈撻�佺嚎鏁版嵁瀵硅薄锛岀敤浜嶹ebSocket鍙戦��
+                    var conveyorLineCPData = new Dictionary<string, object>();
+                    conveyorLineCPData["璁惧缂栧彿"] = device.DeviceCode;
+                    conveyorLineCPData["璁惧鍚嶇О"] = "鎴愬搧搴撲竴妤艰緭閫佺嚎";
+                    conveyorLineCPData["ConveyorLineCPInfo"] = new Dictionary<string, Dictionary<string, object>>();
+                    foreach (var item in stationMangers.Where(x => deviceStations.Contains(x.StationCode)))
                     {
-                        R_ConveyorLineCPInfo conveyorLineInfoRead = device.Communicator.ReadCustomer<R_ConveyorLineCPInfo>(deviceProRead.DeviceProAddress);
-                        if (conveyorLineInfoRead != null && conveyorLineInfoRead.Command == 4 && conveyorLineInfoRead.TaskNo <= 0)
+                        DeviceProDTO? deviceProRead = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(R_ConveyorLineCPDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
+                        DeviceProDTO? deviceProWrite = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(W_ConveyorLineCPDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
+                        
+                        // 鍏堣鍙栨墍鏈夌被鍨嬬珯鍙扮殑璁惧淇℃伅骞舵坊鍔犲埌WebSocket鏁版嵁涓�
+                        if (deviceProRead != null)
                         {
-                            //鑾峰彇鍑哄簱绔欏彴鏄惁瀛樺湪鍑哄簱寰呮墽琛屼换鍔�
-                            Dt_Task task = _taskRepository.QueryFirst(x => x.CurrentAddress == item.StationCode && _taskService.TaskOutboundTypes.Contains(x.TaskType)
-                            && x.TaskState == TaskStatusEnum.Line_Execute.ObjToInt() && x.DeviceCode == device.DeviceCode);
-                            if (task != null)
+                            R_ConveyorLineCPInfo conveyorLineInfoRead = device.Communicator.ReadCustomer<R_ConveyorLineCPInfo>(deviceProRead.DeviceProAddress);
+                            
+                            // 鐒跺悗鏍规嵁涓嶅悓鐨勭珯鍙扮被鍨嬫墽琛屼笉鍚岀殑涓氬姟閫昏緫
+                            if (conveyorLineInfoRead != null)
                             {
-                                //鑾峰彇璺敱閰嶇疆
-                                List<Dt_Router> routers = _routerRepository.QueryData(x => x.InOutType == task.TaskType && task.CurrentAddress == x.StartPosi);
-                                Dt_Router router = routers.FirstOrDefault();
-                                if (router == null)
+                                // 鎴愬搧涓�妤煎叆搴撳彛鍐欏叆瀵瑰簲鍏ュ簱绔欏彴鍦板潃
+                                if (item.StationType == StationTypeEnum.StationType_InStartAndOutEnd.ObjToInt() && deviceProRead != null && deviceProWrite != null)
                                 {
-                                    WriteError(item.StationName, $"浠诲姟鍙�:{task.TaskNum}鏈壘鍒拌矾鐢遍厤缃俊鎭�");
-                                    return Task.CompletedTask;
+                                    if (conveyorLineInfoRead != null && conveyorLineInfoRead.Command == 4 && conveyorLineInfoRead.TaskNo <= 0)
+                                    {
+                                        Dt_Task task = _taskRepository.QueryFirst(x => x.CurrentAddress == item.StationCode && _taskService.TaskInboundTypes.Contains(x.TaskType) && x.TaskState == TaskStatusEnum.AGV_Finish.ObjToInt() && x.DeviceCode == device.DeviceCode);
+                                        if (task != null)
+                                        {
+                                            //鍒嗛厤宸烽亾 瀛樺叆鎵樼洏鏁版嵁
+                                            List<Dt_Router> routers = _routerService.QueryNextRoutes(item.StationCode, task.Roadway, task.TaskType);
+                                            Dt_Router? router = routers.FirstOrDefault();
+                                            if (routers == null || routers.Count == 0 || router == null)
+                                            {
+                                                WriteError(item.StationName, $"鏈壘鍒板搴旇矾鐢变俊鎭�,璁惧缂栧彿:{item.StationCode},浠诲姟鍙�:{task.TaskNum}");
+                                                continue;
+                                            }
+                                            if (routers.Count > 1)
+                                            {
+                                                WriteError(item.StationName, $"璺敱淇℃伅閰嶇疆閿欒,璁惧缂栧彿:{item.StationCode},浠诲姟鍙�:{task.TaskNum}");
+                                                continue;
+                                            }
+                                            //鍐欏叆鍏ュ簱绾夸綋鍦板潃
+                                            device.SetValue(W_ConveyorLineCPDB.W_TargetAddress, router.NextPosi, item.StationCode);
+                                            device.SetValue(W_ConveyorLineCPDB.W_TaskNo, task.TaskNum, item.StationCode);
+                                            device.SetValue(W_ConveyorLineCPDB.W_Command, 1, item.StationCode);
+                                            //鏇存柊浠诲姟淇℃伅
+                                            task.CurrentAddress = item.StationCode;
+                                            task.NextAddress = router.NextPosi;
+                                            _taskService.UpdateTask(task, TaskStatusEnum.Line_Executing);
+                                            WriteInfo(item.StationName, $"浠诲姟鍙穥conveyorLineInfoRead.TaskNo}涓嬩竴姝�");
+                                        }
+                                    }
+                                    // 涓�妤煎嚭搴撳彛鍒ゆ柇鐢熸垚AGV鍙栨枡浠诲姟
+                                    else if (conveyorLineInfoRead != null && conveyorLineInfoRead.Command == 2 && conveyorLineInfoRead.TaskNo > 0)//涓�妤煎嚭搴撳彛鍒ゆ柇鐢熸垚AGV鍙栨枡浠诲姟
+                                    {
+                                        Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == conveyorLineInfoRead.TaskNo && x.NextAddress == item.StationCode && _taskService.TaskOutboundTypes.Contains(x.TaskType) && x.TaskState == TaskStatusEnum.Line_Executing.ObjToInt() && x.DeviceCode == device.DeviceCode);
+                                        if (task != null)
+                                        {
+                                            //鍒嗛厤宸烽亾 瀛樺叆鎵樼洏鏁版嵁
+                                            List<Dt_Router> routers = _routerRepository.QueryData(x => x.InOutType == task.TaskType && task.NextAddress == x.StartPosi);
+                                            Dt_Router router = routers.FirstOrDefault();
+                                            if (router == null)
+                                            {
+                                                WriteError(item.StationName, $"浠诲姟鍙�:{task.TaskNum}鏈壘鍒拌矾鐢遍厤缃俊鎭�");
+                                                return Task.CompletedTask;
+                                            }
+                                            //鏇存柊浠诲姟淇℃伅
+                                            task.CurrentAddress = router.StartPosi;
+                                            task.NextAddress = task.TargetAddress;
+                                            task.DeviceCode = router.NextPosi;
+                                            _taskService.UpdateTask(task, TaskStatusEnum.AGV_Execute);
+                                            WriteInfo(item.StationName, $"浠诲姟鍙穥conveyorLineInfoRead.TaskNo}涓嬩竴姝�");
+                                        }
+                                    }
                                 }
-                                //鍐欏叆鍑哄簱绾夸綋鍦板潃
-                                device.SetValue(W_ConveyorLineCPDB.W_TargetAddress, router.NextPosi, item.StationCode);
-                                device.SetValue(W_ConveyorLineCPDB.W_TaskNo, task.TaskNum, item.StationCode);
-                                device.SetValue(W_ConveyorLineCPDB.W_Command, 1, item.StationCode);
-                                _taskService.UpdateTask(task, TaskStatusEnum.Line_Executing);
-                                WriteError(item.StationName, $"浠诲姟鍙�:{task.TaskNum}杩涜绾夸綋鍑哄簱");
+                                // 鍏ュ簱绔欏彴鍒嗛厤璐т綅鍑嗗鍫嗗灈鍏ュ簱
+                                else if (item.StationType == StationTypeEnum.StationType_OnlyInbound.ObjToInt() && deviceProRead != null)
+                                {
+                                    if (conveyorLineInfoRead != null && conveyorLineInfoRead.Command == 2 && conveyorLineInfoRead.TaskNo > 0)
+                                    {
+                                        Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == conveyorLineInfoRead.TaskNo && x.NextAddress == item.StationCode && _taskService.TaskInboundTypes.Contains(x.TaskType) && x.TaskState == TaskStatusEnum.Line_Executing.ObjToInt() && x.DeviceCode == device.DeviceCode);
+                                        if (task != null)
+                                        {
+                                            //鍒嗛厤璐т綅
+                                            string? local = _taskService.RequestAssignLocation(task.TaskNum, task.Roadway);
+                                            if (!string.IsNullOrEmpty(local))
+                                            {
+                                                task.CurrentAddress = item.StackerCraneStationCode;
+                                                task.TargetAddress = local;
+                                                task.NextAddress = local;
+                                                task.DeviceCode = item.StackerCraneCode;
+                                                _taskService.UpdateTask(task, TaskStatusEnum.SC_Execute);
+                                                WriteInfo(item.StationName, $"浠诲姟鍙�:{task.TaskNum}杩涜鍫嗗灈鏈哄叆搴�");
+                                            }
+                                        }
+                                    }
+                                }
+                                else if (item.StationType == StationTypeEnum.StationType_OnlyOutbound.ObjToInt() && deviceProRead != null && deviceProWrite != null)
+                                {
+                                    //鍑哄簱绔欏彴鍒嗛厤鍑哄簱瀵瑰簲鐩爣鍑哄簱鍙e湴鍧�
+                                    if (conveyorLineInfoRead != null && conveyorLineInfoRead.Command == 4 && conveyorLineInfoRead.TaskNo <= 0)
+                                    {
+                                        //鑾峰彇鍑哄簱绔欏彴鏄惁瀛樺湪鍑哄簱寰呮墽琛屼换鍔�
+                                        Dt_Task task = _taskRepository.QueryFirst(x => x.CurrentAddress == item.StationCode && _taskService.TaskOutboundTypes.Contains(x.TaskType)
+                                        && x.TaskState == TaskStatusEnum.Line_Execute.ObjToInt() && x.DeviceCode == device.DeviceCode);
+                                        if (task != null)
+                                        {
+                                            //鑾峰彇璺敱閰嶇疆
+                                            List<Dt_Router> routers = _routerRepository.QueryData(x => x.InOutType == task.TaskType && task.CurrentAddress == x.StartPosi);
+                                            Dt_Router router = routers.FirstOrDefault();
+                                            if (router == null)
+                                            {
+                                                WriteError(item.StationName, $"浠诲姟鍙�:{task.TaskNum}鏈壘鍒拌矾鐢遍厤缃俊鎭�");
+                                                return Task.CompletedTask;
+                                            }
+                                            //鍐欏叆鍑哄簱绾夸綋鍦板潃
+                                            device.SetValue(W_ConveyorLineCPDB.W_TargetAddress, router.NextPosi, item.StationCode);
+                                            device.SetValue(W_ConveyorLineCPDB.W_TaskNo, task.TaskNum, item.StationCode);
+                                            device.SetValue(W_ConveyorLineCPDB.W_Command, 1, item.StationCode);
+                                            _taskService.UpdateTask(task, TaskStatusEnum.Line_Executing);
+                                            WriteInfo(item.StationName, $"浠诲姟鍙�:{task.TaskNum}杩涜绾夸綋鍑哄簱");
+                                        }
+                                    }
+                                }
+                                // 灏嗗綋鍓嶇珯鍙扮殑杈撻�佺嚎淇℃伅娣诲姞鍒版暟鎹璞★紝鍙寘鍚渶瑕佺殑瀛楁
+                                if (conveyorLineInfoRead != null)
+                                {
+                                    var simplifiedInfo = new Dictionary<string, object>();
+                                    simplifiedInfo["LineCode"] = device.DeviceCode;
+                                    simplifiedInfo["TaskNum"] = conveyorLineInfoRead.TaskNo; // 浠诲姟鍙�
+                                    simplifiedInfo["PalletCode"] = conveyorLineInfoRead.PalletCode;// 鎵樼洏鏉$爜
+                                    simplifiedInfo["HasGoods"] = conveyorLineInfoRead.TaskNo > 0; // 鏄惁鏈夎揣
+                                    simplifiedInfo["Status"] = conveyorLineInfoRead.StatusPV;// 鐘舵��
+                                    simplifiedInfo["Command"] = conveyorLineInfoRead.Command; // 鍛戒护
+                                    ((Dictionary<string, Dictionary<string, object>>)conveyorLineCPData["ConveyorLineCPInfo"])[item.StationCode] = simplifiedInfo;
+                                }
                             }
                         }
+                        else
+                        {
+                            WriteError(item.StationName, $"鏈壘鍒拌澶囧瓙缂栧彿{item.StationCode}鐨勫崗璁俊鎭�");
+                        }
                     }
-                    else
-                    {
-                        WriteError(item.StationName, $"鏈壘鍒拌澶囧瓙缂栧彿{item.StationCode}鐨勫崗璁俊鎭�");
-                    }
+                    // 閫氳繃WebSocket鍙戦�佽緭閫佺嚎鏁版嵁鍒板墠绔�
+                    // 搴忓垪鍖栨暟鎹�
+                    string jsonData = JsonConvert.SerializeObject(conveyorLineCPData);
+                    // 鍙戦�佹暟鎹埌鎵�鏈夊鎴风
+                    _webSocketServer.PublishAllClientPayload(jsonData);
+                    // 璁板綍鍙戦�佹棩蹇�
+                    WriteInfo(device.DeviceCode, $"WebSocket鍙戦�佽緭閫佺嚎鏁版嵁锛歿jsonData}");
                 }
             }
+            catch (Exception ex)
+            {
+                WriteError(nameof(ConveyorLineJob_CPD),ex.Message);
+            }
             return Task.CompletedTask;
         }
     }

--
Gitblit v1.9.3