From 3799054e5cd8a42a49cc902cc9d9cb46059b3a7d Mon Sep 17 00:00:00 2001
From: huangxiaoqiang <huangxiaoqiang@hnkhzn.com>
Date: 星期日, 14 九月 2025 10:54:54 +0800
Subject: [PATCH] 添加WebSocket将设备运行信息推送到前端,前端将数据接收赋值到对应设备监控页面

---
 项目代码/WCS/WCSServices/WIDESEAWCS_Tasks/原料库/ConveyorLineJob_YL2ndFloor.cs |  181 ++++++++++++++++++++++++++++++++++++---------
 1 files changed, 144 insertions(+), 37 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/ConveyorLineJob_YL2ndFloor.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/ConveyorLineJob_YL2ndFloor.cs"
index d8e3192..68bb3e2 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/ConveyorLineJob_YL2ndFloor.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/ConveyorLineJob_YL2ndFloor.cs"
@@ -1,16 +1,21 @@
-锘縰sing Microsoft.AspNetCore.Components.Routing;
+锘縰sing Autofac.Core;
+using AutoMapper;
+using Microsoft.AspNetCore.Components.Routing;
 using Newtonsoft.Json;
 using Quartz;
 using SqlSugar.Extensions;
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Reflection;
 using System.Text;
 using System.Threading.Tasks;
 using WIDESEAWCS_Common;
 using WIDESEAWCS_Common.TaskEnum;
 using WIDESEAWCS_Communicator;
 using WIDESEAWCS_Core;
+using WIDESEAWCS_Core.Helper;
+using WIDESEAWCS_Core.HttpContextUser;
 using WIDESEAWCS_IBasicInfoRepository;
 using WIDESEAWCS_ITaskInfoRepository;
 using WIDESEAWCS_ITaskInfoService;
@@ -26,65 +31,167 @@
 namespace WIDESEAWCS_Tasks
 {
     [DisallowConcurrentExecution]
-    public class ConveyorLineJob_YL2ndFloor : JobBase, IJob
+    public partial class ConveyorLineJob_YL2ndFloor : JobBase, IJob
     {
-        private readonly ICacheService _cacheService;
         private readonly ITaskService _taskService;
+        private readonly IMapper _mapper;
         private readonly ITaskExecuteDetailService _taskExecuteDetailService;
         private readonly ITaskRepository _taskRepository;
-        private readonly IStationMangerRepository _stationMangerRepository;
         private readonly IRouterRepository _routerRepository;
-        private readonly IRouterService _routerService;
-        private readonly IRouterExtension _routerExtension;
-        private readonly List<Dt_WarehouseDevice> warehouseDevices;
+        private readonly IStationMangerRepository _stationMangerRepository;
 
-        public ConveyorLineJob_YL2ndFloor(ICacheService cacheService, ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IStationMangerRepository stationMangerRepository, IRouterRepository routerRepository, IRouterService routerService, IRouterExtension routerExtension)
+        public ConveyorLineJob_YL2ndFloor(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IStationMangerRepository stationMangerRepository,IMapper mapper, IRouterRepository routerRepository)
         {
-            _cacheService = cacheService;
             _taskService = taskService;
             _taskExecuteDetailService = taskExecuteDetailService;
             _taskRepository = taskRepository;
             _stationMangerRepository = stationMangerRepository;
+            _mapper = mapper;
             _routerRepository = routerRepository;
-            _routerService = routerService;
-            _routerExtension = routerExtension;
-
-            string? warehouseDevicesStr = _cacheService.Get<string>(nameof(Dt_WarehouseDevice));
-            if (!string.IsNullOrEmpty(warehouseDevicesStr))
-            {
-                warehouseDevices = JsonConvert.DeserializeObject<List<Dt_WarehouseDevice>>(warehouseDevicesStr) ?? new List<Dt_WarehouseDevice>();
-            }
-            else
-            {
-                warehouseDevices = new List<Dt_WarehouseDevice>();
-            }
         }
 
         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).ToList();
-                //鑾峰彇鏈夊崗璁殑杈撻�佺嚎-z
-                List<Dt_StationManger> stationMangers = _stationMangerRepository.QueryData(x => x.StationDeviceCode == device.DeviceCode);
-                //璁╂瘡涓緭閫佺嚎鍚姩-z
-                foreach (var item in stationMangers.Where(x => deviceStations.Contains(x.StationCode)))
+                CommonConveyorLine conveyorLine = (CommonConveyorLine)context.JobDetail.JobDataMap.Get("JobParams");
+                if (conveyorLine != 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 (deviceProRead != null)
+                    #region 绔欏彴鏂瑰紡
+
+                    List<Dt_StationManger> stationManagers = _stationMangerRepository.QueryData(x => x.StationDeviceCode == conveyorLine.DeviceCode);
+
+                    foreach (var station in stationManagers)
                     {
-                        
+                        ConveyorLineTaskCommand command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(station.StationCode);
+
+                        DeviceProtocolDetailDTO? deviceProtocolDetails = conveyorLine.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(ConveyorLineTaskCommand.InteractiveSignal) && x.ProtocalDetailValue == command.InteractiveSignal.ToString());
+                        if (deviceProtocolDetails != null)
+                        {
+                            MethodInfo? method = GetType().GetMethod(deviceProtocolDetails.ProtocolDetailType);
+                            if (method != null)
+                            {
+                                method.Invoke(this, new object[] { conveyorLine, command, station });
+                            }
+                        }
                     }
-                    else
-                    {
-                        WriteError(item.StationName, $"鏈壘鍒拌澶囧瓙缂栧彿{item.StationCode}鐨勫崗璁俊鎭�");
-                    }
+                    #endregion 绔欏彴鏂瑰紡
                 }
             }
+            catch (Exception ex)
+            {
+                Console.Out.WriteLine(nameof(ConveyorLineJob_YL2ndFloor) + ":" + DateTime.Now + ":" + ex.ToString(), ex.StackTrace);
+            }
+            finally
+            {
+            }
             return Task.CompletedTask;
         }
+
+        /// <summary>
+        /// 杈撻�佺嚎璇锋眰鍏ュ簱
+        /// </summary>
+        /// <param name="conveyorLine">杈撻�佺嚎瀹炰緥瀵硅薄</param>
+        /// <param name="command">璇诲彇鐨勮姹備俊鎭�</param>
+        /// <param name="childDeviceCode">瀛愯澶囩紪鍙�</param>
+        /// <param name="ProtocalDetailValue">绾夸綋褰撳墠bool璇诲彇鍋忕Щ鍦板潃</param>
+        public void RequestInbound(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, Dt_StationManger stationManger)
+        {
+            try
+            {
+                
+                var task = _taskService.QueryLineExecuteTaskByBarcode(command.TaskNum, stationManger.StationCode);
+                var log = $"鏃堕棿锛氥�恵DateTime.Now}銆戙�恵conveyorLine.DeviceName}銆戞墭鐩樺彿锛氥�恵command.Barcode}銆戜换鍔″彿锛氥�恵command.TaskNum}銆戣澶囩紪鐮侊細銆恵stationManger.StationCode}銆�";
+                ConsoleHelper.WriteSuccessLine(log);
+
+                WriteInfo(conveyorLine.DeviceName, log);
+
+                if(task != null)
+                {
+                    ConveyorLineTaskCommandWrite taskCommand = _mapper.Map<ConveyorLineTaskCommandWrite>(task);
+                    bool sendFlag = SendCommand(taskCommand, conveyorLine, stationManger.StationCode);
+                    if (sendFlag)
+                    {
+                        _taskService.UpdateTaskStatusToNext(task, stationManger);
+                    }
+                }
+                else
+                {
+                    HandleNewTask(conveyorLine, command, stationManger);
+                }
+                
+            }
+            catch (Exception ex)
+            {
+                Console.Out.WriteLine(ex.ToString());
+            }
+        }
+
+        /// <summary>
+        /// 杈撻�佺嚎鍏ュ簱瀹屾垚
+        /// </summary>
+        /// <param name="conveyorLine">杈撻�佺嚎瀹炰緥瀵硅薄</param>
+        /// <param name="command">璇诲彇鐨勮姹備俊鎭�</param>
+        /// <param name="childDeviceCode">瀛愯澶囩紪鍙�</param>
+        /// <param name="ProtocalDetailValue">绾夸綋褰撳墠bool璇诲彇鍋忕Щ鍦板潃</param>
+        public void ConveyorLineInFinish(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, Dt_StationManger stationManger)
+        {
+            var task = _taskService.QueryExecutingTaskByBarcode(command.TaskNum, stationManger.StationCode);
+            if (task != null && task.TaskState != (int)TaskStatusEnum.SC_Execute)
+            {
+                WebResponseContent content = _taskService.UpdateTaskStatusToNext(task, stationManger);
+
+                if (content.Status)
+                {
+                    conveyorLine.SetValue(ConveyorLineDBName.ResponState, 86, stationManger.StationCode);
+                }
+                Console.Out.WriteLine(content.Serialize());
+            }
+        }
+
+        public bool SendCommand(ConveyorLineTaskCommandWrite taskCommand, CommonConveyorLine conveyorLine, string childDeviceCode)
+        {
+            conveyorLine.SetValue(ConveyorLineDBName.TaskNum, taskCommand.TaskNum, childDeviceCode);
+            conveyorLine.SetValue(ConveyorLineDBName.TargetAddress, taskCommand.TargetAddress, childDeviceCode);
+            conveyorLine.SetValue(ConveyorLineDBName.Weight, taskCommand.Weight, childDeviceCode);
+            conveyorLine.SetValue(ConveyorLineDBName.Barcode, taskCommand.Barcode, childDeviceCode);
+            conveyorLine.SetValue(ConveyorLineDBName.ResponState, 86, childDeviceCode);
+
+            for (int i = 0; i < 6; i++)
+            {
+                ConveyorLineTaskCommand command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(childDeviceCode);
+                if (command != null)
+                {
+                    if (command.TaskNum == taskCommand.TaskNum && command.TargetAddress == taskCommand.TargetAddress && command.Weight == taskCommand.Weight && command.Barcode == taskCommand.Barcode)
+                    {
+                        WriteInfo(conveyorLine.DeviceName, $"鏃堕棿锛氥�恵DateTime.Now}銆戝啓鍏ヤ换鍔℃垚鍔熷啓鍏ユ鏁皗i}鍐欏叆浠诲姟銆恵JsonConvert.SerializeObject(taskCommand)}銆�");
+                        return true;
+                    }
+                    if (command.TaskNum != taskCommand.TaskNum)
+                    {
+                        conveyorLine.SetValue(ConveyorLineDBName.TaskNum, taskCommand.TaskNum, childDeviceCode);
+                        Thread.Sleep(100);
+                    }
+                    if (command.Barcode != taskCommand.Barcode)
+                    {
+                        conveyorLine.SetValue(ConveyorLineDBName.Barcode, taskCommand.Barcode, childDeviceCode);
+                        Thread.Sleep(100);
+                    }
+                    if (command.TaskNum != taskCommand.TaskNum)
+                    {
+                        conveyorLine.SetValue(ConveyorLineDBName.TaskNum, taskCommand.TaskNum, childDeviceCode);
+                        Thread.Sleep(100);
+                    }
+                    if (command.TargetAddress != taskCommand.TargetAddress)
+                    {
+                        conveyorLine.SetValue(ConveyorLineDBName.TargetAddress, taskCommand.TargetAddress, childDeviceCode);
+                        Thread.Sleep(100);
+                    }
+                }
+                conveyorLine.SetValue(ConveyorLineDBName.ResponState, 86, childDeviceCode);
+            }
+            WriteInfo(conveyorLine.DeviceName, $"鏃堕棿锛氥�恵DateTime.Now}銆戝啓鍏ヤ换鍔℃垚鍔熶换鍔″彿銆恵taskCommand.TaskNum}銆戞墭鐩樺彿銆恵taskCommand.Barcode}銆戠洰鏍囧湴鍧�銆恵taskCommand.TargetAddress}銆戝綋鍓嶈妭鐐广�恵childDeviceCode}銆�");
+            return false;
+        }
     }
 }

--
Gitblit v1.9.3