| using Quartz; | 
| using System; | 
| using System.Collections.Generic; | 
| using System.Linq; | 
| using System.Text; | 
| using System.Threading.Tasks; | 
| using WIDESEAWCS_Common; | 
| using WIDESEAWCS_Common.Helper; | 
| using WIDESEAWCS_Common.TaskEnum; | 
| using WIDESEAWCS_Core; | 
| using WIDESEAWCS_Core.Helper; | 
| 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_Tasks.ConveyorLineJob; | 
|   | 
| namespace WIDESEAWCS_Tasks | 
| { | 
|     [DisallowConcurrentExecution] | 
|     public class TSJConveyorLineJob_PP : JobBase, IJob | 
|     { | 
|   | 
|         private readonly ITaskService _taskService; | 
|         private readonly ITaskExecuteDetailService _taskExecuteDetailService; | 
|         private readonly ITaskRepository _taskRepository; | 
|         private readonly IStationMangerRepository _stationMangerRepository; | 
|         private readonly IRouterRepository _routerRepository; | 
|   | 
|         public TSJConveyorLineJob_PP(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IStationMangerRepository stationMangerRepository, IRouterRepository routerRepository) | 
|         { | 
|             _taskService = taskService; | 
|             _taskExecuteDetailService = taskExecuteDetailService; | 
|             _taskRepository = taskRepository; | 
|             _stationMangerRepository = stationMangerRepository; | 
|             _routerRepository = routerRepository; | 
|         } | 
|   | 
|         public Task Execute(IJobExecutionContext context) | 
|         { | 
|             bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value); | 
|             if (flag && value != null) | 
|             { | 
|                 OtherDevice device = (OtherDevice)value; | 
|                 List<string> deviceStations = device.DeviceProDTOs.Select(x => x.DeviceChildCode).ToList(); | 
|                 List<Dt_StationManger> stationMangers = _stationMangerRepository.QueryData(x => x.StationDeviceCode == device.DeviceCode); | 
|                 foreach (var item in stationMangers.Where(x => deviceStations.Contains(x.StationCode))) | 
|                 { | 
|                     DeviceProDTO? deviceProRead = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(R_ConveyorLineDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault(); | 
|   | 
|                     DeviceProDTO? deviceProWrite = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(W_ConveyorLineDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault(); | 
|   | 
|                     if (deviceProRead != null && deviceProWrite != null) | 
|                     { | 
|                         R_ConveyorLineInfo conveyorLineInfoRead = device.Communicator.ReadCustomer<R_ConveyorLineInfo>(deviceProRead.DeviceProAddress); | 
|   | 
|                         W_ConveyorLineInfo conveyorLineInfoWrite = device.Communicator.ReadCustomer<W_ConveyorLineInfo>(deviceProWrite.DeviceProAddress); | 
|                         if (conveyorLineInfoRead == null || conveyorLineInfoWrite == null) | 
|                         { | 
|                             continue; | 
|                         } | 
|   | 
|                         R_ConveyorLineStatus conveyorLineStatus = conveyorLineInfoRead.Status.ByteToBoolObject<R_ConveyorLineStatus>(); | 
|   | 
|                         ConveyorLineSignal conveyorLineSignalRead = conveyorLineInfoRead.Signal.ByteToBoolObject<ConveyorLineSignal>(); | 
|   | 
|                         ConveyorLineSignal conveyorLineSignalWrite = conveyorLineInfoWrite.Signal.ByteToBoolObject<ConveyorLineSignal>(); | 
|                         #region | 
|                         //if (item.StationType == StationTypeEnum.StationType_OnlyInbound.ObjToInt()) | 
|                         //{ | 
|                         //    if (conveyorLineSignalRead.STB && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !conveyorLineSignalWrite.ACK) | 
|                         //    { | 
|                         //        if (!string.IsNullOrEmpty(conveyorLineInfoRead.Barcode)) | 
|                         //        { | 
|                         //            WebResponseContent content = _taskService.RequestWMSTask(conveyorLineInfoRead.Barcode, item.StationCode); | 
|                         //            if (content.Status) | 
|                         //            { | 
|                         //                device.SetValue(W_ConveyorLineDB.ACK, true, item.StationCode); | 
|                         //            } | 
|                         //        } | 
|                         //    } | 
|                         //    else if (!conveyorLineSignalRead.STB && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && conveyorLineSignalWrite.ACK) | 
|                         //    { | 
|                         //        Dt_Task task = _taskRepository.QueryFirst(x => x.CurrentAddress == item.StationCode && x.PalletCode == conveyorLineInfoRead.Barcode && x.DeviceCode == item.StationDeviceCode && x.TaskState == TaskStatusEnum.New.ObjToInt()); | 
|                         //        if (task != null) | 
|                         //        { | 
|                         //            _taskService.UpdateTask(task, TaskStatusEnum.SC_Execute, currentAddress: item.StackerCraneStationCode, deviceCode: task.Roadway, nextAddress: task.TargetAddress); | 
|                         //        } | 
|   | 
|                         //        device.SetValue(W_ConveyorLineDB.ACK, false, item.StationCode); | 
|                         //    } | 
|   | 
|                         //} | 
|                         #endregion | 
|                         //2016出库站台任务完成 | 
|                         if (item.StationType == StationTypeEnum.StationType_OnlyOutbound.ObjToInt()) | 
|                         { | 
|                             if (conveyorLineSignalRead.STB) | 
|                             { | 
|                                 device.SetValue(W_ConveyorLineDB.ACK, true, item.StationCode); | 
|                                 Thread.Sleep(500); | 
|                                 device.SetValue(W_ConveyorLineDB.ACK, false, item.StationCode); | 
|                                 _taskService.TaskCompleted(conveyorLineInfoRead.TaskNum); | 
|                             } | 
|                         } | 
|                         else if (item.StationType == StationTypeEnum.StationType_InboundAndOutbound.ObjToInt()) | 
|                         { | 
|                             //判断空中连廊出库空闲 | 
|                             if (!conveyorLineSignalRead.STB && !conveyorLineSignalRead.ACK && conveyorLineStatus.Online && !conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !conveyorLineSignalWrite.STB && !conveyorLineSignalWrite.ACK) | 
|                             { | 
|                                 Dt_Task task = _taskRepository.QueryFirst(x => _taskService.TaskOutboundTypes.Contains(x.TaskType) && x.DeviceCode == item.StackerCraneCode && x.TaskState == TaskStatusEnum.New.ObjToInt() && string.IsNullOrEmpty(x.TargetAddress) && !string.IsNullOrEmpty(x.NextAddress)); | 
|                                 if (task != null) | 
|                                 { | 
|                                     _taskService.UpdateTask(task, TaskStatusEnum.SC_Execute, nextAddress: item.StackerCraneStationCode, deviceCode: item.StackerCraneCode, targetAddress: item.StationCode); | 
|                                 } | 
|                             } | 
|   | 
|                             if (conveyorLineSignalRead.STB && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !conveyorLineSignalWrite.ACK)//入库 | 
|                             { | 
|                                 Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == conveyorLineInfoRead.TaskNum && x.DeviceCode == item.StationDeviceCode && x.NextAddress == item.StationCode); | 
|                                 if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup) | 
|                                 { | 
|                                     Dt_StationManger? stationManger = stationMangers.FirstOrDefault(x => x.StationCode == item.StationCode); | 
|                                     if (stationManger == null) | 
|                                     { | 
|                                         WriteError(item.StationName, $"未找到对应站台信息,设备编号:{item.StationCode},任务号:{task.TaskNum}"); | 
|                                         continue; | 
|                                     } | 
|                                     string? locationCode = _taskService.RequestAssignLocation(task.TaskNum, stationManger.StackerCraneCode); | 
|                                     if (string.IsNullOrEmpty(locationCode)) | 
|                                     { | 
|                                         WriteError(item.StationName, $"请求分配货位返回信息错误,设备编号:{item.StationCode},任务号:{task.TaskNum}"); | 
|                                         continue; | 
|                                     } | 
|                                     _taskService.UpdateTask(task, TaskStatusEnum.SC_Execute, currentAddress: stationManger.StackerCraneStationCode, targetAddress: locationCode, nextAddress: locationCode, deviceCode: stationManger.StackerCraneCode); | 
|   | 
|                                     device.SetValue(W_ConveyorLineDB.Spare1, ConveyorWorkTypeEnum.Inbound.ObjToInt(), item.StationCode); | 
|                                     device.SetValue(W_ConveyorLineDB.ACK, true, item.StationCode); | 
|                                 } | 
|                             } | 
|                             else if (!conveyorLineSignalRead.STB && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && conveyorLineSignalWrite.ACK) | 
|                             { | 
|                                 device.SetValue(W_ConveyorLineDB.ACK, false, item.StationCode); | 
|                             } | 
|                             else | 
|                             { | 
|                                 #region 出库 | 
|                                 if (!conveyorLineSignalWrite.STB && !conveyorLineSignalRead.ACK && conveyorLineStatus.Goods) | 
|                                 { | 
|                                     Dt_Task task = _taskRepository.QueryFirst(x => x.CurrentAddress == item.StationCode && x.TaskState == TaskStatusEnum.Line_Execute.ObjToInt() && x.DeviceCode == item.StationDeviceCode); | 
|                                     if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup) | 
|                                     { | 
|                                         Dt_StationManger? stationManger = stationMangers.FirstOrDefault(x => x.StationCode == item.StationCode); | 
|                                         if (stationManger == null) | 
|                                         { | 
|                                             WriteError(item.StationName, $"未找到对应站台信息,设备编号:{item.StationCode},任务号:{task.TaskNum}"); | 
|                                             continue; | 
|                                         } | 
|   | 
|                                         Dt_Router router = _routerRepository.QueryFirst(x => x.InOutType == task.TaskType && (task.CurrentAddress == x.StartPosi)); | 
|                                         if (router == null) | 
|                                         { | 
|                                             WriteError(item.StationName, $"未找到路由配置信息,设备编号:{item.StationCode},任务号:{task.TaskNum}"); | 
|                                             continue; | 
|                                         } | 
|   | 
|                                         _taskService.UpdateTask(task, TaskStatusEnum.Line_Executing, nextAddress: router.NextPosi, targetAddress: router.NextPosi); | 
|   | 
|                                         device.SetValue(W_ConveyorLineDB.TaskNum, task.TaskNum, item.StationCode); | 
|                                         device.SetValue(W_ConveyorLineDB.EndPos, task.NextAddress, item.StationCode); | 
|                                         device.SetValue(W_ConveyorLineDB.StartPos, task.CurrentAddress, item.StationCode); | 
|                                         device.SetValue(W_ConveyorLineDB.Spare1, ConveyorWorkTypeEnum.Outbound.ObjToInt(), item.StationCode); | 
|                                         device.SetValue(W_ConveyorLineDB.STB, true, item.StationCode); | 
|                                     } | 
|                                 } | 
|                                 else if (conveyorLineSignalWrite.STB && conveyorLineSignalRead.ACK && conveyorLineStatus.Goods) | 
|                                 { | 
|                                     _taskService.TaskCompleted(conveyorLineInfoRead.TaskNum); | 
|                                     device.SetValue(W_ConveyorLineDB.STB, false, item.StationCode); | 
|                                 } | 
|                                 #endregion | 
|                             } | 
|                         } | 
|                         else if (item.StationType == StationTypeEnum.StationType_InStartAndOutEnd.ObjToInt()) | 
|                         { | 
|                             if (!conveyorLineSignalWrite.STB && !conveyorLineSignalWrite.ACK && !conveyorLineSignalRead.STB && !conveyorLineSignalRead.ACK && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm) | 
|                             { | 
|                                 Dt_Task outTask = _taskRepository.QueryFirst(x => _taskService.TaskOutboundTypes.Contains(x.TaskType) && x.TaskState != TaskStatusEnum.New.ObjToInt() && x.TaskState != TaskStatusEnum.SC_Execute.ObjToInt() && x.TargetAddress == item.StationCode); | 
|                                 if (outTask == null) | 
|                                 { | 
|                                     Dt_Task task = _taskRepository.QueryFirst(x => x.TaskState == TaskStatusEnum.New.ObjToInt() && string.IsNullOrEmpty(x.TargetAddress) && x.SourceAddress == item.StationCode && _taskService.TaskInboundTypes.Contains(x.TaskType)); | 
|                                     if (task != null) | 
|                                     { | 
|                                         //todo 输送线工作模式需要判断 | 
|                                         _taskService.UpdateTask(task, TaskStatusEnum.Line_Execute); | 
|   | 
|                                         device.SetValue(W_ConveyorLineDB.TaskNum, task.TaskNum, item.StationCode); | 
|                                         device.SetValue(W_ConveyorLineDB.EndPos, task.NextAddress, item.StationCode); | 
|                                         device.SetValue(W_ConveyorLineDB.Spare1, ConveyorWorkTypeEnum.Inbound.ObjToInt(), item.StationCode); | 
|                                         device.SetValue(W_ConveyorLineDB.STB, true, item.StationCode); | 
|                                     } | 
|                                 } | 
|                             } | 
|                             else if (conveyorLineSignalWrite.STB && conveyorLineSignalRead.ACK && !conveyorLineStatus.Alarm) | 
|                             { | 
|                                 device.SetValue(W_ConveyorLineDB.STB, false, item.StationCode); | 
|                             } | 
|                             else if (!conveyorLineSignalRead.STB && conveyorLineSignalWrite.ACK && !conveyorLineStatus.Alarm) | 
|                             { | 
|                                 device.SetValue(W_ConveyorLineDB.ACK, false, item.StationCode); | 
|                             } | 
|                         } | 
|                     } | 
|                     else | 
|                     { | 
|                         WriteInfo(device.DeviceName, $"未找到设备子编号{item.StationCode}的协议信息"); | 
|                     } | 
|                 } | 
|             } | 
|   | 
|             return Task.CompletedTask; | 
|         } | 
|     } | 
| } |