#region << 版 本 注 释 >> /*---------------------------------------------------------------- * 命名空间:WIDESEAWCS_Tasks.ConveyorLineJob * 创建者:胡童庆 * 创建时间:2024/8/2 16:13:36 * 版本:V1.0.0 * 描述: * * ---------------------------------------------------------------- * 修改人: * 修改时间: * 版本:V1.0.1 * 修改说明: * *----------------------------------------------------------------*/ #endregion << 版 本 注 释 >> using AutoMapper; using Quartz; using WIDESEAWCS_Common.TaskEnum; using WIDESEAWCS_Core; using WIDESEAWCS_Core.Helper; using WIDESEAWCS_ITaskInfoRepository; using WIDESEAWCS_ITaskInfoService; using WIDESEAWCS_Model.Models; using WIDESEAWCS_QuartzJob; using WIDESEAWCS_QuartzJob.Service; using WIDESEAWCS_Tasks.ConveyorLineJob; namespace WIDESEAWCS_Tasks { [DisallowConcurrentExecution] public class CommonConveyorLineJob : JobBase, IJob, IDisposable { private readonly ITaskService _taskService; private readonly ITaskRepository _taskRepository; private readonly ITaskCZRepository _taskCZRepository; private readonly ITaskExecuteDetailService _taskExecuteDetailService; private readonly IRouterService _routerService; private readonly IMapper _mapper; public CommonConveyorLineJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IMapper mapper, ITaskRepository taskRepository, ITaskCZRepository taskCZRepository) { _taskService = taskService; _taskExecuteDetailService = taskExecuteDetailService; _routerService = routerService; _mapper = mapper; _taskRepository = taskRepository; _taskCZRepository = taskCZRepository; } public Task Execute(IJobExecutionContext context) { try { // 从上下文中获取 JobParams 并转换为 CommonConveyorLine 类型 CommonConveyorLine conveyorLine = (CommonConveyorLine)context.JobDetail.JobDataMap.Get("JobParams"); if (conveyorLine == null) { throw new Exception("JobParams 不包含 CommonConveyorLine 类型参数"); } // 定义线体实盘入库请求的地址和任务类型 var requests = new Dictionary { { "DB1002.1093.0", "ZJXL-WLX002" }, { "DB1002.1493.0", "FJXL-WLX002" } }; // 定义线体空盘回流请求的地址和任务类型 var requestsKP = new Dictionary { { "DB1002.1893.0", "ZJXL-KPHLX001" }, { "DB1002.2293.0", "FJXL-KPHLX001" } }; // 处理实盘入库请求 ProcessRequests(conveyorLine, requests, "下线请求入库"); // 处理空盘回流请求 ProcessKpRequests(conveyorLine, requestsKP); } catch (Exception ex) { // 记录异常信息 // Console.Out.WriteLine(nameof(CommonConveyorLineJob) + ":" + ex.ToString()); ConsoleHelper.WriteErrorLine($"{nameof(CommonConveyorLineJob)}: 发生异常 - {ex.Message}"); } finally { // 写调试信息 WriteDebug("CommonConveyorLineJob", "test"); // Console.Out.WriteLine(DateTime.Now); } return Task.CompletedTask; } private void ProcessRequests(CommonConveyorLine conveyorLine, Dictionary requests, string requestType) { foreach (var request in requests) { var isDownRequest = conveyorLine.Communicator.Read(request.Key); if (!isDownRequest) { continue; } string fromAdd = request.Value; string taskType = request.Value.Contains("ZJXL") ? "正极" : "负极"; // 查询是否存在已生成的新任务 var task = _taskRepository.QueryFirst(x => x.SourceAddress == fromAdd && x.TaskState == (int)TaskInStatusEnum.InNew); if (task != null) { ConsoleHelper.WriteInfoLine($"{nameof(CommonConveyorLineJob)}: {taskType}{requestType},任务已生成存在,稍后重试......"); continue; } // 查询任务类型对应的czTask DtCZTask czTask = _taskCZRepository.QueryFirst(x => x.TaskType == taskType); if (czTask == null) { ConsoleHelper.WriteInfoLine($"{nameof(CommonConveyorLineJob)}: {taskType}{requestType},{taskType}任务不存在,稍后重试......"); continue; } // 创建并添加新任务到任务仓库 task = CreateTask(fromAdd, czTask.TaskEndAddress, czTask.TaskOrderNo, czTask.TaskProductCode, taskType); _taskRepository.AddData(task); ConsoleHelper.WriteInfoLine($"{nameof(CommonConveyorLineJob)}: {taskType}{requestType},任务已生成,等待执行......"); } } private void ProcessKpRequests(CommonConveyorLine conveyorLine, Dictionary requestsKP) { foreach (var request in requestsKP) { var isDownRequest = conveyorLine.Communicator.Read(request.Key); if (!isDownRequest) { continue; } string fromAdd = request.Value; string taskType = request.Value.Contains("ZJXL") ? "正极" : "负极"; // 根据任务类型确定请求地址 (string upRequest, string gmRequest, string gmState) = GetRequestAddresses(taskType); var isUpRequest = conveyorLine.Communicator.Read(upRequest); var isGMRequest = conveyorLine.Communicator.Read(gmRequest); var isGMState = conveyorLine.Communicator.Read(gmState); // 根据条件创建任务对象 Dt_Task task = null; if (isUpRequest) { task = CreateTask(fromAdd, "WaitBind", "正极物流线002的上料请求", "空托盘", taskType); } else if (isGMRequest && isGMState == 1) { task = CreateTask(fromAdd, "WaitBind", "正极物流线002的上料请求", "隔膜空托盘", taskType); } // 添加任务到任务表 if (task != null) { _taskRepository.AddData(task); } } } private (string upRequest, string gmRequest, string gmState) GetRequestAddresses(string taskType) { if (taskType == "正极") { // 物流线002的上料请求 return ("DB1002.93.0", "DB1002.2493.0", "DB1002.2424.0"); } else { // 物流线002的上料请求 return ("DB1002.1293.0", "DB1002.2893.0", "DB1002.2824.0"); } } // 辅助方法:创建任务对象 private Dt_Task CreateTask(string currentAddress, string targetAddress, string remark, string palletCode, string taskType) { return new Dt_Task { TaskNum = _taskService.GetTaskNum(), CreateDate = DateTime.Now, Creater = "system", CurrentAddress = currentAddress, SourceAddress = currentAddress, TaskState = targetAddress != "WaitBind" ? (int)TaskInStatusEnum.InNew : (int)TaskInStatusEnum.InPending, TaskType = (int)TaskOutboundTypeEnum.Outbound, Grade = 1, PalletCode = palletCode, TargetAddress = targetAddress, NextAddress = targetAddress, Barcode = "", Roadway = $"{taskType}AGV", WMSId = 0, Remark = remark }; } /// /// 输送线请求入库 /// /// 输送线实例对象 /// 读取的请求信息 /// 子设备编号 public void RequestInbound(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode) { if (_taskService.RequestWMSTask(command.Barcode, childDeviceCode).Status) { Dt_Task task = _taskService.QueryConveyorLineTask(conveyorLine.DeviceCode, childDeviceCode); if (task != null) { ConveyorLineTaskCommand taskCommand = _mapper.Map(task); taskCommand.InteractiveSignal = command.InteractiveSignal; conveyorLine.SendCommand(taskCommand, childDeviceCode); _taskService.UpdateTaskStatusToNext(task); } } } /// /// 输送线请求入库下一地址 /// /// 输送线实例对象 /// 读取的请求信息 /// 子设备编号 public void RequestInNextAddress(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode) { Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.TaskNum, childDeviceCode); if (task != null) { Dt_Task? newTask = _taskService.UpdatePosition(task.TaskNum, task.CurrentAddress); if (newTask != null) { ConveyorLineTaskCommand taskCommand = _mapper.Map(newTask); taskCommand.InteractiveSignal = command.InteractiveSignal; conveyorLine.SendCommand(taskCommand, childDeviceCode); } } } /// /// 输送线入库完成 /// /// 输送线实例对象 /// 读取的请求信息 /// 子设备编号 public void ConveyorLineInFinish(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode) { Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.TaskNum, childDeviceCode); if (task != null) { conveyorLine.SetValue(ConveyorLineDBName.WriteInteractiveSignal, 0, childDeviceCode); WebResponseContent content = _taskService.UpdateTaskStatusToNext(task); Console.Out.WriteLine(content.Serialize()); } } /// /// 输送线请求出信息 /// /// 输送线实例对象 /// 读取的请求信息 /// 子设备编号 public void RequestOutbound(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode) { Dt_Task task = _taskService.QueryConveyorLineTask(conveyorLine.DeviceCode, childDeviceCode); if (task != null) { ConveyorLineTaskCommand taskCommand = _mapper.Map(task); taskCommand.InteractiveSignal = command.InteractiveSignal; conveyorLine.SendCommand(taskCommand, childDeviceCode); _taskService.UpdateTaskStatusToNext(task); } } /// /// 输送线请求出库下一地址 /// /// 输送线实例对象 /// 读取的请求信息 /// 子设备编号 public void RequestOutNextAddress(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode) { Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.TaskNum, childDeviceCode); if (task != null) { Dt_Task? newTask = _taskService.UpdatePosition(task.TaskNum, task.CurrentAddress); if (newTask != null) { ConveyorLineTaskCommand taskCommand = _mapper.Map(newTask); taskCommand.InteractiveSignal = command.InteractiveSignal; conveyorLine.SendCommand(taskCommand, childDeviceCode); } } } /// /// 输送线出库完成 /// /// 输送线实例对象 /// 读取的请求信息 /// 子设备编号 public void ConveyorLineOutFinish(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode) { Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.TaskNum, childDeviceCode); if (task != null) { conveyorLine.SetValue(ConveyorLineDBName.WriteInteractiveSignal, 0, childDeviceCode); WebResponseContent content = _taskService.UpdateTaskStatusToNext(task); Console.Out.WriteLine(content.Serialize()); } } public void Dispose() { GC.SuppressFinalize(this); } } }