duyongjia
2025-01-08 66820281eb452bd79735b03c3175b64cec861699
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
#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 命名空间:WIDESEAWCS_Tasks.ConveyorLineJob
 * 创建者:胡童庆
 * 创建时间:2024/8/2 16:13:36
 * 版本:V1.0.0
 * 描述:
 *
 * ----------------------------------------------------------------
 * 修改人:
 * 修改时间:
 * 版本:V1.0.1
 * 修改说明:
 * 
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
 
using AutoMapper;
using Quartz;
using System.Data;
using System.Threading.Tasks;
using WIDESEAWCS_Common.TaskEnum;
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
    {
        private readonly ITaskService _taskService;
        private readonly ITaskRepository _taskRepository;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
  
        private readonly IRouterService _routerService;
        private readonly IMapper _mapper;
 
        public CommonConveyorLineJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IMapper mapper,ITaskRepository taskRepository)
        {
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _routerService = routerService;
            _mapper = mapper;
            _taskRepository = taskRepository;
        }
 
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                CommonConveyorLine conveyorLine = (CommonConveyorLine)context.JobDetail.JobDataMap.Get("JobParams");
                if (conveyorLine != null)
                {
                    List<string> childDeviceCodes = _routerService.QueryAllPositions(conveyorLine.DeviceCode);
                    List<Task> tasks = new List<Task>();
                    ConveyorLineTaskCommand command = new ConveyorLineTaskCommand();
                    command.Barcode=conveyorLine.Communicator.Read<string>("DB801.10.0");
                    command.ISOutFinish = conveyorLine.Communicator.Read<short>("DB801.0.0");
                    command.CL1001Free = conveyorLine.Communicator.Read<short>("DB801.2.0");
                    command.CL1001Status = conveyorLine.Communicator.Read<short>("DB801.4.0");
                    command.TaskNum = conveyorLine.Communicator.Read<int>("DB801.6.0");
                    command.ISInFinish = conveyorLine.Communicator.Read<short>("DB801.42.0");
                    command.CL1002Free = conveyorLine.Communicator.Read<short>("DB801.44.0");
                    command.CL1002Status = conveyorLine.Communicator.Read<short>("DB801.46.0");
                    command.Weight = conveyorLine.Communicator.Read<short>("DB801.48.0");
                    command.CL1002Barcode = conveyorLine.Communicator.Read<string>("DB801.52.0");
                    command.ISIn = conveyorLine.Communicator.Read<short>("DB801.84.0");//等于1的时候不让出库
                    if (command != null)
                    {
                        if (!string.IsNullOrEmpty(command.CL1002Barcode.Trim()))
                        {
                            Dt_Task Intask = _taskService.QueryConveyorLineTask(command.CL1002Barcode);
                            if (Intask != null)
                            {
                                //入库处理
                                //如果入库到位,则根据托盘码(条码)去获取WCS任务号
                                if (command.ISInFinish == 1)
                                {
                                    _taskService.UpdateTaskStatus(Intask.TaskId, (int)TaskInStatusEnum.Line_InExecuting);
                                    _taskExecuteDetailService.AddTaskExecuteDetail(Intask.TaskId, $"输送线入库执行中");
                                    //入库确认信号
                                    conveyorLine.Communicator.Write<short>("DB800.6.0", 1);
                                    _taskService.UpdateTaskStatus(Intask.TaskId, (int)TaskInStatusEnum.Line_InFinish);
                                    _taskExecuteDetailService.AddTaskExecuteDetail(Intask.TaskId, $"输送线入库完成");
                                }
                            }
                            else//如果根据托盘条码查不到任务,则认为是空托盘,空托盘入库可以不需要组盘,直接新建任务,也可以直接下空托出库任务
                            {
 
                                if (command.ISInFinish == 1)//增加根据重量判断是否是空托盘判断逻辑
                                {
                                    if (command.Weight <= 600)
                                    {
                                        //先不给PLC入库确认信号,调WMS空托入库任务接口
                                        _taskService.GenerateEmptyInBoundTask(command.CL1002Barcode);
                                    }
                                    else
                                    {
                                        WriteDebug(nameof(CommonConveyorLineJob), $"生成空托入库任务时,重量超重,生成空托入库任务失败!");
                                    }
                                }
 
                            }
                        }
                        if (!string.IsNullOrEmpty(command.Barcode.Trim()))
                        {
                            Dt_Task Outtask = _taskService.QueryConveyorLineOutTask(command.Barcode);
                            if (Outtask != null)
                            {
                                //出库处理
                                //如果出库到位,出库任务完成
                                if (command.ISOutFinish == 1)
                                {
                                    _taskService.UpdateTaskStatus(Outtask.TaskId, (int)TaskOutStatusEnum.Line_OutExecuting);
                                    _taskExecuteDetailService.AddTaskExecuteDetail(Outtask.TaskId, $"输送线出库执行中");
                                    _taskService.UpdateTaskStatus(Outtask.TaskId, (int)TaskOutStatusEnum.Line_OutFinish);
                                    _taskExecuteDetailService.AddTaskExecuteDetail(Outtask.TaskId, $"输送线出库完成");
                                    //出库确认信号
                                    conveyorLine.Communicator.Write<short>("DB800.0.0", 1);
                                    _taskService.UpdateTaskStatus(Outtask.TaskId, (int)TaskOutStatusEnum.OutFinish);
                                    _taskExecuteDetailService.AddTaskExecuteDetail(Outtask.TaskId, $"出库任务完成");
                                    _taskService.OutBoundTaskByWMS(Outtask.TaskNum);
 
                                }
                            }
                        }
 
                        //Intask = _taskService.QueryConveyorLineTask(command.CL1002Barcode);
                        //Outtask = _taskService.QueryConveyorLineOutTask(command.Barcode);
                        //if (Intask==null&&Outtask==null && command.CL1001Free==1 && command.CL1002Free==1&& command.ISIn==0)
                        //{
                        //    //生成空车出库任务
                        //    _taskService.GenerateEmptyOutBoundTask();
                        //}
 
                    }
                    Task.WaitAll(tasks.ToArray());
                }
 
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine(nameof(CommonConveyorLineJob) + ":" + ex.ToString());
            }
            finally
            {
              
            }
            return Task.CompletedTask;
        }
 
 
        /// <summary>
        /// 获取任务编号
        /// </summary>
        /// <returns></returns>
        public int GetTaskNo()
        {
            DataTable dt = _taskRepository.QueryTable("select next value for dbo.seqTaskNum");
            return int.Parse(dt.Rows[0][0].ToString());
        }
 
        /// <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());
        //    }
        }
    }
}