终端一体化运控平台
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. MqttHelper.GetInstance().MqttSubscriptionAsync(TopicDefine.GetInstance().SubscribTopics.ToArray());
  51. });
  52. //MQTT 数据接收
  53. mqttProxy.MessageRecive = new Action<string>((message) =>
  54. {
  55. msg.Enqueue(message);
  56. });
  57. MessageLog.GetInstance.Show("准备连接MQTT");
  58. //MQTT 初始化
  59. mqttProxy.Connect(Plugin.GetInstance().GetPlugin<ConfigMgr>().MQTT_Config.Host, Plugin.GetInstance().GetPlugin<ConfigMgr>().MQTT_Config.Port,
  60. 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. }