wanshenmean
2026-03-09 1181f9f764b14abd6e9f598f89f8507b4bbfad0d
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
#region << °æ ±¾ ×¢ ÊÍ >>
 
/*----------------------------------------------------------------
 * ÃüÃû¿Õ¼ä£ºWIDESEAWCS_Tasks.ConveyorLineJob
 * ´´½¨ÕߣººúͯÇì
 * ´´½¨Ê±¼ä£º2024/8/2 16:13:36
 * °æ±¾£ºV1.0.0
 * ÃèÊö£º
 *
 * ----------------------------------------------------------------
 * ÐÞ¸ÄÈË£º
 * ÐÞ¸Äʱ¼ä£º
 * °æ±¾£ºV1.0.1
 * ÐÞ¸Ä˵Ã÷£º
 *
 *----------------------------------------------------------------*/
 
#endregion << °æ ±¾ ×¢ ÊÍ >>
 
using AutoMapper;
using System.Data;
using WIDESEAWCS_Common.TaskEnum;
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) 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) 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)
                {
                    if (_taskService.UpdateTaskStatusToNext(newTask).Status && newTask.TaskState == (int)TaskInStatusEnum.Line_InFinish)
                    {
                        conveyorLine.SetValue(ConveyorLineDBNameNew.WCS_STB, 1, childDeviceCode);
                    }
                }
            }
        }
 
        /// <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);
 
                conveyorLine.SetValue(ConveyorLineDBNameNew.TaskNo, task.TaskNum, childDeviceCode);
                conveyorLine.SetValue(ConveyorLineDBNameNew.Barcode, task.PalletCode, childDeviceCode);
                conveyorLine.SetValue(ConveyorLineDBNameNew.Target, task.TargetAddress, childDeviceCode);
                conveyorLine.SetValue(ConveyorLineDBNameNew.WCS_ACK, 1, 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>
        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);
            }
        }
    }
}