wangxinhui
2026-03-17 89bccb9b8fb1070f7ac5f36510c868fecead9384
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
using Autofac.Core;
using Microsoft.AspNetCore.Components.Routing;
using Newtonsoft.Json;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using Org.BouncyCastle.Asn1.Ocsp;
using Org.BouncyCastle.Tls;
using Quartz;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Channels;
using System.Threading.Tasks;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Communicator;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO.TaskInfo;
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_TaskInfoRepository;
using WIDESEAWCS_Tasks.ConveyorLineJob;
using ICacheService = WIDESEAWCS_Core.Caches.ICacheService;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class ConveyorLineJob_CPH : 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;
        private readonly IPackaxisTaskRepository _packaxisTaskRepository;
        private readonly IPackaxisTaskService _packaxisTaskService;
        private static object lockObj = 0;//
 
        public ConveyorLineJob_CPH(ICacheService cacheService, ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IStationMangerRepository stationMangerRepository, IRouterRepository routerRepository, IRouterService routerService, IRouterExtension routerExtension, IPackaxisTaskRepository packaxisTaskRepository, IPackaxisTaskService packaxisTaskService)
        {
            _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>();
            }
            _packaxisTaskRepository = packaxisTaskRepository;
            _packaxisTaskService = packaxisTaskService;
        }
 
        public Task Execute(IJobExecutionContext context)
        {
            lock (lockObj)
            {
                if (lockObj.ToString()=="0")
                {
                    lockObj = 1;
                    bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value);
                    if (flag && value != null)
                    {
                        try
                        {
                            //获取当前设备
                            OtherDevice device = (OtherDevice)value;
                            List<Dt_StationManger> stationMangers = _stationMangerRepository.QueryData(x => x.StationDeviceCode == device.DeviceCode);
                            Task.Run(() =>
                            {
                                bool shouldRestart = true;
 
                                while (shouldRestart)
                                {
                                    try
                                    {
                                        shouldRestart = false;
                                        Dt_StationManger? RequestIn = stationMangers.FirstOrDefault(x => x.StationDeviceCode == device.DeviceCode && x.StationType == StationTypeEnum.StationType_OnlyInbound.ObjToInt());
                                        WriteInfo($"线程 {Thread.CurrentThread.ManagedThreadId}分垛口","开始执行");
                                        while (true)
                                        {
                                            //WriteInfo($"分垛口运行时间" , $"开始时间:" + DateTime.Now.ToString("HH:mm:ss.fff"));
                                            if (RequestIn != null)
                                            {
                                                DeviceProDTO? deviceProRead = device.DeviceProDTOs.Where(x => x.DeviceChildCode == RequestIn.StationCode && x.DeviceProParamType == nameof(R_CLineCPHDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
                                                DeviceProDTO? deviceProWrite = device.DeviceProDTOs.Where(x => x.DeviceChildCode == RequestIn.StationCode && x.DeviceProParamType == nameof(W_CLineCPHDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
                                                if (deviceProRead != null && deviceProWrite != null)
                                                {
                                                    R_CLineCPHInfo conveyorLineInfoRead = device.Communicator.ReadCustomer<R_CLineCPHInfo>(deviceProRead.DeviceProAddress);
                                                    //码垛环线请求任务 
                                                    if (conveyorLineInfoRead != null && (conveyorLineInfoRead.R_State == 2 || conveyorLineInfoRead.R_State == 3) && conveyorLineInfoRead.R_TaskNo <= 0 && !string.IsNullOrEmpty(conveyorLineInfoRead.R_BoxCode) && conveyorLineInfoRead.R_Request == 1)
                                                    {
                                                        //WriteInfo($"分垛口运行时间", $"{conveyorLineInfoRead.R_BoxCode}请求" + DateTime.Now.ToString("HH:mm:ss.fff"));
                                                        //判断是否任务已经存在
                                                        Dt_PackaxisTask packaxisTask = _packaxisTaskRepository.Db.Queryable<Dt_PackaxisTask>().Where(x => x.BarCode == conveyorLineInfoRead.R_BoxCode.Trim() && x.TaskState == TaskStatusEnum.Line_Executing.ObjToInt()).First();
                                                        if (packaxisTask != null)
                                                        {
                                                            //写入执行数据
                                                            device.SetValue(W_CLineCPHDB.W_TaskNo, packaxisTask.PackTaskNum, RequestIn.StationCode);
                                                            device.SetValue(W_CLineCPHDB.W_Channel, packaxisTask.NextAddress, RequestIn.StationCode);
                                                            device.SetValue(W_CLineCPHDB.W_Long, packaxisTask.PackLength, RequestIn.StationCode);
                                                            device.SetValue(W_CLineCPHDB.W_Wide, packaxisTask.PackWidth, RequestIn.StationCode);
                                                            device.SetValue(W_CLineCPHDB.W_High, packaxisTask.PackHeight, RequestIn.StationCode);
                                                            device.SetValue(W_CLineCPHDB.W_TMID, conveyorLineInfoRead.R_BoxCode.Trim(), RequestIn.StationCode);
                                                            device.SetValue(W_CLineCPHDB.W_Request, 1, RequestIn.StationCode);
                                                            //WriteInfo($"分垛口运行时间", $"{conveyorLineInfoRead.R_BoxCode}请求完成" + DateTime.Now.ToString("HH:mm:ss.fff"));
                                                        }
                                                        else
                                                        {
                                                            #region
                                                            WebResponseContent webResponse = _packaxisTaskService.ReqPackTask(conveyorLineInfoRead.R_BoxCode.Trim(), RequestIn.StationDeviceCode);
                                                            if (webResponse.Status)
                                                            {
                                                                Dt_PackaxisTask _PackaxisTask = webResponse.Data as Dt_PackaxisTask;
                                                                //写入执行数据
                                                                device.SetValue(W_CLineCPHDB.W_TaskNo, _PackaxisTask.PackTaskNum, RequestIn.StationCode);
                                                                device.SetValue(W_CLineCPHDB.W_Channel, _PackaxisTask.NextAddress.ObjToInt(), RequestIn.StationCode);
                                                                device.SetValue(W_CLineCPHDB.W_Long, _PackaxisTask.PackLength, RequestIn.StationCode);
                                                                device.SetValue(W_CLineCPHDB.W_Wide, _PackaxisTask.PackWidth, RequestIn.StationCode);
                                                                device.SetValue(W_CLineCPHDB.W_High, _PackaxisTask.PackHeight, RequestIn.StationCode);
                                                                device.SetValue(W_CLineCPHDB.W_TMID, conveyorLineInfoRead.R_BoxCode.Trim(), RequestIn.StationCode);
                                                                device.SetValue(W_CLineCPHDB.W_Request, 1, RequestIn.StationCode);
                                                                //WriteInfo($"分垛口运行时间", $"{conveyorLineInfoRead.R_BoxCode}请求完成" + DateTime.Now.ToString("HH:mm:ss.fff"));
                                                                WebResponseContent content = _taskService.MESBoxCodeNotice(conveyorLineInfoRead.R_BoxCode.Trim());
                                                                if (content.Status)
                                                                {
                                                                    WriteInfo(RequestIn.StationName, content.Message);
                                                                }
                                                                else
                                                                {
                                                                    WriteError(RequestIn.StationName, content.Message);
                                                                }
                                                            }
                                                            else
                                                            {
                                                                WriteError(RequestIn.StationName, webResponse.Message);
                                                            }
                                                            #endregion
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    WriteError(RequestIn.StationName, $"未找到设备子编号{RequestIn.StationCode}的协议信息");
                                                }
                                            }
                                            //WriteInfo($"分垛口运行时间", $"结束时间:" + DateTime.Now.ToString("HH:mm:ss.fff"));
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        WriteInfo($"线程 {Thread.CurrentThread.ManagedThreadId}分垛口", $"异常:{ex.Message}");
                                        Thread.Sleep(1500);
                                        shouldRestart = true;
                                    }
                                }
                            });
                            Task.Run(() =>
                            {
                                bool shouldRestart = true;
 
                                while (shouldRestart)
                                {
                                    try
                                    {
                                        shouldRestart = false;
                                        //获取所有码垛口
                                        List<Dt_StationManger> stationMangersMD = stationMangers.Where(x => x.StationDeviceCode == device.DeviceCode && x.StationType == StationTypeEnum.StationType_OnlyOutbound.ObjToInt()).ToList();
                                        WriteInfo($"线程 {Thread.CurrentThread.ManagedThreadId}码垛口", "开始执行");
                                        while (true)
                                        {
                                            if (stationMangersMD.Count > 0)
                                            {
                                                foreach (var item in stationMangersMD)
                                                {
                                                    DeviceProDTO? deviceProRead = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(R_CLineCPHDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
                                                    if (deviceProRead != null)
                                                    {
                                                        R_CLineCPHChannelInfo conveyorLineInfoRead = device.Communicator.ReadCustomer<R_CLineCPHChannelInfo>(deviceProRead.DeviceProAddress);
                                                        //获取码垛口任务更新任务状态
                                                        if (conveyorLineInfoRead != null && conveyorLineInfoRead.R_State == 5 && conveyorLineInfoRead.R_TaskNo > 0)
                                                        {
                                                            Dt_PackaxisTask packaxisTask = _packaxisTaskRepository.QueryFirst(x => x.PackTaskNum == conveyorLineInfoRead.R_TaskNo && x.NextAddress == item.StationCode && x.TaskState == TaskStatusEnum.Line_Executing.ObjToInt() && x.DeviceCode == item.StationDeviceCode);
                                                            if (packaxisTask != null)
                                                            {
                                                                Dt_PackaxisTask packaxisTaskOld = _packaxisTaskRepository.QueryFirst(x => x.TaskState == TaskStatusEnum.MD_Executing.ObjToInt() && x.DeviceCode == item.StackerCraneCode);
                                                                if (packaxisTaskOld != null)
                                                                {
                                                                    WriteError(item.StationName, $"码垛{item.StackerCraneCode}正执行任务{packaxisTaskOld.PackTaskNum},无法继续下发");
                                                                }
                                                                else
                                                                {
                                                                    packaxisTask.CurrentAddress = item.StationCode;
                                                                    packaxisTask.NextAddress = packaxisTask.TargetAddress;
                                                                    packaxisTask.DeviceCode = item.StackerCraneCode;
                                                                    packaxisTask.TaskState = TaskStatusEnum.MD_Executing.ObjToInt();
                                                                    _packaxisTaskRepository.UpdateData(packaxisTask);
                                                                }
                                                            }
                                                            else
                                                            {
                                                                WriteError(item.StationName, $"未找到任务{conveyorLineInfoRead.R_TaskNo}信息");
                                                            }
                                                            Thread.Sleep(2000);
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                WriteError(nameof(ConveyorLineJob_CPH), $"错误信息:未找到码垛口信息");
                                            }
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        WriteInfo($"线程 {Thread.CurrentThread.ManagedThreadId}码垛口", $"异常:{ex.Message}");
                                        Thread.Sleep(1500);
                                        shouldRestart = true;
                                    }
                                }
                            });
                            
                        }
                        catch (Exception ex)
                        {
                            lockObj = 0;
                            WriteError(nameof(ConveyorLineJob_CPH), $"错误信息:{ex.Message}");
                        }
                        
                    }
                    
                }
                
            }
            
            return Task.CompletedTask;
        }
    }
}