终端一体化运控平台
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

130 righe
4.2 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.Connected = () =>
  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 = "";
  50. if (deviceConfig.Count > 0 && deviceConfig[0].deviceModels.Count > 0)
  51. {
  52. deviceId = deviceConfig[0].deviceModels[0].DeviceId;
  53. }
  54. clientId = Plugin.GetInstance().GetPlugin<ConfigMgr>().ClientId;
  55. //MQTT 初始化
  56. mqttProxy.Connect(new BPA.Communication.Base.ConfigurationOptions()
  57. {
  58. UserName = MqttServerAccount.UserName,
  59. Password = MqttServerAccount.Password,
  60. IpAddress = MqttServerConfig.Host,
  61. Port = MqttServerConfig.Port,
  62. ClientId = $"ClientId-[{clientId}]-DeviceId-[{deviceId}]-{Guid.NewGuid()}"
  63. });
  64. TaskManage.GetInstance.Start(() =>
  65. {
  66. while (running)
  67. {
  68. while (msg.Count > 0 && msg.TryDequeue(out string temp))
  69. {
  70. if (0 == clientId)
  71. clientId = Plugin.GetInstance().GetPlugin<ConfigMgr>().ClientId;
  72. var package = BPAPackage.Deserialize(temp);
  73. if (package?.ClientId == clientId)
  74. {
  75. if (package.Message != null)
  76. {
  77. for (int i = messageRecives.Count - 1; i >= 0; i--)
  78. {
  79. messageRecives[i]?.Invoke(package.Message);
  80. }
  81. }
  82. }
  83. }
  84. Thread.Sleep(50);
  85. }
  86. }, "MQTT 消息处理", isRestart: true);
  87. }
  88. /// <summary>
  89. /// MQTT消息推送
  90. /// </summary>
  91. /// <param name="topic">主题</param>
  92. /// <param name="message">消息体</param>
  93. public void Publish(string topic, string message)
  94. {
  95. mqttProxy.Publish(topic, message);
  96. }
  97. /// <summary>
  98. /// 设置消息处理者
  99. /// </summary>
  100. /// <param name="messageRecive">消息处理者</param>
  101. public void SetMessageReciveHandler(RecivedHandle messageRecive)
  102. {
  103. if (messageRecives.Contains(messageRecive))
  104. return;
  105. messageRecives.Add(messageRecive);
  106. }
  107. public void Dispose()
  108. {
  109. running = false;
  110. mqttProxy.Dispose();
  111. messageRecives.Clear();
  112. }
  113. }
  114. }