duyongjia
2025-01-07 48091350d466941e7d1a6a9c803ad4205d8c3d86
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
using Microsoft.AspNetCore.Components.Routing;
using Quartz;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Common.Http;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Enums;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DeviceBase;
using WIDESEAWCS_QuartzJob.Models;
using WIDESEAWCS_QuartzJob.Service;
using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
using WIDESEAWCS_Tasks.StackerCraneJob;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class CommonStackerCraneJob :JobBase, IJob
    {
        private readonly ITaskService _taskService;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly ITaskRepository _taskRepository;
        private readonly IRouterService _routerService;
 
        public CommonStackerCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService)
        {
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _taskRepository = taskRepository;
            _routerService = routerService;
        }
        public string url = AppSettings.Configuration["WMS"];
        public Task Execute(IJobExecutionContext context)
        {
            CommonStackerCrane commonStackerCrane = (CommonStackerCrane)context.JobDetail.JobDataMap.Get("JobParams");
            try
            {
                //CommonStackerCrane commonStackerCrane = (CommonStackerCrane)context.JobDetail.JobDataMap.Get("JobParams");
                if (commonStackerCrane != null)
                {
                    //if (!commonStackerCrane.IsEventSubscribed)
                    //{
                    //    commonStackerCrane.StackerCraneTaskCompletedEventHandler += CommonStackerCrane_StackerCraneTaskCompletedEventHandler;//订阅任务完成事件
                    //}
 
                    if (commonStackerCrane.StackerCraneAutoStatusValue == StackerCraneAutoStatus.Automatic && commonStackerCrane.StackerCraneStatusValue == StackerCraneStatus.Normal)
                    {
                        //commonStackerCrane.CheckStackerCraneTaskCompleted();//防止任务完成事件监测超时,再手动触发一次
 
                        if (commonStackerCrane.StackerCraneWorkStatusValue == StackerCraneWorkStatus.Standby)
                        {
                            WriteDebug(nameof(CommonStackerCraneJob), $"堆垛机开始执行,获取任务号");
                            Dt_Task? task = GetTask(commonStackerCrane);
                            if (task != null)
                            {
                                WriteDebug(nameof(CommonStackerCraneJob), $"堆垛机开始执行,任务号:{task.TaskNum}");
                                StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task);
                                int isIn = commonStackerCrane.Communicator.Read<byte>("DB106.12.0");
                                if((task.TaskType == (int)TaskOutboundTypeEnum.Outbound|| task.TaskType == (int)TaskOutboundTypeEnum.OutInventory) &&isIn==1)//如果在入库中,不允计下发出库任务
                                {
                                    WriteDebug(nameof(CommonStackerCraneJob), $"出库校验,在入库中,不允计下发出库类型任务,任务号:{task.TaskNum}");
                                    return Task.CompletedTask;
                                }
                                if (stackerCraneTaskCommand != null)
                                {
                                    bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand);
                                    if (sendFlag)
                                    {
                                        if (task.TaskType == (int)TaskInboundTypeEnum.Inbound|| task.TaskType == (int)TaskInboundTypeEnum.InInventory||task.TaskType==(int)TaskInboundTypeEnum.InPick)
                                        {
                                            _taskService.UpdateTaskStatus(task.TaskId, (int)TaskInStatusEnum.SC_InExecuting);
                                            _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskId, $"堆垛机入库执行中");
                                            WriteDebug(nameof(CommonStackerCraneJob), $"WCS堆垛机入库执行中,开始回写WMS任务状态,任务号:{task.TaskNum}");
                                            var response = HttpHelpers.Post<WebResponseContent>(url.Replace("PDA", "Task") + "UpdateTaskStatus?taskNum=" + task.TaskNum, "", "更新任务状态");
                                            if(response.Status==true)
                                            {
                                                WriteDebug(nameof(CommonStackerCraneJob), $"WCS堆垛机入库执行中,回写WMS任务状态成功,任务号:{task.TaskNum}");
                                            }
                                            else
                                            {
                                                WriteDebug(nameof(CommonStackerCraneJob), $"WCS堆垛机入库执行中,回写WMS任务状态失败,任务号:{task.TaskNum},失败原因{response.Message}");
                                            }
                                        }
                                        else if (task.TaskType == (int)TaskOutboundTypeEnum.Outbound|| task.TaskType == (int)TaskOutboundTypeEnum.OutInventory)
                                        {
                                            _taskService.UpdateTaskStatus(task.TaskId, (int)TaskOutStatusEnum.SC_OutExecuting);
                                            _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskId, $"堆垛机出库执行中");
                                            var response = HttpHelpers.Post<WebResponseContent>(url.Replace("PDA", "Task") + "UpdateTaskStatus?taskNum=" + task.TaskNum, "", "更新任务状态");
                                            if (response.Status == true)
                                            {
                                                WriteDebug(nameof(CommonStackerCraneJob), $"WCS堆垛机入库执行中,回写WMS任务状态成功,任务号:{task.TaskNum}");
                                            }
                                            else
                                            {
                                                WriteDebug(nameof(CommonStackerCraneJob), $"WCS堆垛机入库执行中,回写WMS任务状态失败,任务号:{task.TaskNum},失败原因{response.Message}");
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                WriteDebug(nameof(CommonStackerCraneJob), $"堆垛机执行异常,{ex.Message}");     
            }
 
            if (commonStackerCrane != null)
            {
                //读取大堆垛机任务号和任务完成确认状态(因为封装的事件经常不触发,所以加个直接读的逻辑,以免出现堆垛机一直接处于等待wcs确认)
                byte IsOver = commonStackerCrane.Communicator.Read<byte>("DB106.22");
                int tasknum = commonStackerCrane.Communicator.Read<int>("DB106.18");
                //WriteDebug(nameof(CommonStackerCraneJob), $"大堆垛机SC01任务号堆垛机任务号:{tasknum}状态:{IsOver}");
                if (IsOver == 6)
                {
                    if (commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WorkType) != 5)
                    {
                        _taskService.StackCraneTaskCompleted(tasknum);
                        WriteDebug(nameof(CommonStackerCraneJob), $"堆垛机WCS任务执行完成,任务号:{tasknum}");
                        Console.Out.WriteLine("TaskCompleted" + tasknum);
                        bool issuccess = commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 5);
                        if (issuccess)
                        {
                            WriteDebug(nameof(CommonStackerCraneJob), $"堆垛机任务号:{tasknum},堆垛机回写5成功");
 
                        }
                    }
                }
            }
 
            return Task.CompletedTask;
        }
 
        /// <summary>
        /// 任务完成事件订阅的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommonStackerCrane_StackerCraneTaskCompletedEventHandler(object? sender, WIDESEAWCS_QuartzJob.StackerCrane.StackerCraneTaskCompletedEventArgs e)
        {
            CommonStackerCrane? commonStackerCrane = sender as CommonStackerCrane;
            if (commonStackerCrane != null)
            {
                if (commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WorkType) != 5)
                {
                    Console.Out.WriteLine("TaskCompleted" + e.TaskNum);
                    _taskService.StackCraneTaskCompleted(e.TaskNum);
                    commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 5);
                }
            }
        }
 
        /// <summary>
        /// 获取任务
        /// </summary>
        /// <param name="commonStackerCrane">堆垛机对象</param>
        /// <returns></returns>
        private Dt_Task? GetTask(CommonStackerCrane commonStackerCrane)
        {
            Dt_Task task;
            task = _taskService.QueryStackerCraneTask("R01");
            return task;
        }
 
        /// <summary>
        /// 出库任务判断出库站台是否被占用
        /// </summary>
        /// <param name="task">任务实体</param>
        /// <returns>如果未被占用,返回传入的任务信息,否则,返回null</returns>
        private Dt_Task? OutTaskStationIsOccupied([NotNull] Dt_Task task)
        {
            Dt_Router? router = _routerService.QueryNextRoutes(task.Roadway, task.NextAddress).FirstOrDefault();
            if (router != null)
            {
                IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == router.ChildPosiDeviceCode);
                if (device != null)
                {
                    CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
                    if (conveyorLine.IsOccupied(router.ChildPosi))//出库站台未被占用
                    {
                        return task;
                    }
                }
                else
                {
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到出库站台【{router.ChildPosiDeviceCode}】对应的通讯对象,无法判断出库站台是否被占用");
                }
            }
            else
            {
                _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法校验站台");
            }
            return null;
        }
 
        /// <summary>
        /// 任务实体转换成命令Model
        /// </summary>
        /// <param name="task">任务实体</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public StackerCraneTaskCommand? ConvertToStackerCraneTaskCommand([NotNull] Dt_Task task)
        {
            StackerCraneTaskCommand stackerCraneTaskCommand = new StackerCraneTaskCommand();
 
            stackerCraneTaskCommand.Barcode = task.PalletCode;
            stackerCraneTaskCommand.TaskNum = task.TaskNum;
            stackerCraneTaskCommand.WorkType = 1;
            if(task.PalletCode.Substring(0,1)=="B")
            {
                stackerCraneTaskCommand.TrayType = 2;
            }
            else
            {
                stackerCraneTaskCommand.TrayType = 1;
            }
            //stackerCraneTaskCommand.TrayType = 1;
            string[] SourceCodes = task.SourceAddress.Split("-");
            if (SourceCodes.Length == 4)
            {
                stackerCraneTaskCommand.StartRow = Convert.ToInt16(SourceCodes[1]);
                stackerCraneTaskCommand.StartColumn = Convert.ToInt16(SourceCodes[2]);
                stackerCraneTaskCommand.StartLayer = Convert.ToInt16(SourceCodes[3]);
            }
            else
            {
                //数据配置错误
                _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"任务源地址配置错误!");
                return null;
            }
            string[] targetCodes = task.TargetAddress.Split("-");
            if (targetCodes.Length == 4)
            {
                stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[1]);
                stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[2]);
                stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[3]);
            }
            else
            {
                //数据配置错误
                _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"任务目标地址配置错误");
                return null;
            }
            return stackerCraneTaskCommand;
        }
    }
}