From 2d2d6bf8565f5b89fc9ee054bf6e62f9592f8673 Mon Sep 17 00:00:00 2001
From: huangxiaoqiang <huangxiaoqiang@hnkhzn.com>
Date: 星期一, 23 六月 2025 08:37:37 +0800
Subject: [PATCH] 1

---
 项目代码/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/CommonConveyorLineJob.cs |  324 +++++++++++++++++++++++++++--------------------------
 1 files changed, 164 insertions(+), 160 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/CommonConveyorLineJob.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/CommonConveyorLineJob.cs"
index 77f391b..e967125 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/CommonConveyorLineJob.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/CommonConveyorLineJob.cs"
@@ -15,240 +15,244 @@
  *----------------------------------------------------------------*/
 #endregion << 鐗� 鏈� 娉� 閲� >>
 
+using AngleSharp.Dom;
+using Autofac.Core;
 using AutoMapper;
+using HslCommunication;
+using Microsoft.CodeAnalysis.CSharp.Syntax;
+using Newtonsoft.Json;
+using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
 using Quartz;
+using System.Reflection;
 using WIDESEA_Common.Log;
 using WIDESEAWCS_Common.TaskEnum;
+using WIDESEAWCS_Core.Caches;
+using WIDESEAWCS_Core.Helper;
+using WIDESEAWCS_Core.HttpContextUser;
 using WIDESEAWCS_DTO.Enum;
+using WIDESEAWCS_DTO.TaskInfo;
+using WIDESEAWCS_IBasicInfoService;
 using WIDESEAWCS_ISystemServices;
+using WIDESEAWCS_ITaskInfoRepository;
 using WIDESEAWCS_ITaskInfoService;
 using WIDESEAWCS_Model.Models;
-using WIDESEAWCS_Model.Models.System;
+using WIDESEAWCS_Model.Models.BasicInfo;
 using WIDESEAWCS_QuartzJob;
+using WIDESEAWCS_QuartzJob.DeviceBase;
+using WIDESEAWCS_QuartzJob.DTO;
 using WIDESEAWCS_QuartzJob.Service;
+using WIDESEAWCS_SignalR;
 using WIDESEAWCS_Tasks.ConveyorLineJob;
+using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
 
 namespace WIDESEAWCS_Tasks
 {
     [DisallowConcurrentExecution]
-    public class CommonConveyorLineJob : IJob
+    public class CommonConveyorLineJob :JobBase, IJob
     {
         private readonly ITaskService _taskService;
+        private readonly ITaskRepository _taskRepository;
         private readonly ITaskExecuteDetailService _taskExecuteDetailService;
         private readonly IRouterService _routerService;
-        private readonly IAgvStationService _agvStationService;
+        private readonly IDt_StationManagerService _stationManagerService;
         private readonly IMapper _mapper;
+        private readonly ICacheService _cacheService;
+        private readonly INoticeService _noticeService;
+        private static List<string>? userTokenIds;
+        private static List<int>? userIds;
 
-        public CommonConveyorLineJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IAgvStationService agvStation, IMapper mapper)
+        public CommonConveyorLineJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IDt_StationManagerService stationManagerService, IMapper mapper, ICacheService cacheService, INoticeService noticeService, ITaskRepository taskRepository)
         {
             _taskService = taskService;
             _taskExecuteDetailService = taskExecuteDetailService;
             _routerService = routerService;
-            _agvStationService = agvStation;
+            _stationManagerService = stationManagerService;
             _mapper = mapper;
+            _cacheService = cacheService;
+            _noticeService = noticeService;
+            _taskRepository = taskRepository;
         }
 
-        public Task Execute(IJobExecutionContext context)
+        public async Task Execute(IJobExecutionContext context)
         {
             try
             {
-                wcsWriteLog("LineJob", "杩愯鐘舵��", $"寮�濮嬫椂闂达細{DateTime.Now}");
                 CommonConveyorLine conveyorLine = (CommonConveyorLine)context.JobDetail.JobDataMap.Get("JobParams");
                 if (conveyorLine != null)
                 {
-                    //鐢熸垚鍙栬揣浠诲姟锛屾斁鑷充笂鏂欎綅
-                    RequestInbound(conveyorLine);
-                    //鏍规嵁agv涓嬫枡浠诲姟锛屾妸鏂欐斁鑷充笂鏂欏彛鎴栫紦瀛樹綅
-                    RequestOutbound(conveyorLine);  //涓婃枡鍙i渶瑕佹壘浠诲姟锛屾壘缂撳瓨鍖�
+                    // 鑾峰彇鎵�鏈夌珯鐐圭鐞嗗櫒
+                    List<Dt_StationManager> stationManagers = _stationManagerService.GetAllStationByDeviceCode(conveyorLine.DeviceCode);
+
+                    // 骞惰澶勭悊姣忎釜瀛愯澶�
+                    var tasks = stationManagers.Select(station => ProcessDeviceAsync(conveyorLine, station)).ToList();
+
+                    await Task.WhenAll(tasks);
                 }
 
             }
             catch (Exception ex)
             {
-                //Console.Out.WriteLine(nameof(CommonConveyorLineJob) + ":" + ex.ToString());
-                wcsWriteLog("LineJob閿欒淇℃伅", "寮傚父淇℃伅", ex.ToString());
+                Console.Out.WriteLine(nameof(CommonConveyorLineJob) + ":" + ex.ToString());
             }
-            wcsWriteLog("LineJob", "杩愯鐘舵��", $"缁撴潫鏃堕棿锛歿DateTime.Now}");
+            return;
+        }
+        private Task ProcessDeviceAsync(CommonConveyorLine conveyorLine, Dt_StationManager station)
+        {
+            try
+            {
+                ConveyorLineTaskCommand command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(station.stationName);
+                if (command != null)
+                {
+                    #region 璋冪敤浜嬩欢鎬荤嚎閫氱煡鍓嶇
+
+                    // 鑾峰彇缂撳瓨涓殑鐢ㄦ埛淇℃伅
+                    var tokenInfos = _cacheService.Get<List<UserInfo>>("Cache_UserToken");
+                    if (tokenInfos != null && tokenInfos.Any())
+                    {
+                        userTokenIds = tokenInfos.Select(x => x.Token_ID).ToList();
+                        userIds = tokenInfos.Select(x => x.UserId).ToList();
+                    }
+
+                    #endregion 璋冪敤浜嬩欢鎬荤嚎閫氱煡鍓嶇
+
+                    // 灏嗕氦浜掍俊鍙疯浆鎹负甯冨皵鏁扮粍
+                    var structs = BitConverter.GetBytes(command.InteractiveSignal).Reverse().ToArray().ToBoolArray();
+                    // 鑾峰彇璁惧鍗忚璇︽儏
+                    List<DeviceProtocolDetailDTO>? deviceProtocolDetails = conveyorLine.DeviceProtocolDetailDTOs.Where(x => x.DeviceProParamName == nameof(ConveyorLineTaskCommand.InteractiveSignal)).ToList();
+
+                    if (deviceProtocolDetails != null)
+                    {
+                        foreach (var item in deviceProtocolDetails)
+                        {
+                            int itemValue = Convert.ToInt32(item.ProtocalDetailValue);
+                            if (structs[itemValue] == true)
+                            {
+                                // 鑾峰彇澶勭悊鏂规硶
+
+                                MethodInfo? method = GetType().GetMethod(item.ProtocolDetailType);
+                                if (method != null)
+                                {
+                                    method.Invoke(this, new object[] { conveyorLine, station });
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                WriteInfo(conveyorLine.DeviceName, ex.Message);
+            }
             return Task.CompletedTask;
         }
-
         /// <summary>
-        /// 杈撻�佺嚎璇锋眰鍏ュ簱
+        /// 鐢熸垚鍙栬揣浠诲姟锛屾斁鑷充笂鏂欎綅
         /// </summary>
         /// <param name="conveyorLine">杈撻�佺嚎瀹炰緥瀵硅薄</param>
         /// <param name="command">璇诲彇鐨勮姹備俊鎭�</param>Request outbound
         /// <param name="childDeviceCode">瀛愯澶囩紪鍙�</param>
-        public void RequestInbound(CommonConveyorLine conveyorLine)
+        public async Task RequestInbound(CommonConveyorLine conveyorLine, Dt_StationManager station)
         {
-            List<string> _Task = _taskService.QueryConveyorLineTaskSourceAddress();
-            List<AGVStation> childDeviceCodes = _agvStationService.QuerypLatform(conveyorLine.DeviceCode, _Task);  //瀵瑰簲姣忎釜ip鎵惧叏閮ㄧ殑涓嬫枡绔欑偣涓庡灚鏉垮洖鏀剁偣绔欏彴鐐�
-            foreach (var childDeviceCode in childDeviceCodes)
+            try
             {
-                ConveyorLineCommand command = conveyorLine.ReadCustomer<ConveyorLineCommand>(childDeviceCode.Station_name);
-                if (command != null)
-                {
-                    if (command.R_Releasespermitted == 1)
-                    {
-                        if (childDeviceCode.Station_material==(int)AgvStationEnum.PadRecycle)
-                        {
-                            RequestHcdbst(conveyorLine, childDeviceCode.Station_Area, childDeviceCode.Station_name, childDeviceCode.Station_remark);
-                        }
-                        else
-                        {
-                            Dt_Task taskDTO = new Dt_Task()
-                            {
-                                TaskNum = Convert.ToInt32(DateTime.Now.ToString("HHmmss")) + childDeviceCode.Station_code,
-                                Grade = 1,
-                                Roadway = childDeviceCode.Station_Area.ToString(),
-                                SourceAddress = childDeviceCode.Station_name,
-                                CurrentAddress = childDeviceCode.Station_name,
-                                TaskState = (int)TaskInStatusEnum.InNew,
-                                TaskType = (int)childDeviceCode.Station_material,
-                            };
-                            _taskService.ReceiveWMSTask(taskDTO);
-                        }
-                    }
-                    else
-                    {
-                        wcsWriteLog(childDeviceCode.Station_name, "璇诲彇淇℃伅", $"鏈壘鍒拌璇诲彇妯″潡");
-                    }
-                }
-                else
-                {
-                    wcsWriteLog(childDeviceCode.Station_name, "璇诲彇淇℃伅", $"閫氳杩炴帴閿欒锛屾湭鎵惧埌璇诲彇妯″潡鍦板潃");
-                }
-            }
-        }
+                // 杈撳嚭淇℃伅锛岃〃绀虹珯鍙拌姹傚彇璐�
+                var log = $"銆恵conveyorLine._deviceName}銆戠珯鍙般�恵station.stationName}銆戣姹傚彇璐�";
+                await LogAndWarn(conveyorLine.DeviceName, log);
 
-        /// <summary>
-        /// 杈撻�佺嚎璇锋眰鍑虹珯
-        /// </summary>
-        /// <param name="conveyorLine">杈撻�佺嚎瀹炰緥瀵硅薄</param>
-        /// <param name="command">璇诲彇鐨勮姹備俊鎭�</param>
-        /// <param name="childDeviceCode">瀛愯澶囩紪鍙�</param>
-        public void RequestOutbound(CommonConveyorLine conveyorLine)
-        {
-            List<AGVStation> AGVStationListdata = _agvStationService.QuerypLatformarer(conveyorLine.DeviceCode);
-            foreach (var AGVStationitem in AGVStationListdata)
-            {
-                ConveyorLineCommand command = conveyorLine.ReadCustomer<ConveyorLineCommand>(AGVStationitem.Station_name);
-                if (command != null)
+                // 鑾峰彇鎵�鏈変换鍔℃簮鍦板潃
+                List<string> taskSourceAddress = _taskService.QueryConveyorLineTask();
+                if (_taskService.QueryConveyorLineTask(station.stationName) != null)
                 {
-                    if (command.R_Pickuppermitted == 1)
+                    ConveyorLineSendFinish(conveyorLine, station.stationName);
+                    await LogAndWarn(conveyorLine.DeviceName, $"銆恵conveyorLine._deviceName}銆戠珯鍙般�恵station.stationName}銆戝瓨鍦ㄤ换鍔�");
+                    return;
+                }
+
+                // 鏍规嵁璁惧缂栧彿鍜屼换鍔℃簮鍦板潃鏌ヨ鎵�鏈夋湭鎵ц鐨勪换鍔�
+                List<Dt_StationManager> childDeviceCodes = _stationManagerService.QueryPlatform(station, taskSourceAddress);  //瀵瑰簲姣忎釜ip鎵惧叏閮ㄧ殑涓嬫枡绔欑偣涓庡灚鏉垮洖鏀剁偣绔欏彴鐐�
+
+                if(childDeviceCodes.Count()==0|| childDeviceCodes == null)
+                {
+                    ConveyorLineSendFinish(conveyorLine, station.stationName);
+                    await LogAndWarn(conveyorLine.DeviceName, $"銆恵conveyorLine._deviceName}銆戠珯鍙般�恵station.stationName}銆戞湭鎵惧埌鍙墠寰�绔欏彴");
+                    return;
+                }
+                
+                foreach (var item in childDeviceCodes)
+                {
+                    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == item.DeviceCode);
+                    if (device != null)
                     {
-                        if (AGVStationitem.Station_material == (int)AgvStationEnum.BoardLoad)
+                        CommonConveyorLine commonConveyorLine = (CommonConveyorLine)device;
+                        ConveyorLineTaskCommand command = commonConveyorLine.ReadCustomer<ConveyorLineTaskCommand>(item.stationName);
+                        if (command != null)
                         {
-                            Dt_Task agvstack= _taskService.QueryConveyorLineTaskRoadway(AGVStationitem.Station_Area);
-                            if(agvstack != null)
+                            var structs = BitConverter.GetBytes(command.InteractiveSignal).Reverse().ToArray().ToBoolArray();
+                            if (structs[2])
                             {
-                                _taskService.UpdateTargetAddress(agvstack.TaskId, AGVStationitem.Station_name);
+                                Dt_Task taskDTO = new Dt_Task()
+                                {
+                                    TaskNum = _taskRepository.GetTaskNo().Result,
+                                    Grade = 1,
+                                    Roadway = item.stationArea.ToString(),
+                                    SourceAddress = station.stationName,
+                                    CurrentAddress= station.stationName,
+                                    TargetAddress = item.stationName,
+                                    NextAddress = item.stationName,
+                                    TaskState = (int)AGVTaskStatusEnum.AGVNew,
+                                    TaskType = (int)AGVTaskTypeEnum.AGVCarry,
+                                    AGVName = item.stationArea switch
+                                    {
+                                        1 => "AGV01",
+                                        4 => "AGV03",
+                                        _ => "AGV02",
+                                    }
+                                };
+                                _taskService.ReceiveWMSTask(new List<Dt_Task> { taskDTO });
+                                ConveyorLineSendFinish(conveyorLine,station.stationName);
+                                return;
                             }
                             else
                             {
-                                //鍘绘壘缂撳瓨鍖哄簱瀛�
-                                RequestHcst(conveyorLine, AGVStationitem.Station_Area, AGVStationitem.Station_name, AGVStationitem.Station_remark);
+                                await LogAndWarn(conveyorLine.DeviceName, $"绔欏彴{item.stationName}鐘舵�佷笉鍙敤{structs[2]}");
                             }
                         }
-                    }
-                    else
-                    {
-                        wcsWriteLog(AGVStationitem.Station_name, "璇诲彇淇℃伅", $"閫氳杩炴帴閿欒锛屾湭鎵惧埌璇诲彇妯″潡鍦板潃");
-                    }
-                }
-            }
-        }
-
-        public void RequestHcst(CommonConveyorLine conveyorLine,int Station_Area,string Station_names,string Station_remark)
-        {
-            List<AGVStation> AGVStationListdata = _agvStationService.QuerypLatformmaterial(Station_Area);   //鏌ュ嚭缂撳瓨浣�
-            if (Station_remark != null)   //鎵惧瀵瑰簲鐨勬斁璐х珯鍙�
-            {
-                AGVStationListdata = _agvStationService.QuerypStation_Area2(Station_remark);
-            }
-            foreach (var AGVStationitem in AGVStationListdata)
-            {
-                ConveyorLineCommand command = conveyorLine.ReadCustomer<ConveyorLineCommand>(AGVStationitem.Station_name);
-                if (command != null)
-                {
-                    if (command.R_Releasespermitted == 1)
-                    {
-                        if (_taskService.QueryConveyorLinetaeersuadd(AGVStationitem.Station_name))
+                        else
                         {
-                            Dt_Task taskDTO = new Dt_Task()
-                            {
-                                TaskNum = Convert.ToInt32(DateTime.Now.ToString("HHmmss")) + AGVStationitem.Station_code,
-                                Grade = 1,
-                                Roadway = AGVStationitem.Station_Area.ToString(),
-                                SourceAddress = AGVStationitem.Station_name,
-                                CurrentAddress = AGVStationitem.Station_name,
-                                TaskState = (int)TaskInStatusEnum.InNew,
-                                TaskType = (int)AGVStationitem.Station_material,
-                                TargetAddress = Station_names,
-                                NextAddress = Station_names,
-                            };
-                            _taskService.ReceiveWMSTask(taskDTO);
+                            WriteInfo(conveyorLine.DeviceName, $"閫氳杩炴帴閿欒锛寋item.stationName}鏈壘鍒拌鍙栨ā鍧楀湴鍧�");
                         }
                     }
                     else
                     {
-                        wcsWriteLog(AGVStationitem.Station_name, "璇诲彇淇℃伅", $"閫氳杩炴帴閿欒锛屾湭鎵惧埌璇诲彇妯″潡鍦板潃");
+                        var logerror = $"銆恵conveyorLine.DeviceName}銆戠珯鍙帮細銆恵item.stationName}銆戞湭閰嶇疆璁惧琛ㄥご";
+                        await LogAndWarn(conveyorLine.DeviceName, logerror);
                     }
                 }
             }
-        }
-
-        public void RequestHcdbst(CommonConveyorLine conveyorLine, int Station_Area, string Station_names,string Station_remark)
-        {
-            List<AGVStation> AGVStationListdata = _agvStationService.QuerypStation_Area(Station_Area);   //鏌ュ嚭缂撳瓨浣�
-            if(Station_remark !=null)   //鎵惧瀵瑰簲鐨勬斁璐х珯鍙�
+            catch (Exception ex)
             {
-                AGVStationListdata= _agvStationService.QuerypStation_Area2(Station_remark);
-            }
-            
-            foreach (var AGVStationitem in AGVStationListdata)
-            {
-                ConveyorLineCommand command = conveyorLine.ReadCustomer<ConveyorLineCommand>(AGVStationitem.Station_name);
-                if (command != null)
-                {
-                    if (command.R_Releasespermitted == 1)
-                    {
-                        if (_taskService.QueryConveyorLinetaeersuadd(AGVStationitem.Station_name))
-                        {
-                            Dt_Task taskDTO = new Dt_Task()
-                            {
-                                TaskNum = Convert.ToInt32(DateTime.Now.ToString("HHmmss")) + AGVStationitem.Station_code,
-                                Grade = 1,
-                                Roadway = AGVStationitem.Station_Area.ToString(),
-                                SourceAddress = Station_names,
-                                CurrentAddress = Station_names,
-                                TaskState = (int)TaskInStatusEnum.InNew,
-                                TaskType = (int)AGVStationitem.Station_material,
-                                TargetAddress = AGVStationitem.Station_name,
-                                NextAddress = AGVStationitem.Station_name,
-                            };
-                            _taskService.ReceiveWMSTask(taskDTO);
-                        }
-                    }
-                    else
-                    {
-                        wcsWriteLog(AGVStationitem.Station_name, "璇诲彇淇℃伅", $"璇诲彇鏀捐揣淇″彿涓猴細{command.R_Releasespermitted}");
-                    }
-                }
+                WriteInfo(conveyorLine.DeviceName, ex.Message);
             }
         }
-
-
-
         /// <summary>
-        /// 璇诲彇淇℃伅璁板綍
+        /// 杈撻�佺嚎浜や簰瀹屾垚
         /// </summary>
-        /// <param name="SCLLinStack"></param>
-        /// <param name="Logtype"></param>
-        /// <param name="Magessadd"></param>
-        public void wcsWriteLog(string SCLLinStack,string Logtype,string Magessadd)
+        /// <param name="conveyorLine">杈撻�佺嚎瀹炰緥瀵硅薄</param>
+        /// <param name="childDeviceCode">瀛愯澶囩紪鍙�</param>
+        /// <param name="ProtocalDetailValue">绾夸綋褰撳墠bool璇诲彇鍋忕Щ鍦板潃</param>
+        /// <param name="value">鍊�</param>
+        public void ConveyorLineSendFinish(CommonConveyorLine conveyorLine, string childDeviceCode)
         {
-            WriteLog.Write_Log("绔欏彴璇诲彇淇℃伅璁板綍", SCLLinStack+"绔欏彴", Logtype, new{淇℃伅 = Magessadd });
+            DeviceProDTO? devicePro = conveyorLine.DeviceProDTOs.Where(x => x.DeviceProParamType == nameof(DeviceCommand) && x.DeviceChildCode == childDeviceCode && x.DeviceProParamName == "WriteInteractiveSignal").OrderBy(x => x.DeviceProOffset).FirstOrDefault();
+
+            conveyorLine.Communicator.Write(devicePro.DeviceProAddress, true);
         }
 
+        public async Task LogAndWarn(string deviceName, string log, string color = "red")
+        {
+            ConsoleHelper.WriteWarningLine(log);
+            await _noticeService.Logs(userTokenIds, new { deviceName, log = log, time = DateTime.Now.ToString("G"), color = color });
+            WriteInfo(deviceName, log);
+        }
     }
 }

--
Gitblit v1.9.3