dengjunjie
20 小时以前 6d9829c889a826d39cbc911ef5d01825edd1bffc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using AutoMapper;
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;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.Service;
using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
using WIDESEAWCS_TaskInfoService;
using WIDESEAWCS_Tasks.ConveyorLineJob;
using WIDESEAWCS_Tasks.ShuttleCarJob;
using WIDESEAWCS_Tasks.StackerCraneJob;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    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, 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;
        }
 
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                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 穿梭车任务完成
                        var complete = shuttleCar.GetValue<ShuttleCarDBName, bool>(ShuttleCarDBName.TaskComplete);
                        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
 
                        #region 创建移车充电任务
                        if (command.Err_Status == (short)ShuttleCarErr.LowBattery || command.ElectricQuantity <= (short)ShuttleCarErr.LowBattery)
                        {
                            var ShuttleCar = QueryCode(shuttleCar.DeviceCode);
                            #region 判断穿梭车当前位置是否有任务
                            if (_taskRepository.QueryFirst(x => x.SourceAddress == ShuttleCar.ShuttleCarPosition || x.TargetAddress == ShuttleCar.ShuttleCarPosition) == null)
                            {
                                Dt_Task _Task = _taskRepository.QueryFirst(x => x.TaskType == TaskOtherTypeEnum.RelocationCarCharging.ObjToInt());
                                if (_Task != null)
                                {
                                    if (_Task.ShuttleCarCode != shuttleCar.DeviceCode)
                                    {
                                        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);
                                            }
                                        }
                                    }
                                }
                                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());
                                    }
                                }
                            }
                            #endregion
                        }
                        #endregion
 
                        #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 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
                    }
                }
            }
            catch (Exception ex)
            {
                //Console.Out.WriteLine(nameof(CommonShuttleCarJob) + ":" + ex.ToString());
            }
            return Task.CompletedTask;
        }
    }
}