1
czw
9 天以前 c6c6ca4fc4038aefb13ffcd8e024652c84a5b32f
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.Serialization;
using System.Collections.Concurrent;
using System.ServiceModel;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net;
using NLog;
using NLog.Config;
using NLog.Targets;
using GZ.DB.Map.OIDATABASE;
using GZ.DB.App.OIDATABASE;
using GZ.DB.Repository.OIDATABASE;
using GZ.DB.IRepository.OIDATABASE;
using GZ.DB.Entity.OIDATABASE;
namespace GZ.Projects.AuxAllWCS
{
    public partial class ViewModel : System.ComponentModel.INotifyPropertyChanged
    {
        #region 私有变量
        //集成互联
        private Conn Conn = new Conn();
        //设备通信
        private Device Device = new Device();
        //变量数据库
        private Tag tag = new Tag();
        public Tag Tag
        {
            get { return tag; }
            set { tag = value; RaisePropertyChanged(nameof(Tag)); }
        }
        //简化PLC
        public EasyPLC EasyPLC = new EasyPLC();
        #endregion
 
        #region 构造函数
        private static ViewModel _ViewModel = null;
        private static List<object> _Windows = new List<object>();
        public static ViewModel CreateInstance(object obj, Type type)
        {
            if (_ViewModel == null)
            {
                _ViewModel = new ViewModel();
            }
            if (!_Windows.Contains(obj))
            {
                _Windows.Add(obj);
            }
            return _ViewModel;
        }
        public ViewModel()
        {
 
        }
        #endregion
 
        #region 自定义指令
        public void Method1(System.Int32 param1)
        {
            #region    [自定义指令][20250323111354905][Method1]
 
            #endregion [自定义指令][20250323111354905][Method1]
        }
        public void SettingInit()
        {
            #region    [自定义指令][20250323144605974][SettingInit]
            try
            {
                var jsonFile = System.AppDomain.CurrentDomain.BaseDirectory + "/Static/config.json";
                using (System.IO.StreamReader file = System.IO.File.OpenText(jsonFile))
                {
                    using (JsonTextReader reader = new JsonTextReader(file))
                    {
                        JObject o = (JObject)JToken.ReadFrom(reader);
                        foreach (Newtonsoft.Json.Linq.JProperty keyValue in o.Properties())
                        {
                            Console.WriteLine(keyValue.Name);
                            if (keyValue.Name == "HostToAgvServerUrl")
                            {
                                Settings.HostToAgvServerUrl = keyValue.Value.ToString();
                            }
                            if (keyValue.Name == "deviceInfo")
                            {
                                Settings.deviceInfos = JsonConvert.DeserializeObject<List<deviceInfo>>(keyValue.Value.ToString());
                            }
                            if (keyValue.Name == "HKbaseUrl")
                            {
                                Settings.HKbaseUrl = keyValue.Value.ToString();
                            }
                            if (keyValue.Name == "MESbaseUrl")
                            {
                                Settings.MESbaseUrl = keyValue.Value.ToString();
                            }
                            if (keyValue.Name == "WMSbaseUrl")
                            {
                                Settings.WMSbaseUrl = keyValue.Value.ToString();
                            }
                        }
                    }
                }
                //if(!Settings.FirstG)
                //LogHelper.Info("加载配置文件信息 完成");
            }
            catch (Exception ex)
            {
                LogHelper.Error("加载配置文件失败!" + ex.Message, ex);
            }
            #endregion [自定义指令][20250323144605974][SettingInit]
        }
        public void OperateHKTaskStatus(System.Object param1)
        {
            #region    [自定义指令][20250324165239206][OperateHKTaskStatus]
            HaiKangOrderInfo model = (HaiKangOrderInfo)param1;
            if (string.IsNullOrEmpty(model.taskCode))
                return;
            var hkr = AutoThread.InvokeMethod(AutoThread.Instance, "OperateHKTaskStatus", new object[] { model });
            //return JsonConvert.SerializeObject(hkr);
            #endregion [自定义指令][20250324165239206][OperateHKTaskStatus]
        }
        public GZ.DB.Entity.OIDATABASE.TaskEntity Begin(GZ.DB.Entity.OIDATABASE.TaskEntity task)
        {
            #region    [自定义指令][20250324170811305][Begin]
            if (task.S_B_STATE.Trim() == "已推送")
            {
                task.S_B_STATE = "执行中";
                task.T_START_TIME = DateTime.Now;
            }
            return task;
            #endregion [自定义指令][20250324170811305][Begin]
        }
        public GZ.DB.Entity.OIDATABASE.TaskEntity End(GZ.DB.Entity.OIDATABASE.TaskEntity task)
        {
            #region    [自定义指令][20250324170922920][End]
            if (task != null)
            {
                task.S_B_STATE = "完成";
                task.T_END_TIME = DateTime.Now;
            }
            return task;
            #endregion [自定义指令][20250324170922920][End]
        }
        public GZ.DB.Entity.OIDATABASE.TaskEntity UpdateStatus(GZ.DB.Entity.OIDATABASE.TaskEntity task, System.String status)
        {
            #region    [自定义指令][20250324171118766][UpdateStatus]
            task.S_B_STATE = status;
            return task;
            #endregion [自定义指令][20250324171118766][UpdateStatus]
        }
        public void RunafterMac(System.Object dev, System.Boolean blen)
        {
            #region    [自定义指令][20250325085905222][RunafterMac]
            var device = (deviceInfo)dev;
            if (device.areaPriy.Any() && device.deviceNo.Any())
            {
                LocRepository locRepository = new LocRepository();
                LocContainerRepository locContainerRepository = new LocContainerRepository();
                ContainerRepository containerRepository = new ContainerRepository();
                LocEntity startLoc = null;
                string Sign = device.deviceType == 1 ? "F" : "E";
                foreach (var sloc in device.deviceNo)
                {
                    ContainerEntity c = null;
                    var Startloc = locRepository.FindEntity(x => x.S_LOC_CODE == sloc);
                    if (Startloc != null && Startloc.N_CURRENT_NUM == 1 && Startloc.S_LOCK_STATE == "无")
                    {
                        var cntr = locContainerRepository.FindEntity(x => x.S_LOC_CODE == sloc);
                        if (cntr != null)
                        {
                            c = containerRepository.FindEntity(x => x.S_CODE == cntr.S_CNTR_CODE);
                            if (c != null && c.S_TYPE == Sign)
                            {
                                startLoc = Startloc;
                            }
                        }
                    }
 
                    if (startLoc != null)
                    {
                        foreach (var dn in device.areaPriy)
                        {
                            LocEntity endLoc = null;
                            var di = Settings.deviceInfos.Find(x => x.deviceName == dn);
                            if (di != null)
                            {
                                foreach (var ssloc in di.deviceNo)
                                {
                                    var endloc = locRepository.FindEntity(x => x.S_LOC_CODE == ssloc);
                                    if (endloc != null && endloc.N_CURRENT_NUM == 0 && endloc.S_LOCK_STATE == "无")
                                    {
                                        endLoc = endloc;
                                        break;
                                    }
                                }
                                if (endLoc != null)
                                {
                                    TaskProcess.CreateTask("", startLoc.S_LOC_CODE, endLoc.S_LOC_CODE, $"托盘流转-{Sign}", 5, new List<string> { c.S_CODE });
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            #endregion [自定义指令][20250325085905222][RunafterMac]
        }
        public System.Boolean RunTask(GZ.DB.Entity.OIDATABASE.TaskEntity task)
        {
            #region    [自定义指令][20250325092323531][RunTask]
 
            ILocRepository locrep = new LocRepository();
            var startloc = locrep.FindEntity(x => x.S_LOC_CODE == task.S_START_LOC);
            var endLoc = locrep.FindEntity(x => x.S_LOC_CODE == task.S_END_LOC);
            if (startloc == null || endLoc == null)
            {
                LogHelper.Info($"{task.S_TASK_NO}-任务起/终点 没找到数据。");
                return false;
            }
            var start = startloc.S_AGV_SITE;
            var end = endLoc.S_AGV_SITE;
 
            // 创建两个路径
            var positions = new List<positionCodePath>(){
                            new positionCodePath()
                            {
                                positionCode = start,
                                Type = "00"
                            },
                            new positionCodePath()
                            {
                                positionCode = end,
                                Type = "00"
                            }
            };
 
            //调第三方接口
            var model = new HaiKangOrderInfo
            {
                reqCode = task.S_TASK_NO.Trim(),
                reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                taskCode = task.S_TASK_NO.Trim(),
                ctnrCode = task.S_CNTRS,
                taskTyp = task.S_INDEX,
                positionCodePath = positions
            };
 
            return CreateOrder(model);
            #endregion [自定义指令][20250325092323531][RunTask]
        }
        public System.Boolean CreateOrder(System.Object m)
        {
            #region    [自定义指令][20250325095053549][CreateOrder]
            var model = (HaiKangOrderInfo)m;
            var msg = "";
            var result = false;
            var request = JsonConvert.SerializeObject(model);
            var response = Settings.apiHelper.Post(Settings.HKbaseUrl + "rcms/services/rest/hikRpcService/genAgvSchedulingTask", request);
            msg = $"[haikang-CreateOrder] request={request} response={response}";
 
            LogHelper.Info(msg);
            if (response != "")
            {
                try
                {
                    var dataResult = JsonConvert.DeserializeObject<HKResult>(response);
                    if (dataResult.code == "0")
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    LogHelper.Info(msg, "海康");
                }
            }
            else
            {
                msg = "[haikang-CreateOrder]创建订单失败";
                Console.WriteLine(msg);
            }
 
            LogHelper.Info(msg, "海康");
            return result;
            #endregion [自定义指令][20250325095053549][CreateOrder]
        }
        public void TaskisOk(GZ.DB.Entity.OIDATABASE.TaskEntity task)
        {
            #region    [自定义指令][20250325100336909][TaskisOk]
            // 车辆取放申请。
            if ("取货申请" != "")
            {
                var device1 = Settings.deviceInfos.Find(x => x.location.Contains(task.S_START_LOC));
                if (device1 != null)
                {
                    var left = device1.location.ToList().FindIndex(x => x == task.S_START_LOC) == 0;
                    if (device1.deviceType == 1)
                    {
                        if (device1.deviceName.Contains("维希尔抓臂"))
                        {
                            if ("IN_BEFORE" == "IN_BEFORE" || "out_BEFORE" == "out_BEFORE")
                            {
                                string V = left ? "R01" : "R03";
                                Conn.默认Redis.SetValue(device1.deviceName + "." + V, "true", device1.deviceName + "Queue");
                            }
                            if ("IN_after" == "IN_after" || "out_after" == "out_after")
                            {
                                string V = left ? "R02" : "R04";
                                Conn.默认Redis.SetValue(device1.deviceName + "." + V, "true", device1.deviceName + "Queue");
                            }
                        }
                        else if (device1.deviceName.Contains("Js捷顺"))
                        {
                            if ("IN_BEFORE" == "IN_BEFORE")
                            {
                                string V = left ? "M0" : "M1";
                                Conn.默认Redis.SetValue(device1.deviceName + "." + V, "true", device1.deviceName + "Queue");
                            }
                            if ("out_BEFORE" == "out_BEFORE")
                            {
                                //string V = left ? "R01" : "R03";
                                //Conn.默认Redis.SetValue(device1.deviceName + "." + V, "true", device1.deviceName + "Queue");
                            }
                            if ("IN_after" == "IN_after")
                            {
                                string V = left ? "M2" : "M3";
                                Conn.默认Redis.SetValue(device1.deviceName + "." + V, "true", device1.deviceName + "Queue");
                            }
                            if ("out_after" == "out_after")
                            {
                                string V = left ? "M6" : "M7";
                                Conn.默认Redis.SetValue(device1.deviceName + "." + V, "true", device1.deviceName + "Queue");
                            }
                        }
                    }
                }
            }
            if ("放货申请" != "")
            {
                var device1 = Settings.deviceInfos.Find(x => x.location.Contains(task.S_END_LOC));
                if (device1 != null)
                {
                    var left = device1.location.ToList().FindIndex(x => x == task.S_END_LOC) == 0;
                    if (device1.deviceType == 1)
                    {
                        if (device1.deviceName.Contains("维希尔抓臂"))
                        {
                            if ("IN_BEFORE" == "IN_BEFORE" || "out_BEFORE" == "out_BEFORE")
                            {
                                string V = left ? "R01" : "R03";
                                Conn.默认Redis.SetValue(device1.deviceName + "." + V, "true", device1.deviceName + "Queue");
                            }
                            if ("IN_after" == "IN_after" || "out_after" == "out_after")
                            {
                                string V = left ? "R02" : "R04";
                                Conn.默认Redis.SetValue(device1.deviceName + "." + V, "true", device1.deviceName + "Queue");
                            }
                        }
                        else if (device1.deviceName.Contains("Js捷顺"))
                        {
                            if ("IN_BEFORE" == "IN_BEFORE")
                            {
                                string V = left ? "M0" : "M1";
                                Conn.默认Redis.SetValue(device1.deviceName + "." + V, "true", device1.deviceName + "Queue");
                            }
                            if ("out_BEFORE" == "out_BEFORE")
                            {
                                string V = left ? "M4" : "M5";
                                Conn.默认Redis.SetValue(device1.deviceName + "." + V, "true", device1.deviceName + "Queue");
                            }
                            if ("IN_after" == "IN_after")
                            {
                                string V = left ? "M2" : "M3";
                                Conn.默认Redis.SetValue(device1.deviceName + "." + V, "true", device1.deviceName + "Queue");
                            }
                            if ("out_after" == "out_after")
                            {
                                string V = left ? "M6" : "M7";
                                Conn.默认Redis.SetValue(device1.deviceName + "." + V, "true", device1.deviceName + "Queue");
                            }
                        }
                    }
                }
            }
            #endregion [自定义指令][20250325100336909][TaskisOk]
        }
        public System.Boolean continueTask(System.Object m)
        {
            #region    [自定义指令][20250325100607380][continueTask]
            var model = (HaiKangOrderInfo)m;
            bool result = false;
            string msg = "";
            var request = JsonConvert.SerializeObject(model);
            var response = Settings.apiHelper.Post(Settings.HKbaseUrl + "rcms/services/rest/hikRpcService/continueTask", request);
            msg = $"[haikang-CancelOrder] request={request};response={response}";
            Console.WriteLine(msg);
            if (response != "")
            {
                var dataResult = JsonConvert.DeserializeObject<HKResult>(response);
                if (dataResult.code == "0")
                {
                    result = true;
                }
            }
            else
            {
                msg = "[haikang-CancelOrder]获取任务状态失败";
                Console.WriteLine(msg);
            }
            LogHelper.Info(msg, "海康");
            return result;
            #endregion [自定义指令][20250325100607380][continueTask]
        }
        public System.Boolean CancelOrder(System.Object m)
        {
            #region    [自定义指令][20250325100705351][CancelOrder]
            var model = (HaiKangOrderInfo)m;
            bool result = false;
            string msg = "";
            var request = JsonConvert.SerializeObject(model);
            var response = Settings.apiHelper.Post(Settings.HKbaseUrl + "rcms/services/rest/hikRpcService/continueTask", request);
            msg = $"[haikang-CancelOrder] request={request};response={response}";
            Console.WriteLine(msg);
            if (response != "")
            {
                var dataResult = JsonConvert.DeserializeObject<HKResult>(response);
                if (dataResult.code == "0")
                {
                    result = true;
                }
            }
            else
            {
                msg = "[haikang-CancelOrder]取消订单失败";
                Console.WriteLine(msg);
            }
            LogHelper.Info(msg, "海康");
            return result;
            #endregion [自定义指令][20250325100705351][CancelOrder]
        }
        public System.String GetSerialNumber(System.String snType, System.String prefix)
        {
            #region    [自定义指令][20250325131530407][GetSerialNumber]
            int result = 0;
            return "";
            #endregion [自定义指令][20250325131530407][GetSerialNumber]
        }
        #endregion
    }
}