using Autofac.Core; using AutoMapper; using Microsoft.AspNetCore.Components.Routing; using Newtonsoft.Json; using Quartz; using SqlSugar.Extensions; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using WIDESEAWCS_Common; using WIDESEAWCS_Common.TaskEnum; using WIDESEAWCS_Communicator; using WIDESEAWCS_Core; using WIDESEAWCS_Core.Helper; using WIDESEAWCS_Core.HttpContextUser; using WIDESEAWCS_IBasicInfoRepository; using WIDESEAWCS_ITaskInfoRepository; using WIDESEAWCS_ITaskInfoService; using WIDESEAWCS_Model.Models; using WIDESEAWCS_QuartzJob; using WIDESEAWCS_QuartzJob.DTO; using WIDESEAWCS_QuartzJob.Models; using WIDESEAWCS_QuartzJob.Repository; using WIDESEAWCS_QuartzJob.Service; using WIDESEAWCS_Tasks.ConveyorLineJob; using ICacheService = WIDESEAWCS_Core.Caches.ICacheService; namespace WIDESEAWCS_Tasks { [DisallowConcurrentExecution] public partial class ConveyorLineJob_YL2ndFloor : JobBase, IJob { private readonly ITaskService _taskService; private readonly IMapper _mapper; private readonly ITaskExecuteDetailService _taskExecuteDetailService; private readonly ITaskRepository _taskRepository; private readonly IRouterRepository _routerRepository; private readonly IStationMangerRepository _stationMangerRepository; public ConveyorLineJob_YL2ndFloor(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IStationMangerRepository stationMangerRepository,IMapper mapper, IRouterRepository routerRepository) { _taskService = taskService; _taskExecuteDetailService = taskExecuteDetailService; _taskRepository = taskRepository; _stationMangerRepository = stationMangerRepository; _mapper = mapper; _routerRepository = routerRepository; } public Task Execute(IJobExecutionContext context) { try { CommonConveyorLine conveyorLine = (CommonConveyorLine)context.JobDetail.JobDataMap.Get("JobParams"); if (conveyorLine != null) { #region 站台方式 List stationManagers = _stationMangerRepository.QueryData(x => x.StationDeviceCode == conveyorLine.DeviceCode); foreach (var station in stationManagers) { ConveyorLineTaskCommand command = conveyorLine.ReadCustomer(station.StationCode); DeviceProtocolDetailDTO? deviceProtocolDetails = conveyorLine.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(ConveyorLineTaskCommand.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 站台方式 } } catch (Exception ex) { Console.Out.WriteLine(nameof(ConveyorLineJob_YL2ndFloor) + ":" + DateTime.Now + ":" + ex.ToString(), ex.StackTrace); } finally { } return Task.CompletedTask; } /// /// 输送线请求入库 /// /// 输送线实例对象 /// 读取的请求信息 /// 子设备编号 /// 线体当前bool读取偏移地址 public void RequestInbound(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, Dt_StationManger stationManger) { try { var task = _taskService.QueryLineExecuteTaskByBarcode(command.TaskNum, stationManger.StationCode); var log = $"时间:【{DateTime.Now}】【{conveyorLine.DeviceName}】托盘号:【{command.Barcode}】任务号:【{command.TaskNum}】设备编码:【{stationManger.StationCode}】"; ConsoleHelper.WriteSuccessLine(log); WriteInfo(conveyorLine.DeviceName, log); if(task != null) { ConveyorLineTaskCommandWrite taskCommand = _mapper.Map(task); bool sendFlag = SendCommand(taskCommand, conveyorLine, stationManger.StationCode); if (sendFlag) { _taskService.UpdateTaskStatusToNext(task, stationManger); } } else { HandleNewTask(conveyorLine, command, stationManger); } } catch (Exception ex) { Console.Out.WriteLine(ex.ToString()); } } /// /// 输送线入库完成 /// /// 输送线实例对象 /// 读取的请求信息 /// 子设备编号 /// 线体当前bool读取偏移地址 public void ConveyorLineInFinish(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, Dt_StationManger stationManger) { var task = _taskService.QueryExecutingTaskByBarcode(command.TaskNum, stationManger.StationCode); if (task != null && task.TaskState != (int)TaskStatusEnum.SC_Execute) { WebResponseContent content = _taskService.UpdateTaskStatusToNext(task, stationManger); if (content.Status) { conveyorLine.SetValue(ConveyorLineDBName.ResponState, 86, stationManger.StationCode); } Console.Out.WriteLine(content.Serialize()); } } public bool SendCommand(ConveyorLineTaskCommandWrite taskCommand, CommonConveyorLine conveyorLine, string childDeviceCode) { conveyorLine.SetValue(ConveyorLineDBName.TaskNum, taskCommand.TaskNum, childDeviceCode); conveyorLine.SetValue(ConveyorLineDBName.TargetAddress, taskCommand.TargetAddress, childDeviceCode); conveyorLine.SetValue(ConveyorLineDBName.Weight, taskCommand.Weight, childDeviceCode); conveyorLine.SetValue(ConveyorLineDBName.Barcode, taskCommand.Barcode, childDeviceCode); conveyorLine.SetValue(ConveyorLineDBName.ResponState, 86, childDeviceCode); for (int i = 0; i < 6; i++) { ConveyorLineTaskCommand command = conveyorLine.ReadCustomer(childDeviceCode); if (command != null) { if (command.TaskNum == taskCommand.TaskNum && command.TargetAddress == taskCommand.TargetAddress && command.Weight == taskCommand.Weight && command.Barcode == taskCommand.Barcode) { WriteInfo(conveyorLine.DeviceName, $"时间:【{DateTime.Now}】写入任务成功写入次数{i}写入任务【{JsonConvert.SerializeObject(taskCommand)}】"); return true; } if (command.TaskNum != taskCommand.TaskNum) { conveyorLine.SetValue(ConveyorLineDBName.TaskNum, taskCommand.TaskNum, childDeviceCode); Thread.Sleep(100); } if (command.Barcode != taskCommand.Barcode) { conveyorLine.SetValue(ConveyorLineDBName.Barcode, taskCommand.Barcode, childDeviceCode); Thread.Sleep(100); } if (command.TaskNum != taskCommand.TaskNum) { conveyorLine.SetValue(ConveyorLineDBName.TaskNum, taskCommand.TaskNum, childDeviceCode); Thread.Sleep(100); } if (command.TargetAddress != taskCommand.TargetAddress) { conveyorLine.SetValue(ConveyorLineDBName.TargetAddress, taskCommand.TargetAddress, childDeviceCode); Thread.Sleep(100); } } conveyorLine.SetValue(ConveyorLineDBName.ResponState, 86, childDeviceCode); } WriteInfo(conveyorLine.DeviceName, $"时间:【{DateTime.Now}】写入任务成功任务号【{taskCommand.TaskNum}】托盘号【{taskCommand.Barcode}】目标地址【{taskCommand.TargetAddress}】当前节点【{childDeviceCode}】"); return false; } } }