#region MyRegion #region << 版 本 注 释 >> /*---------------------------------------------------------------- * 命名空间:WIDESEAWCS_Tasks.ConveyorLineJob * 创建者:胡童庆 * 创建时间:2024/8/2 16:13:36 * 版本:V1.0.0 * 描述: * * ---------------------------------------------------------------- * 修改人: * 修改时间: * 版本:V1.0.1 * 修改说明: * *----------------------------------------------------------------*/ #endregion << 版 本 注 释 >> using AutoMapper; using HslCommunication; using Masuit.Tools; using Microsoft.CodeAnalysis; using Newtonsoft.Json; using Quartz; using SqlSugar; using System.ComponentModel.Design; using System.Reflection; using WIDESEAWCS_BasicInfoRepository; using WIDESEAWCS_Common; using WIDESEAWCS_Common.TaskEnum; using WIDESEAWCS_Core; using WIDESEAWCS_Core.Helper; using WIDESEAWCS_Core.HttpContextUser; using WIDESEAWCS_DTO.MOM; using WIDESEAWCS_DTO.TaskInfo; using WIDESEAWCS_IProcessRepository; using WIDESEAWCS_ISystemServices; using WIDESEAWCS_ITaskInfoRepository; using WIDESEAWCS_ITaskInfoService; using WIDESEAWCS_Model.Models; using WIDESEAWCS_QuartzJob; using WIDESEAWCS_QuartzJob.DTO; using WIDESEAWCS_QuartzJob.Repository; using WIDESEAWCS_QuartzJob.Service; using WIDESEAWCS_SignalR; using WIDESEAWCS_Tasks.ConveyorLineJob; using ICacheService = WIDESEAWCS_Core.Caches.ICacheService; using Platform = WIDESEAWCS_Model.Models.Platform; namespace WIDESEAWCS_Tasks { [DisallowConcurrentExecution] public partial class CommonConveyorLine_CWJob : JobBase, IJob { public readonly ITaskService _taskService; private readonly ITaskRepository _taskRepository; private readonly ITaskExecuteDetailService _taskExecuteDetailService; private readonly IRouterService _routerService; private readonly IPlatFormRepository _platFormRepository; private readonly ISys_ConfigService _sys_ConfigService; private readonly IMapper _mapper; private readonly IDt_StationManagerRepository _stationManagerRepository; private readonly ICacheService _cacheService; private readonly INoticeService _noticeService; private readonly IDt_needBarcodeRepository _needBarcodeRepository; private readonly IDeviceInfoRepository _deviceInfoRepository; private static List? userTokenIds; private static List? userIds; private static List childCodeList = new List(); 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) { _taskService = taskService; _taskExecuteDetailService = taskExecuteDetailService; _routerService = routerService; _mapper = mapper; _taskRepository = taskRepository; _platFormRepository = platFormRepository; _sys_ConfigService = sys_ConfigService; _stationManagerRepository = stationManagerRepository; _cacheService = cacheService; _noticeService = noticeService; _needBarcodeRepository = needBarcodeRepository; _deviceInfoRepository = deviceInfoRepository; } public Task Execute(IJobExecutionContext context) { try { CommonConveyorLine_CW conveyorLine = (CommonConveyorLine_CW)context.JobDetail.JobDataMap.Get("JobParams"); if (conveyorLine != null) { #region 站台方式 //List stationManagers = _stationManagerService.GetAllStationByDeviceCode(conveyorLine.DeviceCode); //foreach (var station in stationManagers) //{ // ConveyorLineTaskCommand_After command = conveyorLine.ReadCustomer(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 childDeviceCodes = _routerService.QueryAllPositions(conveyorLine.DeviceCode); childDeviceCodes.Add("4000"); foreach (string childDeviceCode in childDeviceCodes) { ConveyorLineTaskCommand_After command = conveyorLine.ReadCustomer(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) { 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") { 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>("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 调用事件总线通知前端 } #endregion 路由方式 } } catch (Exception ex) { Console.Out.WriteLine(nameof(CommonConveyorLine_CWJob) + ":" + DateTime.Now + ":" + ex.ToString(),ex.StackTrace); } finally { } return Task.CompletedTask; } /// /// 输送线请求入库 /// /// 输送线实例对象 /// 读取的请求信息 /// 子设备编号 /// 线体当前bool读取偏移地址 public void RequestInbound(CommonConveyorLine_CW conveyorLine, ConveyorLineTaskCommand_After command, string childDeviceCode) { try { var task = _taskService.QueryBarCodeConveyorLineTask(command.ConveyorLineBarcode, childDeviceCode); 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") { conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineTargetAddress, Convert.ToInt16(1601), childDeviceCode); Thread.Sleep(100); conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineTaskNum, 9999, childDeviceCode); } if (task == null) { HandleNewTask(conveyorLine, command, childDeviceCode); } 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; } } ConveyorLineTaskCommand_After taskCommand = _mapper.Map(task); bool sendFlag = SendCommand(taskCommand, conveyorLine, childDeviceCode); if (sendFlag) { conveyorLine.SetValue(ConveyorLineDBName_After.ResponState, Convert.ToInt16(1), childDeviceCode); _taskService.UpdateTaskStatusToNext(task); } } } catch (Exception ex) { Console.Out.WriteLine(ex.ToString()); } } /// /// 输送线请求入库下一地址 /// /// 输送线实例对象 /// 读取的请求信息 /// 子设备编号 public void RequestInNextAddress(CommonConveyorLine_CW conveyorLine, ConveyorLineTaskCommand_After command, string childDeviceCode) { Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.ConveyorLineTaskNum, childDeviceCode, command.ConveyorLineBarcode); if (task != null) { if (command.ConveyorLineBarcode != task.PalletCode) { conveyorLine.SetValue(ConveyorLineDBName_After.ResponState, Convert.ToInt16(1), childDeviceCode); return; } Dt_Task? newTask = _taskService.UpdatePosition(task.TaskNum, task.CurrentAddress); if (newTask != null) { ConveyorLineTaskCommand_After taskCommand = _mapper.Map(newTask); //conveyorLine.SendCommand(taskCommand, childDeviceCode); bool sendFlag = SendCommand(taskCommand, conveyorLine, childDeviceCode); if (sendFlag) { conveyorLine.SetValue(ConveyorLineDBName_After.ResponState, Convert.ToInt16(1), childDeviceCode); _taskService.UpdateData(newTask); } } } //else //{ // //当前地址请求 寻找当前地址的托盘号 任务号的任务,如存在任务则重新再次写入新目标地址 // Dt_Task currentTask = _taskService.QueryExecutingCurrentConveyorLineTask(command.ConveyorLineTaskNum, childDeviceCode, command.ConveyorLineBarcode); // if (currentTask != null) // { // conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineTargetAddress, Convert.ToInt16(currentTask.TargetAddress), childDeviceCode); // } //} } /// /// 输送线入库完成 /// /// 输送线实例对象 /// 读取的请求信息 /// 子设备编号 /// 线体当前bool读取偏移地址 public void ConveyorLineInFinish(CommonConveyorLine_CW conveyorLine, ConveyorLineTaskCommand_After command, string childDeviceCode) { var task = _taskService.QueryExecutingTaskByBarcode(command.ConveyorLineBarcode, childDeviceCode); if (task != null && task.TaskState != (int)TaskInStatusEnum.Line_InFinish) { WebResponseContent content = _taskService.UpdateTaskStatusToNext(task); if (content.Status) { conveyorLine.SetValue(ConveyorLineDBName_After.ResponState, Convert.ToInt16(1), childDeviceCode); } Console.Out.WriteLine(content.Serialize()); } } /// /// 输送线请求出信息 /// /// 输送线实例对象 /// 读取的请求信息 /// 子设备编号 /// 线体当前bool读取偏移地址 /// /// 触发输送线出库请求处理 /// /// 输送线实例(CommonConveyorLine_CW类型),包含输送线设备基础信息和操作方法 /// 输送线任务指令对象(后续版本),暂未直接使用,预留指令上下文 /// 子设备编码,用于定位输送线的具体子设备 public void RequestOutbound(CommonConveyorLine_CW conveyorLine, ConveyorLineTaskCommand_After command, string childDeviceCode) { // 根据主输送线设备编码和子设备编码,查询对应的输送线任务 var task = _taskService.QueryConveyorLineTask(conveyorLine.DeviceCode, childDeviceCode); // 若查询到有效任务,则执行后续指令发送和状态更新逻辑 if (task != null) { // 将数据库查询到的任务对象映射为指令对象(适配指令发送的数据结构) ConveyorLineTaskCommand_After taskCommand = _mapper.Map(task); // 发送任务指令到指定输送线子设备,并获取发送结果(原注释掉的conveyorLine.SendCommand已替换为封装后的方法) bool sendFlag = SendCommand(taskCommand, conveyorLine, childDeviceCode); // 若指令发送成功,执行状态更新操作 if (sendFlag) { // 更新输送线子设备的响应状态为1(1代表指令已响应/处理成功,对应数据库字段ConveyorLineDBName_After.ResponState) conveyorLine.SetValue(ConveyorLineDBName_After.ResponState, Convert.ToInt16(1), childDeviceCode); // 将任务状态更新到下一个阶段(通用任务状态流转) _taskService.UpdateTaskStatusToNext(task); // 特殊处理:若任务类型为出托盘(OutTray),额外再执行一次任务状态更新(适配出托盘任务的特殊状态流转逻辑) if (task.TaskType == (int)TaskOutboundTypeEnum.OutTray) { _taskService.UpdateTaskStatusToNext(task); } } } } /// /// 输送线请求出库下一地址 /// /// 输送线实例对象 /// 读取的请求信息 /// 子设备编号 /// /// 处理输送线下一地址请求(核心逻辑:查询执行中任务→获取MOM配置→调用MOM接口校验→根据校验结果/任务状态发送指令并更新任务) /// /// 输送线实例(CommonConveyorLine_CW类型),包含输送线设备基础信息和操作方法 /// 输送线任务指令对象(后续版本),携带任务编号、条码、子设备编码等关键指令参数 /// 子设备编码,用于定位输送线的具体子设备 public void RequestOutNextAddress(CommonConveyorLine_CW conveyorLine, ConveyorLineTaskCommand_After command, string childDeviceCode) { // 1. 查询指定条件下正在执行的输送线任务(任务编号+子设备编码+输送线条码) Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.ConveyorLineTaskNum, childDeviceCode, command.ConveyorLineBarcode); // 仅当查询到有效执行中任务时,执行后续逻辑 if (task != null) { // 2. 获取系统配置中「IP地址」分类下的配置项(用于拼接MOM接口地址) var config = _sys_ConfigService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress); // 提取MOM基础IP配置值 var wmsBase = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.MOMIP_BASE)?.ConfigValue; // 提取托盘格口状态接口的IP路径配置值 var ipAddress = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.TrayCellsStatus)?.ConfigValue; // 校验MOM IP配置完整性,缺失则抛出异常终止流程 if (wmsBase == null || ipAddress == null) { throw new InvalidOperationException("MOM IP 未配置"); } // 3. 查询当前输送线设备+子设备对应的工位配置信息(关联PLC编码、子编码与MOM设备编码) Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationPLC == conveyorLine.DeviceCode && x.stationChildCode == childDeviceCode); // 4. 构建托盘格口状态校验的请求DTO(适配MOM接口入参格式) TrayCellsStatusDto trayCells = new TrayCellsStatusDto() { Software = "WMS", // 调用方系统标识(固定为WMS) TrayBarcode = command.ConveyorLineBarcode, // 输送线条码(托盘条码) EquipmentCode = stationManager.stationEquipMOM, // MOM系统对应的设备编码 SessionId = Guid.NewGuid().ToString(), // 唯一会话ID(防止重复请求) EmployeeNo = "MITest", // 操作员工编号(测试固定值) SceneType = "1", // 场景类型(1代表入站校验场景) RequestTime = TimeZoneInfo.ConvertTimeToUtc(DateTime.Now).ToString("yyyy-MM-ddTHH:mm:ss.fffZ") // UTC格式请求时间 }; // 拼接完整的MOM接口地址(基础IP + 接口路径) var MOMIpAddress = wmsBase + ipAddress; // 5. 调用MOM接口(POST异步请求,同步等待结果) var result = HttpHelper.PostAsync(MOMIpAddress, trayCells.Serialize()).Result; // 记录接口调用日志(请求参数、返回参数),便于问题排查 WriteInfo("入站校验", $"【{childDeviceCode}】入站校验请求参数【{trayCells.Serialize()}】"); WriteInfo("入站校验", ""); WriteInfo("入站校验", $"【{childDeviceCode}】入站校验返回参数【{result}】"); // 6. 解析MOM接口返回结果为实体对象 ResultTrayCellsStatus result1 = JsonConvert.DeserializeObject(result); // 7. 分支1:接口校验成功 或 任务备注非NG(正常流程) if (result1.Success || task.Remark != "NG") { // 更新任务的位置信息(任务编号+当前地址),返回更新后的任务对象 Dt_Task? newTask = _taskService.UpdatePosition(task.TaskNum, task.CurrentAddress); if (newTask != null) { // 将更新后的任务对象映射为输送线指令对象(适配指令发送结构) ConveyorLineTaskCommand_After taskCommand = _mapper.Map(newTask); // 发送指令到输送线子设备,获取发送结果(封装后的SendCommand方法) bool sendFlag = SendCommand(taskCommand, conveyorLine, childDeviceCode); // 指令发送成功则更新设备响应状态和任务数据 if (sendFlag) { // 更新输送线子设备响应状态为1(代表指令处理成功) conveyorLine.SetValue(ConveyorLineDBName_After.ResponState, Convert.ToInt16(1), childDeviceCode); // 持久化更新后的任务数据到数据库 _taskService.UpdateData(newTask); } } } // 8. 分支2:接口校验失败 且 任务备注为NG(异常流程,发送NG地址指令) else { // 将原任务对象映射为输送线指令对象 ConveyorLineTaskCommand_After taskCommand = _mapper.Map(task); // 设置指令的目标地址为工位配置中的NG子编码(导向异常处理工位) taskCommand.ConveyorLineTargetAddress = Convert.ToInt16(stationManager.stationNGChildCode); // 发送NG地址指令到输送线子设备,获取发送结果 bool sendFlag = SendCommand(taskCommand, conveyorLine, childDeviceCode); // 指令发送成功则更新设备响应状态和任务状态 if (sendFlag) { // 更新输送线子设备响应状态为1 conveyorLine.SetValue(ConveyorLineDBName_After.ResponState, Convert.ToInt16(1), childDeviceCode); // 将任务状态流转到下一个阶段(适配NG任务的状态逻辑) _taskService.UpdateTaskStatusToNext(task); } } } } /// /// 输送线出库完成 /// /// 输送线实例对象 /// 读取的请求信息 /// 子设备编号 public void ConveyorLineOutFinish(CommonConveyorLine_CW conveyorLine, ConveyorLineTaskCommand_After command, string childDeviceCode) { 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); var task = _taskService.QueryExecutingConveyorLineTask(command.ConveyorLineTaskNum, childDeviceCode, command.ConveyorLineBarcode); if (task != null) { WebResponseContent content = new WebResponseContent(); ConveyorLineTaskCommand_After taskCommand = _mapper.Map(task); taskCommand.InteractiveSignal = command.InteractiveSignal; Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationPLC == conveyorLine.DeviceCode && x.stationChildCode == childDeviceCode); if (task.PalletCode != command.ConveyorLineBarcode) { taskCommand.ConveyorLineTargetAddress = Convert.ToInt16(stationManager.stationNGChildCode); } else { taskCommand.ConveyorLineTargetAddress = Convert.ToInt16(stationManager.stationLocation); } if (stationManager.stationPLC == "1018" && stationManager.stationArea == "Cache") //更新在途数据 { dt_needBarcode needBarcode = _needBarcodeRepository.QueryFirst(x => x.productLine == stationManager.productLine && x.toArea == stationManager.stationChildCode); if (needBarcode != null) { needBarcode.inLineNum--; _needBarcodeRepository.UpdateData(needBarcode); } } bool sendFlag = SendCommand(taskCommand, conveyorLine, childDeviceCode); if (sendFlag) { conveyorLine.SetValue(ConveyorLineDBName_After.ResponState, Convert.ToInt16(1), childDeviceCode); 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 (commonConveyorBZ != null) { ConveyorLineTaskCommand_After readSignal = conveyorLine.ReadCustomer(station.stationLocation); if (readSignal.Reserved5 == 1) { var tasks = _taskRepository.QueryData(x => x.TargetAddress == childDeviceCode); if (tasks.Count == 0) { commonConveyorBZ.SetValue(ConveyorLineDBName_After.ResponState, Convert.ToInt16(1), station.stationLocation); } } } } else { var taskNext = _taskService.QueryExecutingConveyorLineTask(childDeviceCode, command.ConveyorLineBarcode); if (taskNext != null) { WebResponseContent content = new WebResponseContent(); ConveyorLineTaskCommand_After taskCommand = _mapper.Map(taskNext); taskCommand.InteractiveSignal = command.InteractiveSignal; Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationPLC == conveyorLine.DeviceCode && x.stationChildCode == childDeviceCode); if (taskNext.PalletCode != command.ConveyorLineBarcode) { taskCommand.ConveyorLineTargetAddress = Convert.ToInt16(stationManager.stationNGChildCode); } else { taskCommand.ConveyorLineTargetAddress = Convert.ToInt16(stationManager.stationLocation); } if (stationManager.stationPLC == "1018" && stationManager.stationArea == "Cache") //更新在途数据 { dt_needBarcode needBarcode = _needBarcodeRepository.QueryFirst(x => x.productLine == stationManager.productLine && x.toArea == stationManager.stationChildCode); if (needBarcode != null) { needBarcode.inLineNum--; _needBarcodeRepository.UpdateData(needBarcode); } } bool sendFlag = SendCommand(taskCommand, conveyorLine, childDeviceCode); if (sendFlag) { conveyorLine.SetValue(ConveyorLineDBName_After.ResponState, Convert.ToInt16(1), childDeviceCode); taskNext.ExceptionMessage = log; content = _taskService.UpdateTaskStatusToNext(taskNext); } 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 (commonConveyorBZ != null) { ConveyorLineTaskCommand_After readSignal = conveyorLine.ReadCustomer(station.stationLocation); if (readSignal.Reserved5 == 1) { var tasks = _taskRepository.QueryData(x => x.TargetAddress == childDeviceCode); if (tasks.Count == 0) { commonConveyorBZ.SetValue(ConveyorLineDBName_After.ResponState, Convert.ToInt16(1), station.stationLocation); } } } } } } /// /// 监测空托盘实盘出库 /// /// 输送线实例对象 /// 读取的请求信息 /// 子设备编号 /// 线体当前bool读取偏移地址 public async void EmptyTrayReturn(CommonConveyorLine_CW conveyorLine, ConveyorLineTaskCommand_After command, string childDeviceCode, int index, WIDESEAWCS_Model.Models.Platform platform) { try { TaskOutboundTypeEnum taskOutboundTypeEnum; if (platform.PlatformType.Contains("OutTray")) taskOutboundTypeEnum = TaskOutboundTypeEnum.OutTray; else taskOutboundTypeEnum = TaskOutboundTypeEnum.Outbound; await CheckAndCreateTask(taskOutboundTypeEnum, childDeviceCode, index, platform); } catch (Exception) { } } /// /// 检查任务并创建新任务 /// private async Task CheckAndCreateTask(TaskOutboundTypeEnum taskType, string childDeviceCode, int index, Platform platform) { var tasks = _taskRepository.QueryData(x => x.TaskType == (int)taskType && x.TargetAddress == childDeviceCode); if (tasks.Count < platform.Capacity) { #region 调用WMS获取出库任务 WMSTaskDTO taskDTO = new WMSTaskDTO(); // 获取WMSip地址 var config = _sys_ConfigService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress); var wmsBase = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.WMSIP_BASE)?.ConfigValue; var requestTrayOutTask = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.RequestTrayOutTask)?.ConfigValue; if (wmsBase == null || requestTrayOutTask == null) { throw new InvalidOperationException("WMS IP 未配置"); } var wmsIpAddress = wmsBase + requestTrayOutTask; List 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")); WebResponseContent content = JsonConvert.DeserializeObject(result); // 检查状态并返回 if (!content.Status) return; taskDTO = JsonConvert.DeserializeObject(content.Data.ToString()); #endregion 调用WMS获取出库任务 CreateAndSendTask(taskDTO); } } /// /// 创建任务 /// public WebResponseContent CreateAndSendTask(WMSTaskDTO taskDTO) { var content = _taskService.ReceiveWMSTask(new List { taskDTO }); if (content.Status) { Console.WriteLine($"{taskDTO.TaskType}呼叫成功"); } return content; } public bool SendCommand(ConveyorLineTaskCommand_After taskCommand, CommonConveyorLine_CW conveyorLine, string childDeviceCode) { conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineTargetAddress, Convert.ToInt16(taskCommand.ConveyorLineTargetAddress), childDeviceCode); Thread.Sleep(100); conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineBarcode, taskCommand.ConveyorLineBarcode, childDeviceCode); Thread.Sleep(100); conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineTaskNum, taskCommand.ConveyorLineTaskNum, childDeviceCode); for (int i = 0; i < 6; i++) { ConveyorLineTaskCommand_After command = conveyorLine.ReadCustomer(childDeviceCode); if (command != null) { if (command.ConveyorLineBarcode == taskCommand.ConveyorLineBarcode && command.ConveyorLineTaskNum == taskCommand.ConveyorLineTaskNum && command.ConveyorLineTargetAddress == taskCommand.ConveyorLineTargetAddress) { WriteInfo(conveyorLine.DeviceName, $"时间:【{DateTime.Now}】写入任务成功写入次数{i}写入任务【{JsonConvert.SerializeObject(taskCommand)}】"); return true; } if (command.ConveyorLineTargetAddress != taskCommand.ConveyorLineTargetAddress) { conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineTargetAddress, Convert.ToInt16(taskCommand.ConveyorLineTargetAddress), childDeviceCode); Thread.Sleep(100); } if (command.ConveyorLineBarcode != taskCommand.ConveyorLineBarcode) { conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineBarcode, taskCommand.ConveyorLineBarcode, childDeviceCode); Thread.Sleep(100); } if (command.ConveyorLineTaskNum != taskCommand.ConveyorLineTaskNum) { conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineTaskNum, taskCommand.ConveyorLineTaskNum, childDeviceCode); Thread.Sleep(100); } } } WriteInfo(conveyorLine.DeviceName, $"时间:【{DateTime.Now}】写入任务失败任务号【{taskCommand.ConveyorLineTaskNum}】托盘号【{taskCommand.ConveyorLineBarcode}】目标地址【{taskCommand.ConveyorLineTargetAddress}】当前节点【{childDeviceCode}】"); return false; } } } #endregion