From 7c926c1e3ade5158e6b1b0b805cd1b9c142e4b6c Mon Sep 17 00:00:00 2001 From: 肖洋 <cathay_xy@163.com> Date: 星期六, 21 十二月 2024 09:45:33 +0800 Subject: [PATCH] 更新代码以使用SqlSugarScope替代SqlSugarClient,并添加生产产线属性 --- Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/CommonConveyorLineJob.cs | 325 +++++++++++++++++++++++++++++++----------------------- 1 files changed, 186 insertions(+), 139 deletions(-) diff --git a/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/CommonConveyorLineJob.cs b/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/CommonConveyorLineJob.cs index a64d5e3..8352fc6 100644 --- a/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/CommonConveyorLineJob.cs +++ b/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/CommonConveyorLineJob.cs @@ -58,160 +58,207 @@ _noticeService = noticeService; } - public Task Execute(IJobExecutionContext context) + public async Task Execute(IJobExecutionContext context) { - try + string jobName = context.JobDetail.Key.Name; + if (MemoryLockManager.TryAcquireLock(jobName)) { - CommonConveyorLine conveyorLine = (CommonConveyorLine)context.JobDetail.JobDataMap.Get("JobParams"); - if (conveyorLine != null) + try { - List<string> childDeviceCodes = _routerService.QueryAllPositions(conveyorLine.DeviceCode); - foreach (string childDeviceCode in childDeviceCodes) + CommonConveyorLine conveyorLine = (CommonConveyorLine)context.JobDetail.JobDataMap.Get("JobParams"); + if (conveyorLine != null) { - ProcessDeviceAsync(conveyorLine, childDeviceCode); - } + List<string> childDeviceCodes = _routerService.QueryAllPositions(conveyorLine.DeviceCode); + 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) - { - if (station.stationType == 11) + List<Dt_StationManager> stationManagers = _stationManagerService.GetAllStationByDeviceCode(conveyorLine.DeviceCode); + foreach (var station in stationManagers) { - ConveyorLineTaskCommand command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(station.stationChildCode); - ConveyorLineTaskCommandWrite commandWrite = conveyorLine.ReadCustomer<ConveyorLineTaskCommandWrite>(station.stationChildCode, "DeviceCommand"); - if (command != null && commandWrite != null) + if (station.stationType == 11) { - var structs = BitConverter.GetBytes(commandWrite.WriteInteractiveSignal).Reverse().ToArray().ToBoolArray(); - if (structs[0] == true) + ConveyorLineTaskCommand command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(station.stationChildCode); + ConveyorLineTaskCommandWrite commandWrite = conveyorLine.ReadCustomer<ConveyorLineTaskCommandWrite>(station.stationChildCode, "DeviceCommand"); + if (command != null && commandWrite != null) { - ConsoleHelper.WriteWarningLine($"銆恵conveyorLine.DeviceName}銆戜换鍔″彿锛氥�恵command.TaskNum}銆�,鎵樼洏鏉$爜锛氥�恵command.Barcode}銆戝凡鍒拌揪銆恵station.stationChildCode}銆戠┖鎵樼洏璇锋眰鎵爜鍏ュ簱"); - NGRequestTaskInbound(conveyorLine, command, station.stationChildCode, 0, station.stationLocation); - } - else - { - ConveyorLineSendFinish(conveyorLine, station.stationChildCode, 0, false); - } - } - } - } - } - } - catch (Exception ex) - { - Console.Out.WriteLine(nameof(CommonConveyorLineJob) + ":" + ex.ToString()); - } - return Task.CompletedTask; - } - - private void 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()) - { - //throw new Exception(conveyorLine.DeviceName + "缂撳瓨涓湭鎵惧埌Token缂撳瓨"); - return; - } - 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 }); - - #endregion 璋冪敤浜嬩欢鎬荤嚎閫氱煡鍓嶇 - - 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) - { - var outDeviceCodes = _routerService.QueryOutDeviceCodes(conveyorLine.DeviceCode); - 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); - - //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); - //} - } - } - } - - 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) + var structs = BitConverter.GetBytes(commandWrite.WriteInteractiveSignal).Reverse().ToArray().ToBoolArray(); + if (structs[0] == true) { - command.InteractiveSignal = commandWrite.WriteInteractiveSignal; - //int count = strings.Count - index; - int count = strings.Count; - method.Invoke(this, new object[] { conveyorLine, command, childDeviceCode, count, platform }); + ConsoleHelper.WriteWarningLine($"銆恵conveyorLine.DeviceName}銆戜换鍔″彿锛氥�恵command.TaskNum}銆�,鎵樼洏鏉$爜锛氥�恵command.Barcode}銆戝凡鍒拌揪銆恵station.stationChildCode}銆戠┖鎵樼洏璇锋眰鎵爜鍏ュ簱"); + NGRequestTaskInbound(conveyorLine, command, station.stationChildCode, 0, station.stationLocation); + } + else + { + ConveyorLineSendFinish(conveyorLine, station.stationChildCode, 0, false); } } } } } } - + catch (Exception ex) + { + Console.Out.WriteLine(nameof(CommonConveyorLineJob) + ":" + ex.ToString()); + } + finally + { + MemoryLockManager.ReleaseLock(jobName); + } } + else + { + ConsoleHelper.WriteErrorLine($"[CommonConveyorLineJob]銆恵jobName}銆戜换鍔″凡琚攣瀹氾紝鏃犳硶澶勭悊"); + } + return; + } + + 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()) + { + 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 }); + } + + #endregion 璋冪敤浜嬩欢鎬荤嚎閫氱煡鍓嶇 + + 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) + { + 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) + { + command.InteractiveSignal = writeInteractiveSignal; + method.Invoke(this, new object[] { conveyorLine, command, childDeviceCode, itemValue }); + } + } + } + else + { + ConveyorLineSendFinish(conveyorLine, childDeviceCode, itemValue, false); + } + } + } + + Platform platform = _platFormRepository.QueryFirst(x => x.PLCCode == conveyorLine.DeviceCode && x.PlatCode == childDeviceCode && x.Status == "Active"); + if (platform != null && !string.IsNullOrEmpty(platform.Location)) + { + var strings = platform.Location.Split(',').ToList(); + foreach (var ite in strings) + { + 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; + 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); -- Gitblit v1.9.3