服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2024-09-04 0116b5d2ed16a0825da9e7474fc786ee06b2d60c
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using Newtonsoft.Json;
using Rhea.Common.Authorization;
using Newtonsoft.Json.Linq;
using Tiger.Model.Minsun;
using System.Globalization;
using System.Data;
using System.Text.RegularExpressions;
using System.Text;
using Dm;
using Tiger.Model.DigitalTwin;
using System.IO;
using System.Net;
using Rhea.Common;
using static Tiger.Model.Minsun.IqcManagementSides;
 
namespace Tiger.Business.WMS
{
    public static partial class Shelf
    {
        public static long UserId { get; set; }
        public static string _shelfApiUrl { get; set; } = GetSysCodeText("ShelfSetting", "ShelfApiUrl");
        private static string _shelfLightColor { get; set; } = GetSysCodeText("ShelfSetting", "ShelfLightColor");
 
 
        /// <summary>
        /// 上架
        /// </summary>
        /// <param name="operateInput"></param>
        /// <returns></returns>
        public static async Task<Result<ShelfOperateOutput>> UpShelf(ShelfOperateInput operateInput)
        {
            Result<ShelfOperateOutput> result = new(Result.Flags.Success, $"成功上架");
            string uniqueID = Guid.NewGuid().ToString();
            // 1. 发送上架指令
            // 准备 POST 请求 body 参数
 
            var data1 = new
            {
                shelfNo = operateInput.ShelfNo,
                operateType = 1,
                pdaid = string.IsNullOrEmpty(operateInput.PdaId) ? UserId.ToString() : operateInput.PdaId,
                keepSeconds = 30,
                uniqueNumber = uniqueID
            };
 
            string apiUrl1 = _shelfApiUrl + "/OperateConfig";
            var jsonString1 = JsonConvert.SerializeObject(data1);
 
            Result<JObject> result1 = await PostShelfApi(apiUrl1, jsonString1);
            if (result1.Flag != Result.Flags.Success)
            {
                result.Flag = Result.Flags.Failed;
                result.Message = result1.Message;
                return result;
            }
            JObject resp1 = result1.Data;
            ShelfApiResult apiResult = new ShelfApiResult();
            apiResult.IsSuccess = resp1["OperateConfigResult"]["IsSuccess"].ToString();
            apiResult.ItemCode = resp1["OperateConfigResult"]["ItemCode"].ToString();
            apiResult.ResultValue = resp1["OperateConfigResult"]["ResultValue"].ToString();
            apiResult.Type = resp1["OperateConfigResult"]["Type"].ToString();
 
            //定义返回参数
            ShelfOperateOutput shelfOperateOutput = new ShelfOperateOutput();
 
            // 如果上架操作指令成功
            if (apiResult.IsSuccess.ToUpper() == "TRUE")
            {
                // 2. 发送 GetOperateChuteChangeInfo 指令,获取上架位置(ledAddr)及 uniqueNumber
                // 准备 POST 请求 body 参数
                var data2 = new
                {
                    shelfNo = operateInput.ShelfNo,
                    operateType = operateInput.OperateType
                };
 
                string apiUrl2 = _shelfApiUrl + "/GetOperateChuteChangeInfo";
                var jsonString2 = JsonConvert.SerializeObject(data2);
 
                shelfOperateOutput.IsSuccess = "FALSE";
                DateTime timeStart = DateTime.Now;
                TimeSpan timeSpan = DateTime.Now - timeStart;
                while (shelfOperateOutput.IsSuccess.ToUpper() != "TRUE" && timeSpan.TotalSeconds < 30)
                {
                    DateTime timeEnd = DateTime.Now;
                    timeSpan = DateTime.Now - timeStart;
 
                    Result<JObject> result2 = await PostShelfApi(apiUrl2, jsonString2);
                    if (result2.Flag == Result.Flags.Success)
                    {
                        JObject resp2 = result2.Data;
 
                        shelfOperateOutput.IsSuccess = resp2["GetOperateChuteChangeInfoResult"]["IsSuccess"].ToString();
                        string resultValue = resp2["GetOperateChuteChangeInfoResult"]["ResultValue"].ToString();
 
                        if (shelfOperateOutput.IsSuccess.ToUpper() == "TRUE" && !String.IsNullOrEmpty(resultValue))
                        {
                            JObject jo = JObject.Parse(resultValue);
                            shelfOperateOutput.ShelfNo = jo["shelfNo"].ToString();
                            shelfOperateOutput.OperateType = jo["operateType"].ToString();
                            shelfOperateOutput.LedAddr = jo["ledAddr"].ToString();
                            shelfOperateOutput.PdaId = jo["pdaid"].ToString();
                            shelfOperateOutput.UniqueNumber = jo["uniqueNumber"].ToString();
 
                            if (uniqueID != shelfOperateOutput.UniqueNumber)
                            {
                                shelfOperateOutput.IsSuccess = "False";
                                shelfOperateOutput.Message = "UniqueNumber 不同";
                            };
 
                            if (operateInput.ShelfNo != shelfOperateOutput.ShelfNo)
                            {
                                shelfOperateOutput.IsSuccess = "False";
                                shelfOperateOutput.Message = "ShelfNo 不同";
                            };
                        }
                    }
                }
 
                // 3. 结束上架
                var data3 = new
                {
                    shelfNo = data1.shelfNo,
                    operateType = 7,
                    pdaid = data1.pdaid,
                    keepSeconds = 30,
                    uniqueNumber = data1.uniqueNumber,
                };
                string apiUrl3 = _shelfApiUrl + "/OperateConfig";
                var jsonString3 = JsonConvert.SerializeObject(data3);
                Result<JObject> result3 = await PostShelfApi(apiUrl3, jsonString3);
                if (result3.Flag != Result.Flags.Success)
                {
                    result.Flag = Result.Flags.Failed;
                    result.Message = result3.Message;
                    return result;
                }
                JObject resp3 = result3.Data;
                if (timeSpan.TotalSeconds >= 30)
                {
                    result.Flag = Result.Flags.Failed;
                    result.Message = $"上架操作超时";
                    return result;
                }
            }
 
            // 更新库位代码,LocationCode
            WMS_LOCATION locOut = await Biz.Db.Queryable<WMS_LOCATION>().Where(t => t.RACKCODE == shelfOperateOutput.ShelfNo
                                     && t.LedAddr.ToString() == shelfOperateOutput.LedAddr).FirstAsync();
            if (locOut != null)
                shelfOperateOutput.LocationCode = locOut.LOCATIONCODE;
            result.Data = shelfOperateOutput;
            return result;
 
        }
 
        // 读取数据字典,根据CodeType,CodeValue获取参数Text
        private static string GetSysCodeText(string codeType, string codeValue)
        {
            var code = Biz.Db.Queryable<Sys_Code>().Where(x => x.CodeType.ToUpper() == codeType && x.Value.ToUpper() == codeValue).First();
            return code?.Text;
        }
 
        // Http Post ShelfApi
        private static async Task<Result<JObject>> PostShelfApi(string apiUrl, string strJson)
        {
            Result<JObject> result = new(Result.Flags.Success, $"货架API返回成功");
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(apiUrl);
            httpWebRequest.ContentType = "application/json;charset=utf-8";
            httpWebRequest.Method = "POST";
            httpWebRequest.Timeout = 10000;
 
            byte[] btBodys = Encoding.UTF8.GetBytes(strJson);
            httpWebRequest.ContentLength = btBodys.Length;
            //Logger.Default.Info(string.Format("智能货架:开始请求{0},参数{1}", apiUrl, strJson));
            try
            {
                httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
            }
            catch (Exception ex)
            {
                Logger.Default.Info(string.Format("智能货架:请求异常;返回信息:{0}", JsonConvert.SerializeObject(ex)));
                result.Flag = Result.Flags.Failed;
                result.Message = $"智能货架:请求异常;返回信息:{JsonConvert.SerializeObject(ex)}";
                return result;
            }
 
            HttpWebResponse httpResponse;
            string respnsetContent;
            try
            {
                httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                if (httpResponse.StatusCode != HttpStatusCode.OK)
                {
                    result.Flag = Result.Flags.Failed;
                    result.Message = $"智能货架API通讯异常,请检查货架是否正常";
                    return result;
                }
 
                StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream());
                respnsetContent = streamReader.ReadToEnd();
 
                httpResponse.Close();
                streamReader.Close();
                httpWebRequest.Abort();
 
                //Logger.Default.Info(string.Format("智能货架:请求成功;返回信息:{0}", respnsetContent));
            }
            catch (Exception ex)
            {
                Logger.Default.Info(string.Format("智能货架:请求异常;返回信息:{0}", JsonConvert.SerializeObject(ex)));
                //不捕获指令发出后的异常
                result.Flag = Result.Flags.Failed;
                result.Message = $"智能货架:请求异常;返回信息:{JsonConvert.SerializeObject(ex)}";
                return result;
            }
 
            JObject jo = JObject.Parse(respnsetContent);
            result.Data = jo;
            return result;
        }
 
 
        #region 货架亮灯
        /// <summary>
        /// 全部亮灯
        /// </summary>
        /// <param name="lightParam"></param>
        /// <returns></returns>
        public static async Task<Result<ShelfApiResult>> LightAll(ShelfLightParam lightParam)
        {
            Result<ShelfApiResult> result = new(Result.Flags.Success, $"全部亮灯成功");
            // 准备 POST 请求 body 参数
            var data = new
            {
                shelfNo = lightParam.ShelfNo,
                color = lightParam.LightColor,
                isCover = lightParam.IsCover,
                shelfType = "sensor_shelf"
            };
 
            string apiUrl = _shelfApiUrl + "/LightAll";
            var jsonString = JsonConvert.SerializeObject(data);
 
            Result<JObject> result1 = await PostShelfApi(apiUrl, jsonString);
            if (result1.Flag != Result.Flags.Success)
            {
                result.Flag = Result.Flags.Failed;
                result.Message = result1.Message;
                return result;
            }
 
            result.Data = new ShelfApiResult
            {
                LightType = "Light All",
                IsSuccess = result1.Data["LightAllResult"]["IsSuccess"].ToString(),
                ItemCode = result1.Data["LightAllResult"]["ItemCode"].ToString(),
                ResultValue = result1.Data["LightAllResult"]["ResultValue"].ToString(),
                Type = result1.Data["LightAllResult"]["Type"].ToString(),
            };
            return result;
        }
 
        /// <summary>
        /// 全部灭灯
        /// </summary>
        /// <param name="lightParam"></param>
        /// <returns></returns>
        public static async Task<Result<ShelfApiResult>> DownAll(ShelfLightParam lightParam)
        {
            Result<ShelfApiResult> result = new(Result.Flags.Success, $"全部灭灯成功");
            // 准备 POST 请求 body 参数
            var data = new
            {
                shelfNo = lightParam.ShelfNo,
                shelfType = "sensor_shelf"
            };
 
            string apiUrl = _shelfApiUrl + "/DownAll";
            var jsonString = JsonConvert.SerializeObject(data);
            Result<JObject> result1 = await PostShelfApi(apiUrl, jsonString);
            if (result1.Flag != Result.Flags.Success)
            {
                result.Flag = Result.Flags.Failed;
                result.Message = result1.Message;
                return result;
            }
            result.Data = new ShelfApiResult
            {
                LightType = "Down All",
                IsSuccess = result1.Data["DownAllResult"]["IsSuccess"].ToString(),
                ItemCode = result1.Data["DownAllResult"]["ItemCode"].ToString(),
                ResultValue = result1.Data["DownAllResult"]["ResultValue"].ToString(),
                Type = result1.Data["DownAllResult"]["Type"].ToString(),
            };
            return result;
        }
 
        /// <summary>
        /// 单灯亮
        /// </summary>
        /// <param name="lightParam"></param>
        /// <returns></returns>
        public static async Task<Result<ShelfApiResult>> LightSingle(ShelfLightParam lightParam)
        {
            Result<ShelfApiResult> result = new(Result.Flags.Success, $"单灯亮成功");
            //如果参数传入LocationCode
            var nLocation = await Biz.Db.Queryable<WMS_LOCATION>().Where(t => t.LOCATIONCODE == lightParam.LocationCode).FirstAsync();
 
            // 准备 POST 请求 body 参数
            var data = new
            {
                shelfNo = lightParam.ShelfNo,
                ledAddr = (nLocation == null) ? lightParam.LedAddr : nLocation.LedAddr.ToString(),
                color = lightParam.LightColor.IsNullOrEmpty() ? _shelfLightColor : lightParam.LightColor,
                isCover = lightParam.IsCover.IsNullOrEmpty() ? "1" : lightParam.IsCover,
                shelfType = "sensor_shelf"
            };
 
            string apiUrl = _shelfApiUrl + "/LightSingle";
            var jsonString = JsonConvert.SerializeObject(data);
 
            Result<JObject> result1 = await PostShelfApi(apiUrl, jsonString);
            if (result1.Flag != Result.Flags.Success)
            {
                result.Flag = Result.Flags.Failed;
                result.Message = result1.Message;
                return result;
            }
            result.Data = new ShelfApiResult
            {
                LightType = "Light Single",
                IsSuccess = result1.Data["LightSingleResult"]["IsSuccess"].ToString(),
                ItemCode = result1.Data["LightSingleResult"]["ItemCode"].ToString(),
                ResultValue = result1.Data["LightSingleResult"]["ResultValue"].ToString(),
                Type = result1.Data["LightSingleResult"]["Type"].ToString(),
            };
 
            return result;
        }
 
        /// <summary>
        /// 单灯灭
        /// </summary>
        /// <param name="lightParam"></param>
        /// <returns></returns>
        public static async Task<Result<ShelfApiResult>> DownSingle(ShelfLightParam lightParam)
        {
            Result<ShelfApiResult> result = new(Result.Flags.Success, $"单灯灭成功");
            // 准备 POST 请求 body 参数
            var data = new
            {
                shelfNo = lightParam.ShelfNo,
                ledAddr = lightParam.LedAddr,
                shelfType = "sensor_shelf"
            };
 
            string apiUrl = _shelfApiUrl + "/DownSingle";
            var jsonString = JsonConvert.SerializeObject(data);
 
            Result<JObject> result1 = await PostShelfApi(apiUrl, jsonString);
            if (result1.Flag != Result.Flags.Success)
            {
                result.Flag = Result.Flags.Failed;
                result.Message = result1.Message;
                return result;
            }
            result.Data = new ShelfApiResult()
            {
                LightType = "Down Single",
                IsSuccess = result1.Data["DownSingleResult"]["IsSuccess"].ToString(),
                ItemCode = result1.Data["DownSingleResult"]["ItemCode"].ToString(),
                ResultValue = result1.Data["DownSingleResult"]["ResultValue"].ToString(),
                Type = result1.Data["DownSingleResult"]["Type"].ToString(),
            };
            return result;
        }
 
        /// <summary>
        /// 多灯亮
        /// </summary>
        /// <param name="lightParam"></param>
        /// <returns></returns>
        public static async Task<Result<ShelfApiResult>> LightMulti(ShelfLightParam lightParam)
        {
            Result<ShelfApiResult> result = new(Result.Flags.Success, $"多灯亮成功");
            // 库位列表 LedAddrList
            List<Object> ledList = new List<object>();
            foreach (string led_addr in lightParam.LedAddrList)
            {
                //ledList.Add(new { ledAddr = led_addr, uniqueNumber = "" });
                ledList.Add(new { ledAddress = led_addr, uniqueNumber = "" });
            }
 
            // 货架库位列表
            List<Object> shelfLedAddr = new List<Object>();
            shelfLedAddr.Add(
                new
                {
                    shelfNo = lightParam.ShelfNo,
                    shelfType = "sensor_shelf",
                    ledAddressList = ledList
                }
            );
 
            // 准备 POST 请求 body 参数
            var data = new
            {
                shelfLedAddressList = shelfLedAddr,
                parameters = new
                {
                    pdaId = "WMS_PDA",
                    orderCode = "WMS_PDA",
                    userItem1 = "",
                    userItem2 = "",
                    userItem3 = "",
                    userItem4 = "",
                    userItem5 = "",
                },
                operateType = 0,
                color = lightParam.LightColor.IsNullOrEmpty() ? _shelfLightColor : lightParam.LightColor,
                isCover = lightParam.IsCover.IsNullOrEmpty() ? "1" : lightParam.IsCover,
            };
 
            string apiUrl = _shelfApiUrl + "/LightMulti";
            var jsonString = JsonConvert.SerializeObject(data);
 
            Result<JObject> result1 = await PostShelfApi(apiUrl, jsonString);
            if (result1.Flag != Result.Flags.Success)
            {
                result.Flag = Result.Flags.Failed;
                result.Message = result1.Message;
                return result;
            }
            result.Data = new ShelfApiResult
            {
                LightType = "Light Multi",
                IsSuccess = result1.Data["LightMultiResult"]["IsSuccess"].ToString(),
                ItemCode = result1.Data["LightMultiResult"]["ItemCode"].ToString(),
                ResultValue = result1.Data["LightMultiResult"]["ResultValue"].ToString(),
                Type = result1.Data["LightMultiResult"]["Type"].ToString(),
            };
            return result;
        }
 
        /// <summary>
        /// 多灯灭
        /// </summary>
        /// <param name="lightParam"></param>
        /// <returns></returns>
        public static async Task<Result<ShelfApiResult>> DownMulti(ShelfLightParam lightParam)
        {
            Result<ShelfApiResult> result = new(Result.Flags.Success, $"多灯灭成功");
            // 库位列表 LedAddrList
            List<Object> ledList = new List<object>();
            foreach (string led_addr in lightParam.LedAddrList)
            {
                //ledList.Add(new { ledAddr = led_addr, uniqueNumber = "" });
                ledList.Add(new { ledAddress = led_addr, uniqueNumber = "" });
            }
 
            // 货架库位列表
            List<Object> shelfLedAddr = new List<Object>();
            shelfLedAddr.Add(
                new
                {
                    shelfNo = lightParam.ShelfNo,
                    shelfType = "sensor_shelf",
                    ledAddressList = ledList
                }
            );
 
            // 准备 POST 请求 body 参数
            var data = new
            {
                shelfLedAddressList = shelfLedAddr,
                parameters = new
                {
                    pdaId = "",
                    orderCode = "",
                    userItem1 = "",
                    userItem2 = ""
                },
                operateType = lightParam.OperateType,
                color = lightParam.LightColor,
                isCover = lightParam.IsCover,
            };
 
            string apiUrl = _shelfApiUrl + "/DownMulti";
            var jsonString = JsonConvert.SerializeObject(data);
 
            Result<JObject> result1 = await PostShelfApi(apiUrl, jsonString);
            if (result1.Flag != Result.Flags.Success)
            {
                result.Flag = Result.Flags.Failed;
                result.Message = result1.Message;
                return result;
            }
            result.Data = new ShelfApiResult
            {
                LightType = "Down Multi",
                IsSuccess = result1.Data["DownMultiResult"]["IsSuccess"].ToString(),
                ItemCode = result1.Data["DownMultiResult"]["ItemCode"].ToString(),
                ResultValue = result1.Data["DownMultiResult"]["ResultValue"].ToString(),
                Type = result1.Data["DownMultiResult"]["Type"].ToString(),
            };
            return result;
        }
 
        /// <summary>
        /// 操作货架灯塔
        /// </summary>
        /// <param name="lightParam"></param>
        /// <returns></returns>
        public static async Task<Result<ShelfApiResult>> OperateTowerLight(ShelfLightParam lightParam)
        {
            Result<ShelfApiResult> result = new(Result.Flags.Success, $"多灯灭成功");
            // 准备 POST 请求 body 参数
            var data = new
            {
                shelfNo = lightParam.ShelfNo,
                color = 3,
                operEnum = lightParam.OperEnum,
                shelfType = "sensor_shelf"
            };
 
            string apiUrl = _shelfApiUrl + "/OperateTowerLight";
            var jsonString = JsonConvert.SerializeObject(data);
 
            Result<JObject> result1 = await PostShelfApi(apiUrl, jsonString);
            if (result1.Flag != Result.Flags.Success)
            {
                result.Flag = Result.Flags.Failed;
                result.Message = result1.Message;
                return result;
            }
            result.Data = new ShelfApiResult
            {
                LightType = "Operate TowerLight",
                IsSuccess = result1.Data["OperateTowerLightResult"]["IsSuccess"].ToString(),
                ItemCode = result1.Data["OperateTowerLightResult"]["ItemCode"].ToString(),
                ResultValue = result1.Data["OperateTowerLightResult"]["ResultValue"].ToString(),
                Type = result1.Data["OperateTowerLightResult"]["Type"].ToString()
            };
            return result;
        }
        #endregion
 
 
    }
}