终端一体化运控平台
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

121 rinda
3.9 KiB

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