1
dengjunjie
2025-01-13 cebd0800e9b5e1a1055a48d5e52f3f8a85e8d82c
´úÂë¹ÜÀí/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/°å²Ä²Ö/RGVJob_BC.cs
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using Quartz;
using System;
using System.Collections.Generic;
@@ -10,6 +11,7 @@
using WIDESEAWCS_Common.Helper;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Caches;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_IBasicInfoRepository;
using WIDESEAWCS_ITaskInfoRepository;
@@ -27,22 +29,34 @@
    [DisallowConcurrentExecution]
    public class RGVJob_BC : 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 List<Dt_WarehouseDevice> warehouseDevices;
        public RGVJob_BC(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IStationMangerRepository stationMangerRepository, IRouterRepository routerRepository, IRouterService routerService)
        public RGVJob_BC(ICacheService cacheService, ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IStationMangerRepository stationMangerRepository, IRouterRepository routerRepository, IRouterService routerService)
        {
            _cacheService = cacheService;
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _taskRepository = taskRepository;
            _stationMangerRepository = stationMangerRepository;
            _routerRepository = routerRepository;
            _routerService = routerService;
            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)
@@ -51,6 +65,14 @@
            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<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)))
@@ -77,88 +99,104 @@
                        if (item.StationType == StationTypeEnum.StationType_InStartAndOutEnd.ObjToInt())
                        {
                            if (conveyorLineSignalRead.STB /*&& conveyorLineStatus.Online && conveyorLineStatus.Goods*/ && !conveyorLineStatus.Alarm && !conveyorLineSignalWrite.ACK && !string.IsNullOrEmpty(conveyorLineInfoRead.Barcode) && conveyorLineInfoRead.TaskNum == 0)//入库
                            if (conveyorLineSignalRead.STB && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !conveyorLineSignalWrite.ACK && !string.IsNullOrEmpty(conveyorLineInfoRead.Barcode) && conveyorLineInfoRead.TaskNum == 0)//入库
                            {
                                Dt_Task task = _taskRepository.QueryFirst(x => x.SourceAddress == item.StationDeviceCode && x.TaskState == TaskStatusEnum.New.ObjToInt() && _taskService.TaskInboundTypes.Contains(x.TaskType) && x.PalletCode == conveyorLineInfoRead.Barcode && string.IsNullOrEmpty(x.DeviceCode) && string.IsNullOrEmpty(x.NextAddress) && string.IsNullOrEmpty(x.TargetAddress));
                                if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)
                                WebResponseContent content = _taskService.RequestWMSTaskSimple(conveyorLineInfoRead.Barcode, item.StationCode);
                                if (content.Status)
                                {
                                    Dt_StationManger? stationManger = stationMangers.FirstOrDefault(x => x.StationCode == item.StationCode);
                                    if (stationManger == null)
                                    Dt_Task task = _taskRepository.QueryFirst(x => x.PalletCode == conveyorLineInfoRead.Barcode && x.CurrentAddress == item.StationCode && x.DeviceCode == device.DeviceCode && x.TaskState == TaskStatusEnum.New.ObjToInt());
                                    if (task != null)
                                    {
                                        WriteError(item.StationName, $"未找到对应站台信息,设备编号:{item.StationCode},任务号:{task.TaskNum}");
                                        continue;
                                    }
                                    List<Dt_Router> routers = _routerRepository.QueryData(x => x.InOutType == task.TaskType && (task.Roadway == x.ChildPosiDeviceCode || task.Roadway == x.ChildPosi));
                                    Dt_Router? router = routers.FirstOrDefault();
                                    if (router == null)
                                    {
                                        WriteError(device.DeviceName, $"未找到任务类型{task.TaskType}对应的路由信息");
                                    }
                                    else
                                    {
                                        if (routers.Count == 1)
                                        {
                                            task.Dispatchertime = DateTime.Now;
                                            _taskService.UpdateTask(task, TaskStatusEnum.Line_Executing, deviceCode: router.ChildPosi, targetAddress: router.NextPosi, nextAddress: router.StartPosi, currentAddress: item.StationCode);
                                            device.SetValue(W_ConveyorLineDB.StartPos, item.StationCode, item.StationCode);
                                            device.SetValue(W_ConveyorLineDB.EndPos, router.StartPosi, item.StationCode);
                                            device.SetValue(W_ConveyorLineDB.TaskNum, task.TaskNum, item.StationCode);
                                            device.SetValue(W_ConveyorLineDB.Spare1, ConveyorWorkTypeEnum.Inbound.ObjToInt(), item.StationCode);
                                            device.SetValue(W_ConveyorLineDB.ACK, true, item.StationCode);
                                        }
                                        else
                                        {
                                            List<string> canInboundStations = new List<string>();
                                            List<string> stations = routers.Select(x => x.NextPosi).ToList();
                                            foreach (string station in stations)
                                            {
                                                Dt_StationManger stationManger1 = _stationMangerRepository.QueryFirst(x => x.StationCode == station);
                                                if (stationManger1 != null)
                                                {
                                                    IDevice? device1 = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger1.StationDeviceCode);
                                                    if (device1 != null)
                                                    {
                                                        OtherDevice otherDevice = (OtherDevice)device1;
                                                        //short workType = otherDevice.GetValue<R_ConveyorLineWorkType, short>(R_ConveyorLineWorkType.ConveyorLineWorkType, item.StationCode);
                                                        //if (workType == ConveyorWorkTypeEnum.Undefined.ObjToInt() || workType == ConveyorWorkTypeEnum.Inbound.ObjToInt())
                                                        {
                                                            canInboundStations.Add(stationManger1.Remark);
                                                        }
                                                    }
                                                }
                                            }
                                            WebResponseContent responseContent = _taskService.RequestWMSAssignRoadway(task.TaskNum, canInboundStations);
                                            if (responseContent.Status)
                                            {
                                                List<Dt_Router> dt_Routers = _routerService.QueryNextRoutes(item.StationCode, responseContent.Data.ToString() ?? "", task.TaskType);
                                                Dt_Router? routerTemp = _routerService.QueryNextRoutes(item.StationCode, responseContent.Data.ToString() ?? "", task.TaskType).FirstOrDefault();
                                                if (routerTemp != null)
                                                {
                                                    task.Dispatchertime = DateTime.Now;
                                                    task.Roadway = responseContent.Data.ToString() ?? task.Roadway;
                                                    _taskService.UpdateTask(task, TaskStatusEnum.Line_Executing, deviceCode: routerTemp.ChildPosiDeviceCode, targetAddress: routerTemp.NextPosi, nextAddress: routerTemp.NextPosi, currentAddress: item.StationCode);
                                                    device.SetValue(W_ConveyorLineDB.StartPos, item.StationCode, item.StationCode);
                                                    device.SetValue(W_ConveyorLineDB.EndPos, routerTemp.NextPosi, item.StationCode);
                                                    device.SetValue(W_ConveyorLineDB.TaskNum, task.TaskNum, item.StationCode);
                                                    device.SetValue(W_ConveyorLineDB.Spare1, ConveyorWorkTypeEnum.Inbound.ObjToInt(), item.StationCode);
                                                    device.SetValue(W_ConveyorLineDB.ACK, true, item.StationCode);
                                                }
                                                else
                                                {
                                                    WriteError(device.DeviceName, $"未找到任务类型{task.TaskType},起点{item.StationCode},终点{responseContent.Data.ToString()}的路由信息");
                                                }
                                            }
                                            else
                                            {
                                                WriteError(device.DeviceName, responseContent.Message);
                                            }
                                        }
                                        device.SetValue(W_ConveyorLineDB.TaskNum, task.TaskNum, item.StationCode);
                                        device.SetValue(W_ConveyorLineDB.StartPos, task.CurrentAddress, item.StationCode);
                                        device.SetValue(W_ConveyorLineDB.Spare1, ConveyorWorkTypeEnum.Inbound.ObjToInt(), item.StationCode);
                                        device.SetValue(W_ConveyorLineDB.ACK, true, item.StationCode);
                                        _taskService.UpdateTask(task, TaskStatusEnum.Line_Executing);
                                    }
                                }
                                #region
                                //Dt_Task task = _taskRepository.QueryFirst(x => x.SourceAddress == item.StationDeviceCode && x.TaskState == TaskStatusEnum.New.ObjToInt() && _taskService.TaskInboundTypes.Contains(x.TaskType) && x.PalletCode == conveyorLineInfoRead.Barcode && string.IsNullOrEmpty(x.DeviceCode) && string.IsNullOrEmpty(x.NextAddress) && string.IsNullOrEmpty(x.TargetAddress));
                                //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;
                                //    }
                                //    List<Dt_Router> routers = _routerRepository.QueryData(x => x.InOutType == task.TaskType && (task.Roadway == x.ChildPosiDeviceCode || task.Roadway == x.ChildPosi));
                                //    Dt_Router? router = routers.FirstOrDefault();
                                //    if (router == null)
                                //    {
                                //        WriteError(device.DeviceName, $"未找到任务类型{task.TaskType}对应的路由信息");
                                //    }
                                //    else
                                //    {
                                //        if (routers.Count == 1)
                                //        {
                                //            task.Dispatchertime = DateTime.Now;
                                //            _taskService.UpdateTask(task, TaskStatusEnum.Line_Executing, deviceCode: router.ChildPosi, targetAddress: router.NextPosi, nextAddress: router.StartPosi, currentAddress: item.StationCode);
                                //            device.SetValue(W_ConveyorLineDB.StartPos, item.StationCode, item.StationCode);
                                //            device.SetValue(W_ConveyorLineDB.EndPos, router.StartPosi, item.StationCode);
                                //            device.SetValue(W_ConveyorLineDB.TaskNum, task.TaskNum, item.StationCode);
                                //            device.SetValue(W_ConveyorLineDB.Spare1, ConveyorWorkTypeEnum.Inbound.ObjToInt(), item.StationCode);
                                //            device.SetValue(W_ConveyorLineDB.ACK, true, item.StationCode);
                                //        }
                                //        else
                                //        {
                                //            List<string> canInboundStations = new List<string>();
                                //            List<string> stations = routers.Select(x => x.NextPosi).ToList();
                                //            foreach (string station in stations)
                                //            {
                                //                Dt_StationManger stationManger1 = _stationMangerRepository.QueryFirst(x => x.StationCode == station);
                                //                if (stationManger1 != null)
                                //                {
                                //                    IDevice? device1 = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger1.StationDeviceCode);
                                //                    if (device1 != null)
                                //                    {
                                //                        OtherDevice otherDevice = (OtherDevice)device1;
                                //                        //short workType = otherDevice.GetValue<R_ConveyorLineWorkType, short>(R_ConveyorLineWorkType.ConveyorLineWorkType, item.StationCode);
                                //                        //if (workType == ConveyorWorkTypeEnum.Undefined.ObjToInt() || workType == ConveyorWorkTypeEnum.Inbound.ObjToInt())
                                //                        {
                                //                            canInboundStations.Add(stationManger1.Remark);
                                //                        }
                                //                    }
                                //                }
                                //            }
                                //            WebResponseContent responseContent = _taskService.RequestWMSAssignRoadway(task.TaskNum, canInboundStations);
                                //            if (responseContent.Status)
                                //            {
                                //                List<Dt_Router> dt_Routers = _routerService.QueryNextRoutes(item.StationCode, responseContent.Data.ToString() ?? "", task.TaskType);
                                //                Dt_Router? routerTemp = _routerService.QueryNextRoutes(item.StationCode, responseContent.Data.ToString() ?? "", task.TaskType).FirstOrDefault();
                                //                if (routerTemp != null)
                                //                {
                                //                    task.Dispatchertime = DateTime.Now;
                                //                    task.Roadway = responseContent.Data.ToString() ?? task.Roadway;
                                //                    _taskService.UpdateTask(task, TaskStatusEnum.Line_Executing, deviceCode: routerTemp.ChildPosiDeviceCode, targetAddress: routerTemp.NextPosi, nextAddress: routerTemp.NextPosi, currentAddress: item.StationCode);
                                //                    device.SetValue(W_ConveyorLineDB.StartPos, item.StationCode, item.StationCode);
                                //                    device.SetValue(W_ConveyorLineDB.EndPos, routerTemp.NextPosi, item.StationCode);
                                //                    device.SetValue(W_ConveyorLineDB.TaskNum, task.TaskNum, item.StationCode);
                                //                    device.SetValue(W_ConveyorLineDB.Spare1, ConveyorWorkTypeEnum.Inbound.ObjToInt(), item.StationCode);
                                //                    device.SetValue(W_ConveyorLineDB.ACK, true, item.StationCode);
                                //                }
                                //                else
                                //                {
                                //                    WriteError(device.DeviceName, $"未找到任务类型{task.TaskType},起点{item.StationCode},终点{responseContent.Data.ToString()}的路由信息");
                                //                }
                                //            }
                                //            else
                                //            {
                                //                WriteError(device.DeviceName, responseContent.Message);
                                //            }
                                //        }
                                //    }
                                //}
                                #endregion
                            }
                            else if (conveyorLineSignalRead.STB && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !conveyorLineSignalWrite.ACK && conveyorLineInfoRead.TaskNum > 0)//出库
                            {