终端一体化运控平台
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

68 linhas
2.2 KiB

  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. string MQTT_IP;
  23. string MQTT_PORT;
  24. string MQTT_USERNAME;
  25. string MQTT_PASSWORD;
  26. public void Init(ScreenDeviceType DeviceType)
  27. {
  28. MqttPushs.Name = DeviceType;
  29. LocDeviceType = DeviceType;
  30. mQTTProxy.Connected = new Action(() =>
  31. {
  32. mQTTProxy.Subscrib(ScreenTOPIC.GetInstance.GetTopic(DeviceType)) ;
  33. });
  34. MQTT_IP = System.Configuration.ConfigurationManager.AppSettings["MQTT_DevieScreem_IP"].ToString();
  35. MQTT_PORT = System.Configuration.ConfigurationManager.AppSettings["MQTT_DevieScreem_Port"].ToString();
  36. MQTT_USERNAME = System.Configuration.ConfigurationManager.AppSettings["MQTT_DevieScreem_User"].ToString();
  37. MQTT_PASSWORD = System.Configuration.ConfigurationManager.AppSettings["MQTT_DevieScreem_Password"].ToString();
  38. mQTTProxy.Connect(MQTT_USERNAME, MQTT_PASSWORD, MQTT_IP, int.Parse(MQTT_PORT), $"{DeviceType}_设备监听数据{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");//"10.2.1.254""admin", "public"
  39. }
  40. public void Start()
  41. {
  42. }
  43. /// <summary>
  44. /// MQTT消息推送
  45. /// </summary>
  46. /// <param name="topic">主题</param>
  47. /// <param name="message">消息体</param>
  48. public void Publish(object message)
  49. {
  50. MqttPushs.Value = message;
  51. mQTTProxy.Publish(ScreenTOPIC.GetInstance.GetTopic(LocDeviceType), JsonConvert.SerializeObject(MqttPushs));
  52. }
  53. }
  54. }