dengjunjie
2024-10-24 0fb0f17319ecf71d66b96a6acfd07f754be9443e
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using AutoMapper;
using Newtonsoft.Json;
using OfficeOpenXml.Drawing.Chart;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Common.ShuttleCarEnum;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Common.WMSInfo;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_IShuttleCar;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
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]
    public class CommonShuttleCarJob : IJob
    {
        private readonly ITaskService _taskService;
        private readonly IShuttleCarService _shuttleCarService;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly IRouterService _routerService;
        private readonly IMapper _mapper;
 
        public CommonShuttleCarJob(ITaskService taskService, IShuttleCarService shuttleCarService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IMapper mapper)
        {
            _taskService = taskService;
            _shuttleCarService = shuttleCarService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _routerService = routerService;
            _mapper = mapper;
        }
 
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                ShuttleCar shuttleCar = (ShuttleCar)context.JobDetail.JobDataMap.Get("JobParams");
                if (shuttleCar != null)
                {
                    ShuttleCarTaskCommandR command = shuttleCar.ReadCustomer<ShuttleCarTaskCommandR>(shuttleCar.DeviceCode);//读取穿梭车信息
                    if (command != null && command.JoinStatus == 1)
                    {
                        #region 穿梭车任务完成
                        if (command.TaskTypeComplete > (short)ShuttleCarTaskComplete.Standby)
                        {
                            //查找任务,判断任务状态做对应处理!!!!!!!!!!!!!!!!!!!!!!!!!
                            var Status = _taskService.StackCraneTaskCompleted(command.number).Status;//需优化!!!
                            shuttleCar.SetValue(ShuttleCarDBName.ConfirmComplete, Status, shuttleCar.DeviceCode);
                        }
                        #endregion
 
                        #region 移库充电任务
                        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 穿梭车移库任务
 
                        #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))//完成移库任务
                            {
                                //移入历史任务!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                                //删除当前任务
                            }
                            else//下发充电任务
                            {
                                SendTask(dt_Task);
                            }
                        }
                        #endregion
 
                        dt_Task = GetTask((int)TaskCarStatusEnum.CarNew, (int)TaskOtherTypeEnum.RelocationCar);//获取新建移车任务
                        if (dt_Task != null)
                        {
                            if (string.IsNullOrEmpty(dt_Task.SourceAddress))
                            {
                                if (command.Status == (short)ShuttleCarStatus.Standby && command.TaskTypeComplete == (short)ShuttleCarTaskComplete.Standby && command.Err_Status == (short)ShuttleCarErr.Normal)
                                {
                                    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 向WMS申请
                                WebResponseContent content = new WebResponseContent();
                                var ResultData = HttpHelper.PostAsync(WMSIP.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)
                                    {
                                        dt_Task.TargetAddress = content.Message;
                                        dt_Task.NextAddress = dt_Task.TargetAddress;
                                        _taskService.UpdateData(dt_Task);
                                    }
                                }
                                #endregion
                            }
                            //穿梭车移库任务下发
                            if (!string.IsNullOrEmpty(dt_Task.SourceAddress) && !string.IsNullOrEmpty(dt_Task.TargetAddress) && !string.IsNullOrEmpty(dt_Task.ShuttleCarCode))
                            {
                                SendTask(dt_Task);
                            }
                        }
                        #endregion
 
                        #region 穿梭车出入库任务
                        else
                        {
                            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);
                                    }
                                    #region 创建堆垛机移车任务
                                    //dt_Task = new Dt_Task()
                                    //{
                                    //    TaskNum = Convert.ToInt32(DateTime.Now.ToString("HHmmss")),
                                    //    Grade = 2,
                                    //    Roadway = "SC01",
                                    //    TaskType = (int)TaskOtherTypeEnum.RelocationCar,
                                    //    SourceAddress = "",
                                    //    TargetAddress = task.SourceAddress,
                                    //    CurrentAddress = "",
                                    //    NextAddress = task.SourceAddress,
                                    //    CreateDate = DateTime.Now,
                                    //    Creater = "WCS",
                                    //    TaskState = (int)TaskCarStatusEnum.CarNew,
                                    //    WMSId = 0,
                                    //};
                                    //_taskService.AddData(dt_Task);
                                    #endregion
                                }
                            }
                        }
                        #endregion
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine(nameof(CommonShuttleCarJob) + ":" + ex.ToString());
            }
            finally
            {
 
            }
            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 = shuttleCar.ReadCustomer<ShuttleCarTaskCommandR>(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)
                {
 
                }
            }
        }
        private Dt_Task GetTask(int TaskState, int TaskType)
        {
            return _taskService.GetTaskState(TaskState, TaskType);
        }
 
        public Dt_ShuttleCar QueryCode(string ShuttleCarCode)
        {
            return _shuttleCarService.QueryCode(ShuttleCarCode);
        }
 
        ShuttleCarTaskType GetCarTaskType(string remark) => remark switch
        {
            "Charging" => ShuttleCarTaskType.Charging,
            "ExitCharge" => ShuttleCarTaskType.ExitCharge,
        };
 
        ShuttleCarTaskType GetCarTaskType(int TaskType, short Direction)
        {
            ShuttleCarTaskType taskType = new ShuttleCarTaskType();
            switch (TaskType)
            {
                case (int)TaskInboundTypeEnum.Inbound:
                    taskType = ShuttleCarTaskType.In;
                    break;
                case (int)TaskOutboundTypeEnum.Outbound:
                    taskType = ShuttleCarTaskType.Out;
                    break;
                case (int)TaskOtherTypeEnum.RelocationCar:
                    taskType = Direction == 1 ? ShuttleCarTaskType.ZeroA : ShuttleCarTaskType.ZeroB;
                    break;
                default:
                    break;
            }
            return taskType;
        }
 
        int GetTaskState(int TaskType)
        {
            int state = 0;
            switch (TaskType)
            {
                case (int)TaskInboundTypeEnum.Inbound:
                    state = (int)TaskInStatusEnum.Car_InExecuting;
                    break;
                case (int)TaskOutboundTypeEnum.Outbound:
                    state = (int)TaskOutStatusEnum.Car_OutExecuting;
                    break;
                case (int)TaskOtherTypeEnum.RelocationCar:
                    state = (int)TaskCarStatusEnum.ShuttleCar_Executing;
                    break;
                default:
                    break;
            }
            return state;
        }
    }
}