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/原料库/StackerCraneJob_YLSC2.cs |  162 ++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 140 insertions(+), 22 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WCSServices/WIDESEAWCS_Tasks/\345\216\237\346\226\231\345\272\223/StackerCraneJob_YLSC2.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WCSServices/WIDESEAWCS_Tasks/\345\216\237\346\226\231\345\272\223/StackerCraneJob_YLSC2.cs"
index d20a39b..76f7d93 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WCSServices/WIDESEAWCS_Tasks/\345\216\237\346\226\231\345\272\223/StackerCraneJob_YLSC2.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WCSServices/WIDESEAWCS_Tasks/\345\216\237\346\226\231\345\272\223/StackerCraneJob_YLSC2.cs"
@@ -1,4 +1,6 @@
-锘縰sing Microsoft.AspNetCore.Components.Routing;
+锘縰sing HslCommunication.WebSocket;
+using Microsoft.AspNetCore.Components.Routing;
+using Newtonsoft.Json;
 using Quartz;
 using System;
 using System.Collections.Generic;
@@ -7,22 +9,30 @@
 using System.Text;
 using System.Threading.Tasks;
 using WIDESEAWCS_Common.TaskEnum;
+using WIDESEAWCS_Core;
 using WIDESEAWCS_Core.Enums;
+using WIDESEAWCS_Core.Helper;
+using WIDESEAWCS_DTO.Equipment;
 using WIDESEAWCS_IBasicInfoRepository;
 using WIDESEAWCS_ITaskInfoRepository;
 using WIDESEAWCS_ITaskInfoService;
 using WIDESEAWCS_Model.Models;
 using WIDESEAWCS_QuartzJob;
 using WIDESEAWCS_QuartzJob.DeviceBase;
+using WIDESEAWCS_QuartzJob.DTO;
 using WIDESEAWCS_QuartzJob.Models;
+using WIDESEAWCS_QuartzJob.Repository;
 using WIDESEAWCS_QuartzJob.Service;
+using WIDESEAWCS_QuartzJob.StackerCrane;
 using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
-using WIDESEAWCS_Tasks.StackerCraneJob;
 using WIDESEAWCS_Tasks;
-using WIDESEAWCS_Core;
-using SqlSugar.Extensions;
 using WIDESEAWCS_Tasks.ConveyorLineJob;
 using WIDESEAWCS_QuartzJob.Repository;
+using WIDESEAWCS_QuartzJob.DTO;
+using WIDESEAWCS_QuartzJob.StackerCrane;
+using WIDESEAWCS_Tasks.StackerCraneJob;
+using WIDESEAWCS_DTO.TaskInfo;
+using WIDESEAWCS_Tasks.鍘熸枡搴�;
 
 namespace WIDESEAWCS_Tasks
 {
@@ -35,8 +45,9 @@
         private readonly IRouterService _routerService;
         private readonly IRouterRepository _routerRepository;
         private readonly IStationMangerRepository _stationMangerRepository;
+        private WebSocketServer _webSocketServer;
 
-        public StackerCraneJob_YLSC2(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IStationMangerRepository stationMangerRepository, IRouterRepository routerRepository)
+        public StackerCraneJob_YLSC2(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IStationMangerRepository stationMangerRepository, IRouterRepository routerRepository, WebSocketServer webSocketServer)
         {
             _taskService = taskService;
             _taskExecuteDetailService = taskExecuteDetailService;
@@ -44,6 +55,7 @@
             _routerService = routerService;
             _stationMangerRepository = stationMangerRepository;
             _routerRepository = routerRepository;
+            _webSocketServer = webSocketServer;
         }
 
         public Task Execute(IJobExecutionContext context)
@@ -57,8 +69,14 @@
                     {
                         commonStackerCrane.StackerCraneTaskCompletedEventHandler += CommonStackerCrane_StackerCraneTaskCompletedEventHandler;//璁㈤槄浠诲姟瀹屾垚浜嬩欢
                     }
-                    commonStackerCrane.CheckStackerCraneTaskCompleted();//闃叉浠诲姟瀹屾垚浜嬩欢鐩戞祴瓒呮椂锛屽啀鎵嬪姩瑙﹀彂涓�娆�
-                    if (commonStackerCrane.StackerCraneAutoStatusValue == StackerCraneAutoStatus.Automatic && commonStackerCrane.StackerCraneStatusValue == StackerCraneStatus.Normal && commonStackerCrane.StackerCraneWorkStatusValue == StackerCraneWorkStatus.Standby)
+                    //commonStackerCrane.CheckStackerCraneTaskCompleted();//闃叉浠诲姟瀹屾垚浜嬩欢鐩戞祴瓒呮椂锛屽啀鎵嬪姩瑙﹀彂涓�娆�
+                    //鑾峰彇褰撳墠鍫嗗灈鏈虹姸鎬�
+                    if (commonStackerCrane.CurrentTaskNum > 0 && (commonStackerCrane.StackerCraneWorkStatusValue == StackerCraneWorkStatus.WorkCompleted || commonStackerCrane.Communicator.Read<short>("DB1000.40.0") == 3))
+                    {
+                        StackerCraneTaskCompletedEventArgs e = new StackerCraneTaskCompletedEventArgs(commonStackerCrane.CurrentTaskNum);
+                        CommonStackerCrane_StackerCraneTaskCompletedEventHandler(commonStackerCrane, e);
+                    }
+                    else if (commonStackerCrane.StackerCraneAutoStatusValue == StackerCraneAutoStatus.Automatic && commonStackerCrane.StackerCraneStatusValue == StackerCraneStatus.Normal && commonStackerCrane.StackerCraneWorkStatusValue == StackerCraneWorkStatus.Standby)//鍒ゆ柇涓嬪彂
                     {
                         short stackerError = commonStackerCrane.Communicator.Read<short>("DB1000.54.0");
                         if (stackerError==0)
@@ -82,8 +100,78 @@
                                     }
                                 }
                             }
+                            ErrorDeviceInfo? deviceInfo = RoadwayError.Roadways.FirstOrDefault(x => x.Code == commonStackerCrane.DeviceCode);
+                            if (deviceInfo != null)
+                            {
+                                RoadwayError.Roadways.Remove(deviceInfo);
+                            }
+                        }
+                        else
+                        {
+                            ErrorDeviceInfo? deviceInfo = RoadwayError.Roadways.FirstOrDefault(x => x.Code == commonStackerCrane.DeviceCode);
+                            if (deviceInfo==null)
+                            {
+                                RoadwayError.Roadways.Add(new ErrorDeviceInfo() { Code = commonStackerCrane.DeviceCode, Roadway = commonStackerCrane.DeviceCode });
+                            }
                         }
                     }
+                    // 璁剧疆璁惧缂栧彿锛屽墠绔敤浜庤瘑鍒爢鍨涙満
+                    YL_DB yL_DB = new YL_DB();
+                    yL_DB.R_PP_Status = commonStackerCrane.DeviceCode;
+                    // 璁剧疆鍫嗗灈鏈虹姸鎬�
+                    yL_DB.YL_Status = commonStackerCrane.StackerCraneStatusValue switch
+                    {
+                        StackerCraneStatus.Fault => 0,
+                        StackerCraneStatus.Normal => 1,
+                        StackerCraneStatus.EmergencyStop => 2,
+                        StackerCraneStatus.Unkonw => 3,
+                    };
+                    yL_DB.YL_AutoStatus = commonStackerCrane.StackerCraneAutoStatusValue switch
+                    {
+                        StackerCraneAutoStatus.SemiAutomatic => 1,
+                        StackerCraneAutoStatus.Maintenance => 2,
+                        StackerCraneAutoStatus.Automatic => 3,
+                        StackerCraneAutoStatus.Manual => 4,
+                        StackerCraneAutoStatus.Unkonw => 5,
+                    };
+                    yL_DB.YL_WorkStatus = commonStackerCrane.StackerCraneWorkStatusValue switch
+                    {
+                        StackerCraneWorkStatus.Standby => 1,
+                        StackerCraneWorkStatus.PickUpCompleted => 2,
+                        StackerCraneWorkStatus.PickUp => 3,
+                        StackerCraneWorkStatus.Putting => 5,
+                        StackerCraneWorkStatus.WorkCompleted => 9,
+                    };
+                    yL_DB.YL_TaskNum = commonStackerCrane.CurrentTaskNum;
+                    yL_DB.YL_WorkType = commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WorkType);
+                    yL_DB.YL_Row = commonStackerCrane.Communicator.Read<short>("DB1000.46");
+                    yL_DB.YL_Column = commonStackerCrane.Communicator.Read<short>("DB1000.48.0");
+                    yL_DB.YL_Layer = commonStackerCrane.Communicator.Read<short>("DB1000.50.0");
+                    // 璇诲彇鎶ヨ淇℃伅
+                    short stackerError2 = commonStackerCrane.Communicator.Read<short>("DB1000.54.0");
+                    yL_DB.StackerAlarm = stackerError2 == 0 ? "鏃�" : $"鎶ヨ浠g爜: {stackerError2}";
+                    // 搴忓垪鍖栧苟鍙戦�佹暟鎹�
+                    string ylDB = JsonConvert.SerializeObject(yL_DB);
+                    _webSocketServer.PublishAllClientPayload(ylDB);
+
+                    #region 璋冪敤浜嬩欢鎬荤嚎閫氱煡鍓嶇
+
+                    EquipmentDTO equipmentDTO = new EquipmentDTO();
+                    object obj = new
+                    {
+                        commonStackerCrane.DeviceName,
+                        commonStackerCrane.DeviceCode,
+                        StackerCraneAutoStatus.Automatic,
+                        StackerCraneStatus.Normal,
+                        StackerCraneWorkStatus.Standby,
+                        TaskNum = commonStackerCrane.CurrentTaskNum,
+                        WorkType = commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WorkType),
+                        StackerAlarm = commonStackerCrane.Communicator.Read<short>("DB1000.54.0"),
+                    };
+                    equipmentDTO.StackerDataJson = obj.Serialize();
+                    _webSocketServer.PublishAllClientPayload(equipmentDTO.Serialize());
+
+                    #endregion
                 }
             }
             catch (Exception ex)
@@ -98,7 +186,7 @@
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
-        private void CommonStackerCrane_StackerCraneTaskCompletedEventHandler(object? sender, WIDESEAWCS_QuartzJob.StackerCrane.StackerCraneTaskCompletedEventArgs e)
+        private void CommonStackerCrane_StackerCraneTaskCompletedEventHandler(object? sender, StackerCraneTaskCompletedEventArgs e)
         {
             CommonStackerCrane? commonStackerCrane = sender as CommonStackerCrane;
             if (commonStackerCrane != null)
@@ -121,14 +209,19 @@
                 {
                     if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                     {
-                        Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.StackerCraneStationCode == task.NextAddress && x.StackerCraneCode == deviceCode);
+                        Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.StationCode == task.NextAddress && x.StackerCraneCode == deviceCode);
                         if (stationManger == null)
                         {
                             _taskExecuteDetailService.AddTaskExecuteDetail(taskNum, $"杈撻�佺嚎鍑哄簱绔欑偣鏈厤缃�,{task.NextAddress}");
                             _taskService.UpdateTaskExceptionMessage(taskNum, $"杈撻�佺嚎鍑哄簱绔欑偣鏈厤缃�,{task.NextAddress}");
                             return WebResponseContent.Instance.Error($"杈撻�佺嚎鍑哄簱绔欑偣鏈厤缃�,{task.NextAddress}");
                         }
-                        Dt_Router router = _routerRepository.QueryFirst(x => x.InOutType == task.TaskType && x.StartPosi == stationManger.StationCode && x.ChildPosi == stationManger.StationDeviceCode);
+                        bool depth = false;
+                        if (task.Grade > 0 && task.TaskType==TaskTypeEnum.PrintYLOutbound.ObjToInt())
+                        {
+                            depth = true;
+                        }
+                        Dt_Router router = _routerRepository.QueryFirst(x => x.InOutType == task.TaskType && x.StartPosi == stationManger.StationCode && x.ChildPosi == stationManger.StationDeviceCode && (depth ? x.Depth == 2 :  true));
                         if (router == null)
                         {
                             _taskService.UpdateTaskExceptionMessage(taskNum, $"鏈壘鍒拌矾鐢变俊鎭�,{task.NextAddress}");
@@ -136,26 +229,42 @@
                             return WebResponseContent.Instance.Error($"鏈壘鍒拌矾鐢变俊鎭�,{task.NextAddress}");
                         }
                         int oldStatus = task.TaskState;
-                        // 鍫嗗灈鏈哄畬鎴�
-                        _taskService.UpdateTask(task, TaskStatusEnum.Line_Execute, deviceCode: stationManger.StationDeviceCode, currentAddress: stationManger.StationCode, nextAddress: router.NextPosi);
+                        IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode);
+                        if (device != null)
+                        {
+                            OtherDevice otherDevice = (OtherDevice)device;
+                            otherDevice.SetValue(WR_CLineYLDB.WR_Task, taskNum, stationManger.StationCode);
+                            // 鍫嗗灈鏈哄畬鎴�
+                            _taskService.UpdateTask(task, TaskStatusEnum.Line_Execute, deviceCode: stationManger.StationDeviceCode, currentAddress: stationManger.StationCode, nextAddress: router.NextPosi);
+                        }
+                        else
+                        {
+                            WriteError(deviceCode, $"鏈壘鍒板嚭搴撶珯鍙般�恵stationManger.StationDeviceCode}銆戝搴旂殑閫氳瀵硅薄锛屾棤娉曞彂閫佽緭閫佺嚎鍑哄簱淇″彿");
+                            _taskService.UpdateTaskExceptionMessage(taskNum, $"鏈壘鍒板嚭搴撶珯鍙般�恵stationManger.StationDeviceCode}銆戝搴旂殑閫氳瀵硅薄锛屾棤娉曞彂閫佽緭閫佺嚎鍑哄簱淇″彿");
+                            return WebResponseContent.Instance.Error($"鏈壘鍒板嚭搴撶珯鍙般�恵stationManger.StationDeviceCode}銆戝搴旂殑閫氳瀵硅薄锛屾棤娉曞彂閫佽緭閫佺嚎鍑哄簱淇″彿");
+                        }
                         _taskExecuteDetailService.AddTaskExecuteDetail(taskNum, $"绯荤粺鑷姩娴佺▼,浠诲姟鐘舵�佷粠銆恵oldStatus}銆戣浆鍒般�恵task.TaskState}銆�");
                         WriteInfo(deviceCode, $"鍫嗗灈鏈哄嚭搴撲换鍔″畬鎴�,浠诲姟鍙�:{taskNum}");
                     }
                     else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup || task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.RelocationGroup)
                     {
-                        WriteInfo(deviceCode, $"鍫嗗灈鏈轰换鍔″畬鎴�,浠诲姟鍙�:{taskNum}");
-                        _taskService.TaskCompleted(taskNum);
+                        WebResponseContent responseContent = _taskService.TaskCompleted(taskNum);
+                        if (!responseContent.Status)
+                        {
+                            _taskService.UpdateTaskExceptionMessage(taskNum, $"{responseContent.Message}");
+                        }
+                        WriteInfo(deviceCode, $"鍫嗗灈鏈哄叆搴撲换鍔″畬鎴�,浠诲姟鍙�:{taskNum}");
                     }
                     else
                     {
-                        WriteInfo(deviceCode, $"鏈壘鍒拌浠诲姟绫诲瀷鍥炶皟WMS浠诲姟瀹屾垚鎺ュ彛,{task.TaskType}");
+                        WriteError(deviceCode, $"鏈壘鍒拌浠诲姟绫诲瀷鍥炶皟WMS浠诲姟瀹屾垚鎺ュ彛,{task.TaskType}");
                         _taskExecuteDetailService.AddTaskExecuteDetail(taskNum, $"鏈壘鍒拌浠诲姟绫诲瀷鍥炶皟WMS浠诲姟瀹屾垚鎺ュ彛,{task.TaskType}");
                         _taskService.UpdateTaskExceptionMessage(taskNum, $"鏈壘鍒拌浠诲姟绫诲瀷鍥炶皟WMS浠诲姟瀹屾垚鎺ュ彛,{task.TaskType}");
                     }
                 }
                 else
                 {
-                    WriteInfo(deviceCode, $"鏈壘鍒颁换鍔′俊鎭�,浠诲姟鍙�:{taskNum}");
+                    WriteError(deviceCode, $"鏈壘鍒颁换鍔′俊鎭�,浠诲姟鍙�:{taskNum}");
                     return WebResponseContent.Instance.Error($"鏈壘鍒颁换鍔′俊鎭�,浠诲姟鍙�:{taskNum}");
                 }
 
@@ -176,10 +285,17 @@
         /// <returns></returns>
         private Dt_Task? GetTask(CommonStackerCrane commonStackerCrane)
         {
+            //鍒ゆ柇鏄惁鏈夋鎵ц鐨勪换鍔★紝濡傛灉鏈夊垯涓嶄笅鍙�
+            Dt_Task? taskOld = _taskRepository.QueryFirst(x => x.DeviceCode == commonStackerCrane.DeviceCode && x.Roadway == commonStackerCrane.DeviceCode && x.TaskState == TaskStatusEnum.SC_Executing.ObjToInt());
+            if (taskOld != null)
+            {
+                return null;
+            }
+
             Dt_Task? task;
             if (commonStackerCrane.LastTaskType == null)
             {
-                task = _taskService.QueryStackerCraneTask(commonStackerCrane.DeviceCode);
+                task = _taskService.QueryStackerYLTask(commonStackerCrane.DeviceCode);
             }
             else
             {
@@ -189,13 +305,13 @@
                     task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode);
                     if (task == null)
                     {
-                        task = _taskService.QueryStackerCraneOutTask(commonStackerCrane.DeviceCode);
+                        task = _taskService.QueryStackerYLTask(commonStackerCrane.DeviceCode, TaskTypeGroup.OutbondGroup);
                     }
                 }
                 else
                 {
                     //涓婁竴涓负鍏ュ簱鍒欐煡鏄惁瀛樺湪鍑哄簱浠诲姟 浜ゆ浛鎵ц
-                    task = _taskService.QueryStackerCraneOutTask(commonStackerCrane.DeviceCode);
+                    task = _taskService.QueryStackerYLTask(commonStackerCrane.DeviceCode, TaskTypeGroup.OutbondGroup);
                     if (task == null)
                     {
                         task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode);
@@ -249,14 +365,15 @@
                     OtherDevice client = (OtherDevice)device;
                     if (client.GetValue<WR_CLineYLDB, short>(WR_CLineYLDB.WR_Request, stationManger.StationCode) != 99)//鍑哄簱绔欏彴鏈鍗犵敤
                     {
-                        task.NextAddress = stationManger.StackerCraneStationCode;
-                        _taskRepository.UpdateData(task);
+                        //task.NextAddress = stationManger.StackerCraneStationCode;
+                        //_taskRepository.UpdateData(task);
                         return task;
                     }
                     else
                     {
                         task.ExceptionMessage = "鍑哄簱绔欏彴鏃犳硶涓嬪彂";
                         _taskRepository.UpdateData(task);
+                        WriteInfo(task.DeviceCode, $"鍑哄簱绔欏彴鏃犳硶涓嬪彂");
                     }
                 }
                 else
@@ -316,7 +433,8 @@
             }
             else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)//鍒ゆ柇鏄惁鏄嚭搴撲换鍔�
             {
-                string[] targetCodes = task.NextAddress.Split("-");
+                Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.StationCode == task.NextAddress && x.StackerCraneCode == task.DeviceCode);
+                string[] targetCodes = stationManger.StackerCraneStationCode.Split("-");
                 if (targetCodes.Length == 3)
                 {
                     stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[0]);

--
Gitblit v1.9.3