服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
6 天以前 756e6c531d49a4565e47949120cb33bae431566e
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
using Tiger.Business.WMS.Transaction;
using Rhea.Common;
using Tiger.Model.SeaStone.Shelf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using Microsoft.AspNetCore.Http;
using Tiger.IBusiness;
 
namespace Tiger.Business.WMS
{
    /// <summary>
    /// WMS上下文
    /// </summary>
    public class WMSContext : IWMSContext
    {
        /// <summary>
        /// 亮灯颜色列表
        /// </summary>
        public static List<LightColor> LightColors = InitLightColors();
        /// <summary>
        /// 料车上报request接收列表("{RackId}-{LedId}-{CellStatus}")
        /// </summary>
        public static List<string> SlotChangedList = new List<string>();
 
        #region 亮灯颜色管理
        private static List<LightColor> InitLightColors()
        {
            var colors = new List<LightColor>();
            colors.Add(new LightColor("Blue"));
            colors.Add(new LightColor("Yellow"));
            colors.Add(new LightColor("Orange"));
            colors.Add(new LightColor("Purple"));
            return colors;
        }
 
        private static readonly object UseLightColorLock = new object();
        public static string GetDefaultLightColor()
        {
            lock (UseLightColorLock)
            {
                var cur = LightColors.Where(q => q.isUse == false).FirstOrDefault();
                cur.isUse = true;
                return cur.Color;
            }
        }
 
        public static void UseLightColor(string color)
        {
            lock (UseLightColorLock)
            {
                var cur = LightColors.Where(q => q.Color == color).SingleOrDefault();
                if (cur != null)
                {
                    cur.isUse = true;
                }
            }
        }
 
        public static void UnuseLightColor(string color)
        {
            lock (UseLightColorLock)
            {
                var cur = LightColors.Where(q => q.Color == color).SingleOrDefault();
                if (cur != null)
                {
                    cur.isUse = false;
                }
            }
        }
        #endregion
 
        #region 事务管理
        /// <summary>
        /// WMS事务字典
        /// </summary>
        public static Dictionary<string, WMSTransactionBase> TransactionDic => BizContext.TransactionDic.Where(q => q.Value is WMSTransactionBase).ToDictionary(k => k.Key, v => v.Value as WMSTransactionBase);
        public Dictionary<string, IWMSTransaction> GetTransDic() => BizContext.TransactionDic.Where(q => q.Value is IWMSTransaction).ToDictionary(k => k.Key, v => v.Value as IWMSTransaction);
        /// <summary>
        /// 创建一个新事务,并加到事务列表
        /// </summary>
        /// <param name="context">请求的HttpContext</param>
        /// <param name="trans">新的事务</param>
        /// <param name="IsUniqueInSameClient">相同客户端是否只允许开启一个同类型事务</param>
        public static void NewTransaction(HttpContext context, WMSTransactionBase trans, bool IsUniqueInSameClient = true)
        {
            BizContext.NewTransaction(context, trans, IsUniqueInSameClient);
        }
        public void NewTransaction(HttpContext context, IWMSTransaction trans, bool IsUniqueInSameClient = true)
        {
            BizContext.NewTransaction(context, trans, IsUniqueInSameClient);
        }
        #endregion
 
        public static string GetApiIP()
        {
            using (var webClient = new WebClient())
            {
                try
                {
                    var temp = webClient.DownloadString("http://localhost:9527/WeatherWebForm.aspx");//一般指定网址
                    var ip = Regex.Match(temp, @"\[(?<ip>\d+\.\d+\.\d+\.\d+)]").Groups["ip"].Value;
                    return !string.IsNullOrEmpty(ip) ? ip : null;
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
        }
    }
}