Admin
4 天以前 bd6818fc9d40f343547bafca0743658f3c0379dc
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
using System;
using System.Collections.Generic;
using System.Text;
using WIDESEA_WCS.WCSClient;
using WIDESEA_Entity.DomainModels;
using WIDESEA_Services.Repositories;
using System.Linq;
using System.Threading;
using WIDESEA_Core.Utilities;
using System.Diagnostics;
using Quartz.Impl;
using WIDESEA_Common.CutomerModel;
using WIDESEA_Common;
using WIDESEA_Services.IRepositories;
using HslCommunication.WebSocket;
using HslCommunication;
using WIDESEA_Services;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Common.LogEnum;
using WIDESEA_Services.Services;
using WIDESEA_Core.Services;
using WIDESEA_Core.Enums;
 
namespace WIDESEA_WCS
{
    public class WCSService
    {
 
        /// <summary>
        /// PLC连接集合
        /// </summary>
        public static List<PLCClient> Clients;
 
        /// <summary>
        /// 调度中心
        /// </summary>
        public static ISchedulerCenterServer Scheduler;
 
        /// <summary>
        /// Job集合
        /// </summary>
        public static List<JobOptions> jobs = new List<JobOptions>();
 
 
        //WebServices推送监控数据
        public static WebSocketServer webServer = new WebSocketServer();
 
        /// <summary>
        /// 用来监控组盘工位的托盘RFID值
        /// </summary>
        public static string RfidValue = "";
 
        /// <summary>
        /// 当一个测量出库的任务穿梭车部分完成后,用此变量记录下该条任务的任务号
        /// </summary>
        public static string MeasureTaskNumber = "";
 
 
        //在入库工位处获取托盘码码,然后PDA定时获取,用于组盘,但是入库工位没有RFID读码器了,该函数暂时没用
        public static WebResponseContent GetBoxingRfidValue()
        {
            RfidValue = "121212";
            WebResponseContent content = new WebResponseContent();
            content.OK(data: RfidValue);
            return content;
        }
 
 
        /// <summary>
        /// 切换入库线体区的模式,正常模式和应急模式
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public static WebResponseContent ChangeInboundLineModel(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                string modelValue = saveModel.MainData["model"].ToString();
                PLCClient client = Clients.Find(r => r.PLCName == "LineDevice");
 
                if (client.IsConnected)
                {
                    int modelResult = int.Parse(client.ReadValue(CLineInfoDBName.R_System_Inline_Run_Model.ToString(), "System").ToString());
                    if (modelResult != 3)
                        return content.Error("当前非联机模式,不能切换模式.");
 
                    if (modelValue == "normal")
                    {
                        bool writeModelResult = client.WriteValue(CLineInfoDBName.W_System_Normal_Model.ToString(), 1);
                        bool writeModelResult1 = client.WriteValue(CLineInfoDBName.W_System_Emery_Model.ToString(), 0);
                        if (!writeModelResult || !writeModelResult1)
                        {
                            content.Error("切换到正常模式失败");
                        }
                        else
                        {
                            Thread.Sleep(666);
                            for (int i = 0; i < 5; i++)
                            {
                                bool logicResult = bool.Parse(client.ReadValue(CLineInfoDBName.R_System_Normal_Model.ToString(), "System").ToString());
                                if (logicResult)
                                    return content.OK("切换到正常模式成功");
                                else
                                    Thread.Sleep(88);
                            }
                            return content.Error("当前线体切换到正常模式失败");
                        }
                    }
                    else if (modelValue == "emerge")
                    {
                        bool writeModelResult = client.WriteValue(CLineInfoDBName.W_System_Emery_Model.ToString(), 1);
                        bool writeModelResult1 = client.WriteValue(CLineInfoDBName.W_System_Normal_Model.ToString(), 0);
                        if (!writeModelResult || !writeModelResult1)
                            content.Error("切换到应急模式失败");
                        else
                        {
                            Thread.Sleep(666);
                            for (int i = 0; i < 5; i++)
                            {
                                bool logicResult = bool.Parse(client.ReadValue(CLineInfoDBName.R_System_Emery_Model.ToString(), "System").ToString());
                                if (logicResult)
                                    return content.OK("切换到应急模式成功");
                                else
                                    Thread.Sleep(88);
                            }
                            return content.Error("当前线体切换到应急模式失败");
 
                            //bool logicResult = bool.Parse(client.ReadValue(CLineInfoDBName.R_System_Emery_Model.ToString(), "System").ToString());
                            ////bool leftState = bool.Parse(client.ReadValue(CLineInfoDBName.R_System_LeftState.ToString(), "System").ToString());
                            ////bool rightState = bool.Parse(client.ReadValue(CLineInfoDBName.R_System_RightState.ToString(), "System").ToString());
                            //if (logicResult)
                            //{
                            //    content.OK("切换到应急模式成功");
                            //}
                            //else
                            //    content.Error("当前线体不允许切换到应急模式");
                        }
                    }
                }
                else
                {
                    content.Error("当前与PLC断开连接,请检查网络");
                }
 
                return content;
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
 
 
        /// <summary>
        /// 获取入库线体当前模式状态
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public static WebResponseContent GetInboundLineCurrentModel(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            Dictionary<string, string> resultDic = new Dictionary<string, string>();
            try
            {
                PLCClient client = Clients.Find(r => r.PLCName == "LineDevice");
                if (client.IsConnected)
                {
                    bool modelResult1 = bool.Parse(client.ReadValue(CLineInfoDBName.R_System_Normal_Model.ToString(), "System").ToString());
                    if (modelResult1)
                        resultDic.Add("normal", "1");
                    else
                        resultDic.Add("normal", "0");
 
 
                    Thread.Sleep(300);
                    modelResult1 = bool.Parse(client.ReadValue(CLineInfoDBName.R_System_Emery_Model.ToString(), "System").ToString());
 
                    if (modelResult1)
                    {
                        resultDic.Add("emerge", "1");
                        //false是未停用
                        bool leftState = bool.Parse(client.ReadValue(CLineInfoDBName.R_System_LeftState.ToString(), "System").ToString());
                        if (!leftState)
                        {
                            resultDic.Add("hoisterState", "left");
                        }
                        else
                        {
                            bool rightState = bool.Parse(client.ReadValue(CLineInfoDBName.R_System_RightState.ToString(), "System").ToString());
                            if (!rightState)
                                resultDic.Add("hoisterState", "right");
                            else
                                resultDic.Add("hoisterState", "empty");
                        }
                    }
                    else
                    {
                        resultDic.Add("emerge", "0");
                        resultDic.Add("hoisterState", "empty");
                    }
 
 
                    Thread.Sleep(300);
                    int val = int.Parse(client.ReadValue(CLineInfoDBName.R_System_Inline_Run_Model.ToString(), "System").ToString());
                    if (val == 1)
                        resultDic.Add("controlModel", "手动控制");
                    else if (val == 2)
                        resultDic.Add("controlModel", "单机控制");
                    else if (val == 3)
                        resultDic.Add("controlModel", "联机控制");
                    else
                        resultDic.Add("controlModel", "未知状态");
                }
                else
                {
                    content.Error("当前与PLC断开连接,请检查网络");
                }
 
                content.OK(data: resultDic);
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
 
 
        /// <summary>
        /// 当校验托盘码不一致时,人工触发继续任务
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public static WebResponseContent CheckBarcodeSameOk(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            Dictionary<string, string> resultDic = new Dictionary<string, string>();
            try
            {
                PLCClient client = Clients.Find(r => r.PLCName == "LineDevice");
                if (client.IsConnected)
                {
                    string taskNumber = saveModel.MainData["taskNumber"].ToString();
                    string station = saveModel.MainData["station"].ToString();
                    string barcode = saveModel.MainData["systemRFID"].ToString();
                    if (string.IsNullOrEmpty(taskNumber) || string.IsNullOrEmpty(station) || string.IsNullOrEmpty(barcode))
                    {
                        content.Error("当前无需操作!");
                        return content;
                    }
 
                    Dt_TaskWCSinfo inWcsInfo = Dt_TaskWCSinfoRepository.Instance.FindFirst(x =>
                                x.wcstask_taskNumber == int.Parse(taskNumber) &&
                                x.wcstask_barcode == barcode);
                    if (inWcsInfo != null)
                    {
                        bool goOn = client.WriteValue(CLineInfoDBName.W_Line_RFID_Done.ToString(), station, 1);
                        if (goOn)
                        {
                            content.OK();
                        }
                        else
                        {
                            content.Error("给PLC下发继续任务指令失败,请重试");
                        }
                    }
                    else
                    {
                        content.Error($"系统检测没有检测到该工位:【{station}】对应的任务");
                    }
                }
                else
                {
                    content.Error("当前与PLC断开连接,请检查网络");
                }
 
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
 
 
        /// <summary>
        /// 称重后,由人工继续任务
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public static WebResponseContent WeightCheckOk(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            Dictionary<string, string> resultDic = new Dictionary<string, string>();
            try
            {
                PLCClient client = Clients.Find(r => r.PLCName == "LineDevice");
                if (client.IsConnected)
                {
                    string barcode = saveModel.MainData["barcode"].ToString();
                    if (string.IsNullOrEmpty(barcode))
                    {
                        content.Error("当前无需操作!");
                        return content;
                    }
 
                    Dt_TaskWCSinfo inWcsInfo = Dt_TaskWCSinfoRepository.Instance.FindFirst(x =>
                                (x.wcstask_type == TaskType.TaskType_Empty_Pallet_Inbound.ToString() ||
                                x.wcstask_type == TaskType.TaskType_Box_Pallet_Inbound.ToString()) &&
                                x.wcstask_state == TaskState.TaskState_ConveyorLineExecuting.ToString() &&
                                x.wcstask_barcode == barcode);
                    if (inWcsInfo != null)
                    {
                        bool goOn = client.WriteValue(CLineInfoDBName.W_Line_Weight_OK.ToString(), "20201", 1);
                        if (goOn)
                        {
                            content.OK();
                        }
                        else
                        {
                            content.Error("给PLC下发继续任务指令失败,请重试");
                        }
                    }
                    else
                    {
                        content.Error($"系统检测没有检测到该工位:【20201】对应的任务");
                    }
                }
                else
                {
                    content.Error("当前与PLC断开连接,请检查网络");
                }
 
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
 
 
 
        /// <summary>
        /// WCS接收RGV上报的任务状态
        /// </summary>
        /// <param name="taskModel">任务信息</param>
        /// <returns></returns>
        public static Dictionary<string, object> WCSReceiveRGVData(Dictionary<string, object> statusData)
        {
            WebResponseContent content = new WebResponseContent();
            Dictionary<string, object> resultDic = new Dictionary<string, object>();
            try
            {
                //任务编号
                string taskId = statusData["taskId"]?.ToString();
                //小车编号
                string rgvId = statusData["rgvId"]?.ToString();
                //任务状态
                int taskStatus = int.Parse(statusData["status"].ToString());
 
                LogRecord.WriteLog(LogEnum.RGV, $"接受到RGV上报的任务状态:任务号:{taskId},小车编号:{rgvId},任务状态:{taskStatus}");
                DateTime now = DateTime.Now;
                //在此根据上报的任务号,更新任务状态以及任务完成后的相关操作
                Dt_TaskRGVinfo taskRGVinfo = Dt_TaskRGVinfoRepository.Instance.FindFirst(x => x.rgvtask_taskId == taskId);
                if (taskRGVinfo == null)
                {
                    resultDic.Add("Code", "0");
                    resultDic.Add("Message", "成功");
                    resultDic.Add("data ", null);
                }
                else
                {
                    Dt_TaskWCSinfo taskWCSinfo = Dt_TaskWCSinfoRepository.Instance.FindFirst(x => x.wcstask_taskNumber == taskRGVinfo.rgvtask_wcsTaskNumber);
                    if (taskWCSinfo != null)
                    {
                        //RGV上报对应的任务正常完成
                        if (taskStatus == 1)
                        {
                            taskRGVinfo.rgvtask_backup_1 = rgvId;
                            content = CommonFunction.FinishedRgvTask(taskRGVinfo, rgvId);
                            if (content.Status)
                            {
                                if (taskRGVinfo.rgvtask_areaCode == "OutboundArea")
                                {
                                    PLCClient client = WCSService.Clients.Find(r => r.PLCName == "LineDevice");
                                    //写入AGV执行完成
                                    Jobs.ConveyorLine.OutboundArea.OutboundAreaDispatch.WriteRGVState(client, true);
                                }
                                content.OK("RGV上报完成穿梭车任务成功");
                            }
                            else
                                content.Error($"RGV上报完成穿梭车任务出错:" + content.Message);
                        }//取放货完成
                        else if (taskStatus == 2)
                        {
                            taskRGVinfo.rgvtask_backup_2 = "2";
                            Dt_TaskRGVinfoRepository.Instance.Update(taskRGVinfo, r => r.rgvtask_backup_2, true);
                            content.OK("RGV上报取放货完成成功");
                        }
                        else
                            content.Error($"RGV上报了任务状态,但是状态不是1,表示非正常完成");
                    }
                    else
                    {
                        CommonFunction.AddRgvTaskToHistory(taskRGVinfo, Dt_TaskRGVinfoRepository.Instance, Dt_TaskRGVinfo_HtyRepository.Instance);
                        content.OK($"RGV上报了任务状态,但是在WCS任务表中没有找到对应的任务,任务号:【{taskId}】");
                    }
 
                    if (content.Status)
                    {
                        resultDic.Add("Code", "0");
                        resultDic.Add("Message", "成功");
                        resultDic.Add("data ", null);
                    }
                    else
                    {
                        resultDic.Add("Code", "-1");
                        resultDic.Add("Message", "失败");
                        resultDic.Add("data ", content.Message);
                    }
                    DateTime end = DateTime.Now;
                    // Console.Out.WriteLine($"状态:{taskStatus}耗时:{end - now}");
                }
            }
            catch (Exception ex)
            {
                resultDic.Add("Code", "-1");
                resultDic.Add("Message", "失败");
                resultDic.Add("data ", ex.Message);
            }
            //Logger.AddLog(LoggerType.RGVUpload, statusData, resultDic, content);
            return resultDic;
        }
 
 
 
        /// <summary>
        /// WCS查询提升机状态信息
        /// </summary>
        /// <param name="taskModel">任务信息</param>
        /// <returns></returns>
        public static Dictionary<string, object> GetElevatorInfo()
        {
            Dictionary<string, object> resultDic = new Dictionary<string, object>();
            try
            {
                //提升机层
                PLCClient client = WCSService.Clients.Find(r => r.PLCName == "LineDevice");
                string hoisterLayer = client.ReadValue(CLineInfoDBName.R_Line_Layer.ToString(), "90100").ToString();
                resultDic.Add("code", "1");
                resultDic.Add("layer", hoisterLayer);
                resultDic.Add("status ", "提升机当前状态");
                //Console.Out.WriteLine(DateTime.Now + "提升机当前状态" + hoisterLayer);
            }
            catch (Exception ex)
            {
                resultDic.Add("code", "0");
                resultDic.Add("layer", "-1");
                resultDic.Add("status ", "获取提升机状态失败" + ex.Message);
            }
 
            return resultDic;
        }
    }
}