dengjunjie
2025-07-09 7ca9651f81d7b84f054194d3d46fdbd1d9c8b922
ÏîÄ¿´úÂë/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ShuttleCarJob/ShuttleCarJob.cs
@@ -2,16 +2,22 @@
using HslCommunication;
using Newtonsoft.Json;
using OfficeOpenXml.Drawing.Chart;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using Quartz;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Common.ShuttleCarEnum;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Caches;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO.System;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_DTO.WMSInfo;
using WIDESEAWCS_IShuttleCar;
using WIDESEAWCS_ITaskInfoRepository;
@@ -37,9 +43,10 @@
        private readonly IShuttleCarService _shuttleCarService;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly IRouterService _routerService;
        private readonly ICacheService _cacheService;
        private readonly IMapper _mapper;
        public CommonShuttleCarJob(ITaskService taskService, ITaskRepository taskRepository, ITaskHtyService taskHtyService, IShuttleCarService shuttleCarService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IMapper mapper)
        public CommonShuttleCarJob(ITaskService taskService, ITaskRepository taskRepository, ITaskHtyService taskHtyService, IShuttleCarService shuttleCarService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, ICacheService cacheService, IMapper mapper)
        {
            _taskService = taskService;
            _taskRepository = taskRepository;
@@ -47,6 +54,7 @@
            _shuttleCarService = shuttleCarService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _routerService = routerService;
            _cacheService = cacheService;
            _mapper = mapper;
        }
@@ -59,60 +67,91 @@
                {
                    shuttleCar.Communicator.IsReadAfterWrite = false;
                    ShuttleCarTaskCommandR command = ReadCustomer<ShuttleCarTaskCommandR>(shuttleCar, shuttleCar.DeviceCode);//读取穿梭车信息
                    _cacheService.AddOrUpdate(shuttleCar.DeviceCode, command);
                    if (command != null && command.JoinStatus == 1)
                    {
                        #region ç©¿æ¢­è½¦ä»»åŠ¡å®Œæˆ
                        var complete = shuttleCar.GetValue<ShuttleCarDBName, bool>(ShuttleCarDBName.TaskComplete);
                        if (complete && command.TaskTypeComplete > (short)ShuttleCarTaskComplete.Standby && command.Status == command.TaskTypeComplete)
                        if (complete && command.TaskTypeComplete > (short)ShuttleCarTaskComplete.Standby)
                        {
                            #region æ—¥å¿—记录
                            FieldInfo? fieldInfo = typeof(ShuttleCarTaskComplete).GetField(((ShuttleCarTaskComplete)command.TaskTypeComplete).ToString());
                            DescriptionAttribute? description = fieldInfo.GetCustomAttribute<DescriptionAttribute>();
                            var type = description?.Description ?? ((ShuttleCarTaskComplete)command.TaskTypeComplete).ToString();
                            WriteDebug($"{shuttleCar.DeviceName}任务完成", $"任务号【{command.number}】,完成类型【{type}】");
                            #endregion
                            _taskService.ShuttleCarTaskCompleted(command.number, command.TaskTypeComplete);
                            shuttleCar.SetValue(ShuttleCarDBName.ConfirmComplete, true, shuttleCar.DeviceCode);
                        }
                        #endregion
                        #region èŽ·å–å †åž›æœºç§»è½¦/移库完成任务
                        GetSC_CarFinish();
                        #endregion
                        GetSC_CarFinish();//堆垛机完成移车/移车充电/移库/入库任务
                        #region åˆ›å»ºç§»è½¦å……电任务
                        if (command.Err_Status == (short)ShuttleCarErr.LowBattery || command.ElectricQuantity <= (short)ShuttleCarErr.LowBattery)
                        {
                            var ShuttleCar = QueryCode(shuttleCar.DeviceCode);//在充电位直接让穿梭车去充电
                            //生成堆垛机移车任务&穿梭车充电任务,判断是否有穿梭车在充电
                            Dt_Task _Task = _taskRepository.QueryFirst(x => x.TaskType == TaskOtherTypeEnum.RelocationCarCharging.ObjToInt());
                            if (_Task != null)
                            var ShuttleCar = QueryCode(shuttleCar.DeviceCode);
                            #region åˆ¤æ–­ç©¿æ¢­è½¦å½“前位置是否有任务
                            if (_taskRepository.QueryFirst(x => x.SourceAddress == ShuttleCar.ShuttleCarPosition || x.TargetAddress == ShuttleCar.ShuttleCarPosition) == null)
                            {
                                if (_Task.ShuttleCarCode != shuttleCar.DeviceCode)
                                Dt_Task _Task = _taskRepository.QueryFirst(x => x.TaskType == TaskOtherTypeEnum.RelocationCarCharging.ObjToInt());
                                if (_Task != null)
                                {
                                    ShuttleCar shuttleCar1 = Storage.Devices.Where(x => x.DeviceCode.Equals(_Task.ShuttleCarCode)).FirstOrDefault() as ShuttleCar;
                                    ShuttleCarTaskCommandR command1 = ReadCustomer<ShuttleCarTaskCommandR>(shuttleCar1, shuttleCar1.DeviceCode);//读取穿梭车信息
                                    if (command1 != null && command1.JoinStatus == 1)
                                    if (_Task.ShuttleCarCode != shuttleCar.DeviceCode)
                                    {
                                        if (command1.Status == (short)ShuttleCarStatus.Charging && command1.ElectricQuantity >= 50)
                                        ShuttleCar shuttleCar1 = Storage.Devices.Where(x => x.DeviceCode.Equals(_Task.ShuttleCarCode)).FirstOrDefault() as ShuttleCar;
                                        ShuttleCarTaskCommandR command1 = ReadCustomer<ShuttleCarTaskCommandR>(shuttleCar1, shuttleCar1.DeviceCode);//读取穿梭车信息
                                        if (command1 != null && command1.JoinStatus == 1)
                                        {
                                            shuttleCar1.SetValue(ShuttleCarDBName.TaskType, (short)ShuttleCarStatus.QuitCharging);
                                            if (command1.Status == (short)ShuttleCarStatus.Charging && command1.ElectricQuantity >= 50)
                                            {
                                                shuttleCar1.SetValue(ShuttleCarDBName.TaskType, (short)ShuttleCarStatus.QuitCharging);
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                //判断充电位是否有车
                                var ShuttleCar1 = _shuttleCarService.QueryShuttleCar("003-002-001");
                                if (ShuttleCar1 != null && ShuttleCar1.ShuttleCarCode != shuttleCar.DeviceCode)
                                {
                                    _taskService.AddRelocationCarTask(ShuttleCar1.ShuttleCarPosition, "", ShuttleCar1.ShuttleCarCode);
                                }
                                else
                                {
                                    _taskService.AddRelocationCarTask(ShuttleCar.ShuttleCarPosition, "003-002-001", ShuttleCar.ShuttleCarCode, ShuttleCarTaskType.Charging.ToString());
                                    string CarChargingStation = AppSettings.Configuration[nameof(CarChargingStation)];//判断充电位是否有车
                                    var ShuttleCar1 = _shuttleCarService.QueryShuttleCar(CarChargingStation);
                                    if (ShuttleCar1 != null && ShuttleCar1.ShuttleCarCode != shuttleCar.DeviceCode)
                                    {
                                        #region åˆ¤æ–­æ˜¯å¦å­˜åœ¨æ–°å»ºç§»è½¦ä»»åŠ¡
                                        if (_taskRepository.QueryFirst(x => x.ShuttleCarCode == ShuttleCar1.ShuttleCarCode) == null)
                                        {
                                            var dt_Task = GetTask((int)TaskCarStatusEnum.CarNew, (int)TaskOtherTypeEnum.RelocationCar);
                                            if (dt_Task != null && string.IsNullOrEmpty(dt_Task.SourceAddress))
                                            {
                                                dt_Task.SourceAddress = ShuttleCar1.ShuttleCarPosition;
                                                dt_Task.CurrentAddress = dt_Task.SourceAddress;
                                                dt_Task.ShuttleCarCode = ShuttleCar1.ShuttleCarCode;
                                                _taskService.UpdateData(dt_Task);
                                                _taskExecuteDetailService.AddTaskExecuteDetail(dt_Task.TaskNum, "更新移车任务起始地址");
                                            }
                                            else
                                                _taskService.AddRelocationCarTask(ShuttleCar1.ShuttleCarPosition, "", ShuttleCar1.ShuttleCarCode);
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        _taskService.AddRelocationCarTask(ShuttleCar.ShuttleCarPosition, CarChargingStation, ShuttleCar.ShuttleCarCode, ShuttleCarTaskType.Charging.ToString());
                                    }
                                }
                            }
                            #endregion
                        }
                        #endregion
                        #region èŽ·å–ç§»è½¦/出入库任务
                        Get_CarTask(shuttleCar, command);
                        if (command != null &&
                            (command.Status == (short)ShuttleCarStatus.Standby || command.Status == (short)ShuttleCarStatus.Charging) &&
                            command.TaskTypeComplete == (short)ShuttleCarTaskComplete.Standby &&
                            (command.Err_Status == (short)ShuttleCarErr.Normal || command.Err_Status == (short)ShuttleCarErr.LowBattery))
                        {
                            //Get_CarTask(shuttleCar, command);
                            GetShuttleCarTask(shuttleCar, command);
                        }
                        #endregion
                    }
                }