wanshenmean
2026-02-05 c388f1acccd0b7e76a4366fbaddaca6551826b74
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
#region << °æ ±¾ ×¢ ÊÍ >>
 
/*----------------------------------------------------------------
 * ÃüÃû¿Õ¼ä£ºWIDESEAWCS_Tasks.ConveyorLineJob
 * ´´½¨ÕߣººúͯÇì
 * ´´½¨Ê±¼ä£º2024/8/2 16:13:36
 * °æ±¾£ºV1.0.0
 * ÃèÊö£º
 *
 * ----------------------------------------------------------------
 * ÐÞ¸ÄÈË£º
 * ÐÞ¸Äʱ¼ä£º
 * °æ±¾£ºV1.0.1
 * ÐÞ¸Ä˵Ã÷£º
 *
 *----------------------------------------------------------------*/
 
#endregion << °æ ±¾ ×¢ ÊÍ >>
 
using AutoMapper;
using System.Data;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.Service;
 
namespace WIDESEAWCS_Tasks
{
    public class ConveyorLineDispatchHandler
    {
        private readonly ITaskService _taskService;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly IRouterService _routerService;
        private readonly IMapper _mapper;
 
        public ConveyorLineDispatchHandler(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IMapper mapper)
        {
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _routerService = routerService;
            _mapper = mapper;
        }
 
        /// <summary>
        /// ÐÄÌø´¦Àí
        /// </summary>
        /// <param name="conveyorLine"></param>
        /// <param name="command"></param>
        /// <param name="childDeviceCode"></param>
        public void HeartBeat(CommonConveyorLine conveyorLine, ConveyorLineTaskCommandNew command, string childDeviceCode)
        {
            //ÐÄÌø´¦ÀíÂß¼­
            conveyorLine.SetValue(ConveyorLineDBNameNew.TaskNo, 0, childDeviceCode);
        }
 
        /// <summary>
        /// ÊäËÍÏßÇëÇóÈë¿â
        /// </summary>
        /// <param name="conveyorLine">ÊäËÍÏßʵÀý¶ÔÏó</param>
        /// <param name="command">¶ÁÈ¡µÄÇëÇóÐÅÏ¢</param>
        /// <param name="childDeviceCode">×ÓÉ豸±àºÅ</param>
        public void RequestInbound(CommonConveyorLine conveyorLine, ConveyorLineTaskCommandNew command, string childDeviceCode)
        {
            if (_taskService.RequestWMSTask(command.Barcode, childDeviceCode).Status)
            {
                Dt_Task task = _taskService.QueryConveyorLineTask(conveyorLine.DeviceCode, childDeviceCode);
                if (task != null)
                {
                    ConveyorLineTaskCommandNew taskCommand = _mapper.Map<ConveyorLineTaskCommandNew>(task);
                    taskCommand.WCS_ACK = command.WCS_ACK;
                    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, ConveyorLineTaskCommandNew command, string childDeviceCode)
        {
            Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.TaskNo, childDeviceCode);
            if (task != null)
            {
                const string ConstraintMachineName = "¾ÐÊø»ú";
                const string PinMachineName = "²å°Î¶¤»ú";
 
                var devices = Storage.Devices;
 
                if (string.Equals(task.NextAddress, ConstraintMachineName, StringComparison.Ordinal))
                {
                    ConstraintMachine? constraint = devices.OfType<ConstraintMachine>().FirstOrDefault(d => d.DeviceName == ConstraintMachineName);
                    if (constraint == null)
                    {
                        // ´¦Àí processing Îª¿ÕµÄÇé¿ö£¨¿É¸ù¾Ýʵ¼ÊÒµÎñÐèÇóÌí¼Ó´¦ÀíÂß¼­£©
                        return;
                    }
 
                    ProcessDeviceRequest(conveyorLine, constraint, childDeviceCode,
                    () => constraint.GetValue<ConstraintMachineDBName, bool>(ConstraintMachineDBName.MaterialRequestUpper),
                    () => constraint.GetValue<ConstraintMachineDBName, bool>(ConstraintMachineDBName.OutputRequestUpper),
                    outputReq => constraint.SetValue(ConstraintMachineDBName.ConstraintTrayOutputReadyUpper, outputReq ? 1 : 0));
                }
                else if (string.Equals(task.NextAddress, PinMachineName, StringComparison.Ordinal))
                {
                    PinMachine? pinMachine = devices.OfType<PinMachine>().FirstOrDefault(d => d.DeviceName == PinMachineName);
                    if (pinMachine == null)
                    {
                        // ´¦Àí pinMachine Îª¿ÕµÄÇé¿ö£¨¿É¸ù¾Ýʵ¼ÊÒµÎñÐèÇóÌí¼Ó´¦ÀíÂß¼­£©
                        return;
                    }
 
                    ProcessDeviceRequest(conveyorLine, pinMachine, childDeviceCode,
                    () => pinMachine.GetValue<PinMachineDBName, bool>(PinMachineDBName.MaterialRequestUpper),
                    () => pinMachine.GetValue<PinMachineDBName, bool>(PinMachineDBName.OutputRequestUpper),
                    outputReq => pinMachine.SetValue(PinMachineDBName.PlugPinTrayOutputReadyUpper, outputReq ? 1 : 0));
                }
 
                Dt_Task? newTask = _taskService.UpdatePosition(task.TaskNum, task.CurrentAddress);
                //if (newTask != null)
                //{
                //    _taskService.UpdateTaskStatusToNext(task);
                //}
            }
        }
 
        /// <summary>
        /// ÊäËÍÏßÈë¿âÍê³É
        /// </summary>
        /// <param name="conveyorLine">ÊäËÍÏßʵÀý¶ÔÏó</param>
        /// <param name="command">¶ÁÈ¡µÄÇëÇóÐÅÏ¢</param>
        /// <param name="childDeviceCode">×ÓÉ豸±àºÅ</param>
        public void ConveyorLineInFinish(CommonConveyorLine conveyorLine, ConveyorLineTaskCommandNew command, string childDeviceCode)
        {
            Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.TaskNo, childDeviceCode);
            if (task != null)
            {
                conveyorLine.SetValue(ConveyorLineDBNameNew.WCS_ACK, 1, 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, ConveyorLineTaskCommandNew command, string childDeviceCode)
        {
            Dt_Task task = _taskService.QueryConveyorLineTask(conveyorLine.DeviceCode, childDeviceCode);
            if (task != null)
            {
                ConveyorLineTaskCommandNew taskCommand = _mapper.Map<ConveyorLineTaskCommandNew>(task);
                taskCommand.WCS_ACK = command.WCS_ACK;
                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, ConveyorLineTaskCommandNew command, string childDeviceCode)
        {
            Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.TaskNo, childDeviceCode);
            if (task != null)
            {
                const string ConstraintMachineName = "¾ÐÊø»ú";
                const string PinMachineName = "²å°Î¶¤»ú";
 
                var devices = Storage.Devices;
 
                if (string.Equals(task.NextAddress, ConstraintMachineName, StringComparison.Ordinal))
                {
                    ConstraintMachine? constraint = devices.OfType<ConstraintMachine>().FirstOrDefault(d => d.DeviceName == ConstraintMachineName);
                    if (constraint == null)
                    {
                        // ´¦Àí processing Îª¿ÕµÄÇé¿ö£¨¿É¸ù¾Ýʵ¼ÊÒµÎñÐèÇóÌí¼Ó´¦ÀíÂß¼­£©
                        return;
                    }
                    ProcessDeviceRequest(conveyorLine, constraint, childDeviceCode,
                    () => constraint.GetValue<ConstraintMachineDBName, bool>(ConstraintMachineDBName.MaterialRequestLower),
                    () => constraint.GetValue<ConstraintMachineDBName, bool>(ConstraintMachineDBName.OutputRequestLower),
                    outputReq => constraint.SetValue(ConstraintMachineDBName.ConstraintTrayOutputReadyLower, outputReq ? 1 : 0));
                }
                else if (string.Equals(task.NextAddress, PinMachineName, StringComparison.Ordinal))
                {
                    PinMachine? pinMachine = devices.OfType<PinMachine>().FirstOrDefault(d => d.DeviceName == PinMachineName);
                    if (pinMachine == null)
                    {
                        // ´¦Àí pinMachine Îª¿ÕµÄÇé¿ö£¨¿É¸ù¾Ýʵ¼ÊÒµÎñÐèÇóÌí¼Ó´¦ÀíÂß¼­£©
                        return;
                    }
                    ProcessDeviceRequest(conveyorLine, pinMachine, childDeviceCode,
                    () => pinMachine.GetValue<PinMachineDBName, bool>(PinMachineDBName.MaterialRequestLower),
                    () => pinMachine.GetValue<PinMachineDBName, bool>(PinMachineDBName.OutputRequestLower),
                    outputReq => pinMachine.SetValue(PinMachineDBName.PlugPinTrayOutputReadyLower, outputReq ? 1 : 0));
                }
 
                Dt_Task? newTask = _taskService.UpdatePosition(task.TaskNum, task.CurrentAddress);
            }
        }
 
        /// <summary>
        /// ÊäËÍÏß³ö¿âÍê³É
        /// </summary>
        /// <param name="conveyorLine">ÊäËÍÏßʵÀý¶ÔÏó</param>
        /// <param name="command">¶ÁÈ¡µÄÇëÇóÐÅÏ¢</param>
        /// <param name="childDeviceCode">×ÓÉ豸±àºÅ</param>
        public void ConveyorLineOutFinish(CommonConveyorLine conveyorLine, ConveyorLineTaskCommandNew command, string childDeviceCode)
        {
            Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.TaskNo, childDeviceCode);
            if (task != null)
            {
                conveyorLine.SetValue(ConveyorLineDBNameNew.WCS_ACK, 1, childDeviceCode);
                WebResponseContent content = _taskService.UpdateTaskStatusToNext(task);
                Console.Out.WriteLine(content.Serialize());
            }
        }
 
        /// <summary>
        /// Èë¿â¾ÐÊø»úÇëÇó´¦Àí
        /// </summary>
        /// <param name="conveyorLine"></param>
        /// <param name="command"></param>
        /// <param name="constraint"></param>
        /// <param name="childDeviceCode"></param>
        public void InboundConstraintMachineRequest(CommonConveyorLine conveyorLine, ConveyorLineTaskCommandNew command, ConstraintMachine? constraint, string childDeviceCode)
        {
            //Èë¿â¾ÐÊø»úÇëÇó´¦ÀíÂß¼­
            bool materialReq = constraint.GetValue<ConstraintMachineDBName, bool>(ConstraintMachineDBName.MaterialRequestUpper);
            bool outputReq = constraint.GetValue<ConstraintMachineDBName, bool>(ConstraintMachineDBName.OutputRequestUpper);
 
            if (materialReq)
            {
                conveyorLine.SetValue(ConveyorLineDBNameNew.Target, 1, childDeviceCode);
                conveyorLine.SetValue(ConveyorLineDBNameNew.WCS_ACK, 1, childDeviceCode);
            }
            else
            {
                constraint.SetValue(ConstraintMachineDBName.ConstraintTrayOutputReadyUpper, outputReq ? 1 : 0);
            }
        }
 
        /// <summary>
        /// Èë¿â²å°Î¶¤»úÇëÇó´¦Àí
        /// </summary>
        /// <param name="conveyorLine"></param>
        /// <param name="command"></param>
        /// <param name="pinMachine"></param>
        /// <param name="childDeviceCode"></param>
        public void InboundPinMachineRequest(CommonConveyorLine conveyorLine, ConveyorLineTaskCommandNew command, PinMachine? pinMachine, string childDeviceCode)
        {
            //Èë¿â²å°Î¶¤»úÇëÇó´¦ÀíÂß¼­
            bool materialReq = pinMachine.GetValue<PinMachineDBName, bool>(PinMachineDBName.MaterialRequestUpper);
            bool outputReq = pinMachine.GetValue<PinMachineDBName, bool>(PinMachineDBName.OutputRequestUpper);
            if (materialReq)
            {
                conveyorLine.SetValue(ConveyorLineDBNameNew.Target, 1, childDeviceCode);
                conveyorLine.SetValue(ConveyorLineDBNameNew.WCS_ACK, 1, childDeviceCode);
            }
            else
            {
                pinMachine.SetValue(PinMachineDBName.PlugPinTrayOutputReadyUpper, outputReq ? 1 : 0);
            }
        }
 
        /// <summary>
        /// ³ö¿â¾ÐÊø»úÇëÇó´¦Àí
        /// </summary>
        /// <param name="conveyorLine"></param>
        /// <param name="command"></param>
        /// <param name="constraint"></param>
        /// <param name="childDeviceCode"></param>
        public void OutConstraintMachineRequest(CommonConveyorLine conveyorLine, ConveyorLineTaskCommandNew command, ConstraintMachine? constraint, string childDeviceCode)
        {
            //³ö¿â¾ÐÊø»úÇëÇó´¦ÀíÂß¼­
 
            bool materialReq = constraint.GetValue<ConstraintMachineDBName, bool>(ConstraintMachineDBName.MaterialRequestLower);
            bool outputReq = constraint.GetValue<ConstraintMachineDBName, bool>(ConstraintMachineDBName.OutputRequestLower);
 
            if (materialReq)
            {
                conveyorLine.SetValue(ConveyorLineDBNameNew.Target, 1, childDeviceCode);
                conveyorLine.SetValue(ConveyorLineDBNameNew.WCS_ACK, 1, childDeviceCode);
            }
            else
            {
                constraint.SetValue(ConstraintMachineDBName.ConstraintTrayOutputReadyLower, outputReq ? 1 : 0);
            }
        }
 
        /// <summary>
        /// ³ö¿â²å°Î¶¤»úÇëÇó´¦Àí
        /// </summary>
        /// <param name="conveyorLine"></param>
        /// <param name="command"></param>
        /// <param name="pinMachine"></param>
        /// <param name="childDeviceCode"></param>
        public void OutPinMachineRequest(CommonConveyorLine conveyorLine, ConveyorLineTaskCommandNew command, PinMachine? pinMachine, string childDeviceCode)
        {
            //³ö¿â²å°Î¶¤»úÇëÇó´¦ÀíÂß¼­
            bool materialReq = pinMachine.GetValue<PinMachineDBName, bool>(PinMachineDBName.MaterialRequestLower);
            bool outputReq = pinMachine.GetValue<PinMachineDBName, bool>(PinMachineDBName.OutputRequestLower);
            if (materialReq)
            {
                conveyorLine.SetValue(ConveyorLineDBNameNew.Target, 1, childDeviceCode);
                conveyorLine.SetValue(ConveyorLineDBNameNew.WCS_ACK, 1, childDeviceCode);
            }
            else
            {
                pinMachine.SetValue(PinMachineDBName.PlugPinTrayOutputReadyLower, outputReq ? 1 : 0);
            }
        }
 
        /// <summary>
        /// Í¨ÓõÄÉ豸ÇëÇó´¦Àí·½·¨
        /// </summary>
        private void ProcessDeviceRequest<T>(CommonConveyorLine conveyorLine, T device, string childDeviceCode,
            Func<bool> getMaterialRequest, Func<bool> getOutputRequest, Action<bool> setOutputReady)
        {
            bool materialReq = getMaterialRequest();
            bool outputReq = getOutputRequest();
 
            if (materialReq)
            {
                conveyorLine.SetValue(ConveyorLineDBNameNew.Target, 1, childDeviceCode);
                conveyorLine.SetValue(ConveyorLineDBNameNew.WCS_ACK, 1, childDeviceCode);
            }
            else
            {
                setOutputReady(outputReq);
            }
        }
    }
}