服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2025-04-02 a8fcfbb5e5ed9a07b1a30ef2d7ef9d407e9b04dd
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
using Apache.NMS;
using Apache.NMS.ActiveMQ;
using Rhea.Common;
using Tiger.Model;
using Tiger.Model.SeaStone.Shelf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
using Tiger.Business.WMS.Seastone;
using Tiger.IBusiness;
using Tiger.Model.Entitys.MES.Position;
 
namespace Tiger.Business.WMS.Transaction
{
    /// <summary>
    /// WMS事务基类
    /// </summary>
    public abstract class WMSTransactionBase : TransactionBase, IWMSTransaction
    {
        /// <summary>
        /// WMS事务基类
        /// </summary>
        public WMSTransactionBase()
        {
            //MQTTThread = new Thread(new ThreadStart(MQTT));
            //MQTTThread.Start();
            //Logger.Console.Info($"Start Transaction MQTT Thread(ID: {TransID}]");
        }
       
        /// <summary>
        /// 关闭事务
        /// </summary>
        /// <param name="needSaveHistoryLog"></param>
        /// <returns></returns>
        public override bool Close(bool needSaveHistoryLog = false)
        {
            return base.Close(needSaveHistoryLog);
        }
 
        public override void Dispose()
        {
            try
            {
                //MQTTThread?.Abort();
                //删除使用的MQTT Topic
                //MQTTHelper.DeleteTopic(TransID);
                Logger.Console.Info($"Dispose Transaction(ID: {TransID}]");
            }
            catch (System.Exception ex)
            {
                Logger.Console.Fatal(ex, $"Dispose Transaction Exception(ID: {TransID}]");
            }
            base.Dispose();
        }
 
        #region WMS业务
        /// <summary>
        /// 执行中条码
        /// </summary>
        public string ProcessingSn {  get; set; }
        /// <summary>
        /// 执行中单据明细
        /// </summary>
        public string ProcessingOrderDetail { get; set; }
        /// <summary>
        /// 重置事务操作
        /// </summary>
        /// <returns></returns>
        public ApiAction Reset()
        {
            var action = new ApiAction();
 
            ResetTrans();
            action.IsSuccessed = true;
            //action.LocaleMsg = new($"操作已重置,请重新扫描");
            action.LocaleMsg = new("WMS.Transaction.Reset");
 
            return action;
        }
        /// <summary>
        /// 重置事务数据,有需要则重写此方法
        /// </summary>
        public virtual void ResetTrans()
        {
            ProcessingSn = null;
        }
        #endregion
 
        #region MQTT
        /// <summary>
        /// 设置当前条码的MQTT信息
        /// </summary>
        public ApiAction<ScanOutput> SetOutPutMqttMsg(ApiAction<ScanOutput> action, string locale = null)
        {
            MQTT.Message msg = new()
            {
                IsSuccessed = action.IsSuccessed,
                Content = Biz.T(action.LocaleMsg, locale),
            };
            switch (action.Status)
            {
                case ApiAction.StatusCodes.Success:
                    msg.Voice = MQTT.Voice.Success;
                    msg.Color = "#FF1E90FF";
                    break;
                case ApiAction.StatusCodes.Warning:
                    msg.Voice = MQTT.Voice.Warning;
                    msg.Color = "#FFB8860B";
                    break;
                case ApiAction.StatusCodes.Error:
                case ApiAction.StatusCodes.Failed:
                    msg.Voice = MQTT.Voice.Fail;
                    msg.Color = "#FFFF0000";
                    break;
                case ApiAction.StatusCodes.Exception:
                    msg.Voice = MQTT.Voice.Fail;
                    msg.Color = "#FF8B0000";
                    break;
                case ApiAction.StatusCodes.Normal:
                case ApiAction.StatusCodes.NeedConfrim:
                case ApiAction.StatusCodes.Confrimed:
                default:
                    msg.Voice = MQTT.Voice.Silent;
                    msg.Color = "#FF000000";
                    break;
            }
            if (action.IsSuccessed)
            {
                msg.Voice = MQTT.Voice.Success;
                msg.Color = "#FF228B22";
            }
            else if (!action.IsSuccessed)
            {
                msg.Voice = MQTT.Voice.Fail;
                msg.Color = "#FFFF0000";
            }
            else
            {
                msg.Voice = MQTT.Voice.Silent;
                msg.Color = "#FF000000";
            }
            action.Data.MqttMsg = msg;
            return action;
        }
 
        private Thread MQTTThread;
        private void Mqtt()
        {
            try
            {
                //Create the Connection factory  
                IConnectionFactory factory = new ConnectionFactory(ApiConfig.Configuration["MQTTService:IPAddress"]);
 
                //Create the connection  
                using (IConnection connection = factory.CreateConnection())
                {
                    connection.ClientId = $"{Environment.MachineName}-Receive-{TransID}";
                    connection.Start();
 
                    //Create the Session  
                    using (ISession session = connection.CreateSession())
                    {
                        //Create the Consumer  
                        IMessageConsumer consumer = session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic(TransID));
                        consumer.Listener += new MessageListener(consumer_Listener);
                        while (!IsFinished)
                        {
 
                        }
                    }
                    connection.Stop();
                    connection.Close();
                }
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        private string ReceiveMsg;
        private void consumer_Listener(IMessage message)
        {
            try
            {
                ITextMessage msg = (ITextMessage)message;
                Console.WriteLine("MQTT Receive: " + msg.Text);
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
 
        #endregion
    }
}