终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

125 lines
4.2 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. var deviceConfig = Plugin.GetInstance().GetPlugin<ConfigMgr>().deviceConfigModelJsons;
  60. string deviceId = deviceConfig[0].deviceModels[0].DeviceId;
  61. clientId = Plugin.GetInstance().GetPlugin<ConfigMgr>().ClientId;
  62. //MQTT 初始化
  63. mqttProxy.Connect(MqttServerAccount.UserName, MqttServerAccount.Password, MqttServerConfig.Host, MqttServerConfig.Port, "ClientId:" + clientId + "DeviceId:" + deviceId + Guid.NewGuid().ToString());
  64. ThreadManage.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 消息处理", 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.CloseConnect();
  111. messageRecives.Clear();
  112. }
  113. }
  114. }