From 6d9829c889a826d39cbc911ef5d01825edd1bffc Mon Sep 17 00:00:00 2001
From: dengjunjie <dengjunjie@hnkhzn.com>
Date: 星期四, 26 六月 2025 16:33:29 +0800
Subject: [PATCH] 添加设备交互日志

---
 项目代码/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ShuttleCarJob/ShuttleCarJob.cs |  197 ++++++++++++++++++------------------------------
 1 files changed, 74 insertions(+), 123 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ShuttleCarJob/ShuttleCarJob.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ShuttleCarJob/ShuttleCarJob.cs"
index 3033440..8169f2e 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ShuttleCarJob/ShuttleCarJob.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ShuttleCarJob/ShuttleCarJob.cs"
@@ -2,18 +2,25 @@
 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;
 using WIDESEAWCS_ITaskInfoService;
 using WIDESEAWCS_Model.Models;
 using WIDESEAWCS_QuartzJob;
@@ -31,19 +38,23 @@
     partial class CommonShuttleCarJob : JobBase, IJob
     {
         private readonly ITaskService _taskService;
+        private readonly ITaskRepository _taskRepository;
         private readonly ITaskHtyService _taskHtyService;
         private readonly IShuttleCarService _shuttleCarService;
         private readonly ITaskExecuteDetailService _taskExecuteDetailService;
         private readonly IRouterService _routerService;
+        private readonly ICacheService _cacheService;
         private readonly IMapper _mapper;
 
-        public CommonShuttleCarJob(ITaskService taskService, 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;
             _taskHtyService = taskHtyService;
             _shuttleCarService = shuttleCarService;
             _taskExecuteDetailService = taskExecuteDetailService;
             _routerService = routerService;
+            _cacheService = cacheService;
             _mapper = mapper;
         }
 
@@ -54,96 +65,87 @@
                 ShuttleCar shuttleCar = (ShuttleCar)context.JobDetail.JobDataMap.Get("JobParams");
                 if (shuttleCar != null)
                 {
+                    shuttleCar.Communicator.IsReadAfterWrite = false;
                     ShuttleCarTaskCommandR command = ReadCustomer<ShuttleCarTaskCommandR>(shuttleCar, shuttleCar.DeviceCode);//璇诲彇绌挎杞︿俊鎭�
+                    _cacheService.AddOrUpdate(shuttleCar.DeviceCode, command);
                     if (command != null && command.JoinStatus == 1)
                     {
                         #region 绌挎杞︿换鍔″畬鎴�
-                        if (command.TaskTypeComplete > (short)ShuttleCarTaskComplete.Standby)
+                        var complete = shuttleCar.GetValue<ShuttleCarDBName, bool>(ShuttleCarDBName.TaskComplete);
+                        if (complete && command.TaskTypeComplete > (short)ShuttleCarTaskComplete.Standby)
                         {
-                            var Status = _taskService.ShuttleCarTaskCompleted(command.number, command.TaskTypeComplete).Status;
-                            shuttleCar.SetValue(ShuttleCarDBName.ConfirmComplete, Status, shuttleCar.DeviceCode);
+                            #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
 
                         #region 鍒涘缓绉昏溅鍏呯數浠诲姟
-                        if (command.Err_Status == (short)ShuttleCarErr.LowBattery || command.ElectricQuantity == (short)ShuttleCarErr.LowBattery)
+                        if (command.Err_Status == (short)ShuttleCarErr.LowBattery || command.ElectricQuantity <= (short)ShuttleCarErr.LowBattery)
                         {
-                            //鐢熸垚鍫嗗灈鏈虹Щ杞︿换鍔�&绌挎杞﹀厖鐢典换鍔�
-                            var ShuttleCar = QueryCode(shuttleCar.DeviceCode);//鍏呯數缁堢偣寰呯‘璁わ紒锛侊紒锛侊紒锛侊紒锛侊紒锛侊紒锛侊紒锛侊紒锛侊紒锛侊紒锛侊紒
-                            _taskService.AddRelocationCarTask(ShuttleCar.ShuttleCarPosition, "", ShuttleCar.ShuttleCarCode, ShuttleCarTaskType.Charging.ToString());
-                        }
-                        #endregion
-
-                        #region 鑾峰彇鍫嗗灈鏈虹Щ杞﹀畬鎴愪换鍔�
-                        Dt_Task dt_Task = GetTask((int)TaskCarStatusEnum.SC_CarFinish, (int)TaskOtherTypeEnum.RelocationCar);
-                        if (dt_Task != null)
-                        {
-                            var ShuttleCar = QueryCode(dt_Task.ShuttleCarCode);
-                            ShuttleCar.ShuttleCarPosition = dt_Task.TargetAddress;
-                            _shuttleCarService.UpdateData(ShuttleCar);
-                            if (string.IsNullOrEmpty(dt_Task.Remark))//瀹屾垚绉诲簱浠诲姟
-                                _taskService.AddTask_Hty(dt_Task, TaskOperateTypeEnum.AutoComplete.ToJson());
-                            else//涓嬪彂鍏呯數浠诲姟
-                                SendTask(dt_Task);
-                        }
-                        #endregion
-
-                        #region 鑾峰彇绉昏溅浠诲姟
-                        dt_Task = GetTask((int)TaskCarStatusEnum.CarNew, (int)TaskOtherTypeEnum.RelocationCar);//鑾峰彇鏂板缓绉昏溅浠诲姟
-                        if (dt_Task != null)
-                        {
-                            #region 鍒ゆ柇鏄惁瀛樺湪绌洪棽绌挎杞�,濡傞兘鍦ㄥ厖鐢�(閫夋嫨鐢甸噺楂樼殑骞惰秴杩�50%鐢甸噺鐨勭┛姊溅閫�鍑哄厖鐢�)
-
-                            #endregion
-
-                            if (string.IsNullOrEmpty(dt_Task.SourceAddress))
+                            var ShuttleCar = QueryCode(shuttleCar.DeviceCode);
+                            #region 鍒ゆ柇绌挎杞﹀綋鍓嶄綅缃槸鍚︽湁浠诲姟
+                            if (_taskRepository.QueryFirst(x => x.SourceAddress == ShuttleCar.ShuttleCarPosition || x.TargetAddress == ShuttleCar.ShuttleCarPosition) == null)
                             {
-                                if (command.Status == (short)ShuttleCarStatus.Standby && command.TaskTypeComplete == (short)ShuttleCarTaskComplete.Standby && command.Err_Status == (short)ShuttleCarErr.Normal)
+                                Dt_Task _Task = _taskRepository.QueryFirst(x => x.TaskType == TaskOtherTypeEnum.RelocationCarCharging.ObjToInt());
+                                if (_Task != null)
                                 {
-                                    dt_Task.SourceAddress = QueryCode(shuttleCar._deviceCode).ShuttleCarPosition;
-                                    dt_Task.CurrentAddress = dt_Task.SourceAddress;
-                                    dt_Task.ShuttleCarCode = shuttleCar._deviceCode;
-                                    _taskService.UpdateData(dt_Task);
-                                }
-                            }
-                            else if (string.IsNullOrEmpty(dt_Task.TargetAddress))
-                            {
-                                #region 鍚慦MS鐢宠
-                                WebResponseContent content = new WebResponseContent();
-                                var ResultData = HttpHelper.PostAsync(WMSInterfaceAddress.GetPosition + $"?position={dt_Task.SourceAddress}", "", headers: new Dictionary<string, string>());
-                                if (ResultData.Result != null)
-                                {
-                                    content = JsonConvert.DeserializeObject<WebResponseContent>(ResultData.Result);
-                                    if (content != null && content.Status)
+                                    if (_Task.ShuttleCarCode != shuttleCar.DeviceCode)
                                     {
-                                        dt_Task.TargetAddress = content.Message;
-                                        dt_Task.NextAddress = dt_Task.TargetAddress;
-                                        _taskService.UpdateData(dt_Task);
+                                        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 (command1.Status == (short)ShuttleCarStatus.Charging && command1.ElectricQuantity /*> command.ElectricQuantity*/ >= 50)
+                                            {
+                                                shuttleCar1.SetValue(ShuttleCarDBName.TaskType, (short)ShuttleCarStatus.QuitCharging);
+                                            }
+                                        }
                                     }
                                 }
-                                #endregion
+                                else
+                                {
+                                    string CarChargingStation = AppSettings.Configuration[nameof(CarChargingStation)];//鍏呯數浣�
+                                                                                                                      //鍒ゆ柇鍏呯數浣嶆槸鍚︽湁杞�
+                                    var ShuttleCar1 = _shuttleCarService.QueryShuttleCar(CarChargingStation);
+                                    if (ShuttleCar1 != null && ShuttleCar1.ShuttleCarCode != shuttleCar.DeviceCode)
+                                    {
+                                        if (_taskRepository.QueryFirst(x => x.ShuttleCarCode == ShuttleCar1.ShuttleCarCode) == null)
+                                            _taskService.AddRelocationCarTask(ShuttleCar1.ShuttleCarPosition, "", ShuttleCar1.ShuttleCarCode);
+                                    }
+                                    else
+                                    {
+                                        _taskService.AddRelocationCarTask(ShuttleCar.ShuttleCarPosition, CarChargingStation, ShuttleCar.ShuttleCarCode, ShuttleCarTaskType.Charging.ToString());
+                                    }
+                                }
                             }
-                            //绌挎杞︾Щ搴撲换鍔′笅鍙�
-                            if (!string.IsNullOrEmpty(dt_Task.SourceAddress) && !string.IsNullOrEmpty(dt_Task.TargetAddress) && !string.IsNullOrEmpty(dt_Task.ShuttleCarCode))
-                            {
-                                SendTask(dt_Task);
-                            }
+                            #endregion
                         }
                         #endregion
 
-                        #region 绌挎杞﹀嚭鍏ュ簱浠诲姟
-                        else
+                        #region 鑾峰彇绉昏溅/鍑哄叆搴撲换鍔�
+                        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)
+                            )
                         {
-                            var task = _taskService.QueryShuttleCarTask(shuttleCar.DeviceCode);
-                            if (task != null)
-                            {
-                                if (task.TaskType == (int)TaskOutboundTypeEnum.Outbound && !task.IsPickPlace)
-                                {
-                                    var ShuttleCar = _shuttleCarService.QueryShuttleCar(task.SourceAddress);//鍑哄簱浠诲姟鍒ゆ柇鏄惁瀛樺湪绌挎杞�
-                                    if (ShuttleCar != null) SendTask(task);
-                                    else _taskService.AddRelocationCarTask("", task.SourceAddress);
-                                }
-                            }
+                            var dt_Task = GetTask(TaskRelocationStatusEnum.SC_RelocationFinish.ObjToInt(), TaskRelocationTypeEnum.Relocation.ObjToInt());//绉诲簱浠诲姟
+                            if (dt_Task != null && shuttleCar.DeviceCode == dt_Task.ShuttleCarCode)
+                                SendTask(dt_Task);
+                            else
+                                Get_CarTask(shuttleCar, command);
                         }
                         #endregion
                     }
@@ -151,60 +153,9 @@
             }
             catch (Exception ex)
             {
-                Console.Out.WriteLine(nameof(CommonShuttleCarJob) + ":" + ex.ToString());
-            }
-            finally
-            {
-
+                //Console.Out.WriteLine(nameof(CommonShuttleCarJob) + ":" + ex.ToString());
             }
             return Task.CompletedTask;
         }
-        private void SendTask(Dt_Task task)
-        {
-            ShuttleCar shuttleCar = Storage.Devices.FirstOrDefault(x => x.DeviceCode == task.ShuttleCarCode) as ShuttleCar;
-            if (shuttleCar != null)
-            {
-                ShuttleCarTaskCommandR command = ReadCustomer<ShuttleCarTaskCommandR>(shuttleCar, shuttleCar.DeviceCode);//璇诲彇绌挎杞︿俊鎭�
-                if (command != null && command.Status == (short)ShuttleCarStatus.Standby && command.TaskTypeComplete == (short)ShuttleCarTaskComplete.Standby && command.Err_Status == (short)ShuttleCarErr.Normal)
-                {
-                    //浠诲姟杞崲
-                    ShuttleCarTaskCommandW shuttleCarTaskCommand = new ShuttleCarTaskCommandW();
-                    shuttleCarTaskCommand.Direction = command.Position;
-                    shuttleCarTaskCommand.TaskNum = task.TaskNum;
-                    if (task.TaskType == (int)TaskOtherTypeEnum.RelocationCar)//绉昏溅
-                    {
-                        if (task.TaskState == (int)TaskCarStatusEnum.CarNew)
-                        {
-                            if (task.Remark == ShuttleCarTaskType.ExitCharge.ToString()) shuttleCarTaskCommand.TaskType = (short)GetCarTaskType(task.Remark);
-                            else shuttleCarTaskCommand.TaskType = (short)GetCarTaskType(task.TaskType, shuttleCarTaskCommand.Direction);
-                        }
-                        else if (task.TaskState == (int)TaskCarStatusEnum.SC_CarFinish)
-                        {
-                            if (task.Remark == ShuttleCarTaskType.Charging.ToString()) shuttleCarTaskCommand.TaskType = (short)GetCarTaskType(task.Remark);
-                            else shuttleCarTaskCommand.TaskType = (short)GetCarTaskType(task.TaskType, shuttleCarTaskCommand.Direction);
-                        }
-                    }
-                    else
-                        shuttleCarTaskCommand.TaskType = (short)GetCarTaskType(task.TaskType, shuttleCarTaskCommand.Direction);
-                    //shuttleCarTaskCommand.TaskType = string.IsNullOrEmpty(task.Remark) ? (short)GetCarTaskType(task.TaskType, shuttleCarTaskCommand.Direction) : (short)GetCarTaskType(task.Remark);
-                    //浠诲姟涓嬪彂
-                    if (shuttleCarTaskCommand.TaskType != null)//0
-                    {
-                        if (shuttleCar.SendCommand(shuttleCarTaskCommand, shuttleCar.DeviceCode))
-                        {
-                            if (!string.IsNullOrEmpty(task.Remark)) task.Remark = task.Remark + "宸蹭笅鍙�";
-                            task.TaskState = GetTaskState(task.TaskType);
-                            _taskService.UpdateData(task);
-                        }
-                    }
-                }
-            }
-        }
-
-        ///涓嬪彂閫�鍑哄厖鐢典换鍔″苟鐢熸垚绉诲簱浠诲姟
-        //if (command.ElectricQuantity > 50)
-        //{
-
-        //}
     }
 }

--
Gitblit v1.9.3