刘磊
2024-12-21 a6ea79849f0142b5280f0c5d4b15ecc83f0d015a
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/CommonConveyorLineJob.cs
@@ -58,7 +58,10 @@
            _noticeService = noticeService;
        }
        public Task Execute(IJobExecutionContext context)
        public async Task Execute(IJobExecutionContext context)
        {
            string jobName = context.JobDetail.Key.Name;
            if (MemoryLockManager.TryAcquireLock(jobName))
        {
            try
            {
@@ -66,10 +69,8 @@
                if (conveyorLine != null)
                {
                    List<string> childDeviceCodes = _routerService.QueryAllPositions(conveyorLine.DeviceCode);
                    foreach (string childDeviceCode in childDeviceCodes)
                    {
                        ProcessDeviceAsync(conveyorLine, childDeviceCode);
                    }
                        var tasks = childDeviceCodes.Select(childDeviceCode => ProcessDeviceAsync(conveyorLine, childDeviceCode)).ToList();
                        await Task.WhenAll(tasks);
                    List<Dt_StationManager> stationManagers = _stationManagerService.GetAllStationByDeviceCode(conveyorLine.DeviceCode);
                    foreach (var station in stationManagers)
@@ -99,119 +100,165 @@
            {
                Console.Out.WriteLine(nameof(CommonConveyorLineJob) + ":" + ex.ToString());
            }
            return Task.CompletedTask;
                finally
                {
                    MemoryLockManager.ReleaseLock(jobName);
                }
            }
            else
            {
                ConsoleHelper.WriteErrorLine($"[CommonConveyorLineJob]【{jobName}】任务已被锁定,无法处理");
            }
            return;
        }
        private void ProcessDeviceAsync(CommonConveyorLine conveyorLine, string childDeviceCode)
        private Task ProcessDeviceAsync(CommonConveyorLine conveyorLine, string childDeviceCode)
        {
            ConveyorLineTaskCommand command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(childDeviceCode);
            ConveyorLineTaskCommandWrite commandWrite = conveyorLine.ReadCustomer<ConveyorLineTaskCommandWrite>(childDeviceCode, "DeviceCommand");
            if (command != null && commandWrite != null)
            {
                #region 调用事件总线通知前端
                var tokenInfos = _cacheService.Get<List<UserInfo>>("Cache_UserToken");
                if (tokenInfos == null || !tokenInfos.Any())
                if (tokenInfos != null && tokenInfos.Any())
                {
                    //throw new Exception(conveyorLine.DeviceName + "缓存中未找到Token缓存");
                    return;
                }
                var userTokenIds = tokenInfos?.Select(x => x.Token_ID).ToList();
                var userIds = tokenInfos?.Select(x => x.UserId).ToList();
                    var userTokenIds = tokenInfos.Select(x => x.Token_ID).ToList();
                    var userIds = tokenInfos.Select(x => x.UserId).ToList();
                object obj = new
                {
                    command,
                    commandWrite
                };
                _noticeService.LineData(userIds?.FirstOrDefault(), userTokenIds, new { conveyorLine.DeviceName, childDeviceCode, data = obj });
                    _noticeService.LineData(userIds.FirstOrDefault(), userTokenIds, new { conveyorLine.DeviceName, childDeviceCode, data = obj });
                }
                #endregion 调用事件总线通知前端
                var structs = BitConverter.GetBytes(commandWrite.WriteInteractiveSignal).Reverse().ToArray().ToBoolArray();
                var writeInteractiveSignal = commandWrite.WriteInteractiveSignal;
                var structs = BitConverter.GetBytes(writeInteractiveSignal).Reverse().ToArray().ToBoolArray();
                List<DeviceProtocolDetailDTO>? deviceProtocolDetails = conveyorLine.DeviceProtocolDetailDTOs.Where(x => x.DeviceProParamName == nameof(ConveyorLineTaskCommand.InteractiveSignal)).ToList();
                if (deviceProtocolDetails != null)
                {
                    foreach (var item in deviceProtocolDetails)
                    {
                        var outDeviceCodes = _routerService.QueryOutDeviceCodes(conveyorLine.DeviceCode);
                        if (structs[item.ProtocalDetailValue.ObjToInt()] == true)
                        int itemValue = item.ProtocalDetailValue.ObjToInt();
                        if (structs[itemValue] == true)
                        {
                            var numRead = itemValue;
                            var numWrite = itemValue + 1;
                            ConsoleHelper.WriteWarningLine($"【{conveyorLine.DeviceName}】【{childDeviceCode}】【{numRead.ToString()}】位输送线读取信号:【{structs[itemValue]}】 【{numWrite}】位WCS写入信号:【{structs[itemValue + 1]}】");
                            if (structs[itemValue + 1] != structs[itemValue])
                        {
                            MethodInfo? method = GetType().GetMethod(item.ProtocolDetailType);
                            if (method != null)
                            {
                                var numRead = item.ProtocalDetailValue.ObjToInt();
                                var numWrite = item.ProtocalDetailValue.ObjToInt() + 1;
                                ConsoleHelper.WriteWarningLine($"【{conveyorLine.DeviceName}】【{childDeviceCode}】【{numRead.ToString()}】位输送线读取信号:【{structs[item.ProtocalDetailValue.ObjToInt()]}】 【{numWrite}】位WCS写入信号:【{structs[item.ProtocalDetailValue.ObjToInt() + 1]}】");
                                if (structs[item.ProtocalDetailValue.ObjToInt() + 1] != structs[item.ProtocalDetailValue.ObjToInt()])
                                {
                                    command.InteractiveSignal = commandWrite.WriteInteractiveSignal;
                                    method.Invoke(this, new object[] { conveyorLine, command, childDeviceCode, item.ProtocalDetailValue.ObjToInt() });
                                    command.InteractiveSignal = writeInteractiveSignal;
                                    method.Invoke(this, new object[] { conveyorLine, command, childDeviceCode, itemValue });
                                }
                            }
                        }
                        else
                        {
                            ConveyorLineSendFinish(conveyorLine, childDeviceCode, item.ProtocalDetailValue.ObjToInt(), false);
                            //DeviceProDTO? devicePro = conveyorLine.DeviceProDTOs.Where(x => x.DeviceProParamType == nameof(DeviceCommand) && x.DeviceChildCode == childDeviceCode).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
                            //string[] x = devicePro.DeviceProAddress.Split('.');
                            //x[x.Length - 1] = (item.ProtocalDetailValue.ObjToInt() + 1).ToString();
                            //string DeviceProAddress = string.Join(".", x);
                            //var writeRead = conveyorLine.Communicator.Read<bool>(DeviceProAddress);
                            //if (writeRead)
                            //{
                            //    conveyorLine.Communicator.Write(DeviceProAddress, false);
                            //    //ConveyorLineSendFinish(conveyorLine, childDeviceCode, item.ProtocalDetailValue.ObjToInt(), false);
                            //}
                            ConveyorLineSendFinish(conveyorLine, childDeviceCode, itemValue, false);
                        }
                    }
                }
                Platform platform = _platFormRepository.QueryFirst(x => x.PLCCode == conveyorLine.DeviceCode && x.PlatCode == childDeviceCode && x.Status == "Active");
                if (platform != null)
                {
                    if (command.InteractiveSignal != 2)
                    {
                        MethodInfo? method = GetType().GetMethod(platform.ExecutionMethod);
                        if (method != null)
                        {
                            command.InteractiveSignal = commandWrite.WriteInteractiveSignal;
                            int count = string.IsNullOrEmpty(platform.Location) ? 0 + 1 : platform.Location.Split(',').Count() + 1;
                            method.Invoke(this, new object[] { conveyorLine, command, childDeviceCode, count, platform });
                        }
                    }
                    else
                    {
                        if (platform.Location != null && platform.Location != "")
                if (platform != null && !string.IsNullOrEmpty(platform.Location))
                        {
                            var strings = platform.Location.Split(',').ToList();
                            foreach (var ite in strings)
                            {
                                //int index = strings.FindIndex(p => p == ite);
                                ConveyorLineTaskCommand command1 = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(ite);
                                if (command1.InteractiveSignal != 2)
                                {
                            command.InteractiveSignal = writeInteractiveSignal;
                            int count = strings.Count;
                                    MethodInfo? method = GetType().GetMethod(platform.ExecutionMethod);
                                    if (method != null)
                                    {
                                        command.InteractiveSignal = commandWrite.WriteInteractiveSignal;
                                        //int count = strings.Count - index;
                                        int count = strings.Count;
                                        method.Invoke(this, new object[] { conveyorLine, command, childDeviceCode, count, platform });
                                    }
                                }
                            }
                            };
                        }
                    }
                }
                #region
                //var structs = BitConverter.GetBytes(commandWrite.WriteInteractiveSignal).Reverse().ToArray().ToBoolArray();
                //List<DeviceProtocolDetailDTO>? deviceProtocolDetails = conveyorLine.DeviceProtocolDetailDTOs.Where(x => x.DeviceProParamName == nameof(ConveyorLineTaskCommand.InteractiveSignal)).ToList();
                //if (deviceProtocolDetails != null)
                //{
                //    foreach (var item in deviceProtocolDetails)
                //    {
                //        if (structs[item.ProtocalDetailValue.ObjToInt()] == true)
                //        {
                //            MethodInfo? method = GetType().GetMethod(item.ProtocolDetailType);
                //            if (method != null)
                //            {
                //                var numRead = item.ProtocalDetailValue.ObjToInt();
                //                var numWrite = item.ProtocalDetailValue.ObjToInt() + 1;
                //                ConsoleHelper.WriteWarningLine($"【{conveyorLine.DeviceName}】【{childDeviceCode}】【{numRead.ToString()}】位输送线读取信号:【{structs[item.ProtocalDetailValue.ObjToInt()]}】 【{numWrite}】位WCS写入信号:【{structs[item.ProtocalDetailValue.ObjToInt() + 1]}】");
                //                if (structs[item.ProtocalDetailValue.ObjToInt() + 1] != structs[item.ProtocalDetailValue.ObjToInt()])
                //                {
                //                    command.InteractiveSignal = commandWrite.WriteInteractiveSignal;
                //                    method.Invoke(this, new object[] { conveyorLine, command, childDeviceCode, item.ProtocalDetailValue.ObjToInt() });
                //                }
                //            }
                //        }
                //        else
                //        {
                //            ConveyorLineSendFinish(conveyorLine, childDeviceCode, item.ProtocalDetailValue.ObjToInt(), false);
                //        }
                //    }
                //}
                //Platform platform = _platFormRepository.QueryFirst(x => x.PLCCode == conveyorLine.DeviceCode && x.PlatCode == childDeviceCode && x.Status == "Active");
                //if (platform != null)
                //{
                //    if (command.InteractiveSignal != 2)
                //    {
                //        MethodInfo? method = GetType().GetMethod(platform.ExecutionMethod);
                //        if (method != null)
                //        {
                //            command.InteractiveSignal = commandWrite.WriteInteractiveSignal;
                //            int count = string.IsNullOrEmpty(platform.Location) ? 0 + 1 : platform.Location.Split(',').Count() + 1;
                //            method.Invoke(this, new object[] { conveyorLine, command, childDeviceCode, count, platform });
                //        }
                //    }
                //    else
                //    {
                //        if (platform.Location != null && platform.Location != "")
                //        {
                //            var strings = platform.Location.Split(',').ToList();
                //            foreach (var ite in strings)
                //            {
                //                //int index = strings.FindIndex(p => p == ite);
                //                ConveyorLineTaskCommand command1 = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(ite);
                //                if (command1.InteractiveSignal != 2)
                //                {
                //                    MethodInfo? method = GetType().GetMethod(platform.ExecutionMethod);
                //                    if (method != null)
                //                    {
                //                        command.InteractiveSignal = commandWrite.WriteInteractiveSignal;
                //                        //int count = strings.Count - index;
                //                        int count = strings.Count;
                //                        method.Invoke(this, new object[] { conveyorLine, command, childDeviceCode, count, platform });
                //                    }
                //                }
                //            }
                //        }
                //    }
                //}
                #endregion
            }
            return Task.CompletedTask;
        }
        #region 入库
@@ -312,7 +359,7 @@
            var task = _taskService.QueryExecutingConveyorLineTask(command.TaskNum, childDeviceCode);
            if (task != null && task.TaskState != (int)TaskInStatusEnum.Line_InFinish)
            {
                if (command.Barcode == task.PalletCode)
                if (command.Barcode == task.PalletCode && childDeviceCode == task.NextAddress)
                {
                    ConsoleHelper.WriteWarningLine($"【{conveyorLine._deviceName}】任务号:【{command.TaskNum}】,托盘条码:【{command.Barcode}】已到达【{childDeviceCode}】输送线入库完成,下一目标地址【等待分配货位】");
                    ConveyorLineSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, true);