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

75 行
2.3 KiB

  1. using BPA.Message;
  2. using BPA.Communication;
  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. MqttHelper mQTTProxy = new MqttHelper();
  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(new BPA.Communication.Base.ConfigurationOptions()
  39. {
  40. UserName = MQTT_USERNAME,
  41. Password = MQTT_PASSWORD,
  42. IpAddress = MQTT_IP,
  43. Port = int.Parse(MQTT_PORT),
  44. ClientId = $"{DeviceType}_设备监听数据{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"
  45. });
  46. }
  47. public void Start()
  48. {
  49. }
  50. /// <summary>
  51. /// MQTT消息推送
  52. /// </summary>
  53. /// <param name="topic">主题</param>
  54. /// <param name="message">消息体</param>
  55. public void Publish(object message)
  56. {
  57. MqttPushs.Value = message;
  58. mQTTProxy.Publish(ScreenTOPIC.GetInstance.GetTopic(LocDeviceType), JsonConvert.SerializeObject(MqttPushs));
  59. }
  60. }
  61. }