wankeda
2026-01-13 ce3b28cdcdb2b6a912413cd9e744d0ecadcb78a0
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 命名空间:WIDESEAWCS_Tasks.ConveyorLineJob
 * 创建者:胡童庆
 * 创建时间:2024/8/2 16:13:36
 * 版本:V1.0.0
 * 描述:
 *
 * ----------------------------------------------------------------
 * 修改人:
 * 修改时间:
 * 版本:V1.0.1
 * 修改说明:
 * 
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
 
using Autofac.Core;
using AutoMapper;
using Microsoft.AspNetCore.Routing;
using Newtonsoft.Json;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.Helper;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Caches;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_IBasicInfoRepository;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.Models;
using WIDESEAWCS_QuartzJob.Repository;
using WIDESEAWCS_QuartzJob.Service;
using WIDESEAWCS_Tasks.ConveyorLineJob;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class CommonConveyorLineJob : JobBase, IJob
    {
        private readonly ITaskService _taskService;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly IRouterService _routerService;
        private readonly IRouterRepository _routerRepository;
        private readonly ITaskRepository _taskRepository;
        private readonly IMapper _mapper;
        private readonly IStationMangerRepository _stationMangerRepository;
        private readonly ICacheService _cacheService;
        private readonly List<Dt_WarehouseDevice> warehouseDevices;
 
        public CommonConveyorLineJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IStationMangerRepository stationMangerRepository, IRouterRepository routerRepository, ITaskRepository taskRepository, ICacheService cacheService, IMapper mapper)
        {
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _routerService = routerService;
            _stationMangerRepository = stationMangerRepository;
            _taskRepository = taskRepository;
            _routerRepository = routerRepository;
            _cacheService = cacheService;
            _mapper = mapper;
 
            string? warehouseDevicesStr = _cacheService.Get<string>(nameof(Dt_WarehouseDevice));
            if (!string.IsNullOrEmpty(warehouseDevicesStr))
            {
                warehouseDevices = JsonConvert.DeserializeObject<List<Dt_WarehouseDevice>>(warehouseDevicesStr) ?? new List<Dt_WarehouseDevice>();
            }
            else
            {
                warehouseDevices = new List<Dt_WarehouseDevice>();
            }
 
        }
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                CommonConveyorLine conveyorLine = (CommonConveyorLine)context.JobDetail.JobDataMap.Get("JobParams");
                if (conveyorLine != null)
                {
                    List<string> deviceStations = conveyorLine.DeviceProDTOs.Select(x => x.DeviceChildCode).ToList();
                    List<Dt_StationManger> stationMangers = _stationMangerRepository.QueryData(x => x.StationDeviceCode == conveyorLine.DeviceCode);
                    foreach (var item in stationMangers.Where(x => deviceStations.Contains(x.StationCode)))
                    {
                        try
                        {
                            DeviceProDTO? deviceProRead = conveyorLine.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(R_ConveyorLineDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
 
                            DeviceProDTO? deviceProWrite = conveyorLine.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(W_ConveyorLineDBName)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
 
                            if (deviceProRead != null && deviceProWrite != null)
                            {
                                R_ConveyorLineInfo conveyorLineInfoRead = conveyorLine.Communicator.ReadCustomer<R_ConveyorLineInfo>(deviceProRead.DeviceProAddress);
 
                                W_ConveyorLineInfo conveyorLineInfoWrite = conveyorLine.Communicator.ReadCustomer<W_ConveyorLineInfo>(deviceProWrite.DeviceProAddress);
                                if (conveyorLineInfoRead == null || conveyorLineInfoWrite == null)
                                {
                                    continue;
                                }
 
                                R_ConveyorLineStatus conveyorLineStatus = conveyorLineInfoRead.Status.ByteToBoolObject<R_ConveyorLineStatus>();
 
                                ConveyorLineSignal conveyorLineSignalRead = conveyorLineInfoRead.Signal.ByteToBoolObject<ConveyorLineSignal>();
 
 
                                bool ACK = conveyorLine.GetValue<W_ConveyorLineDBName, bool>(W_ConveyorLineDBName.W_ConveyorLine_ACK, item.StationCode);
 
                                bool STB = conveyorLine.GetValue<W_ConveyorLineDBName, bool>(W_ConveyorLineDBName.W_ConveyorLine_STB, item.StationCode);
 
                                //bool Return = conveyorLine.GetValue<W_ConveyorLineDB, bool>(W_ConveyorLineDB.Return, item.StationCode);
                                if (item.StationType == StationTypeEnum.StationType_OnlyInbound.ObjToInt())
                                {
 
                                    if (conveyorLineSignalRead.STB && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !ACK)
                                    {
                                        if (conveyorLineInfoRead.TaskNo == 0 && !string.IsNullOrEmpty(conveyorLineInfoRead.Barcode))//采购入库
                                        {
                                            WebResponseContent webResponse = _taskService.ApplyLocation(conveyorLineInfoRead.Barcode);
                                            if (webResponse.Status)
                                            {
                                                List<Dt_WarehouseDevice> warehouseDevicesList = warehouseDevices.Where(x => x.WarehouseId == webResponse.Data.ObjToInt()).ToList();
 
                                                foreach (var list in warehouseDevicesList)
                                                {
                                                    Dt_Router routers = _routerRepository.QueryFirst(x => x.StartPosi == item.StationCode && x.ChildPosiDeviceCode == list.DeviceCode);
                                                    #region 先找允许入库的输送线入口
                                                    deviceProRead = conveyorLine.DeviceProDTOs.Where(x => x.DeviceChildCode == routers.NextPosi && x.DeviceProParamType == nameof(R_ConveyorLineDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
                                                    Dt_Task dt_Task = _taskRepository.QueryFirst(x => x.NextAddress == routers.NextPosi);
                                                    if (dt_Task != null) { continue; }
                                                    if (deviceProRead != null)
                                                    {
                                                        R_ConveyorLineInfo conveyorLineInfoRead1 = conveyorLine.Communicator.ReadCustomer<R_ConveyorLineInfo>(deviceProRead.DeviceProAddress);
                                                        R_ConveyorLineStatus conveyorLineStatus1 = conveyorLineInfoRead1.Status.ByteToBoolObject<R_ConveyorLineStatus>();
                                                        if (conveyorLineInfoRead1.Status == 6 && conveyorLineStatus1.Free && conveyorLineStatus1.Online && !conveyorLineStatus1.Goods)
                                                        {
 
                                                            #region 请求WMS生成任务后给输送线启动信号
                                                            WebResponseContent content = _taskService.CPRequestWMSTaskSimple(conveyorLineInfoRead.Barcode, routers.NextPosi, conveyorLineInfoRead.Spare2, item.StationCode);
                                                            if (content.Status)
                                                            {
                                                                Dt_Task task = _taskRepository.QueryFirst(x => x.PalletCode == conveyorLineInfoRead.Barcode && x.CurrentAddress == item.StationCode && x.DeviceCode == conveyorLine.DeviceCode && x.TaskState == TaskStatusEnum.New.ObjToInt());
                                                                if (task != null)
                                                                {
                                                                    conveyorLine.SetValue(W_ConveyorLineDBName.W_ConveyorLine_ACK, true, item.StationCode);
                                                                    _taskService.UpdateTask(task, TaskStatusEnum.RGV_NEW, sourceAddress: item.StationCode, RGVCode: item.AGVStationCode);
                                                                    WebResponseContent responseContent = _taskService.LedShowTask(item.StationCode, task.PalletCode, task.TaskType);
                                                                }
 
                                                            }
                                                            #endregion
                                                        }
                                                    }
                                                    #endregion
                                                }
                                            }
                                        }
                                    }
                                }
                                if (item.StationType == StationTypeEnum.StationType_InboundAndOutbound.ObjToInt())
                                {
                                    if (conveyorLineSignalRead.STB && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !ACK)
                                    {
                                        if (conveyorLineInfoRead.TaskNo == 0 && !string.IsNullOrEmpty(conveyorLineInfoRead.Barcode))//采购入库
                                        {
                                            WebResponseContent webResponse = _taskService.ApplyLocation(conveyorLineInfoRead.Barcode);
                                            if (webResponse.Status)
                                            {
                                                List<Dt_WarehouseDevice> warehouseDevicesList = warehouseDevices.Where(x => x.WarehouseId == webResponse.Data.ObjToInt()).ToList();
 
                                                foreach (var list in warehouseDevicesList)
                                                {
                                                    Dt_Router routers = _routerRepository.QueryFirst(x => x.StartPosi == item.StationCode && x.ChildPosiDeviceCode == list.DeviceCode);
                                                    #region 先找允许入库的输送线入口
                                                    deviceProRead = conveyorLine.DeviceProDTOs.Where(x => x.DeviceChildCode == routers.NextPosi && x.DeviceProParamType == nameof(R_ConveyorLineDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
                                                    Dt_Task dt_Task = _taskRepository.QueryFirst(x => x.NextAddress == routers.NextPosi);
                                                    if (dt_Task != null) { continue; }
                                                    if (deviceProRead != null)
                                                    {
                                                        R_ConveyorLineInfo conveyorLineInfoRead1 = conveyorLine.Communicator.ReadCustomer<R_ConveyorLineInfo>(deviceProRead.DeviceProAddress);
                                                        R_ConveyorLineStatus conveyorLineStatus1 = conveyorLineInfoRead1.Status.ByteToBoolObject<R_ConveyorLineStatus>();
                                                        if (conveyorLineInfoRead1.Status == 6 && conveyorLineStatus1.Free && conveyorLineStatus1.Online && !conveyorLineStatus1.Goods)
                                                        {
 
                                                            #region 请求WMS生成任务后给输送线启动信号
                                                            WebResponseContent content = _taskService.CPRequestWMSTaskSimple(conveyorLineInfoRead.Barcode, routers.NextPosi, conveyorLineInfoRead.Spare2, item.StationCode);
                                                            if (content.Status)
                                                            {
                                                                Dt_Task task = _taskRepository.QueryFirst(x => x.PalletCode == conveyorLineInfoRead.Barcode && x.CurrentAddress == item.StationCode && x.DeviceCode == conveyorLine.DeviceCode && x.TaskState == TaskStatusEnum.New.ObjToInt());
                                                                if (task != null)
                                                                {
                                                                    conveyorLine.SetValue(W_ConveyorLineDBName.W_ConveyorLine_ACK, true, item.StationCode);
                                                                    _taskService.UpdateTask(task, TaskStatusEnum.RGV_NEW, sourceAddress: item.StationCode, RGVCode: item.AGVStationCode);
 
                                                                }
 
                                                            }
                                                            #endregion
                                                        }
                                                    }
                                                }
                                            }
                                            #endregion
                                        }
                                    }
                                }
                                //出库站台
                                if (item.StationType == StationTypeEnum.StationType_OnlyOutbound.ObjToInt())
                                {
 
                                }
                                if (item.StationType == StationTypeEnum.SC_In.ObjToInt())
                                {
                                    if (conveyorLineSignalRead.STB && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !ACK)
                                    {
                                        Dt_Task task = _taskRepository.QueryFirst(x => x.NextAddress == item.StationCode && x.DeviceCode == item.StationDeviceCode && x.TaskState == TaskStatusEnum.RGV_Finish.ObjToInt());
                                        if (task != null)
                                        {
                                            _taskService.UpdateTask(task, TaskStatusEnum.SC_Execute, deviceCode: task.Roadway, currentAddress: task.NextAddress);
                                            conveyorLine.SetValue(W_ConveyorLineDBName.W_ConveyorLine_ACK, true, item.StationCode);
                                        }
                                    }
                                }
                                if (item.StationType == StationTypeEnum.SC_Out.ObjToInt())
                                {
                                    if (!conveyorLineSignalRead.STB && !conveyorLineSignalRead.ACK && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !STB && !ACK)
                                    {
                                        Dt_Task _Task = _taskRepository.QueryFirst(x => x.CurrentAddress == item.StationCode && x.TaskState == TaskStatusEnum.Line_Execute.ObjToInt());
                                        if (_Task != null)
                                        {
                                            Dt_Router routers = _routerRepository.QueryFirst(x => x.StartPosi == _Task.CurrentAddress);
                                            if (routers != null)
                                            {
                                                conveyorLine.SetValue(W_ConveyorLineDBName.W_ConveyorLine_TaskNo, _Task.TaskNum, item.StationCode);
                                                conveyorLine.SetValue(W_ConveyorLineDBName.W_ConveyorLine_EndPos, routers.NextPosi, item.StationCode);
                                                conveyorLine.SetValue(W_ConveyorLineDBName.W_ConveyorLine_STB, true, item.StationCode);
                                                _taskService.UpdateTask(_Task, TaskStatusEnum.Line_Executing, nextAddress: routers.NextPosi);
                                                WriteDebug($"堆垛机出库口{item.StationCode}", $"堆垛机出库口任务更新,任务号{_Task.TaskId},目标出口位置{_Task.TargetAddress},托盘号:{_Task.PalletCode}");
                                            }
                                        }
 
                                    }
                                    //堆垛机出库口外面一段出库站台
                                    if (conveyorLineSignalRead.STB && !conveyorLineSignalRead.ACK && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !STB && !ACK)
                                    {
                                        Dt_Task task = _taskRepository.QueryFirst(x => x.NextAddress == item.StationCode && x.DeviceCode == conveyorLine.DeviceCode && x.TaskState == TaskStatusEnum.Line_Executing.ObjToInt());
                                        if (task != null)
                                        {
                                            List<Dt_Router> routers = _routerRepository.QueryData(x => x.StartPosi == item.StationCode).ToList();
                                            foreach (var items in routers)
                                            {
                                                deviceProRead = conveyorLine.DeviceProDTOs.Where(x => x.DeviceChildCode == items.NextPosi && x.DeviceProParamType == nameof(R_ConveyorLineDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
                                                if (deviceProRead != null)
                                                {
                                                    R_ConveyorLineInfo conveyorLineInfoRead3 = conveyorLine.Communicator.ReadCustomer<R_ConveyorLineInfo>(deviceProRead.DeviceProAddress);
 
                                                    R_ConveyorLineStatus conveyorLineStatus3 = conveyorLineInfoRead3.Status.ByteToBoolObject<R_ConveyorLineStatus>();
 
                                                    ConveyorLineSignal conveyorLineSignalRead3 = conveyorLineInfoRead3.Signal.ByteToBoolObject<ConveyorLineSignal>();
 
                                                    if (!conveyorLineSignalRead3.STB && !conveyorLineSignalRead3.ACK && conveyorLineStatus3.Online && conveyorLineStatus3.Free && !conveyorLineStatus3.Goods && !conveyorLineStatus3.Alarm && !STB && !ACK)//判断出库口是否空闲
                                                    {
                                                        Dt_Task dt_Task = _taskRepository.QueryFirst(x => x.TargetAddress == items.NextPosi);
                                                        if (dt_Task == null)
                                                        {
                                                            conveyorLine.SetValue(W_ConveyorLineDBName.W_ConveyorLine_ACK, true, item.StationCode);
                                                            _taskService.UpdateTask(task, TaskStatusEnum.RGV_Execute, currentAddress: item.StationCode, targetAddress: items.NextPosi, nextAddress: items.NextPosi, RGVCode: item.AGVStationCode);
                                                            Thread.Sleep(500);
                                                        }
                                                    }
                                                }
                                            }
 
                                        }
 
                                    }
                                }
                                if (!conveyorLineSignalRead.STB && conveyorLineSignalRead.ACK && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && STB && !ACK)
                                {
                                    conveyorLine.SetValue(W_ConveyorLineDBName.W_ConveyorLine_STB, false, item.StationCode);
                                }
                                if (!conveyorLineSignalRead.STB && !conveyorLineSignalRead.ACK && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !STB && ACK)
                                {
                                    conveyorLine.SetValue(W_ConveyorLineDBName.W_ConveyorLine_ACK, false, item.StationCode);
                                }
                                if (!conveyorLineSignalRead.STB && !conveyorLineSignalRead.ACK && conveyorLineStatus.Online && !conveyorLineStatus.Alarm && !STB && ACK)
                                {
                                    conveyorLine.SetValue(W_ConveyorLineDBName.W_ConveyorLine_ACK, false, item.StationCode);
                                }
                                //if (!conveyorLineSignalRead.STB && !conveyorLineSignalRead.ACK && conveyorLineStatus.Online && !conveyorLineStatus.Alarm && !STB && !ACK && Return)
                                //{
                                //    conveyorLine.SetValue(W_ConveyorLineDBName.W_ConveyorLine_Return, false, item.StationCode);
                                //}
                                if (conveyorLineSignalRead.STB && !conveyorLineSignalRead.ACK && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && ACK)
                                {
                                    conveyorLine.SetValue(W_ConveyorLineDBName.W_ConveyorLine_ACK, false, item.StationCode);
                                }
 
 
                            }
                        }
                        catch (Exception ex)
                        {
                            WriteError(nameof(CommonConveyorLine), ex.Message, ex);
                        }
 
                    }
 
                }
            }
            catch (Exception ex)
            {
                WriteError(nameof(CommonConveyorLine), ex.Message, ex);
            }
            return Task.CompletedTask;
        }
 
        /// <summary>
        /// 输送线请求入库
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        public void RequestInbound(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode)
        {
 
        }
 
        /// <summary>
        /// 输送线请求入库下一地址
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        public void RequestInNextAddress(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode)
        {
 
        }
 
        /// <summary>
        /// 输送线入库完成
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        public void ConveyorLineInFinish(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode)
        {
 
        }
 
        /// <summary>
        /// 输送线请求出信息
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        public void RequestOutbound(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode)
        {
 
        }
 
        /// <summary>
        /// 输送线请求出库下一地址
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        public void RequestOutNextAddress(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode)
        {
 
        }
 
        /// <summary>
        /// 输送线出库完成
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        public void ConveyorLineOutFinish(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode)
        {
 
        }
    }
}