1
huangxiaoqiang
2026-02-25 02a4fdd78cef9c7d2a26bfb9aa187179cf0baac3
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob_CW/CommonConveyorLine_CWJob.cs
@@ -28,12 +28,16 @@
using SqlSugar;
using System.ComponentModel.Design;
using System.Reflection;
using System.Reflection.Metadata;
using System.Threading;
using WIDESEAWCS_BasicInfoRepository;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Communicator;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.HttpContextUser;
using WIDESEAWCS_Core.LogHelper;
using WIDESEAWCS_DTO.MOM;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_IProcessRepository;
@@ -47,6 +51,7 @@
using WIDESEAWCS_QuartzJob.Service;
using WIDESEAWCS_SignalR;
using WIDESEAWCS_Tasks.ConveyorLineJob;
using static Quartz.Logging.OperationName;
using ICacheService = WIDESEAWCS_Core.Caches.ICacheService;
using Platform = WIDESEAWCS_Model.Models.Platform;
@@ -69,7 +74,7 @@
        private readonly IDeviceInfoRepository _deviceInfoRepository;
        private static List<string>? userTokenIds;
        private static List<int>? userIds;
        private static List<string> childCodeList = new List<string>();
        private static DateTime? lastUpdateTime;
        public CommonConveyorLine_CWJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IMapper mapper, ITaskRepository taskRepository, IPlatFormRepository platFormRepository, ISys_ConfigService sys_ConfigService, IDt_StationManagerRepository stationManagerRepository, ICacheService cacheService, INoticeService noticeService, IDt_needBarcodeRepository needBarcodeRepository, IDeviceInfoRepository deviceInfoRepository)
        {
@@ -94,99 +99,109 @@
                CommonConveyorLine_CW conveyorLine = (CommonConveyorLine_CW)context.JobDetail.JobDataMap.Get("JobParams");
                if (conveyorLine != null)
                {
                    #region 站台方式
                    //List<Dt_StationManager> stationManagers = _stationManagerService.GetAllStationByDeviceCode(conveyorLine.DeviceCode);
                    //foreach (var station in stationManagers)
                    //{
                    //    ConveyorLineTaskCommand_After command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand_After>(station.stationChildCode);
                    //    DeviceProtocolDetailDTO? deviceProtocolDetails = conveyorLine.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(ConveyorLineTaskCommand_After.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 });
                    //        }
                    //    }
                    //}
                    #endregion 站台方式
                    #region 路由方式
                    List<string> childDeviceCodes = _routerService.QueryAllPositions(conveyorLine.DeviceCode);
                    if (conveyorLine.DeviceCode == "1024")
                    var chunks = childDeviceCodes.Chunk(10);
                    //if (conveyorLine.DeviceCode == "1024")
                    //{
                    //    childDeviceCodes.Add("4000");
                    //}
                    //var options = new ParallelOptions
                    //{
                    //    MaxDegreeOfParallelism = 10
                    //    //MaxDegreeOfParallelism = Environment.ProcessorCount
                    //};
                    //{
                    //    SiemensS7 siemensS7 = new SiemensS7(conveyorLine.Communicator.IpAddress, conveyorLine.Communicator.Port, conveyorLine.DeviceName);
                    //    siemensS7.Connect();
                    //    tasks.Add(Task.Run(() =>
                    //    {
                    //    }));
                    //}
                    //Parallel.For(0, childDeviceCodes.Count, options, i =>
                    //Parallel.ForEach(childDeviceCodes, options, childDeviceCode =>
                    var tasks = new List<Task>();
                    foreach (var item in chunks)
                    {
                        childDeviceCodes.Add("4000");
                    }
                    foreach (string childDeviceCode in childDeviceCodes)
                    {
                        ConveyorLineTaskCommand_After command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand_After>(childDeviceCode);
                        if (command == null) continue;
                        //if (command.InteractiveSignal == 0 && command.HasPallet != 1) continue;
                        if (command.ConveyorLineBarcode.Trim().Contains("\0")) command.ConveyorLineBarcode = "";
                        DeviceProtocolDetailDTO? deviceProtocolDetails = conveyorLine.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(ConveyorLineTaskCommand_After.InteractiveSignal) && x.ProtocalDetailValue == command.InteractiveSignal.ToString());
                        if (deviceProtocolDetails != null)
                        tasks.Add(Task.Run(() =>
                        {
                            MethodInfo? method = GetType().GetMethod(deviceProtocolDetails.ProtocolDetailType);
                            if (method != null)
                            foreach (string childDeviceCode in item)
                            {
                                method.Invoke(this, new object[] { conveyorLine, command, childDeviceCode });
                            }
                        }
                                ConveyorLineTaskCommand_After command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand_After>(childDeviceCode);
                        if (childDeviceCode == "1670" || childDeviceCode == "1666" || childDeviceCode == "1548" || childDeviceCode == "1448")
                        {
                            Platform platform = _platFormRepository.QueryFirst(x => x.DeviceCode == conveyorLine.DeviceCode && x.PlatCode == childDeviceCode && x.Status == "Active");
                            if (platform != null)
                            {
                                if (command.HasPallet != 1)
                                if (command != null)
                                {
                                    MethodInfo? method = GetType().GetMethod(platform.ExecutionMethod);
                                    if (method != null)
                                    if (command.ConveyorLineBarcode.Trim().Contains("\0")) command.ConveyorLineBarcode = "";
                                    DeviceProtocolDetailDTO? deviceProtocolDetails = conveyorLine.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(ConveyorLineTaskCommand_After.InteractiveSignal) && x.ProtocalDetailValue == command.InteractiveSignal.ToString());
                                    if (deviceProtocolDetails != null)
                                    {
                                        int count = 1;
                                        method.Invoke(this, new object[] { conveyorLine, command, childDeviceCode, count, platform });
                                        MethodInfo? method = GetType().GetMethod(deviceProtocolDetails.ProtocolDetailType);
                                        if (method != null)
                                        {
                                            method.Invoke(this, new object[] { conveyorLine, command, childDeviceCode });
                                        }
                                    }
                                    if (childDeviceCode == "1670" || childDeviceCode == "1666" || childDeviceCode == "1548" || childDeviceCode == "1448")
                                    {
                                        if (lastUpdateTime > DateTime.Now.AddMinutes(1))
                                        {
                                            lastUpdateTime = DateTime.Now;
                                            Platform platform = _platFormRepository.QueryFirst(x => x.DeviceCode == conveyorLine.DeviceCode && x.PlatCode == childDeviceCode && x.Status == "Active");
                                            if (platform != null)
                                            {
                                                if (command.HasPallet != 1)
                                                {
                                                    MethodInfo? method = GetType().GetMethod(platform.ExecutionMethod);
                                                    if (method != null)
                                                    {
                                                        int count = 1;
                                                        method.Invoke(this, new object[] { conveyorLine, command, childDeviceCode, count, platform });
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    #region 调用事件总线通知前端
                                    var tokenInfos = _cacheService.Get<List<UserInfo>>("Cache_UserToken");
                                    if (tokenInfos != null || tokenInfos.Any())
                                    {
                                        var userTokenIds = tokenInfos?.Select(x => x.Token_ID).ToList();
                                        var userIds = tokenInfos?.Select(x => x.UserId).ToList();
                                        object obj = new
                                        {
                                            childDeviceCode,
                                            commandAfter = command,
                                        };
                                        _noticeService.LineData(userIds?.FirstOrDefault(), userTokenIds, new { conveyorLine.DeviceName, data = obj });
                                    }
                                    #endregion 调用事件总线通知前端
                                }
                            }
                        }
                        #region 调用事件总线通知前端
                        var tokenInfos = _cacheService.Get<List<UserInfo>>("Cache_UserToken");
                        if (tokenInfos == null || !tokenInfos.Any())
                        {
                            continue;
                        }
                        var userTokenIds = tokenInfos?.Select(x => x.Token_ID).ToList();
                        var userIds = tokenInfos?.Select(x => x.UserId).ToList();
                        object obj = new
                        {
                            childDeviceCode,
                            commandAfter = command,
                        };
                        _noticeService.LineData(userIds?.FirstOrDefault(), userTokenIds, new { conveyorLine.DeviceName, data = obj });
                        #endregion 调用事件总线通知前端
                        }));
                    }
                    //Task.WaitAll(tasks.ToArray());
                    #endregion 路由方式
                }
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine(nameof(CommonConveyorLine_CWJob) + ":" + DateTime.Now + ":" + ex.ToString(),ex.StackTrace);
                Console.Out.WriteLine(nameof(CommonConveyorLine_CWJob) + ":" + DateTime.Now + ":" + ex.ToString(), ex.StackTrace);
            }
            finally
            {
@@ -209,7 +224,6 @@
                var log = $"时间:【{DateTime.Now}】【{conveyorLine.DeviceName}】托盘号:【{command.ConveyorLineBarcode}】任务号:【{command.ConveyorLineTaskNum}】设备编码:【{childDeviceCode}】";
                ConsoleHelper.WriteSuccessLine(log);
                //_noticeService.Logs(userTokenIds, new { conveyorLine.DeviceName, log = log, time = DateTime.Now.ToString("G"), color = "red" });
                WriteInfo(conveyorLine.DeviceName, log);
                if (childDeviceCode == "4000")
@@ -225,15 +239,15 @@
                }
                else
                {
                    if (childDeviceCode == "1039")
                    {
                        var GWTask = _taskRepository.QueryData(x => x.Roadway.Contains("GWSC2") && x.SourceAddress == "1039" && (x.TaskState == (int)TaskInStatusEnum.Line_InExecuting || x.TaskState == (int)TaskInStatusEnum.Line_InFinish)).ToList();
                        if (GWTask.Count >= 2 && childDeviceCode == "1039" && task.Roadway.Contains("GWSC2"))
                        {
                            ConsoleHelper.WriteErrorLine($"时间:【{DateTime.Now}】托盘号:【{command.ConveyorLineBarcode}】高温二已存在【{GWTask.Count}】个任务大于2个任务不可下发");
                            return;
                        }
                    }
                    //if (childDeviceCode == "1039")
                    //{
                    //    var GWTask = _taskRepository.QueryData(x => x.Roadway.Contains("GWSC2") && x.SourceAddress == "1039" && (x.TaskState == (int)TaskInStatusEnum.Line_InExecuting || x.TaskState == (int)TaskInStatusEnum.Line_InFinish)).ToList();
                    //    if (GWTask.Count >= 2 && childDeviceCode == "1039" && task.Roadway.Contains("GWSC2"))
                    //    {
                    //        ConsoleHelper.WriteErrorLine($"时间:【{DateTime.Now}】托盘号:【{command.ConveyorLineBarcode}】高温二已存在【{GWTask.Count}】个任务大于2个任务不可下发");
                    //        return;
                    //    }
                    //}
                    ConveyorLineTaskCommand_After taskCommand = _mapper.Map<ConveyorLineTaskCommand_After>(task);
@@ -282,15 +296,6 @@
                    }
                }
            }
            //else
            //{
            //    //当前地址请求 寻找当前地址的托盘号 任务号的任务,如存在任务则重新再次写入新目标地址
            //    Dt_Task currentTask = _taskService.QueryExecutingCurrentConveyorLineTask(command.ConveyorLineTaskNum, childDeviceCode, command.ConveyorLineBarcode);
            //    if (currentTask != null)
            //    {
            //        conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineTargetAddress, Convert.ToInt16(currentTask.TargetAddress), childDeviceCode);
            //    }
            //}
        }
        /// <summary>
@@ -412,12 +417,9 @@
                // 拼接完整的MOM接口地址(基础IP + 接口路径)
                var MOMIpAddress = wmsBase + ipAddress;
                // 5. 调用MOM接口(POST异步请求,同步等待结果)
                WriteInfo("入站校验", $"【{childDeviceCode}】入站校验请求参数【{trayCells.Serialize()}】");
                var result = HttpHelper.PostAsync(MOMIpAddress, trayCells.Serialize()).Result;
                // 记录接口调用日志(请求参数、返回参数),便于问题排查
                WriteInfo("入站校验", $"【{childDeviceCode}】入站校验请求参数【{trayCells.Serialize()}】");
                WriteInfo("入站校验", "");
                WriteInfo("入站校验", $"【{childDeviceCode}】入站校验返回参数【{result}】");
                // 6. 解析MOM接口返回结果为实体对象
@@ -519,7 +521,11 @@
                    content = _taskService.UpdateTaskStatusToNext(task);
                }
                var station = _stationManagerRepository.QueryFirst(x => x.stationChildCode == childDeviceCode && x.stationArea == "Call" && x.stationType == 12);
                CommonConveyorLine_BZ? commonConveyorBZ=Storage.Devices.FirstOrDefault(x=>x.DeviceCode==station.stationPLC) as CommonConveyorLine_BZ;
                if (station == null)
                {
                    return;
                }
                CommonConveyorLine_BZ? commonConveyorBZ = Storage.Devices.FirstOrDefault(x => x.DeviceCode == station.stationPLC) as CommonConveyorLine_BZ;
                if (commonConveyorBZ != null)
                {
                    ConveyorLineTaskCommand_After readSignal = conveyorLine.ReadCustomer<ConveyorLineTaskCommand_After>(station.stationLocation);
@@ -633,14 +639,11 @@
                }
                var wmsIpAddress = wmsBase + requestTrayOutTask;
                List<string> strings = platform.Location.Split(',').ToList();
                WriteInfo("调度执行时间记录", "【调取WMS出库接口开始时间:】" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                var result = await HttpHelper.PostAsync(wmsIpAddress, new { Position = childDeviceCode, Tag = (int)taskType, AreaCdoe = platform.Stacker, AreaCdoes = strings, platform.ProductionLine }.Serialize());
                WriteInfo("调度执行时间记录", "【调取WMS出库接口结束时间:】" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                WriteInfo("OCV请求返回参数", $"时间:【{DateTime.Now}】请求点位【{childDeviceCode}】返回参数【{JsonConvert.SerializeObject(result)}】");
                WebResponseContent content = JsonConvert.DeserializeObject<WebResponseContent>(result);
@@ -662,6 +665,7 @@
        public WebResponseContent CreateAndSendTask(WMSTaskDTO taskDTO)
        {
            var content = _taskService.ReceiveWMSTask(new List<WMSTaskDTO> { taskDTO });
            if (content.Status)
            {
                Console.WriteLine($"{taskDTO.TaskType}呼叫成功");