1
wankeda
2025-04-11 91b610a8cf7ad7a4e52486d3ff904df229873b23
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
#region << 版 本 注 释 >>
 
/*----------------------------------------------------------------
 * 命名空间:WIDESEAWCS_Tasks.ConveyorLineJob
 * 创建者:胡童庆
 * 创建时间:2024/8/2 16:13:36
 * 版本:V1.0.0
 * 描述:
 *
 * ----------------------------------------------------------------
 * 修改人:
 * 修改时间:
 * 版本:V1.0.1
 * 修改说明:
 *
 *----------------------------------------------------------------*/
 
#endregion << 版 本 注 释 >>
 
using AutoMapper;
using Quartz;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.Service;
using WIDESEAWCS_Tasks.ConveyorLineJob;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class CommonConveyorLineJob : JobBase, IJob, IDisposable
    {
        private readonly ITaskService _taskService;
        private readonly ITaskRepository _taskRepository;
        private readonly ITaskCZRepository _taskCZRepository;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly IRouterService _routerService;
        private readonly IMapper _mapper;
 
        public CommonConveyorLineJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IMapper mapper, ITaskRepository taskRepository, ITaskCZRepository taskCZRepository)
        {
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _routerService = routerService;
            _mapper = mapper;
            _taskRepository = taskRepository;
            _taskCZRepository = taskCZRepository;
        }
 
        public Task Execute(IJobExecutionContext context)
 
 
 
        {
            try
            {
                // 从上下文中获取 JobParams 并转换为 CommonConveyorLine 类型
                CommonConveyorLine conveyorLine = (CommonConveyorLine)context.JobDetail.JobDataMap.Get("JobParams");
                if (conveyorLine == null)
                {
                    throw new Exception("JobParams 不包含 CommonConveyorLine 类型参数");
                }
 
                // 定义线体实盘入库请求的地址和任务类型
                var requests = new Dictionary<string, string>
                {
                    { "DB1002.1093.0", "ZJXL-WLX002" },
                    { "DB1002.1493.0", "FJXL-WLX002" }
                };
 
                // 定义线体空盘回流请求的地址和任务类型
                var requestsKP = new Dictionary<string, string>
                {
                    { "DB1002.1893.0", "ZJXL-KPHLX001" },
                    { "DB1002.2293.0", "FJXL-KPHLX001" }
                };
 
                // 处理实盘入库请求
                ProcessRequests(conveyorLine, requests, "下线请求入库");
 
                // 处理空盘回流请求
                ProcessKpRequests(conveyorLine, requestsKP);
            }
            catch (Exception ex)
            {
                // 记录异常信息
                // Console.Out.WriteLine(nameof(CommonConveyorLineJob) + ":" + ex.ToString());
                ConsoleHelper.WriteErrorLine($"{nameof(CommonConveyorLineJob)}: 发生异常 - {ex.Message}");
            }
            finally
            {
                // 写调试信息
                WriteDebug("CommonConveyorLineJob", "test");
                // Console.Out.WriteLine(DateTime.Now);
            }
 
            return Task.CompletedTask;
        }
 
        private void ProcessRequests(CommonConveyorLine conveyorLine, Dictionary<string, string> requests, string requestType)
        {
            foreach (var request in requests)
            {
                var isDownRequest = conveyorLine.Communicator.Read<bool>(request.Key);
                if (!isDownRequest)
                {
                    continue;
                }
 
                string fromAdd = request.Value;
                string taskType = request.Value.Contains("ZJXL") ? "正极" : "负极";
 
                // 查询是否存在已生成的新任务
                var task = _taskRepository.QueryFirst(x => x.SourceAddress == fromAdd);//&& x.TaskState == (int)TaskInStatusEnum.InNew
                if (task != null)
                {
                    ConsoleHelper.WriteInfoLine($"{nameof(CommonConveyorLineJob)}: {taskType}{requestType},任务已生成存在,稍后重试......");
                    continue;
                }
 
                // 查询任务类型对应的czTask
                DtCZTask czTask = _taskCZRepository.QueryFirst(x => x.TaskType == taskType);
                if (czTask == null)
                {
                    ConsoleHelper.WriteInfoLine($"{nameof(CommonConveyorLineJob)}: {taskType}{requestType},{taskType}任务不存在,稍后重试......");
                    continue;
                }
 
                // 创建并添加新任务到任务仓库
                task = CreateTask(fromAdd, czTask.TaskEndAddress, czTask.TaskOrderNo, czTask.TaskProductCode, taskType);
                _taskRepository.AddData(task);
                ConsoleHelper.WriteInfoLine($"{nameof(CommonConveyorLineJob)}: {taskType}{requestType},任务已生成,等待执行......");
            }
        }
 
        private void ProcessKpRequests(CommonConveyorLine conveyorLine, Dictionary<string, string> requestsKP)
        {
            foreach (var request in requestsKP)
            {
                var isDownRequest = conveyorLine.Communicator.Read<bool>(request.Key);
                if (!isDownRequest)
                {
                    continue;
                }
 
                string fromAdd = request.Value;
                string taskType = request.Value.Contains("ZJXL") ? "正极" : "负极";
 
                // 根据任务类型确定请求地址
                (string upRequest, string gmRequest, string gmState) = GetRequestAddresses(taskType);
 
                var isUpRequest = conveyorLine.Communicator.Read<bool>(upRequest);
                var isGMRequest = conveyorLine.Communicator.Read<bool>(gmRequest);
                var isGMState = conveyorLine.Communicator.Read<int>(gmState);
 
                // 根据条件创建任务对象
                Dt_Task task = null;
                if (isUpRequest)
                {
                    task = CreateTask(fromAdd, "WaitBind", "正极物流线002的上料请求", "空托盘", taskType);
                }
                else if (isGMRequest && isGMState == 1)
                {
                    task = CreateTask(fromAdd, "WaitBind", "正极物流线002的上料请求", "隔膜空托盘", taskType);
                }
 
                // 添加任务到任务表
                if (task != null)
                {
                    _taskRepository.AddData(task);
                }
            }
        }
 
        private (string upRequest, string gmRequest, string gmState) GetRequestAddresses(string taskType)
        {
            if (taskType == "正极")
            {
                // 物流线002的上料请求
                return ("DB1002.93.0", "DB1002.2493.0", "DB1002.2424.0");
            }
            else
            {
                // 物流线002的上料请求
                return ("DB1002.1293.0", "DB1002.2893.0", "DB1002.2824.0");
            }
        }
 
        // 辅助方法:创建任务对象
        private Dt_Task CreateTask(string currentAddress, string targetAddress, string remark, string palletCode, string taskType)
        {
            return new Dt_Task
            {
                TaskNum = _taskService.GetTaskNum(),
                CreateDate = DateTime.Now,
                Creater = "system",
                CurrentAddress = currentAddress,
                SourceAddress = currentAddress,
                TaskState = targetAddress != "WaitBind" ? (int)TaskInStatusEnum.InNew : (int)TaskInStatusEnum.InPending,
                TaskType = (int)TaskOutboundTypeEnum.Outbound,
                Grade = 1,
                PalletCode = palletCode,
                TargetAddress = targetAddress,
                NextAddress = targetAddress,
                Barcode = "",
                Roadway = $"{taskType}AGV",
                WMSId = 0,
                Remark = remark
            };
        }
 
        /// <summary>
        /// 输送线请求入库
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        public void RequestInbound(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode)
        {
            if (_taskService.RequestWMSTask(command.Barcode, childDeviceCode).Status)
            {
                Dt_Task task = _taskService.QueryConveyorLineTask(conveyorLine.DeviceCode, childDeviceCode);
                if (task != null)
                {
                    ConveyorLineTaskCommand taskCommand = _mapper.Map<ConveyorLineTaskCommand>(task);
                    taskCommand.InteractiveSignal = command.InteractiveSignal;
                    conveyorLine.SendCommand(taskCommand, childDeviceCode);
 
                    _taskService.UpdateTaskStatusToNext(task);
                }
            }
        }
 
        /// <summary>
        /// 输送线请求入库下一地址
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        public void RequestInNextAddress(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode)
        {
            Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.TaskNum, childDeviceCode);
            if (task != null)
            {
                Dt_Task? newTask = _taskService.UpdatePosition(task.TaskNum, task.CurrentAddress);
                if (newTask != null)
                {
                    ConveyorLineTaskCommand taskCommand = _mapper.Map<ConveyorLineTaskCommand>(newTask);
                    taskCommand.InteractiveSignal = command.InteractiveSignal;
                    conveyorLine.SendCommand(taskCommand, childDeviceCode);
                }
            }
        }
 
        /// <summary>
        /// 输送线入库完成
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        public void ConveyorLineInFinish(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode)
        {
            Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.TaskNum, childDeviceCode);
            if (task != null)
            {
                conveyorLine.SetValue(ConveyorLineDBName.WriteInteractiveSignal, 0, childDeviceCode);
                WebResponseContent content = _taskService.UpdateTaskStatusToNext(task);
                Console.Out.WriteLine(content.Serialize());
            }
        }
 
        /// <summary>
        /// 输送线请求出信息
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        public void RequestOutbound(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode)
        {
            Dt_Task task = _taskService.QueryConveyorLineTask(conveyorLine.DeviceCode, childDeviceCode);
            if (task != null)
            {
                ConveyorLineTaskCommand taskCommand = _mapper.Map<ConveyorLineTaskCommand>(task);
                taskCommand.InteractiveSignal = command.InteractiveSignal;
                conveyorLine.SendCommand(taskCommand, childDeviceCode);
 
                _taskService.UpdateTaskStatusToNext(task);
            }
        }
 
        /// <summary>
        /// 输送线请求出库下一地址
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        public void RequestOutNextAddress(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode)
        {
            Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.TaskNum, childDeviceCode);
            if (task != null)
            {
                Dt_Task? newTask = _taskService.UpdatePosition(task.TaskNum, task.CurrentAddress);
                if (newTask != null)
                {
                    ConveyorLineTaskCommand taskCommand = _mapper.Map<ConveyorLineTaskCommand>(newTask);
                    taskCommand.InteractiveSignal = command.InteractiveSignal;
                    conveyorLine.SendCommand(taskCommand, childDeviceCode);
                }
            }
        }
 
        /// <summary>
        /// 输送线出库完成
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        public void ConveyorLineOutFinish(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode)
        {
            Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.TaskNum, childDeviceCode);
            if (task != null)
            {
                conveyorLine.SetValue(ConveyorLineDBName.WriteInteractiveSignal, 0, childDeviceCode);
                WebResponseContent content = _taskService.UpdateTaskStatusToNext(task);
                Console.Out.WriteLine(content.Serialize());
            }
        }
 
        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }
    }
}