终端一体化运控平台
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.
 
 
 

84 rivejä
2.8 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. using BPA.Communication.Interfaces;
  10. using BPA.Helper;
  11. namespace BPASmartClient.Business
  12. {
  13. public class LocalMqtt
  14. {
  15. private volatile static LocalMqtt _Instance;
  16. public static LocalMqtt GetInstance => _Instance ?? (_Instance = new LocalMqtt());
  17. private LocalMqtt() { }
  18. /// <summary>
  19. ///
  20. /// </summary>
  21. SendScreenDataModel MqttPushs = new SendScreenDataModel();
  22. //MqttHelper mQTTProxy = new MqttHelper();
  23. ICommMqtt mQTTProxy = CommHelper.MQTT;
  24. ScreenDeviceType LocDeviceType;
  25. string MQTT_IP;
  26. string MQTT_PORT;
  27. string MQTT_USERNAME;
  28. string MQTT_PASSWORD;
  29. public void Init(ScreenDeviceType DeviceType)
  30. {
  31. MqttPushs.Name = DeviceType;
  32. LocDeviceType = DeviceType;
  33. //mQTTProxy.Connected = new Action(() =>
  34. //{
  35. // mQTTProxy.Subscrib(ScreenTOPIC.GetInstance.GetTopic(DeviceType));
  36. //});
  37. MQTT_IP = System.Configuration.ConfigurationManager.AppSettings["MQTT_DevieScreem_IP"].ToString();
  38. MQTT_PORT = System.Configuration.ConfigurationManager.AppSettings["MQTT_DevieScreem_Port"].ToString();
  39. MQTT_USERNAME = System.Configuration.ConfigurationManager.AppSettings["MQTT_DevieScreem_User"].ToString();
  40. MQTT_PASSWORD = System.Configuration.ConfigurationManager.AppSettings["MQTT_DevieScreem_Password"].ToString();
  41. string ClientId = $"{DeviceType}_设备监听数据{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}";
  42. CommHelper.CreateMqtt(MQTT_IP, MQTT_USERNAME, MQTT_PASSWORD, ClientId, int.Parse(MQTT_PORT)).OnSuccess((s) =>
  43. {
  44. mQTTProxy = s.Content;
  45. mQTTProxy.Subscrib(ScreenTOPIC.GetInstance.GetTopic(DeviceType));
  46. });
  47. //mQTTProxy.Connect(new BPA.Communication.Base.ConfigurationOptions()
  48. //{
  49. // UserName = MQTT_USERNAME,
  50. // Password = MQTT_PASSWORD,
  51. // IpAddress = MQTT_IP,
  52. // Port = int.Parse(MQTT_PORT),
  53. // ClientId = $"{DeviceType}_设备监听数据{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"
  54. //});
  55. }
  56. public void Start()
  57. {
  58. }
  59. /// <summary>
  60. /// MQTT消息推送
  61. /// </summary>
  62. /// <param name="topic">主题</param>
  63. /// <param name="message">消息体</param>
  64. public void Publish(object message)
  65. {
  66. MqttPushs.Value = message;
  67. mQTTProxy.Publish(ScreenTOPIC.GetInstance.GetTopic(LocDeviceType), JsonConvert.SerializeObject(MqttPushs));
  68. }
  69. }
  70. }