huangxiaoqiang
13 小时以前 850105e8cdf0d048ef843d87520513ac77a64200
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 命名空间:WIDESEAWCS_Tasks.ConveyorLineJob
 * 创建者:胡童庆
 * 创建时间:2024/8/2 16:13:36
 * 版本:V1.0.0
 * 描述:
 *
 * ----------------------------------------------------------------
 * 修改人:
 * 修改时间:
 * 版本:V1.0.1
 * 修改说明:
 * 
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
 
using AutoMapper;
using HslCommunication;
using Newtonsoft.Json;
using Quartz;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_BasicInfoRepository;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core.Caches;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.HttpContextUser;
using WIDESEAWCS_IBasicInfoRepository;
using WIDESEAWCS_IBasicInfoService;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.BasicInfo;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DeviceBase;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.Models;
using WIDESEAWCS_QuartzJob.Service;
using WIDESEAWCS_SignalR;
using WIDESEAWCS_Tasks.ConveyorLineJob;
using WIDESEAWCS_Tasks.RGVJob;
using WIDESEAWCS_Tasks.StackerCraneJob;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class CommonRGVJob :JobBase, IJob
    {
        private readonly ITaskService _taskService;
        private readonly ITaskRepository _taskRepository;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly IRouterService _routerService;
        private readonly IDt_StationManagerService _stationManagerService;
        private readonly IDt_StationManagerRepository _stationManagerRepository;
        private readonly IMapper _mapper;
        private readonly ICacheService _cacheService;
        private readonly INoticeService _noticeService;
        private static List<string>? userTokenIds;
        private static List<int>? userIds;
 
        public CommonRGVJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IDt_StationManagerService stationManagerService, IMapper mapper, ICacheService cacheService, INoticeService noticeService, ITaskRepository taskRepository,IDt_StationManagerRepository stationManagerRepository)
        {
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _routerService = routerService;
            _stationManagerService = stationManagerService;
            _mapper = mapper;
            _cacheService = cacheService;
            _noticeService = noticeService;
            _taskRepository = taskRepository;
            _stationManagerRepository = stationManagerRepository;
        }
 
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                CommonRGV commonRGV = (CommonRGV)context.JobDetail.JobDataMap.Get("JobParams");
                if (commonRGV != null)
                {
                    //if (!commonRGV.IsEventSubscribed)
                    //{
                    //    commonRGV.StackerCraneTaskCompletedEventHandler += CommonStackerCrane_StackerCraneTaskCompletedEventHandler;//订阅任务完成事件
                    //}
 
                    //commonRGV.CheckStackerCraneTaskCompleted();//防止任务完成事件监测超时,再手动触发一次
                    if (commonRGV.GetValue<StackerCraneDBName, bool>(StackerCraneDBName.WorkCompleted))
                    {
                        var x = commonRGV.GetValue<RGVDBName, short>(RGVDBName.RGVTaskNum);
                        RGVTaskFinish(commonRGV, commonRGV.GetValue<RGVDBName, short>(RGVDBName.RGVTaskNum));
                    }
 
                    if (commonRGV.GetValue<RGVDBName, bool>(RGVDBName.Automatic) && !commonRGV.GetValue<RGVDBName, bool>(RGVDBName.Fault) && !commonRGV.GetValue<RGVDBName, bool>(RGVDBName.Running))
                    {
 
                        Dt_Task? task = GetTask(commonRGV, commonRGV.GetValue<RGVDBName, bool>(RGVDBName.InStock));
                        if (task != null)
                        {
                            RGVTaskCommand? rgvTaskCommand = ConvertToRGVTaskCommand(task);
                            if (rgvTaskCommand != null)
                            {
                                Thread.Sleep(1000);
                                bool sendFlag = SendCommand(commonRGV,rgvTaskCommand);
                                if (sendFlag)
                                {
                                    _taskService.UpdateTaskStatusToNext(task.TaskNum);
                                }
                            }
                        }
                    }
 
                    #region 调用事件总线通知前端
 
                    var tokenInfos = _cacheService.Get<List<UserInfo>>("Cache_UserToken");
                    if (tokenInfos == null || !tokenInfos.Any())
                    {
                        return Task.CompletedTask;
                    }
                    var userTokenIds = tokenInfos?.Select(x => x.Token_ID).ToList();
                    var userIds = tokenInfos?.Select(x => x.UserId).ToList();
 
                    object obj = new
                    {
                        Automatic = commonRGV.GetValue<StackerCraneDBName, bool>(StackerCraneDBName.Automatic),
                        Fault = commonRGV.GetValue<StackerCraneDBName, bool>(StackerCraneDBName.Fault),
                        Running = commonRGV.GetValue<StackerCraneDBName, bool>(StackerCraneDBName.Running),
                        //commonRGV.StackerCraneAutoStatusDes,
                        //commonRGV.StackerCraneWorkStatusDes,
                        //commonRGV.DeviceCode,
                        //commonRGV.DeviceName,
                        //commonRGV.CurrentTaskNum,
                        //commonRGV.LastTaskNum,
                    };
                    _noticeService.StackerData(userIds?.FirstOrDefault(), userTokenIds, new { commonRGV.DeviceName, data = obj });
 
                    #endregion 调用事件总线通知前端
                }
            }
            catch (Exception ex)
            {
                WriteError("CommonStackerStationCraneJob", "test", ex);
            }
            return Task.CompletedTask;
        }
        /// <summary>
        /// 任务完成事件订阅的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommonStackerCrane_StackerCraneTaskCompletedEventHandler(object? sender, WIDESEAWCS_QuartzJob.StackerCrane.StackerCraneTaskCompletedEventArgs e)
        {
            CommonRGV? commonRGV = sender as CommonRGV;
                       
        }
 
        public void RGVTaskFinish(CommonRGV commonRGV, int TaskNum)
        {
            if (commonRGV != null)
            {
                #region 日志记录
                ConsoleHelper.WriteColorLine($"【{commonRGV.DeviceName}】堆垛机作业状态:【】时间【{DateTime.Now}】", ConsoleColor.Magenta);
 
                string str = $"【{commonRGV.DeviceName}】任务完成,任务号:【{TaskNum}】时间【{DateTime.Now}】";
                WriteInfo(commonRGV.DeviceName, str);
                ConsoleHelper.WriteColorLine(str, ConsoleColor.Blue);
 
                #endregion
 
                var task = _taskRepository.QueryFirst(x => x.TaskNum == TaskNum);
                if (task != null)
                {
                    if (task.TaskState == (int)TaskInStatusEnum.RGV_Indispatch)
                    {
                        Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationChildCode == task.SourceAddress);
                        if (stationManager != null)
                        {
                            IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManager.stationPLC);
                            if (device != null)
                            {
                                CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
                                Thread.Sleep(100);
                                var Barcode = conveyorLine.GetValue<ConveyorLineDBName, string>(ConveyorLineDBName.ConveyorLineBarcode, stationManager.stationChildCode);
 
                                if (Barcode == null)
                                {
                                    return;
                                }
                                var content = _taskService.RGVTaskCompleted(TaskNum, Barcode);
                            }
                        }
                    }
                    else
                    {
                        var content = _taskService.RGVTaskCompleted(TaskNum);
                    }
                }
            }
        }
 
        /// <summary>
        /// 获取任务
        /// </summary>
        /// <param name="commonRGV">堆垛机对象</param>
        /// <returns></returns>
        private Dt_Task? GetTask(CommonRGV commonRGV, bool InStock)
        {
            Dt_Task task;
            if (InStock)
            {
                task = _taskService.QueryRGVExecutingTask(commonRGV.DeviceCode);
                if (task != null)
                {
                    return task;
                }
            }
            if (commonRGV.LastTaskType == null)
            {
                task = _taskService.QueryRGVTask(commonRGV.DeviceCode);
            }
            else
            {
                if (commonRGV.LastTaskType.GetValueOrDefault().GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                {
                    task = _taskService.QueryRGVInTask(commonRGV.DeviceCode);
                    if (task == null)
                    {
                        task = _taskService.QueryRGVOutTask(commonRGV.DeviceCode);
                    }
                }
                else
                {
                    task = _taskService.QueryRGVOutTask(commonRGV.DeviceCode);
                }
            }
 
            if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
            {
                if (task == null)
                {
                    task = _taskService.QueryRGVInTask(commonRGV.DeviceCode);
                }
                else
                {
                    return task;
                }
            }
            else if (task == null)
            {
                task = _taskService.QueryRGVInTask(commonRGV.DeviceCode);
            }
 
            return task;
        }
 
        /// <summary>
        /// 任务实体转换成命令Model
        /// </summary>
        /// <param name="task">任务实体</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public RGVTaskCommand? ConvertToRGVTaskCommand([NotNull] Dt_Task task)
        {
            RGVTaskCommand rgvTaskCommand = new RGVTaskCommand();
 
            rgvTaskCommand.TaskNum = Convert.ToInt16(task.TaskNum);
            if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)//判断是否是入库任务
            {
                //rgvTaskCommand.TaskType = (byte)Convert.ToSByte(16);
                //rgvTaskCommand.TaskType = (byte)Convert.ToSByte(64);   退回
                if (task.TaskState == (int)TaskInStatusEnum.InNew)
                {
                    Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationChildCode == task.SourceAddress);
                    rgvTaskCommand.TaskType = (byte)Convert.ToSByte(3);
                    rgvTaskCommand.PickupLocation = (byte)Convert.ToSByte(stationManager.stationRemark);
                }
                else if (task.TaskState == (int)TaskInStatusEnum.RGV_IndispatchFinish)
                {
                    Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationChildCode == task.NextAddress);
                    rgvTaskCommand.TaskType = (byte)Convert.ToSByte(4);
                    rgvTaskCommand.PutcargoLocation = (byte)Convert.ToSByte(stationManager.stationRemark);
                }
                else
                {
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法获取对应的堆垛机取货站台信息");
                    return null;
                }
            }
            else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
            {
 
                if (task.TaskState == (int)TaskOutStatusEnum.Line_OutFinish)
                {
                    Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationChildCode == task.CurrentAddress);
                    rgvTaskCommand.TaskType = (byte)Convert.ToSByte(3);
                    rgvTaskCommand.PickupLocation = (byte)Convert.ToSByte(stationManager.stationRemark);
                }
                else if (task.TaskState == (int)TaskOutStatusEnum.RGV_OutdispatchFinish)
                {
                    Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationChildCode == task.NextAddress);
                    rgvTaskCommand.TaskType = (byte)Convert.ToSByte(4);
                    rgvTaskCommand.PutcargoLocation = (byte)Convert.ToSByte(stationManager.stationRemark);
                }
                else
                {
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法获取对应的堆垛机取货站台信息");
                    return null;
                }
            }
            
            return rgvTaskCommand;
        }
 
        public bool SendCommand(CommonRGV commonRGV, RGVTaskCommand? rgvTaskCommand)
        {
            commonRGV.SetValue(StackerCraneDBName.TaskNum, rgvTaskCommand.TaskNum);
            Thread.Sleep(100);
            commonRGV.SetValue(StackerCraneDBName.PutcargoLocation, rgvTaskCommand.PutcargoLocation);
            Thread.Sleep(100);
            commonRGV.SetValue(StackerCraneDBName.PickupLocation, rgvTaskCommand.PickupLocation);
            Thread.Sleep(100);
            commonRGV.SetValue(StackerCraneDBName.TaskType,Convert.ToSByte(rgvTaskCommand.TaskType));
            for (int i = 0; i < 6; i++)
            {
 
                if (commonRGV.GetValue<StackerCraneDBName, short>(StackerCraneDBName.TaskNum) == rgvTaskCommand.TaskNum && commonRGV.GetValue<StackerCraneDBName, byte>(StackerCraneDBName.TaskType) == rgvTaskCommand.TaskType && commonRGV.GetValue<StackerCraneDBName, byte>(StackerCraneDBName.PutcargoLocation) == rgvTaskCommand.PutcargoLocation && commonRGV.GetValue<StackerCraneDBName, byte>(StackerCraneDBName.PickupLocation) == rgvTaskCommand.PickupLocation)
                {
                    WriteInfo(commonRGV.DeviceName, $"写入任务成功写入次数{i}写入任务【{JsonConvert.SerializeObject(rgvTaskCommand)}】");
                    return true;
                }
                if (commonRGV.GetValue<StackerCraneDBName, short>(StackerCraneDBName.TaskNum) == rgvTaskCommand.TaskNum)
                {
                    commonRGV.SetValue(StackerCraneDBName.TaskNum, rgvTaskCommand.TaskNum);
                    Thread.Sleep(100);
                }
                if (commonRGV.GetValue<StackerCraneDBName, short>(StackerCraneDBName.PutcargoLocation) == rgvTaskCommand.PutcargoLocation)
                {
                    commonRGV.SetValue(StackerCraneDBName.PutcargoLocation, rgvTaskCommand.PutcargoLocation);
                }
                if (commonRGV.GetValue<StackerCraneDBName, short>(StackerCraneDBName.PickupLocation) == rgvTaskCommand.PickupLocation)
                {
                    commonRGV.SetValue(StackerCraneDBName.PickupLocation, rgvTaskCommand.PickupLocation);
                }
                if (commonRGV.GetValue<StackerCraneDBName, short>(StackerCraneDBName.TaskType) == rgvTaskCommand.TaskType)
                {
                    commonRGV.SetValue(StackerCraneDBName.TaskType, rgvTaskCommand.TaskType);
                }
            }
            WriteInfo(commonRGV.DeviceName, $"RGV任务写入失败【{JsonConvert.SerializeObject(rgvTaskCommand)}】");
            return false;
        }
 
        public async Task LogAndWarn(string deviceName, string log, string color = "red")
        {
            ConsoleHelper.WriteWarningLine(log);
            await _noticeService.Logs(userTokenIds, new { deviceName, log = log, time = DateTime.Now.ToString("G"), color = color });
            WriteInfo(deviceName, log);
        }
    }
}