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_Communicator; using WIDESEAWCS_Core; using WIDESEAWCS_DTO.TaskInfo; 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 ConveyorLineJob_CPB : 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 warehouseDevices; public ConveyorLineJob_CPB(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(nameof(Dt_WarehouseDevice)); if (!string.IsNullOrEmpty(warehouseDevicesStr)) { warehouseDevices = JsonConvert.DeserializeObject>(warehouseDevicesStr) ?? new List(); } else { warehouseDevices = new List(); } } public Task Execute(IJobExecutionContext context) { bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value); if (flag && value != null) { OtherDevice device = (OtherDevice)value; List deviceStations = device.DeviceProDTOs.Select(x => x.DeviceChildCode).Distinct().ToList(); List 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_ConveyorLineCPDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault(); DeviceProDTO? deviceProWrite = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(W_ConveyorLineCPDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault(); if (item.StationType == StationTypeEnum.StationType_InStartAndOutEnd.ObjToInt() && deviceProRead != null && deviceProWrite != null) { R_ConveyorLineCPInfo conveyorLineInfoRead = device.Communicator.ReadCustomer(deviceProRead.DeviceProAddress); if (conveyorLineInfoRead != null && conveyorLineInfoRead.Command == 5 && conveyorLineInfoRead.TaskNo > 0 && !string.IsNullOrEmpty(conveyorLineInfoRead.PalletCode)) { Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == conveyorLineInfoRead.TaskNo && x.NextAddress == item.StationCode && _taskService.TaskInboundTypes.Contains(x.TaskType) && x.TaskState == TaskStatusEnum.Line_Executing.ObjToInt() && x.DeviceCode == device.DeviceCode); if (task != null) { //分配巷道 存入托盘数据 List routers = _routerService.QueryNextRoutes(item.StationCode, "SC02_CP", task.TaskType); Dt_Router? router = routers.FirstOrDefault(); if (routers == null || routers.Count == 0 || router == null) { WriteError(item.StationName, $"未找到对应路由信息,设备编号:{item.StationCode},任务号:{task.TaskNum}"); continue; } if (routers.Count > 1) { WriteError(item.StationName, $"路由信息配置错误,设备编号:{item.StationCode},任务号:{task.TaskNum}"); continue; } device.SetValue(W_ConveyorLineCPDB.W_TargetAddress, router.NextPosi, item.StationCode); device.SetValue(W_ConveyorLineCPDB.W_TaskNo, task.TaskNum, item.StationCode); device.SetValue(W_ConveyorLineCPDB.W_Command, 1, item.StationCode); task.CurrentAddress = item.StationCode; task.NextAddress = router.NextPosi; task.PalletType = 1; task.PalletCode = conveyorLineInfoRead.PalletCode; task.Roadway = "SC02_CP"; task.DeviceCode = router.ChildPosiDeviceCode; _taskService.UpdateTask(task, TaskStatusEnum.Line_Executing); WriteError(item.StationName, $"任务号{conveyorLineInfoRead.TaskNo}下一步"); //string? locationCode = _taskService.RequestAssignLocation(task.TaskNum, "AGV_CP"); //if (string.IsNullOrEmpty(locationCode) && task.TaskType != TaskTypeEnum.EmptyProductBack.ObjToInt()) //{ // WriteError(item.StationName, $"请求分配货位返回信息错误,设备编号:{item.StationCode},任务号:{task.TaskNum}"); // continue; //} } } } else if (item.StationType == StationTypeEnum.StationType_InboundAndOutbound.ObjToInt() && deviceProRead != null) { R_ConveyorLineCPInfo conveyorLineInfoRead = device.Communicator.ReadCustomer(deviceProRead.DeviceProAddress); if (conveyorLineInfoRead != null && conveyorLineInfoRead.Command == 4 && conveyorLineInfoRead.TaskNo > 0) { Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == conveyorLineInfoRead.TaskNo && x.CurrentAddress == item.StationCode && _taskService.TaskInboundTypes.Contains(x.TaskType) && x.TaskState == TaskStatusEnum.Line_Execute.ObjToInt() && x.DeviceCode == device.DeviceCode); if (task != null) { List routers = _routerRepository.QueryData(x => x.InOutType == task.TaskType && task.CurrentAddress == x.StartPosi); Dt_Router? router = routers.FirstOrDefault(); if (routers == null || routers.Count == 0 || router == null) { WriteError(item.StationName, $"未找到对应路由信息,设备编号:{item.StationCode},任务号:{task.TaskNum}"); continue; } if (routers.Count > 1) { WriteError(item.StationName, $"路由信息配置错误,设备编号:{item.StationCode},任务号:{task.TaskNum}"); continue; } device.SetValue(W_ConveyorLineCPDB.W_Command, 2, item.StationCode); task.NextAddress = router.NextPosi; _taskService.UpdateTask(task, TaskStatusEnum.Line_Executing); WriteError(item.StationName, $"任务号{conveyorLineInfoRead.TaskNo}下一步"); //string? locationCode = _taskService.RequestAssignLocation(task.TaskNum, "AGV_CP"); //if (string.IsNullOrEmpty(locationCode) && task.TaskType != TaskTypeEnum.EmptyProductBack.ObjToInt()) //{ // WriteError(item.StationName, $"请求分配货位返回信息错误,设备编号:{item.StationCode},任务号:{task.TaskNum}"); // continue; //} } } } else if (item.StationType == StationTypeEnum.StationType_StackingPlates.ObjToInt() && deviceProRead != null && deviceProWrite != null) { R_ConveyorLineCPInfo conveyorLineInfoRead = device.Communicator.ReadCustomer(deviceProRead.DeviceProAddress); if (conveyorLineInfoRead != null && conveyorLineInfoRead.Command == 7 && conveyorLineInfoRead.TaskNo <= 0) { Dt_Task task = _taskRepository.QueryFirst(x => x.TargetAddress == item.StationCode && _taskService.TaskOutboundTypes.Contains(x.TaskType)); if (task == null) { //请求任务 WMSTaskDTO taskDTO = new WMSTaskDTO() { Id = 1, SourceAddress = "SC02_CP-001-064-001-01", TaskNum = DateTime.Now.ToString("mmss").ObjToInt(), PalletCode = "" + DateTime.Now.ToString("mmss"), RoadWay = "SC02_CP", TaskType = 160, TaskStatus = TaskStatusEnum.New.ObjToInt(), TargetAddress = item.StationCode, Grade = 0, WarehouseId = 1, PalletType = 1 }; WriteError(item.StationName, $"任务号{conveyorLineInfoRead.TaskNo}生成,下一步"); _taskService.ReceiveWMSTask(new List { taskDTO }); //string? locationCode = _taskService.RequestAssignLocation(task.TaskNum, "AGV_CP"); //if (string.IsNullOrEmpty(locationCode) && task.TaskType != TaskTypeEnum.EmptyProductBack.ObjToInt()) //{ // WriteError(item.StationName, $"请求分配货位返回信息错误,设备编号:{item.StationCode},任务号:{task.TaskNum}"); // continue; //} } } else if (conveyorLineInfoRead != null && conveyorLineInfoRead.Command == 8 && conveyorLineInfoRead.TaskNo > 0) { Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == conveyorLineInfoRead.TaskNo && x.TaskState == TaskStatusEnum.Line_Executing.ObjToInt() && x.DeviceCode == device.DeviceCode); if (task != null) { //任务完成 device.SetValue(W_ConveyorLineCPDB.W_Command, 2, item.StationCode); WriteError(item.StationName, $"任务号{conveyorLineInfoRead.TaskNo}叠盘位出库完成"); } } } else { WriteError(item.StationName, $"未找到设备子编号{item.StationCode}的协议信息"); } } } return Task.CompletedTask; } } }