MqttClientService.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using System.Text;
  2. using System.Threading.Tasks;
  3. using System;
  4. using MQTTnet.Client;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using MQTTnet;
  7. using MQTTnet.Protocol;
  8. using Microsoft.Extensions.Caching.Memory;
  9. using Newtonsoft.Json;
  10. using Newtonsoft.Json.Linq;
  11. using System.Collections.Generic;
  12. using System.Drawing;
  13. using Microsoft.AspNetCore.Mvc;
  14. using System.Linq;
  15. using Microsoft.Extensions.Logging;
  16. using System.Collections;
  17. using Swashbuckle.AspNetCore.SwaggerGen;
  18. //using static Ropin.IOT.MqttService.Controllers.ValueController;
  19. using Microsoft.AspNetCore.DataProtection.KeyManagement;
  20. using static Ropin.IOT.MqttService.MqttClientService;
  21. using System.Collections.Concurrent;
  22. using log4net;
  23. //using MQTTnet.Server;
  24. namespace Ropin.IOT.MqttService
  25. {
  26. public static class MqttClientService
  27. {
  28. public static IMqttClient _mqttClient;
  29. public static IMemoryCache _memoryCache;
  30. public static MqttOptions _mqttOptions;
  31. public static List<MqttDevicePoint> _mqttDevicePointList;
  32. public static ConcurrentDictionary<string, MqttDevicePoint> _mqttDevicePointDic;
  33. public static ConcurrentDictionary<string, string> _mqttDevicePointNameMappingDic;
  34. private static readonly ILog log = LogManager.GetLogger(typeof(MqttClientService));
  35. public static void AddMqttClient(this IServiceCollection services, MqttOptions mqttOptions)
  36. {
  37. _mqttDevicePointList = new List<MqttDevicePoint>();
  38. _mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e76", ChineseName = "模式", BoxId = "02700123060200004510", EnglishName = "MV", GroupName = "分组1" });
  39. _mqttDevicePointDic = new ConcurrentDictionary<string, MqttDevicePoint>();
  40. _mqttDevicePointNameMappingDic = new ConcurrentDictionary<string, string>();
  41. _mqttOptions = mqttOptions ?? new MqttOptions();
  42. var optionsBuilder = new MqttClientOptionsBuilder()
  43. .WithTcpServer(mqttOptions.Ip, mqttOptions.Port) // 要访问的mqtt服务端的 ip 和 端口号
  44. .WithCredentials(mqttOptions.UserName, mqttOptions.Password) // 要访问的mqtt服务端的用户名和密码
  45. .WithClientId(Guid.NewGuid().ToString()) // 设置客户端id
  46. .WithCleanSession()
  47. .WithTls(new MqttClientOptionsBuilderTlsParameters
  48. {
  49. UseTls = false // 是否使用 tls加密
  50. });
  51. var clientOptions = optionsBuilder.Build();
  52. _mqttClient = new MqttFactory().CreateMqttClient();
  53. _mqttClient.ConnectedAsync += _mqttClient_ConnectedAsync; // 客户端连接成功事件
  54. _mqttClient.DisconnectedAsync += _mqttClient_DisconnectedAsync; // 客户端连接关闭事件
  55. _mqttClient.ApplicationMessageReceivedAsync += MqttClient_ApplicationMessageReceivedAsync; // 收到消息事件
  56. _mqttClient.ConnectAsync(clientOptions);
  57. }
  58. public static void AddMqttClient1(MqttOptions mqttOptions,List<MqttDevicePoint> DevicePointList)
  59. {
  60. log.Info(" AddMqttClient1 ");
  61. //_mqttDevicePointList = new List<MqttDevicePoint>();
  62. _mqttDevicePointList = DevicePointList;
  63. _mqttDevicePointDic = new ConcurrentDictionary<string, MqttDevicePoint>();
  64. _mqttDevicePointNameMappingDic = new ConcurrentDictionary<string, string>();
  65. _mqttOptions = mqttOptions ?? new MqttOptions();
  66. var optionsBuilder = new MqttClientOptionsBuilder()
  67. .WithTcpServer(mqttOptions.Ip, mqttOptions.Port) // 要访问的mqtt服务端的 ip 和 端口号
  68. .WithCredentials(mqttOptions.UserName, mqttOptions.Password) // 要访问的mqtt服务端的用户名和密码
  69. .WithClientId(Guid.NewGuid().ToString()) // 设置客户端id
  70. .WithCleanSession()
  71. .WithTls(new MqttClientOptionsBuilderTlsParameters
  72. {
  73. UseTls = false // 是否使用 tls加密
  74. });
  75. var clientOptions = optionsBuilder.Build();
  76. _mqttClient = new MqttFactory().CreateMqttClient();
  77. _mqttClient.ConnectedAsync += _mqttClient_ConnectedAsync; // 客户端连接成功事件
  78. _mqttClient.DisconnectedAsync += _mqttClient_DisconnectedAsync; // 客户端连接关闭事件
  79. _mqttClient.ApplicationMessageReceivedAsync += MqttClient_ApplicationMessageReceivedAsync; // 收到消息事件
  80. _mqttClient.ConnectAsync(clientOptions);
  81. }
  82. /// <summary>
  83. /// 客户端连接关闭事件
  84. /// </summary>
  85. /// <param name="arg"></param>
  86. /// <returns></returns>
  87. private static Task _mqttClient_DisconnectedAsync(MqttClientDisconnectedEventArgs arg)
  88. {
  89. Console.WriteLine($"客户端已断开与服务端的连接……");
  90. _mqttClient.ReconnectAsync();
  91. return Task.CompletedTask;
  92. }
  93. /// <summary>
  94. /// 客户端连接成功事件
  95. /// </summary>
  96. /// <param name="arg"></param>
  97. /// <returns></returns>
  98. private static Task _mqttClient_ConnectedAsync(MqttClientConnectedEventArgs arg)
  99. {
  100. Console.WriteLine($"客户端已连接服务端……");
  101. #region 注释数据
  102. ////{"data":{"wendu":"wendu","shidu":"shidu","saosheng":"saosheng","PM2.5":"PM2.5","PM10":"PM10"},"boxid":"02700123060200004509"}
  103. ////从表读取
  104. ////{"data":{"worknormal":"node0101","workalarm":"1","powernormal":"0","handalarm":"0"},"boxid":"02700122091600005142"}
  105. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e66", ChineseName = "工作正常", BoxId = "02700122091600005142", EnglishName = "worknormal", GroupName = "分组1" });
  106. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e67", ChineseName = "报警", BoxId = "02700122091600005142", EnglishName = "workalarm", GroupName = "分组1" });
  107. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e68", ChineseName = "电源正常", BoxId = "02700122091600005142", EnglishName = "powernormal", GroupName = "分组1" });
  108. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e69", ChineseName = "手工报警", BoxId = "02700122091600005142", EnglishName = "handalarm", GroupName = "分组1" });
  109. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e70", ChineseName = "温度", BoxId = "02700123060200004509", EnglishName = "wendu", GroupName = "分组1" });
  110. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e71", ChineseName = "湿度", BoxId = "02700123060200004509", EnglishName = "shidu", GroupName = "分组1" });
  111. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e72", ChineseName = "噪声", BoxId = "02700123060200004509", EnglishName = "zaosheng", GroupName = "分组1" });
  112. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e73", ChineseName = "PM2.5", BoxId = "02700123060200004509", EnglishName = "PM2.5", GroupName = "分组1" });
  113. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e74", ChineseName = "PM10", BoxId = "02700123060200004509", EnglishName = "PM10", GroupName = "分组1" });
  114. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e75", ChineseName = "TVOC", BoxId = "02700123060200004509", EnglishName = "TVoC", GroupName = "分组1" });
  115. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e76", ChineseName = "模式", BoxId = "02700123060200004510", EnglishName = "MV", GroupName = "分组1" });
  116. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e77", ChineseName = "干燥值", BoxId = "02700123060200004510", EnglishName = "DV", GroupName = "分组1" });
  117. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e78", ChineseName = "开泵时间", BoxId = "02700123060200004510", EnglishName = "PTV", GroupName = "分组1" });
  118. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e79", ChineseName = "等待时间", BoxId = "02700123060200004510", EnglishName = "WTV", GroupName = "分组1" });
  119. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e80", ChineseName = "等待时间倍数", BoxId = "02700123060200004510", EnglishName = "WTM", GroupName = "分组1" });
  120. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e81", ChineseName = "模拟量", BoxId = "02700123060200004510", EnglishName = "AO", GroupName = "分组1" });
  121. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e82", ChineseName = "数据量", BoxId = "02700123060200004510", EnglishName = "DO", GroupName = "分组1" });
  122. ////{"data":{"wendu":"wendu","yacha":"yahca","DI1":"DI1","DI2":"DI2","DO1":"DO1","DO2":"DO2"},"boxid":"02700123110100000884"}
  123. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "0884ce00-8c61-4abb-8a8b-26e2fbf62e83", ChineseName = "炭箱吸附运行", BoxId = "02700123110100000884", EnglishName = "xifurun", GroupName = "分组1" });
  124. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e83", ChineseName = "温度", BoxId = "02700123110100000884", EnglishName = "wendu", GroupName = "分组1" });
  125. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e84", ChineseName = "压差", BoxId = "02700123110100000884", EnglishName = "yacha", GroupName = "分组1" });
  126. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e85", ChineseName = "启动", BoxId = "02700123110100000884", EnglishName = "DI1", GroupName = "分组1" });
  127. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e86", ChineseName = "报警", BoxId = "02700123110100000884", EnglishName = "DI2", GroupName = "分组1" });
  128. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e87", ChineseName = "电源正常", BoxId = "02700123110100000884", EnglishName = "DO1", GroupName = "分组1" });
  129. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e88", ChineseName = "温压监测报警", BoxId = "02700123110100000884", EnglishName = "DO2", GroupName = "分组1" });
  130. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e89", ChineseName = "废气进口浓度", BoxId = "02700123110100000884", EnglishName = "AI1", GroupName = "分组1" });
  131. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e90", ChineseName = "废气出口浓度", BoxId = "02700123110100000884", EnglishName = "AI2", GroupName = "分组1" });
  132. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e91", ChineseName = "一号温度", BoxId = "02700124041100008079", EnglishName = "wendu1", GroupName = "分组1" });
  133. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e92", ChineseName = "一号压差", BoxId = "02700124041100008079", EnglishName = "yacha1", GroupName = "分组1" });
  134. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e93", ChineseName = "设备运行", BoxId = "02700124041100008079", EnglishName = "run", GroupName = "分组1" });
  135. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e94", ChineseName = "设备报警", BoxId = "02700124041100008079", EnglishName = "alarm", GroupName = "分组1" });
  136. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "8079ce00-8c61-4abb-8a8b-26e2fbf62e94", ChineseName = "炭箱吸附运行", BoxId = "02700124041100008079", EnglishName = "htrun", GroupName = "分组1" });
  137. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e95", ChineseName = "一号温度", BoxId = "02700124041100007950", EnglishName = "wendu1", GroupName = "分组1" });
  138. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e96", ChineseName = "一号压差", BoxId = "02700124041100007950", EnglishName = "yacha1", GroupName = "分组1" });
  139. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e97", ChineseName = "设备运行", BoxId = "02700124041100007950", EnglishName = "run", GroupName = "分组1" });
  140. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e98", ChineseName = "设备报警", BoxId = "02700124041100007950", EnglishName = "alarm", GroupName = "分组1" });
  141. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "7950ce00-8c61-4abb-8a8b-26e2fbf62e98", ChineseName = "除尘器运行", BoxId = "02700124041100007950", EnglishName = "qchrun", GroupName = "分组1" });
  142. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "7764ce00-8c61-4abb-8a8b-26e2fbf62e99", ChineseName = "废气设备运行", BoxId = "02700124041100007764", EnglishName = "feiqirun", GroupName = "分组1" });
  143. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e99", ChineseName = "一号温度", BoxId = "02700124041100007764", EnglishName = "wendu1", GroupName = "分组1" });
  144. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62100", ChineseName = "一号压差", BoxId = "02700124041100007764", EnglishName = "yacha1", GroupName = "分组1" });
  145. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62101", ChineseName = "设备运行", BoxId = "02700124041100007764", EnglishName = "run", GroupName = "分组1" });
  146. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62102", ChineseName = "设备报警", BoxId = "02700124041100007764", EnglishName = "alarm", GroupName = "分组1" });
  147. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62103", ChineseName = "一号温度", BoxId = "02700124041100006737", EnglishName = "wendu1", GroupName = "分组1" });
  148. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62104", ChineseName = "一号压差", BoxId = "02700124041100006737", EnglishName = "yacha1", GroupName = "分组1" });
  149. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62105", ChineseName = "设备运行", BoxId = "02700124041100006737", EnglishName = "run", GroupName = "分组1" });
  150. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62106", ChineseName = "设备报警", BoxId = "02700124041100006737", EnglishName = "alarm", GroupName = "分组1" });
  151. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62107", ChineseName = "一号温度", BoxId = "02700124041100008529", EnglishName = "wendu1", GroupName = "分组1" });
  152. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62108", ChineseName = "一号压差", BoxId = "02700124041100008529", EnglishName = "yacha1", GroupName = "分组1" });
  153. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62109", ChineseName = "设备运行", BoxId = "02700124041100008529", EnglishName = "run", GroupName = "分组1" });
  154. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62110", ChineseName = "设备报警", BoxId = "02700124041100008529", EnglishName = "alarm", GroupName = "分组1" });
  155. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62111", ChineseName = "一号压差", BoxId = "02700124041100007872", EnglishName = "yacha1", GroupName = "分组1" });
  156. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62112", ChineseName = "二号压差", BoxId = "02700124041100007872", EnglishName = "yacha2", GroupName = "分组1" });
  157. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62113", ChineseName = "设备运行", BoxId = "02700124041100007872", EnglishName = "run", GroupName = "分组1" });
  158. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62114", ChineseName = "设备报警", BoxId = "02700124041100007872", EnglishName = "alarm", GroupName = "分组1" });
  159. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62115", ChineseName = "一号压差", BoxId = "02700124041100007544", EnglishName = "yacha1", GroupName = "分组1" });
  160. ////_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62116", ChineseName = "二号压差", BoxId = "02700124041100007544", EnglishName = "yacha2", GroupName = "分组1" });
  161. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62117", ChineseName = "设备运行", BoxId = "02700124041100007544", EnglishName = "run", GroupName = "分组1" });
  162. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62118", ChineseName = "设备报警", BoxId = "02700124041100007544", EnglishName = "alarm", GroupName = "分组1" });
  163. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62119", ChineseName = "一号压差", BoxId = "02700124041100007378", EnglishName = "yacha1", GroupName = "分组1" });
  164. ////_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62120", ChineseName = "二号压差", BoxId = "02700124041100007378", EnglishName = "yacha2", GroupName = "分组1" });
  165. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62121", ChineseName = "设备运行", BoxId = "02700124041100007378", EnglishName = "run", GroupName = "分组1" });
  166. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62122", ChineseName = "设备报警", BoxId = "02700124041100007378", EnglishName = "alarm", GroupName = "分组1" });
  167. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8c-26e2fbf62119", ChineseName = "一号压差", BoxId = "02700124041100007099", EnglishName = "yacha1", GroupName = "分组1" });
  168. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8c-26e2fbf62120", ChineseName = "二号压差", BoxId = "02700124041100007099", EnglishName = "yacha2", GroupName = "分组1" });
  169. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8c-26e2fbf62121", ChineseName = "三号压差", BoxId = "02700124041100007099", EnglishName = "yacha3", GroupName = "分组1" });
  170. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8c-26e2fbf62122", ChineseName = "设备运行", BoxId = "02700124041100007099", EnglishName = "run", GroupName = "分组1" });
  171. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8c-26e2fbf62123", ChineseName = "设备报警", BoxId = "02700124041100007099", EnglishName = "alarm", GroupName = "分组1" });
  172. ////_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62119", ChineseName = "一号压差", BoxId = "02700124041100007099", EnglishName = "yacha1", GroupName = "分组1" });
  173. ////_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62120", ChineseName = "二号压差", BoxId = "02700124041100007099", EnglishName = "yacha2", GroupName = "分组1" });
  174. ////_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62121", ChineseName = "三号压差", BoxId = "02700124041100007099", EnglishName = "yacha3", GroupName = "分组1" });
  175. ////_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62122", ChineseName = "设备运行", BoxId = "02700124041100007099", EnglishName = "run", GroupName = "分组1" });
  176. ////_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62123", ChineseName = "设备报警", BoxId = "02700124041100007099", EnglishName = "alarm", GroupName = "分组1" });
  177. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62124", ChineseName = "一号压差", BoxId = "02700124041100007266", EnglishName = "yacha1", GroupName = "分组1" });
  178. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62125", ChineseName = "二号压差", BoxId = "02700124041100007266", EnglishName = "yacha2", GroupName = "分组1" });
  179. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62126", ChineseName = "三号压差", BoxId = "02700124041100007266", EnglishName = "yacha3", GroupName = "分组1" });
  180. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62127", ChineseName = "设备运行", BoxId = "02700124041100007266", EnglishName = "run", GroupName = "分组1" });
  181. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62128", ChineseName = "设备报警", BoxId = "02700124041100007266", EnglishName = "alarm", GroupName = "分组1" });
  182. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62129", ChineseName = "一号压差", BoxId = "02700124041100007764", EnglishName = "yacha1", GroupName = "分组1" });
  183. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62130", ChineseName = "二号压差", BoxId = "02700124041100007764", EnglishName = "yacha2", GroupName = "分组1" });
  184. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62131", ChineseName = "三号压差", BoxId = "02700124041100007764", EnglishName = "yacha3", GroupName = "分组1" });
  185. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62132", ChineseName = "四号压差", BoxId = "02700124041100007764", EnglishName = "yacha4", GroupName = "分组1" });
  186. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62133", ChineseName = "五号压差", BoxId = "02700124041100007764", EnglishName = "yacha5", GroupName = "分组1" });
  187. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62134", ChineseName = "设备运行", BoxId = "02700124041100007764", EnglishName = "run", GroupName = "分组1" });
  188. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62135", ChineseName = "设备报警", BoxId = "02700124041100007764", EnglishName = "alarm", GroupName = "分组1" });
  189. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62136", ChineseName = "一号压差", BoxId = "02700124041100008780", EnglishName = "yacha1", GroupName = "分组1" });
  190. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62137", ChineseName = "二号压差", BoxId = "02700124041100008780", EnglishName = "yacha2", GroupName = "分组1" });
  191. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62138", ChineseName = "三号压差", BoxId = "02700124041100008780", EnglishName = "yacha3", GroupName = "分组1" });
  192. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62139", ChineseName = "四号压差", BoxId = "02700124041100008780", EnglishName = "yacha4", GroupName = "分组1" });
  193. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "7617ce00-8c61-4abb-8a8b-26e2fbf62140", ChineseName = "废气设备运行", BoxId = "02700124041100007617", EnglishName = "feiqirun", GroupName = "分组1" });
  194. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62140", ChineseName = "一号压差", BoxId = "02700124041100007617", EnglishName = "yacha1", GroupName = "分组1" });
  195. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62141", ChineseName = "二号压差", BoxId = "02700124041100007617", EnglishName = "yacha2", GroupName = "分组1" });
  196. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62142", ChineseName = "一号温度", BoxId = "02700124041100007617", EnglishName = "wendu1", GroupName = "分组1" });
  197. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62143", ChineseName = "二号温度", BoxId = "02700124041100007617", EnglishName = "wendu2", GroupName = "分组1" });
  198. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62144", ChineseName = "设备运行", BoxId = "02700124041100007617", EnglishName = "run", GroupName = "分组1" });
  199. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62145", ChineseName = "设备报警", BoxId = "02700124041100007617", EnglishName = "alarm", GroupName = "分组1" });
  200. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62146", ChineseName = "一号压差", BoxId = "02700124051000014941", EnglishName = "yacha1", GroupName = "分组1" });
  201. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62147", ChineseName = "一号温度", BoxId = "02700124051000014941", EnglishName = "wendu1", GroupName = "分组1" });
  202. //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62148", ChineseName = "废气设备运行", BoxId = "02700124051000014941", EnglishName = "feiqirun", GroupName = "分组1" });
  203. #endregion
  204. foreach (var dp in _mqttDevicePointList)
  205. {
  206. _mqttDevicePointDic.TryAdd(dp.BoxId + "分组1" + dp.EnglishName, dp);
  207. _mqttDevicePointNameMappingDic.TryAdd(dp.BoxId + "分组1" + dp.ChineseName, dp.BoxId + "分组1" + dp.EnglishName);
  208. }
  209. // 订阅消息主题
  210. // MqttQualityOfServiceLevel: (QoS): 0 最多一次,接收者不确认收到消息,并且消息不被发送者存储和重新发送提供与底层 TCP 协议相同的保证。
  211. // 1: 保证一条消息至少有一次会传递给接收方。发送方存储消息,直到它从接收方收到确认收到消息的数据包。一条消息可以多次发送或传递。
  212. // 2: 保证每条消息仅由预期的收件人接收一次。级别2是最安全和最慢的服务质量级别,保证由发送方和接收方之间的至少两个请求/响应(四次握手)。
  213. _mqttClient.SubscribeAsync(_mqttOptions.Theme, MqttQualityOfServiceLevel.AtLeastOnce); //topic_02
  214. return Task.CompletedTask;
  215. }
  216. public class MqttDeviceData
  217. {
  218. public Dictionary<string,string> data { set; get; }
  219. public string boxid { set; get; }
  220. }
  221. /// <summary>
  222. /// 收到消息事件
  223. /// </summary>
  224. /// <param name="arg"></param>
  225. /// <returns></returns>
  226. private static Task MqttClient_ApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs arg)
  227. {
  228. Console.WriteLine(Encoding.UTF8.GetString(arg?.ApplicationMessage?.PayloadSegment.ToArray()));
  229. var payload = Encoding.UTF8.GetString(arg?.ApplicationMessage?.PayloadSegment.ToArray());
  230. MqttDeviceData mqttDeviceData = JsonConvert.DeserializeObject<MqttDeviceData>(payload);
  231. if (mqttDeviceData != null && mqttDeviceData.data.Count > 0)
  232. {
  233. foreach (var key in mqttDeviceData.data.Keys)
  234. {
  235. MqttDevicePoint mqttDevicePoint = _mqttDevicePointDic[mqttDeviceData.boxid + "分组1" + key];
  236. mqttDevicePoint.BoxStatus = "1";
  237. mqttDevicePoint.DataType = "3";
  238. mqttDevicePoint.UpLoadTime = DateTime.Now;
  239. mqttDevicePoint.Value = mqttDeviceData.data[key];
  240. _mqttDevicePointDic[mqttDeviceData.boxid + "分组1" + key] = mqttDevicePoint;// mqttDeviceData.data[key];
  241. }
  242. }
  243. //_memoryCache.Set(json["boxId"].ToString(), payload);
  244. //Console.WriteLine(Encoding.UTF8.GetString(arg?.ApplicationMessage?.PayloadSegment.ToArray()));
  245. //Console.WriteLine($"ApplicationMessageReceivedAsync:客户端ID=【{arg.ClientId}】接收到消息。 Topic主题=【{arg.ApplicationMessage.Topic}】 消息=【{Encoding.UTF8.GetString(arg.ApplicationMessage.Payload)}】 qos等级=【{arg.ApplicationMessage.QualityOfServiceLevel}】");
  246. return Task.CompletedTask;
  247. }
  248. }
  249. }