using Microsoft.AspNetCore.Components.Routing; using Newtonsoft.Json; using Quartz; using System; using System.Collections.Generic; using System.Linq; using System.Reflection.Metadata; 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_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_YL1ndFloor : 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_YL1ndFloor(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).ToList(); //获取有协议的输送线-z List stationMangers = _stationMangerRepository.QueryData(x => x.StationDeviceCode == device.DeviceCode); //让每个输送线启动-z foreach (var item in stationMangers.Where(x => deviceStations.Contains(x.StationCode))) { DeviceProDTO? deviceProRead = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(WR_CLineYLDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault(); if (deviceProRead != null) { R_ConveyorLineYLInfo conveyorLineInfoRead = device.Communicator.ReadCustomer(deviceProRead.DeviceProAddress); if (conveyorLineInfoRead != null && item.StationType == StationTypeEnum.StationType_InStartAndOutEnd.ObjToInt() && conveyorLineInfoRead.WR_ToHode <= 0 && conveyorLineInfoRead.WR_Request == 86 && !string.IsNullOrEmpty(conveyorLineInfoRead.WR_TMID)) //一楼来料称重站台允许入库申请 { device.SetValue(WR_CLineYLDB.WR_Reresult, 86, item.StationCode); WriteInfo(item.StationCode, $"请求入库条码:{conveyorLineInfoRead.WR_TMID}"); } else if (conveyorLineInfoRead != null && item.StationType == StationTypeEnum.StationType_InboundAndOutbound.ObjToInt() && conveyorLineInfoRead.WR_ToHode <= 0 && conveyorLineInfoRead.WR_Request == 86 && !string.IsNullOrEmpty(conveyorLineInfoRead.WR_TMID)) //一楼分配站台入库分配目标点 { Dt_Task task = _taskRepository.QueryFirst(x => x.PalletCode == conveyorLineInfoRead.WR_TMID && x.CurrentAddress == item.StationCode && _taskService.TaskInboundTypes.Contains(x.TaskType) && x.TaskState == TaskStatusEnum.New.ObjToInt() && x.DeviceCode == device.DeviceCode); if (task == null) { WebResponseContent content = _taskService.RequestYLWMSTaskSimple(conveyorLineInfoRead.WR_TMID, item.StationCode); if (!content.Status) { WriteError(item.StationCode, $"请求入库失败:{content.Message}"); } } else //发送任务 { device.SetValue(WR_CLineYLDB.WR_Task, task.TaskNum, item.StationCode); device.SetValue(WR_CLineYLDB.WR_ToHode, task.NextAddress, item.StationCode); device.SetValue(WR_CLineYLDB.WR_Reresult, 86, item.StationCode); _taskService.UpdateTask(task, TaskStatusEnum.Line_Executing); WriteInfo(item.StationCode, conveyorLineInfoRead.Serialize()); } } else if (conveyorLineInfoRead != null && item.StationType == StationTypeEnum.StationType_OnlyInbound.ObjToInt() && conveyorLineInfoRead.WR_ToHode > 0 && conveyorLineInfoRead.WR_Request == 86 && !string.IsNullOrEmpty(conveyorLineInfoRead.WR_TMID) && conveyorLineInfoRead.WR_Task>0) //入库站台申请 { Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == conveyorLineInfoRead.WR_Task && x.NextAddress == item.StationCode && _taskService.TaskInboundTypes.Contains(x.TaskType) && x.TaskState == TaskStatusEnum.Line_Executing.ObjToInt() && x.DeviceCode == device.DeviceCode); if (task != null) { //分配货位 string? local = _taskService.RequestAssignLocation(task.TaskNum, task.Roadway); if (!string.IsNullOrEmpty(local)) { device.SetValue(WR_CLineYLDB.WR_Reresult, 86, item.StationCode); task.CurrentAddress = item.StackerCraneStationCode; task.TargetAddress = local; task.NextAddress = local; task.DeviceCode = item.StackerCraneCode; _taskService.UpdateTask(task, TaskStatusEnum.SC_Execute); WriteInfo(item.StationName, $"任务号:{task.TaskNum}进行堆垛机入库"); } else { WriteError(item.StationCode, $"请求入库货位失败"); } } } else if (conveyorLineInfoRead != null && item.StationType == StationTypeEnum.StationType_OnlyOutbound.ObjToInt() && conveyorLineInfoRead.WR_ToHode <= 0 && conveyorLineInfoRead.WR_Request == 99 && !string.IsNullOrEmpty(conveyorLineInfoRead.WR_TMID) && conveyorLineInfoRead.WR_Task <= 0) //出站台分配目标点 { Dt_Task task = _taskRepository.QueryFirst(x => x.CurrentAddress == item.StationCode && _taskService.TaskOutboundTypes.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(WR_CLineYLDB.WR_Task, task.TaskNum, item.StationCode); device.SetValue(WR_CLineYLDB.WR_TMID, task.PalletCode, item.StationCode); device.SetValue(WR_CLineYLDB.WR_ToHode, router.NextPosi, item.StationCode); device.SetValue(WR_CLineYLDB.WR_Reresult, 86, item.StationCode); _taskService.UpdateTask(task,TaskStatusEnum.Line_Executing,nextAddress: router.NextPosi); } } else if (conveyorLineInfoRead != null && item.StationType == StationTypeEnum.StationType_InboundAndOutbound.ObjToInt() && conveyorLineInfoRead.WR_Task > 0 && conveyorLineInfoRead.WR_Request == 98) //一楼老厂出库口完成 { Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == conveyorLineInfoRead.WR_Task && _taskService.TaskOutboundTypes.Contains(x.TaskType) && x.TaskState == TaskStatusEnum.Line_Executing.ObjToInt() && x.DeviceCode == device.DeviceCode); if (task != null) { //任务回复 device.SetValue(WR_CLineYLDB.WR_Reresult, 98, item.StationCode); _taskService.TaskCompleted(task.TaskNum); WriteInfo(item.StationCode, $"任务号:{task.TaskNum}一楼出库至老厂房完成"); } } } else { WriteError(item.StationName, $"未找到设备子编号{item.StationCode}的协议信息"); } } } return Task.CompletedTask; } } }