zhanghonglin
5 天以前 8f9b9411ca279670bd85fcfa7763987295ed9abf
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_Tasks.DBname;
using HslCommunication.WebSocket;
using Newtonsoft.Json;
using WIDESEAWCS_Tasks.Command;
using WIDESEAWCS_Core.BaseRepository;
using WIDESEAWCS_Model.Models.TaskInfo;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core;
using Autofac.Core;
using WIDESEAWCS_DTO.Stock;
using WIDESEAWCS_Tasks.DBName;
using WIDESEAWCS_Core.LogHelper;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class StackerCraneJob : JobBase, IJob
    {
        public static int R_HeartBeat = 0;
        public static string InorOut = "In";
        //public static CommandData CommandData = new CommandData();
        WebSocketServer _webSocketServer;
        private readonly IRepository<Dt_Task> _taskRepository;
        private readonly ITaskService _TaskService;
 
        //存储信号
        private readonly Commands comm = new Commands();
 
        public StackerCraneJob(WebSocketServer webSocketServer, IRepository<Dt_Task> taskRepository, ITaskService TaskService)
        {
            _webSocketServer = webSocketServer;
            _taskRepository = taskRepository;
            _TaskService = TaskService;
        }
 
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                Thread.Sleep(3000);
                //连接堆垛机
                CommonStackerCrane commonStackerCrane = (CommonStackerCrane)context.JobDetail.JobDataMap.Get("JobParams");
                if (commonStackerCrane != null)
                {
                    //判断信号
                    int R_TaskPhases = commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.R_TaskPhases);
                    int R_OrderNo = commonStackerCrane.GetValue<StackerCraneDBName, int>(StackerCraneDBName.R_OrderNo);
                    int R_ControlMode = commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.R_ControlMode);
                    int R_TaskStatus = commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.R_TaskStatus);
                    int R_LoadStatus = commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.R_LoadStatus);
 
                    //Commands.CommandData.R_LoadStatus
                    //信号赋值
                    comm.R_TaskPhases((short)R_TaskPhases);
                    comm.R_OrderNo(R_OrderNo);
                    comm.R_ControlMode((short)R_ControlMode);
                    comm.R_TaskStatus((short)R_TaskStatus);
                    comm.R_LoadStatus((short)R_LoadStatus);
                    comm.R_FireStatus(commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.R_FireStatus));
                    comm.R_FaultCode(commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.R_FaultCode));
 
                    //任务完成
                    if (R_TaskPhases == 5)
                    {
                        //根据设备返回的任务号查询任务
                        Dt_Task tasks = _taskRepository.QueryFirst(x => x.TaskNum == R_OrderNo);
                        if (tasks != null)
                        {
 
                            Stock result = _TaskService.TaskAccomplish(R_OrderNo);
                            if (result.MaterialCode1 != null)
                            {
                                //连接输送线
                                OtherDevice CL = (OtherDevice)Storage.Devices.Find(v => v.DeviceName == "输送线");
                                if (CL.Communicator.IsConnected)
                                {
                                    //发送物料编号
                                    CL.SetValue(ConveyorLineName.W_NumberAD, result.MaterialCode1);
                                    CL.SetValue(ConveyorLineName.W_NumberBD, result.MaterialCode2);
                                    CL.SetValue(ConveyorLineName.W_NumberCD, result.MaterialCode3);
                                    CL.SetValue(ConveyorLineName.W_NumberDD, result.MaterialCode4);
                                }
                                //commonStackerCrane.SetValue(StackerCraneDBName.W_Accomplish, true);
                            }
                        }
                    }
                    //修改任务状态
                    else if (R_TaskPhases == 1 || R_TaskPhases == 2 || R_TaskPhases == 4)
                    {
                        Dt_Task tasks = _TaskService.UpdateTaskStatus(R_OrderNo, R_TaskPhases);
                        if (tasks != null)
                        {
                            //同步WMS
                            string address = AppSettings.Get("WMSApiAddress");
                            if (!string.IsNullOrEmpty(address))
                            {
                                HttpHelper.Post($"{address}/api/Task/WCSTaskState", tasks.Serialize());
                            }
                        }
                        else
                        {
                            WriteInfo("堆垛机:", $"堆垛机修改状态找不到任务");
                        }
                    }
 
                    //判断是否为自动模式
                    if (R_ControlMode == 2 && R_TaskStatus == 2 && R_LoadStatus != 2 && (R_TaskPhases == 0 || R_TaskPhases == 5))
                    {
                        WriteInfo("堆垛机信号", $"{R_ControlMode+"||"+ R_TaskStatus + "||"+R_LoadStatus+"||"+R_TaskPhases}");
                        //判断是否有执行中的任务
                        Dt_Task tasking = _taskRepository.QueryFirst(x => (x.TaskState == (int)TaskStatusEnum.InNew && x.TaskState == (int)TaskStatusEnum.OutNew) || x.TaskState == (int)TaskStatusEnum.Task_Put || x.TaskState == (int)TaskStatusEnum.Task_fetch);
                        if (tasking == null)
                        {
                            //获取任务
                            Dt_Task? task = GetTask(InorOut);
                            if (task != null)
                            {
                                //任务实体转命令Model
                                StackerCraneCommand? StackerCommand = ConvertToStackerCommand(task);
                                if (StackerCommand != null)
                                {
                                    //发送命令
                                    commonStackerCrane.SetValue(StackerCraneDBName.W_SourceRow, StackerCommand.W_SourceRow);
                                    commonStackerCrane.SetValue(StackerCraneDBName.W_SourceColumn, StackerCommand.W_SourceColumn);
                                    commonStackerCrane.SetValue(StackerCraneDBName.W_SourceLayer, StackerCommand.W_SourceLayer);
                                    commonStackerCrane.SetValue(StackerCraneDBName.W_TargeRow, StackerCommand.W_TargeRow);
                                    commonStackerCrane.SetValue(StackerCraneDBName.W_TargeColumn, StackerCommand.W_TargeColumn);
                                    commonStackerCrane.SetValue(StackerCraneDBName.W_TargeLayer, StackerCommand.W_TargeLayer);
                                    commonStackerCrane.SetValue(StackerCraneDBName.W_TaskType, StackerCommand.W_TaskType);
                                    commonStackerCrane.SetValue(StackerCraneDBName.W_TaskMode, StackerCommand.W_TaskMode);
                                    commonStackerCrane.SetValue(StackerCraneDBName.W_DataCheckSum, StackerCommand.W_DataCheckSum);
                                    commonStackerCrane.SetValue(StackerCraneDBName.W_OrderNo, StackerCommand.W_OrderNo);
                                    commonStackerCrane.SetValue(StackerCraneDBName.W_Start, StackerCommand.W_Start);
                                    WriteInfo("堆垛机执行任务:", $"{StackerCommand.W_OrderNo}");
                                }
                            }
                        }
                    }
 
                    //心跳
                    if (R_HeartBeat == 0)
                    {
                        commonStackerCrane.SetValue(StackerCraneDBName.W_HearBeat, 1);
                        R_HeartBeat = 1;
                    }
                    else
                    {
                        commonStackerCrane.SetValue(StackerCraneDBName.W_HearBeat, 0);
                        R_HeartBeat = 0;
                    }
 
                    //WebSocket状态数据发送给前端
                    string ohtData = JsonConvert.SerializeObject(comm.CommandDatas());
                    _webSocketServer.PublishAllClientPayload(ohtData);
                }
            }
            catch (Exception ex)
            {
                //WebSocket状态数据发送给前端
                string ohtData = JsonConvert.SerializeObject(comm.CommandDatas());
                _webSocketServer.PublishAllClientPayload(ohtData);
                WriteError(nameof(StackerCraneJob), ex.Message);
            }
            return Task.CompletedTask;
        }
 
        //获取任务
        public Dt_Task? GetTask(string InorOuts)
        {
            Dt_Task task;
            task = _TaskService.TaskInorOut(InorOuts);
            if (task != null) 
            {
                //满桶出库
                if (task.TaskType == (int)TaskTypeEnum.Outfull)
                {
                    int pick = comm.GETR_PickD();
                    int status = comm.GETR_StatusD();
                    if (pick != 1 || status != 1)
                    {
                        WriteInfo("堆垛机执行满桶出库任务:", $"{"输送线出库信号异常" + pick + "//" + status}");
                        return null;
                    }
                    WriteInfo("堆垛机执行满桶出库任务:", $"{"输送线出库信号正常" + pick + "//" + status}");
                }
                //空桶出库
                else if(task.TaskType == (int)TaskTypeEnum.OutEmpty)
                {
                    int pick = comm.GETR_PickB();
                    int status = comm.GETR_StatusB();
                    if (pick != 1 || status != 1)
                    {
                        WriteInfo("堆垛机执行空桶出库任务:", $"{"输送线出库信号异常" + pick + "//" + status}");
                        return null;
                    }
                    WriteInfo("堆垛机执行空桶出库任务:", $"{"输送线出库信号正常" + pick + "//" + status}");
                }
            }
 
            //一入一出
            if (task != null && (task.TaskState == (int)TaskStatusEnum.InNew || task.TaskState == (int)TaskStatusEnum.OutNew))
            {
                if (task.TaskType == (int)TaskTypeEnum.InEmpty || task.TaskType == (int)TaskTypeEnum.Infull)
                {
                    InorOut = "Out";
                }
                else
                {
                    InorOut = "In";
                }
            }
            else
            {
                if (InorOut.Equals("In"))
                {
                    InorOut = "Out";
                }
                else
                {
                    InorOut = "In";
                }
            }
            return task;
        }
 
        //任务实体转命令Model
        public StackerCraneCommand? ConvertToStackerCommand(Dt_Task task)
        {
            StackerCraneCommand stackerCraneCommand = new StackerCraneCommand();
            string[] TargetAddress = task.TargetAddress.Split('-');
            string[] SourceAddress = task.SourceAddress.Split('-');
            //入库
            if (task.TaskType == (int)TaskTypeEnum.InEmpty || task.TaskType == (int)TaskTypeEnum.Infull)
            {
                stackerCraneCommand.W_SourceRow = (short)int.Parse(SourceAddress[0]);
                stackerCraneCommand.W_SourceColumn = (short)int.Parse(SourceAddress[1]);
                stackerCraneCommand.W_SourceLayer = (short)int.Parse(SourceAddress[2]);
                stackerCraneCommand.W_TargeRow = (short)int.Parse(TargetAddress[0]);
                stackerCraneCommand.W_TargeColumn = (short)int.Parse(TargetAddress[1]);
                stackerCraneCommand.W_TargeLayer = (short)int.Parse(TargetAddress[2]);
                stackerCraneCommand.W_TaskType = 1;
                stackerCraneCommand.W_TaskMode = 1;
                //缺少货物大小
                stackerCraneCommand.W_DataCheckSum = task.TaskNum + stackerCraneCommand.W_SourceRow + stackerCraneCommand.W_SourceColumn + stackerCraneCommand.W_SourceLayer + stackerCraneCommand.W_TargeRow + stackerCraneCommand.W_TargeColumn + stackerCraneCommand.W_TargeLayer + stackerCraneCommand.W_TaskType + stackerCraneCommand.W_TaskMode;
                stackerCraneCommand.W_OrderNo = task.TaskNum;
            }
            //出库
            else if (task.TaskType == (int)TaskTypeEnum.OutEmpty || task.TaskType == (int)TaskTypeEnum.Outfull)
            {
                stackerCraneCommand.W_SourceRow = (short)int.Parse(SourceAddress[0]);
                stackerCraneCommand.W_SourceColumn = (short)int.Parse(SourceAddress[1]);
                stackerCraneCommand.W_SourceLayer = (short)int.Parse(SourceAddress[2]);
                stackerCraneCommand.W_TargeRow = (short)int.Parse(TargetAddress[0]);
                stackerCraneCommand.W_TargeColumn = (short)int.Parse(TargetAddress[1]);
                stackerCraneCommand.W_TargeLayer = (short)int.Parse(TargetAddress[2]);
                stackerCraneCommand.W_TaskType = 1;
                stackerCraneCommand.W_TaskMode = 1;
                //缺少货物大小
                stackerCraneCommand.W_DataCheckSum = task.TaskNum + stackerCraneCommand.W_SourceRow + stackerCraneCommand.W_SourceColumn + stackerCraneCommand.W_SourceLayer + stackerCraneCommand.W_TargeRow + stackerCraneCommand.W_TargeColumn + stackerCraneCommand.W_TargeLayer + stackerCraneCommand.W_TaskType + stackerCraneCommand.W_TaskMode;
                stackerCraneCommand.W_OrderNo = task.TaskNum;
            }
            //确认下发信号
            if (task.TaskState == (int)TaskStatusEnum.InNew || task.TaskState == (int)TaskStatusEnum.OutNew)
            {
                stackerCraneCommand.W_Start = 1;
            }
            else
            {
                stackerCraneCommand.W_Start = 0;
            }
 
            return stackerCraneCommand;
        }
    }
}