1
pengmn
2025-07-07 4fa85e410dc2e34afcbe97d15b6c5d5ef8c09eb6
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
using HH.WCS.XiaoMi.api;
using HH.WCS.XiaoMi.core;
using HH.WCS.XiaoMi.LISTA.dispatch;
using HH.WCS.XiaoMi.util;
using HH.WCS.XiaoMi.wms;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using static HH.WCS.XiaoMi.api.ApiModel;
using static HH.WCS.XiaoMi.LISTA.dispatch.XMWcsHelper;
 
namespace HH.WCS.XiaoMi.dispatch
{
    /// <summary>
    /// 国自调度辅助类
    /// </summary>
    public class GZRobot
    {
        private static readonly HttpHelper apiHelper = new HttpHelper();
        private static readonly string baseUrl = Settings.GZSeverUrl;
        //private static readonly string logName = "guozi";
        public static List<IOState> GetIO() {
            var result = apiHelper.Get(baseUrl + "api/engine/view/iostates/");
            var data = JsonConvert.DeserializeObject<gzResult<IOState>>(result);
            return data.data;
        }
        public static void UpdateIOState() {
            var data = new { data = new List<IOSateInfo>() };
            var result = apiHelper.Post(baseUrl + "api/engine/tasks/iostates/", JsonConvert.SerializeObject(data));
            var dataResult = JsonConvert.DeserializeObject<gzResult<IOStatesInfoResult>>(result);
        }
 
        public static int CreateOrder(string taskNo, int priority, string param, string ts = "p2p") {
            var msg = "";
            var orderId = 0;
            var data = new OrderInfo() { order_name = taskNo, priority = priority, dead_line = DateTime.Now, ts_name = ts, parameters = param };
            var request = JsonConvert.SerializeObject(data);
            var response = apiHelper.Post(baseUrl + "api/om/order/", request);
            msg = $"[guozi-CreateOrder] request={request} response={response}";
            Console.WriteLine(msg);
            if (response != "") {
                try {
                    var dataResult = JsonConvert.DeserializeObject<gzResult<OrderInfoResult>>(response);
                    if (dataResult.code == 0) {
                        orderId = dataResult.data[0].in_order_id;
                    }
                }
                catch (Exception ex) {
                }
            }
            else {
                msg = "[guozi-CreateOrder]创建订单失败";
                Console.WriteLine(msg);
            }
 
 
            return orderId;
        }
        public static bool CancelOrder(int orderId) {
            bool result = false;
            string msg = "";
            var request = new CancelOrderInfo() { order_list = new List<int>(orderId) };
            var response = apiHelper.Post(baseUrl + "api/om/order/cancel/", JsonConvert.SerializeObject(request));
            msg = $"[guozi-CancelOrder] request={request};response={response}";
            Console.WriteLine(msg);
            if (response != "") {
                var dataResult = JsonConvert.DeserializeObject<gzResult<CancelOrderInfoResult>>(response);
                if (dataResult.code == 0) {
                    result = true;
                }
            }
            else {
                msg = "[guozi-CancelOrder]取消订单失败";
                Console.WriteLine(msg);
            }
            return result;
        }
        public static QueryOrderResult QueryOrder(int id) {
            //
            var result = new QueryOrderResult() { agv_list = new List<int> { 0 } };
            string msg = "";
            var response = apiHelper.Get(baseUrl + $"/api/om/order/{id}/");
            msg = $"[guozi-QueryOrderResult] request={id};response={response}";
            Console.WriteLine(msg);
            if (response != "") {
                var data = JsonConvert.DeserializeObject<gzResult<QueryOrderResult>>(response);
                result = data.data[0];
            }
            else {
                msg = "[guozi-QueryOrderResult]查询订单信息失败";
                Console.WriteLine(msg);
            }
            return result;
        }
        /// <summary>
        /// 获取交互信息
        /// </summary>
        /// <param name="typeId">1任务状态  2开门或交管  3目的点</param>
        /// <param name="status"></param>
        /// <returns></returns>
        public static List<InteractInfoResult> QueryInteractInfo(int typeId, string status = "active") {
 
            //string aaa = "{\"app_name\": \"Gouzi client\", \"version\": \"1.0.0\", \"code\": 0, \"msg\": \"success\", \"data\": [{\"interaction_info_id\": 233, \"interaction_info_name\": \"TN2012030001\", \"interaction_info_desp\": null, \"interaction_info_type_id\": 3, \"value_json\": {\"state\": \"4\"}, \"info_status\": \"active\", \"return_value\": null}]}";
            //var data = JsonConvert.DeserializeObject<gzResult<InteractInfoResult>>(aaa);
            var list = new List<InteractInfoResult>();
            string msg = "";
            var result = apiHelper.Get(baseUrl + $"api/om/interaction_info/find_by_type/?type_id={typeId}&info_status={status}");
            if (!string.IsNullOrEmpty(result)) {
                Console.WriteLine(result);
                // {"app_name": "Gouzi client", "version": "1.0.0", "code": 0, "msg": "success", "data": [{"interaction_info_id": 230, "interaction_info_name": "2", "interaction_info_desp": null, "interaction_info_type_id": 3, "value_json": {"state": "4"}, "info_status": "active", "return_value": null}, {"interaction_info_id": 231, "interaction_info_name": "2", "interaction_info_desp": null, "interaction_info_type_id": 3, "value_json": {"state": "6"}, "info_status": "active", "return_value": null}, {"interaction_info_id": 232, "interaction_info_name": "2", "interaction_info_desp": null, "interaction_info_type_id": 3, "value_json": {"state": "2"}, "info_status": "active", "return_value": null}, {"interaction_info_id": 233, "interaction_info_name": "TN2012030001", "interaction_info_desp": null, "interaction_info_type_id": 3, "value_json": {"state": "4"}, "info_status": "active", "return_value": null}]}
                try {
                    var data = JsonConvert.DeserializeObject<gzResult<InteractInfoResult>>(result);
                    if (data.data != null) {
                        list = data.data;
                    }
 
                }
                catch (Exception ex) {
                    Console.WriteLine(ex.Message);
                }
 
            }
            else {
                msg = "[guozi-QueryInteractInfo]读取交互信息失败";
                Console.WriteLine(msg);
            }
            return list;
        }
        public static bool UpdateInteractInfo(UpdateInteractInfo interactInfo) {
            string msg = "";
            var result = false;
            var request = JsonConvert.SerializeObject(interactInfo);
            var response = apiHelper.Post(baseUrl + "api/om/interaction_info/update/", request);
            msg = $"[mes-UpdateInteractInfo] request={request};response={response}";
            if (response != "") {
                var dataResult = JsonConvert.DeserializeObject<gzResult<object>>(response);
                result = dataResult.code == 0;
            }
            else {
                msg = "[guozi-UpdateInteractInfo]更新交互信息失败";
                Console.WriteLine(msg);
            }
            return result;
        }
 
        /// <summary>
        /// 订单状态反馈
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static GzResult orderStatusReport(orderStatusReportParme model)
        {
            GzResult result = new GzResult();
            AgvTaskState agv = new AgvTaskState();
 
            if (model == null)
            {
                result.resultCode = 1;
                result.orderID = 0;
                result.msg = "返回订单状态失败,参数为空";
            }
 
            switch (model.orderStatus)
            {
                case "active":
                    agv.State = 1;
                    break;
                case "source_finish":
                    //取货完成
                    agv.State = 4;
                    break;
                case "dest_finish":
                    //卸货完成
                    agv.State = 6;
                    break;
                case "finish":
                    agv.State = 2;
                    break;
                case "manually_finish":
                    agv.State = 2;
                    break;
                case "cancel_finish":
                    agv.State = 7;
                    break;
                case "error":
                    agv.State = 7;
                    break;
                default:
                    agv.State = 0;
                    break;
            }
            if (agv.State != 0)
            {
                agv.No = model.orderName;
                agv.ForkliftNo = model.agvIDList;
                WCSCore.OperateAgvTaskStatus(agv);
 
                
 
            }
 
            result.resultCode = 0;
            result.msg = "返回订单状态成功";
            result.orderID = model.orderID;
            LogHelper.Info("orderStatusReport 返回" + JsonConvert.SerializeObject(result), "API");
            return result;
        }
 
 
        /// <summary>
        /// agv申请绕路
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static GzResult applyReroute(orderReroute model)
        {
            GzResult result = new GzResult();
            try
            {
                var wcsTask = WCSHelper.GetTask(model.orderName);
                if (wcsTask == null)
                {
                    result.resultCode = 1;
                    result.msg = "订单不存在!";
                    return result;
                }
                if (model.orderType == 1)
                {
                    //1表示是否申请绕路
                    var endLoc = LocationHelper.GetLoc(wcsTask.S_END_LOC);
                    if (endLoc.N_CURRENT_NUM == 0 && endLoc.S_LOCK_STATE == "无")
                    {
                        result.resultCode = 0;
                        result.success = true;
                        result.msg = "目标货位已空";
                        return result;
                    }
                    else
                    {
                        result.resultCode = 1;
                        result.success = false;
                        result.msg = "目标货位为满";
                        return result;
                    }
                }
                else
                {
                    //2表示申请目标库位
                    var apply = new ApplyModel() { wcsTaskCode = wcsTask.S_WORKSHOP_NO };
                    var end = XMWcsHelper.GetPositionApply(apply);
                    if (!string.IsNullOrEmpty(end.body.pointCode))
                    {
                        result.resultCode = 0;
                        result.msg = "目标库位已申请";
                        result.orderData = end.body.pointCode;
                        return result;
                    }
                    else
                    {
                        result.resultCode = 1;
                        result.msg = "目标库位已申请";
                        result.orderData = "";
                        return result;
                    }
                }
            }
            catch (Exception ex)
            {
                result.resultCode = 1;
                result.success = false;
                result.msg = $"agv申请绕路出现错误{JsonConvert.SerializeObject(ex.Message)}";
                return result;
            }
            
        }
 
 
        ///// <summary>
        ///// 调用小米状态回报接口
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //public static int xiaoMiRequestApi(orderStatusReportParme model)
        //{
        //    var msg = "";
        //    var orderId = 0;
        //    var request = JsonConvert.SerializeObject(model);
        //    var response = apiHelper.Post(baseUrl + "api/om/order/", request);
        //    msg = $"[xiaoMiRequestApi] request={request} response={response}";
        //    Console.WriteLine(msg);
        //    if (response != ""&& model.orderStatus == "6")
        //    {
        //        try
        //        {
        //            var dataResult = JsonConvert.DeserializeObject<gzResult<OrderInfoResult>>(response);
        //            if (dataResult.code == 0)
        //            {
        //                orderId = dataResult.data[0].in_order_id;
        //            }
        //        }
        //        catch (Exception ex)
        //        {
        //        }
        //    }
        //    else
        //    {
        //        msg = "[xiaoMiRequestApi]状态汇报失败";
        //        Console.WriteLine(msg);
        //    }
 
        //    return orderId;
        //}
 
    }
 
    
    public class gzResult<T>
    {
        public string app_name { get; set; }
        public string version { get; set; }
        public int code { get; set; }
        public string msg { get; set; }
        public List<T> data { get; set; }
    }
    public class IOState
    {
        public int io_id { get; set; }
        public string io_name { get; set; }
        public string io_type_id { get; set; }
        public string io_type { get; set; }
        public int io_status_id { get; set; }
        public string io_status_type { get; set; }
        public string parameter_definition_int4_1 { get; set; }
        public int io_value_int4_1 { get; set; }
        public string parameter_definition_int4_2 { get; set; }
        public int io_value_int4_2 { get; set; }
        public string parameter_definition_int4_3 { get; set; }
        public int io_value_int4_3 { get; set; }
        public string parameter_definition_int4_4 { get; set; }
        public int io_value_int4_4 { get; set; }
        public string parameter_definition_json { get; set; }
        public string io_value_json { get; set; }
    }
    public class IOSateInfo
    {
        public int io_id { get; set; }
        public int io_status_id { get; set; }
        public int io_value_int4_1 { get; set; }
        public int io_value_int4_2 { get; set; }
        public int io_value_int4_3 { get; set; }
        public int io_value_int4_4 { get; set; }
        public string io_value_json { get; set; }
    }
    public class IOStatesInfoResult
    {
        public List<ResultInfo> success_list { get; set; }
        public List<ResultInfo> error_list { get; set; }
        public class ResultInfo { public int io_id { get; set; } }
    }
    public class OrderInfo
    {
        public string order_name { get; set; }
        public int priority { get; set; }
        public DateTime dead_line { get; set; }
        public string ts_name { get; set; }
        public string parameters { get; set; }//{\"dock\":1}
    }
    public class OrderInfoResult
    {
        public int in_order_id { get; set; }
    }
    public class CancelOrderInfo
    {
        public List<int> order_list { get; set; }
    }
    public class CancelOrderInfoResult
    {
        public List<ResultInfo> success_list { get; set; }
        public List<ResultInfo> error_list { get; set; }
        public class ResultInfo { public int order_id { get; set; } }
    }
    public class QueryOrderResult
    {
        public int order_id { get; set; }
        public string order_name { get; set; }
        public int priority { get; set; }
        public DateTime dead_line { get; set; }
        public string ts_id { get; set; }
        public string parameters { get; set; }//{"TN_LocationName":"3-2-B", "PalletType":1}",
        public string trigger { get; set; }
        public string command { get; set; }
        public string status { get; set; }
        public List<int> agv_list { get; set; }
        public string current_task { get; set; }
        public string current_dest { get; set; }
        public string current_opt { get; set; }
        public string current_omi { get; set; }
        public DateTime create_time { get; set; }
        public DateTime? active_time { get; set; }
        public DateTime? finished_time { get; set; }
        public DateTime? cancel_time { get; set; }
        public string response_timespan { get; set; }
        public string execute_timespa { get; set; }
        public string total_timespan { get; set; }
    }
    public class InteractInfoResult
    {
        public int interaction_info_id { get; set; }
        public string interaction_info_name { get; set; }
        public string interaction_info_desp { get; set; }
        public int interaction_info_type_id { get; set; }
        public object value_json { get; set; }//{"dock": 1},
        public string info_status { get; set; }
        public string return_value { get; set; }
    }
    public class UpdateInteractInfo
    {
        public int interaction_info_id { get; set; }
        public string info_status { get; set; }
        public string return_value { get; set; }
    }
    public class interaction_state
    {
        public string state { get; set; }
    }
    public class interaction_door
    {
        public string door { get; set; }
    }
    public class interaction_bit
    {
        public string order { get; set; }
    }
}