终端一体化运控平台
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

LocalMqtt.cs 1.6 KiB

2 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using BPA.Message;
  2. using BPASmartClient.MQTT;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace BPASmartClient.Business
  10. {
  11. public class LocalMqtt
  12. {
  13. private volatile static LocalMqtt _Instance;
  14. public static LocalMqtt GetInstance => _Instance ?? (_Instance = new LocalMqtt());
  15. private LocalMqtt() { }
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. SendScreenDataModel MqttPushs = new SendScreenDataModel();
  20. MQTTProxy mQTTProxy = new MQTTProxy();
  21. ScreenDeviceType LocDeviceType;
  22. public void Init(ScreenDeviceType DeviceType)
  23. {
  24. MqttPushs.Name = DeviceType;
  25. LocDeviceType = DeviceType;
  26. mQTTProxy.Connected = new Action(() =>
  27. {
  28. mQTTProxy.Subscrib(ScreenTOPIC.GetInstance.GetTopic(DeviceType)) ;
  29. });
  30. mQTTProxy.Connect("admin", "public", "10.2.1.254", 1883, $"{DeviceType}_设备监听数据{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
  31. }
  32. public void Start()
  33. {
  34. }
  35. /// <summary>
  36. /// MQTT消息推送
  37. /// </summary>
  38. /// <param name="topic">主题</param>
  39. /// <param name="message">消息体</param>
  40. public void Publish(object message)
  41. {
  42. MqttPushs.Value = message;
  43. mQTTProxy.Publish(ScreenTOPIC.GetInstance.GetTopic(LocDeviceType), JsonConvert.SerializeObject(MqttPushs));
  44. }
  45. }
  46. }