终端一体化运控平台
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

126 řádky
4.1 KiB

  1. using BPA.Message;
  2. using BPA.Helper;
  3. using BPA.Communication;
  4. using Microsoft.Extensions.Configuration;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace BPASmartClient.Business
  12. {
  13. /// <summary>
  14. /// 消息处理接口
  15. /// </summary>
  16. public delegate void RecivedHandle(IMessage message);
  17. /// <summary>
  18. /// MQTT 管理类
  19. /// </summary>
  20. public class MQTTMgr : IPlugin
  21. {
  22. //客户端ID
  23. private int clientId;
  24. //运行标识
  25. private bool running = false;
  26. //消息缓存
  27. private ConcurrentQueue<string> msg = new ConcurrentQueue<string>();
  28. //MQTT 代理
  29. private MqttHelper mqttProxy = new MqttHelper();
  30. //消息处理者
  31. private List<RecivedHandle> messageRecives = new List<RecivedHandle>();
  32. public void Initialize()
  33. {
  34. }
  35. public void Start()
  36. {
  37. running = true;
  38. //主题初始化
  39. TopicDefine.GetInstance().Initialize(Plugin.GetInstance().GetPlugin<DeviceMgr>().GetDevices());
  40. mqttProxy.ConnectOk = () =>
  41. {
  42. mqttProxy.Subscrib(TopicDefine.GetInstance().SubscribTopics.ToArray());
  43. };
  44. //MQTT 数据接收
  45. mqttProxy.MessageRecive = s => { msg.Enqueue(s); };
  46. var MqttServerConfig = Plugin.GetInstance().GetPlugin<ConfigMgr>().MQTT_Config;
  47. var MqttServerAccount = Plugin.GetInstance().GetPlugin<ConfigMgr>().Mqtt_Account;
  48. var deviceConfig = Plugin.GetInstance().GetPlugin<ConfigMgr>().deviceConfigModelJsons;
  49. string deviceId = deviceConfig[0].deviceModels[0].DeviceId;
  50. clientId = Plugin.GetInstance().GetPlugin<ConfigMgr>().ClientId;
  51. //MQTT 初始化
  52. mqttProxy.Connect(new BPA.Communication.Base.ConfigurationOptions()
  53. {
  54. UserName = MqttServerAccount.UserName,
  55. Password = MqttServerAccount.Password,
  56. IpAddress = MqttServerConfig.Host,
  57. Port = MqttServerConfig.Port,
  58. ClientId = $"ClientId-[{clientId}]-DeviceId-[{deviceId}]-{Guid.NewGuid()}"
  59. });
  60. ThreadManage.GetInstance().Start(() =>
  61. {
  62. while (running)
  63. {
  64. while (msg.Count > 0 && msg.TryDequeue(out string temp))
  65. {
  66. if (0 == clientId)
  67. clientId = Plugin.GetInstance().GetPlugin<ConfigMgr>().ClientId;
  68. var package = BPAPackage.Deserialize(temp);
  69. if (package?.ClientId == clientId)
  70. {
  71. if (package.Message != null)
  72. {
  73. for (int i = messageRecives.Count - 1; i >= 0; i--)
  74. {
  75. messageRecives[i]?.Invoke(package.Message);
  76. }
  77. }
  78. }
  79. }
  80. Thread.Sleep(50);
  81. }
  82. }, "MQTT 消息处理", isRestart: true);
  83. }
  84. /// <summary>
  85. /// MQTT消息推送
  86. /// </summary>
  87. /// <param name="topic">主题</param>
  88. /// <param name="message">消息体</param>
  89. public void Publish(string topic, string message)
  90. {
  91. mqttProxy.Publish(topic, message);
  92. }
  93. /// <summary>
  94. /// 设置消息处理者
  95. /// </summary>
  96. /// <param name="messageRecive">消息处理者</param>
  97. public void SetMessageReciveHandler(RecivedHandle messageRecive)
  98. {
  99. if (messageRecives.Contains(messageRecive))
  100. return;
  101. messageRecives.Add(messageRecive);
  102. }
  103. public void Dispose()
  104. {
  105. running = false;
  106. mqttProxy.Dispose();
  107. messageRecives.Clear();
  108. }
  109. }
  110. }