| using Microsoft.AspNetCore.Components.Routing; | 
| using Newtonsoft.Json; | 
| using Quartz; | 
| using SqlSugar.Extensions; | 
| using System; | 
| using System.Collections.Generic; | 
| using System.Linq; | 
| using System.Text; | 
| using System.Threading.Tasks; | 
| using WIDESEAWCS_Common; | 
| using WIDESEAWCS_Common.TaskEnum; | 
| 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 class ConveyorLineEmptyBack_CP : JobBase, IJob | 
|     { | 
|         private readonly ICacheService _cacheService; | 
|         private readonly ITaskService _taskService; | 
|         private readonly ITaskExecuteDetailService _taskExecuteDetailService; | 
|         private readonly ITaskRepository _taskRepository; | 
|         private readonly IStationMangerRepository _stationMangerRepository; | 
|         private readonly IRouterRepository _routerRepository; | 
|         private readonly IRouterService _routerService; | 
|         private readonly IRouterExtension _routerExtension; | 
|         private readonly List<Dt_WarehouseDevice> warehouseDevices; | 
|   | 
|         public ConveyorLineEmptyBack_CP(ICacheService cacheService, ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IStationMangerRepository stationMangerRepository, IRouterRepository routerRepository, IRouterService routerService, IRouterExtension routerExtension) | 
|         { | 
|             _cacheService = cacheService; | 
|             _taskService = taskService; | 
|             _taskExecuteDetailService = taskExecuteDetailService; | 
|             _taskRepository = taskRepository; | 
|             _stationMangerRepository = stationMangerRepository; | 
|             _routerRepository = routerRepository; | 
|             _routerService = routerService; | 
|             _routerExtension = routerExtension; | 
|   | 
|             string? warehouseDevicesStr = _cacheService.Get<string>(nameof(Dt_WarehouseDevice)); | 
|             if (!string.IsNullOrEmpty(warehouseDevicesStr)) | 
|             { | 
|                 warehouseDevices = JsonConvert.DeserializeObject<List<Dt_WarehouseDevice>>(warehouseDevicesStr) ?? new List<Dt_WarehouseDevice>(); | 
|             } | 
|             else | 
|             { | 
|                 warehouseDevices = new List<Dt_WarehouseDevice>(); | 
|             } | 
|         } | 
|   | 
|         public Task Execute(IJobExecutionContext context) | 
|         { | 
|             bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value); | 
|             if (flag && value != null) | 
|             { | 
|                 OtherDevice device = (OtherDevice)value; | 
|                 Dt_WarehouseDevice? warehouseDevice = warehouseDevices.FirstOrDefault(x => x.DeviceCode == device.DeviceCode); | 
|                 if (warehouseDevice == null) | 
|                 { | 
|                     WriteError(device.DeviceName, $"请配置仓库设备信息"); | 
|                     return Task.CompletedTask; | 
|                 } | 
|                 List<Dt_StationManger> stationMangers = _stationMangerRepository.QueryData(x => x.StationDeviceCode == device.DeviceCode); | 
|                 Dt_StationManger? dt_StationManger = stationMangers.FirstOrDefault(x => x.StationType == StationTypeEnum.StationType_ProductBackTake.ObjToInt()); | 
|                 if (dt_StationManger != null) | 
|                 { | 
|                     List<Dt_StationManger> ProductBackStations = stationMangers.Where(x => x.StationType == StationTypeEnum.StationType_ProductBack.ObjToInt()).OrderBy(x=>x.Id).ToList(); | 
|                     for (int i = 0; i < ProductBackStations.Count(); i++) | 
|                     { | 
|                         short isCanTake = device.GetValue<GroundStationDBName, short>(GroundStationDBName.R_IsCanTake, dt_StationManger.StationCode); | 
|                         Dt_StationManger stationManger = ProductBackStations[i]; | 
|                         string startDb = "DB29."; | 
|                         short stationCurrentStatus = device.Communicator.Read<short>(startDb + stationManger.Remark.ToString()); | 
|                         short stationEndStatus = device.Communicator.Read<short>(startDb + (stationManger.Remark.ObjToInt() + 2).ToString()); | 
|                         bool endStatus = true; | 
|                         if ((i != 2 && i != 5 && i != 8)) | 
|                         { | 
|                             endStatus = (stationEndStatus == 1); | 
|                         } | 
|                         //读取线体段状态 | 
|                         if (isCanTake == 1 && stationCurrentStatus == 1 && stationManger.IsOccupied == 0 && endStatus) | 
|                         { | 
|                             device.SetValue(W_ConveyorLineCPDB.StartPos, dt_StationManger.StationCode, dt_StationManger.StationCode); | 
|                             device.SetValue(W_ConveyorLineCPDB.EndPos, stationManger.AGVStationCode, dt_StationManger.StationCode); | 
|                             device.SetValue(W_ConveyorLineCPDB.Barcode, "C00001", dt_StationManger.StationCode); | 
|                             device.SetValue(W_ConveyorLineCPDB.TaskNum, DateTime.Now.ToString("yyMMddHHmmss"), dt_StationManger.StationCode); | 
|                             device.SetValue(W_ConveyorLineCPDB.WorkType, 1, dt_StationManger.StationCode); | 
|                             device.SetValue(W_ConveyorLineCPDB.WorkMode, 1, dt_StationManger.StationCode); | 
|                             device.SetValue(W_ConveyorLineCPDB.CheckPos, 1, dt_StationManger.StationCode); | 
|                             //启动指令 | 
|                             device.SetValue(W_ConveyorLineCPDB.ConveyArrival, 1, dt_StationManger.StationCode); | 
|                             stationManger.IsOccupied = 1; | 
|                             _stationMangerRepository.UpdateData(stationManger); | 
|                             Thread.Sleep(2000); | 
|                             return Task.CompletedTask; | 
|                         } | 
|                         else if (stationManger.IsOccupied == 1 && (stationCurrentStatus == 3)) | 
|                         { | 
|                             stationManger.IsOccupied = 0; | 
|                             _stationMangerRepository.UpdateData(stationManger); | 
|                         } | 
|                     } | 
|                 } | 
|   | 
|             } | 
|             return Task.CompletedTask; | 
|         } | 
|     } | 
| } |