服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2025-03-18 05fdade2564f0880fcba935aadf12e79a467764b
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
using Microsoft.Extensions.Configuration;
using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Protocol;
using MQTTnet.Server;
using Newtonsoft.Json;
using Rhea.Common;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
 
namespace Tiger.IBusiness.Utility
{
    /// <summary>
    /// MQTTHelper
    /// </summary>
    public static class MQTTHelper
    {
        #region Variables
        private static IConfiguration Setting = ApiConfig.Configuration;
        #endregion
 
        #region Propertys
 
        #endregion
 
        #region Functions
        public static void Start()
        {
            try
            {
                if (Setting["MQTT:Enable"].ToBoolean() == true)
                {
 
                    ConsoleExt.WriteLine("Start MQTT Service..........", ConsoleColor.Yellow);
                    Logger.Default.Info("Start MQTT Service");
                }
            }
            catch (System.Exception ex)
            {
                Logger.Default.Fatal(ex, "Start MQTT Service Exception");
            }
        }
 
        public static void Stop()
        {
            try
            {
 
                Logger.Console.Info("Stop MQTT Service");
            }
            catch (System.Exception ex)
            {
                Logger.Console.Fatal(ex, "Stop MQTT Service Exception");
            }
        }
 
 
        #endregion
    }
    /// <summary>
    /// EMQX 帮助类
    /// </summary>
    public class EMQX : IDisposable
    {
        public EMQX(string address, string username, string password) 
        { 
            Address = address;
            Username = username;
            Password = password;
        }
 
        public EMQX(string address, int port, string username, string password)
        {
            Address = address;
            Port = port;
            Username = username;
            Password = password;
        }
 
        #region Variables
        private List<string> TopicList = new();
        #endregion
 
        #region Propertys
        public string Id { get; set; } = Guid.NewGuid().ToString("N");
        public string Tag { get; set; } = "ClientMonitor";
        public string Name { get; set; } = "ClientMonitor";
        public bool IsRunning { get; set; }
        public IMqttClient Client { get; set; } = new MqttFactory().CreateMqttClient();//MQTT客户端
        public string Address { get; set; }
        public int Port { get; set; } = 1883;
        public string ClientId { get; set; } = $"{Environment.MachineName}-ApiClient-{Guid.NewGuid():N}";
        public string Username { get; set; }
        public string Password { get; set; }
        public int ApiPort { get; set; } = 18083;
        public string ApiKey { get; set; }
        public string ApiSecretKey { get; set; }
        public string ApiCredentials => Convert.ToBase64String(Encoding.ASCII.GetBytes($"{ApiKey}:{ApiSecretKey}"));
        //遗嘱消息
        public string WillMsgTopic { get; set; }
        public MqttQualityOfServiceLevel WillMsgQos { get; set; }
        public bool WillMsgRetain { get; set; }
        public string WillMsgPayload { get; set; }
        public int? WillMsgDelay { get; set; }
        public int? WillMsgExpiry { get; set; }
        #endregion
 
 
        public Result<IMqttClient> Connect(string clientId = null)
        {
            Result<IMqttClient> result = new();
            if (!Address.IsNullOrEmpty())
            {
                ClientId = clientId.IsNullOrEmpty(ClientId);
                var options = new MqttClientOptionsBuilder()
                    .WithTcpServer(Address, Port) // MQTT broker address and port
                    .WithCredentials(Username, Password) // Set username and password
                    .WithClientId(ClientId)
                    .WithCleanSession()
                    .Build();
                var connectResult = Client.ConnectAsync(options).Result;
                if (connectResult.ResultCode == MqttClientConnectResultCode.Success)
                {
                    result.Flag = Result.Flags.Success;
                    result.Data = Client;
                    result.Message = "Connect to MQTT server success.";
                }
                else
                {
                    result.Flag = Result.Flags.Failed;
                    result.Data = null;
                    result.Message = $"Failed to connect to MQTT server: {connectResult.ResultCode}";
                }
            }
            else
            {
                result.Flag = Result.Flags.Failed;
                result.Data = null;
                result.Message = $"Failed to connect to MQTT server: no server address.";
            }
            return result;
        }
 
        public void Disconnect()
        {
            foreach (var topic in TopicList)
            {
                Client.UnsubscribeAsync(topic).Wait();
            }
            Client.DisconnectAsync().Wait();
        }
 
        public void Dispose()
        {
            Disconnect();
        }
 
        public bool Subscribe(string topic, Action<MqttApplicationMessageReceivedEventArgs> onReceive, MqttQualityOfServiceLevel QoS = MqttQualityOfServiceLevel.AtMostOnce)
        {
            if (TopicList.Contains(topic))
            {
                return true;
            }
            else
            { 
                try
                {
                    if (!Client.IsConnected)
                    {
                        Client.ReconnectAsync().Wait();
                    }
 
                    // Subscribe to a topic
                    Client.SubscribeAsync(topic, QoS).Wait();
                    TopicList.Add(topic);
    
                    // Callback function when a message is received
                    Client.ApplicationMessageReceivedAsync += e =>
                    {
                        onReceive.Invoke(e);
                        return Task.CompletedTask;
                    };
    
                    return true;
                }
                catch (System.Exception ex)
                {
                    return false;
                }
            }
        }
 
        public void Unsubscribe(string topic)
        {
            // Unsubscribe
            Client.UnsubscribeAsync(topic).Wait();
            TopicList.Remove(topic);
        }
 
        /// <summary>
        /// 发送一个MQ消息
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="topic">消息的Topic,移动端根据这个Topic来接收消息<param>
        public async Task<bool> SendAsync(string topic, string msg, MqttQualityOfServiceLevel QoS)
        {
            bool isFinish = false;
            int doTimes = 0;
            do
            {
                doTimes++;
                try
                {
                    if (!Client.IsConnected)
                    {
                        await Client.ReconnectAsync();
                    }
                    
                    // Publish a message
                    await Client.PublishAsync(new MqttApplicationMessageBuilder()
                        .WithTopic(topic)
                        .WithPayload(msg)
                        .WithQualityOfServiceLevel(QoS)
                        .WithRetainFlag()
                        .Build());
 
                    isFinish = true;
                }
                catch (System.Exception ex)
                {
                    //Logger.Console.Fatal(ex, $"Send MQTT Message[{sessionId}] Exception(Text: {msg})");
                }
            } while (!isFinish && doTimes < 3);
            return isFinish;
        }
 
        /// <summary>
        /// 发送一个内容是ApiAction的MQ消息,以ApiAction的ID作为消息的Topic
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="action"></param>
        /// <returns></returns>
        public ApiAction<T> Send<T>(ApiAction<T> action, MqttQualityOfServiceLevel QoS = MqttQualityOfServiceLevel.AtLeastOnce)
        {
            MQTTMessage message = new MQTTMessage();
            message.IsVoice = !action.IsSuccessed;
            message.IsSuccessed = action.IsSuccessed;
            message.Content = action.Message;
            message.Color = action.IsSuccessed ? "#878787" : "#880000";
            SendAsync(JsonConvert.SerializeObject(message), action.ID, QoS).Wait();
            return action;
        }
 
        public bool Send<T>(string topic, Result<T> result, string msgTitle, MqttQualityOfServiceLevel QoS = MqttQualityOfServiceLevel.AtLeastOnce)
        {
            try
            {
                MQTTMessage message = new MQTTMessage();
                message.IsVoice = !result.IsSuccessed;
                message.IsSuccessed = result.IsSuccessed;
                message.Content = msgTitle + ":" + result.Message;
                message.Color = result.IsSuccessed ? "#878787" : "#880000";
                return SendAsync(topic, JsonConvert.SerializeObject(message), QoS).Result;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }
 
        public bool Send(string topic, MQTTMessage msg, MqttQualityOfServiceLevel QoS = MqttQualityOfServiceLevel.AtLeastOnce)
        {
            try
            {
                return SendAsync(topic, JsonConvert.SerializeObject(msg), QoS).Result;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }
 
        public HttpResponseMessage GetTopicList(string topic = "")
        {
            //var content = new StringContent(json);
            //content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
 
            //var client = new HttpClient();
            //var response = client.PostAsync(url, content).Result;
 
            ApiKey = "ee26b0dd4af7e749";
            ApiSecretKey = "XHOUC9BQSoa0j0jRINQEkQanIR1nzDQUY8GnYtaktEQJ";
            string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{ApiKey}:{ApiSecretKey}"));
 
            var client = new HttpClient();
            client.DefaultRequestHeaders.Add("Content-Type", "application/json");
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", ApiCredentials);// 添加基本认证头
 
            string url = $"http://{Address}:{ApiPort}/api/v5/topics/";
            var response = client.GetAsync(url).Result;
            return response;
        }
 
        public bool DeleteTopic(string sessionId)
        {
            int doTimes = 0;
            bool isFinish = false;
            do
            {
                doTimes++;
                try
                {
                    
 
                    isFinish = true;
                }
                catch (Exception ex)
                {
                    //Logger.Console.Fatal(ex, $"Delete MQTT Topic[{sessionId}] Exception");
                }
            } while (!isFinish && doTimes < 3);
            return isFinish;
        }
    }
 
}