wangxinhui
2025-02-21 3a0fef4ed5be4b2d3e9259fa0202fa42cb31c875
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
using Newtonsoft.Json;
using Quartz;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
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_TaskInfoService;
using WIDESEAWCS_Tasks.ConveyorLineJob;
using ICacheService = WIDESEAWCS_Core.Caches.ICacheService;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class ConveyorLineJob_BC : JobBase, IJob
    {
        private readonly ICacheService _cacheService;
        private readonly ITaskService _taskService;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly ITaskRepository _taskRepository;
        private readonly IStationMangerRepository _stationMangerRepository;
        private readonly IRouterRepository _routerRepository;
        private readonly IRouterService _routerService;
        private readonly IRouterExtension _routerExtension;
        private readonly List<Dt_WarehouseDevice> warehouseDevices;
 
        public ConveyorLineJob_BC(ICacheService cacheService, ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IStationMangerRepository stationMangerRepository, IRouterRepository routerRepository, IRouterService routerService, IRouterExtension routerExtension)
        {
            _cacheService = cacheService;
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _taskRepository = taskRepository;
            _stationMangerRepository = stationMangerRepository;
            _routerRepository = routerRepository;
            _routerService = routerService;
            _routerExtension = routerExtension;
 
            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)
        {
            bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value);
            if (flag && value != null)
            {
                OtherDevice device = (OtherDevice)value;
                Dt_WarehouseDevice? warehouseDevice = warehouseDevices.FirstOrDefault(x => x.DeviceCode == device.DeviceCode);
                if (warehouseDevice == null)
                {
                    WriteError(device.DeviceName, $"请配置仓库设备信息");
                    return Task.CompletedTask;
                }
                List<string> deviceStations = device.DeviceProDTOs.Select(x => x.DeviceChildCode).ToList();
                List<Dt_StationManger> stationMangers = _stationMangerRepository.QueryData(x => x.StationDeviceCode == device.DeviceCode);
                foreach (var item in stationMangers.Where(x => deviceStations.Contains(x.StationCode)))
                {
                    DeviceProDTO? deviceProRead = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(R_ConveyorLineDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
 
                    DeviceProDTO? deviceProWrite = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(W_ConveyorLineDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
 
                    if (deviceProRead != null && deviceProWrite != null)
                    {
                        R_ConveyorLineInfo conveyorLineInfoRead = device.Communicator.ReadCustomer<R_ConveyorLineInfo>(deviceProRead.DeviceProAddress);
 
                        W_ConveyorLineInfo conveyorLineInfoWrite = device.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>();
 
                        ConveyorLineSignal conveyorLineSignalWrite = conveyorLineInfoWrite.Signal.ByteToBoolObject<ConveyorLineSignal>();
 
                        if (item.StationType == StationTypeEnum.StationType_InboundAndOutbound.ObjToInt())
                        {
                            {
                                #region 生成任务后给输送线启动信号
                                Dt_Task task = _taskRepository.QueryFirst(x => x.CurrentAddress == item.StationCode && _taskService.TaskInboundTypes.Contains(x.TaskType) /*&& x.DeviceCode == item.StationDeviceCode*/ && (x.TaskState == TaskStatusEnum.New.ObjToInt()) && x.WarehouseId == warehouseDevice.WarehouseId);
                                if (task != null && conveyorLineInfoWrite.Spare2 == 0 && conveyorLineStatus.Goods)
                                {
                                    List<string> stations = _routerExtension.GetEndPoint(item.StationCode, TaskTypeEnum.Inbound.ObjToInt()).Select(x => x.NextPosi).ToList();
                                    WebResponseContent responseContent = _taskService.RequestWMSAssignRoadway(stations, task.TaskNum, conveyorLineInfoRead.Spare2);
                                    if (responseContent.Status)
                                    {
                                        device.SetValue(W_ConveyorLineDB.Spare2, 1, item.StationCode);
                                        _taskService.UpdateTask(task, TaskStatusEnum.Line_Execute, deviceCode: item.StationDeviceCode, roadwayNo: responseContent.Data.ToString() ?? "");
                                    }
                                }
                                else
                                {
                                    if (conveyorLineInfoWrite.Spare2 != 0)
                                    {
                                        WriteDebug(device.DeviceName, $"启动信号已写入");
                                    }
                                }
                                #endregion
                            }
 
                            if (conveyorLineSignalRead.STB && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !conveyorLineSignalWrite.ACK)
                            {
                                if (conveyorLineInfoRead.TaskNum == 0 && !string.IsNullOrEmpty(conveyorLineInfoRead.Barcode))//采购入库
                                {
                                    #region 任务号为0,且有托盘号,则是采购入库
                                    Dt_Task task = _taskRepository.QueryFirst(x => x.CurrentAddress == item.StationCode && _taskService.TaskInboundTypes.Contains(x.TaskType) && x.DeviceCode == item.StationDeviceCode && (x.TaskState == TaskStatusEnum.Line_Execute.ObjToInt()) && x.PalletCode == conveyorLineInfoRead.Barcode && x.WarehouseId == warehouseDevice.WarehouseId);
                                    if (task != null)
                                    {
                                        device.SetValue(W_ConveyorLineDB.Spare2, 0, item.StationCode);
 
                                        string currentAddress = task.CurrentAddress;
                                        string nextAddress = task.NextAddress;
                                        string targetAddress = task.TargetAddress;
                                        string deviceCode = task.DeviceCode;
                                        TaskStatusEnum taskState = TaskStatusEnum.Line_Executing;
                                        List<Dt_Router> routers = _routerService.QueryNextRoutes(item.StationCode, task.Roadway, task.TaskType);
                                        Dt_Router? router = routers.FirstOrDefault();
                                        if (routers == null || routers.Count == 0 || router == null)
                                        {
                                            WriteError(item.StationName, $"未找到对应路由信息,设备编号:{item.StationCode},任务号:{task.TaskNum}");
                                            continue;
                                        }
                                        if (routers.Count > 1)
                                        {
                                            WriteError(item.StationName, $"路由信息配置错误,设备编号:{item.StationCode},任务号:{task.TaskNum}");
                                            continue;
                                        }
 
                                        if (router.IsEnd)
                                        {
                                            string? targetLoca = _taskService.RequestAssignLocationByHeight(task.TaskNum, task.Roadway, conveyorLineInfoRead.Spare2);
                                            if (!string.IsNullOrEmpty(targetLoca))
                                            {
                                                currentAddress = item.StackerCraneStationCode;
                                                targetAddress = targetLoca;
                                                nextAddress = targetLoca;
                                                taskState = TaskStatusEnum.SC_Execute;
                                                deviceCode = item.StackerCraneCode;
                                                device.SetValue(W_ConveyorLineDB.EndPos, task.CurrentAddress, item.StationCode);
                                            }
                                            else
                                            {
                                                WriteError(item.StationName, $"请求分配货位失败,设备编号:{item.StationCode},任务号:{task.TaskNum}");
                                                continue;
                                            }
                                        }
                                        else
                                        {
                                            //task.NextAddress可能是router.NextPosi
                                            device.SetValue(W_ConveyorLineDB.EndPos, task.NextAddress, item.StationCode);
                                        }
                                        device.SetValue(W_ConveyorLineDB.TaskNum, task.TaskNum, item.StationCode);
                                        device.SetValue(W_ConveyorLineDB.StartPos, task.CurrentAddress, item.StationCode);
                                        device.SetValue(W_ConveyorLineDB.Spare1, ConveyorWorkTypeEnum.Outbound.ObjToInt(), item.StationCode);
                                        device.SetValue(W_ConveyorLineDB.ACK, true, item.StationCode);
                                        _taskService.UpdateTask(task, taskState, currentAddress: currentAddress, nextAddress: nextAddress, targetAddress: targetAddress, deviceCode: deviceCode);
                                    }
                                    else
                                    {
                                        WriteInfo(device.DeviceName, $"未找到托盘{conveyorLineInfoRead.Barcode}对应的任务信息");
                                    }
                                    #endregion
                                }
                                else//生产退库
                                {
                                    #region 生产退库,带任务号查询任务
                                    Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == conveyorLineInfoRead.TaskNum && x.NextAddress == item.StationCode && _taskService.TaskInboundTypes.Contains(x.TaskType) && x.WarehouseId == warehouseDevice.WarehouseId); // 带任务号查询任务
                                    if (task != null)
                                    {
                                        List<string> stations = _routerExtension.GetEndPoint(item.StationCode, TaskTypeEnum.Inbound.ObjToInt()).Select(x => x.NextPosi).ToList();
                                        WebResponseContent responseContent = _taskService.RequestWMSAssignRoadway(stations, task.TaskNum, conveyorLineInfoRead.Spare2);
                                        if (responseContent.Status)
                                        {
                                            //string currentAddress = task.CurrentAddress;
                                            //string nextAddress = task.NextAddress;
                                            //string targetAddress = task.TargetAddress;
                                            string roadwayNo = task.Roadway;
 
                                            string currentAddress = task.CurrentAddress;
                                            string nextAddress = task.NextAddress;
                                            string targetAddress = task.TargetAddress;
                                            string deviceCode = task.DeviceCode;
                                            TaskStatusEnum taskState = TaskStatusEnum.Line_Executing;
                                            List<Dt_Router> routers = _routerService.QueryNextRoutes(item.StationCode, responseContent.Data.ToString(), task.TaskType);
                                            Dt_Router? router = routers.FirstOrDefault();
                                            if (routers == null || routers.Count == 0 || router == null)
                                            {
                                                WriteError(item.StationName, $"未找到对应路由信息,设备编号:{item.StationCode},任务号:{task.TaskNum}");
                                                continue;
                                            }
                                            if (routers.Count > 1)
                                            {
                                                WriteError(item.StationName, $"路由信息配置错误,设备编号:{item.StationCode},任务号:{task.TaskNum}");
                                                continue;
                                            }
 
                                            if (router.IsEnd)
                                            {
                                                string? targetLoca = _taskService.RequestAssignLocationByHeight(task.TaskNum, responseContent.Data.ToString(), conveyorLineInfoRead.Spare2);
                                                if (!string.IsNullOrEmpty(targetLoca))
                                                {
                                                    currentAddress = item.StackerCraneStationCode;
                                                    targetAddress = targetLoca;
                                                    nextAddress = targetLoca;
                                                    taskState = TaskStatusEnum.SC_Execute;
                                                    deviceCode = item.StackerCraneCode;
                                                    roadwayNo = responseContent.Data.ToString();
                                                    device.SetValue(W_ConveyorLineDB.EndPos, task.NextAddress, item.StationCode);
                                                }
                                                else
                                                {
                                                    WriteError(item.StationName, $"请求分配货位失败,设备编号:{item.StationCode},任务号:{task.TaskNum}");
                                                    continue;
                                                }
                                            }
                                            else
                                            {
                                                //task.NextAddress可能是router.NextPosi
                                                device.SetValue(W_ConveyorLineDB.EndPos, task.NextAddress, item.StationCode);
                                            }
                                            device.SetValue(W_ConveyorLineDB.TaskNum, task.TaskNum, item.StationCode);
                                            device.SetValue(W_ConveyorLineDB.StartPos, task.CurrentAddress, item.StationCode);
                                            device.SetValue(W_ConveyorLineDB.Spare1, ConveyorWorkTypeEnum.Outbound.ObjToInt(), item.StationCode);
                                            device.SetValue(W_ConveyorLineDB.ACK, true, item.StationCode);
                                            _taskService.UpdateTask(task, taskState, currentAddress: currentAddress, nextAddress: nextAddress, targetAddress: targetAddress, deviceCode: deviceCode, roadwayNo: roadwayNo);
                                        }
                                       
                                    }
                                    #endregion
                                }
                            }
                            else if (!conveyorLineSignalRead.STB && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && conveyorLineSignalWrite.ACK)
                            {
                                device.SetValue(W_ConveyorLineDB.ACK, false, item.StationCode);
                            }
                            else if (!conveyorLineSignalRead.STB && !conveyorLineSignalRead.ACK && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !conveyorLineSignalWrite.STB && !conveyorLineSignalWrite.ACK && conveyorLineInfoRead.TaskNum == 0)//出库
                            {
                                Dt_Task task = _taskRepository.QueryFirst(x => x.CurrentAddress == item.StationCode && _taskService.TaskOutboundTypes.Contains(x.TaskType) && x.DeviceCode == item.StationDeviceCode && x.TaskState == TaskStatusEnum.Line_Execute.ObjToInt() && x.WarehouseId == warehouseDevice.WarehouseId);
                                if (task != null)
                                {
                                    _taskService.UpdateTask(task, TaskStatusEnum.Line_Executing);
 
                                    device.SetValue(W_ConveyorLineDB.TaskNum, task.TaskNum, item.StationCode);
                                    device.SetValue(W_ConveyorLineDB.StartPos, task.CurrentAddress, item.StationCode);
                                    device.SetValue(W_ConveyorLineDB.EndPos, task.NextAddress, item.StationCode);
                                    device.SetValue(W_ConveyorLineDB.Spare1, ConveyorWorkTypeEnum.Outbound.ObjToInt(), item.StationCode);
                                    device.SetValue(W_ConveyorLineDB.STB, true, item.StationCode);
                                }
                                //{
                                //    task = _taskRepository.QueryFirst(x => x.CurrentAddress == item.StationCode && _taskService.TaskInboundTypes.Contains(x.TaskType) && string.IsNullOrEmpty(x.DeviceCode) && x.TaskState == TaskStatusEnum.New.ObjToInt() && x.WarehouseId == warehouseDevice.WarehouseId);
                                //    if (task != null)
                                //    {
                                //        Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.StationCode == task.CurrentAddress);
                                //        if (stationManger != null)
                                //        {
                                //            _taskService.UpdateTask(task, TaskStatusEnum.SC_Execute, deviceCode: stationManger.StackerCraneCode, currentAddress: stationManger.StackerCraneStationCode, nextAddress: task.TargetAddress);
                                //        }
                                //    }
                                //}
                                //else
                            }
                            else if (!conveyorLineSignalRead.STB && conveyorLineSignalRead.ACK && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && conveyorLineSignalWrite.STB && !conveyorLineSignalWrite.ACK)
                            {
                                device.SetValue(W_ConveyorLineDB.STB, false, item.StationCode);
                            }
                        }
                        //else if (item.StationType == StationTypeEnum.StationType_InStartAndOutEnd.ObjToInt())
                        //{
                        //    if (!conveyorLineSignalWrite.ACK && conveyorLineSignalRead.STB && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm)
                        //    {
                        //        if (conveyorLineInfoRead.TaskNum == 0 && !string.IsNullOrEmpty(conveyorLineInfoRead.Barcode))//退料入库
                        //        {
 
                        //        }
                        //        else//出库完成
                        //        {
 
                        //        }
                        //    }
                        //    else if (conveyorLineSignalWrite.STB && conveyorLineSignalRead.ACK && !conveyorLineStatus.Alarm)
                        //    {
                        //        device.SetValue(W_ConveyorLineDB.STB, false, item.StationCode);
                        //    }
                        //    else if (!conveyorLineSignalRead.STB && conveyorLineSignalWrite.ACK && !conveyorLineStatus.Alarm)
                        //    {
                        //        device.SetValue(W_ConveyorLineDB.ACK, false, item.StationCode);
                        //    }
                        //}
                    }
                    else
                    {
                        WriteError(device.DeviceName, $"未找到设备子编号{item.StationCode}的协议信息");
                    }
                }
            }
 
            return Task.CompletedTask;
        }
    }
}