|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- using BPA.Message;
- using BPASmartClient.MQTT;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace BPASmartClient.Business
- {
- public class LocalMqtt
- {
- private volatile static LocalMqtt _Instance;
- public static LocalMqtt GetInstance => _Instance ?? (_Instance = new LocalMqtt());
- private LocalMqtt() { }
-
- /// <summary>
- ///
- /// </summary>
- SendScreenDataModel MqttPushs = new SendScreenDataModel();
-
- MQTTProxy mQTTProxy = new MQTTProxy();
-
- ScreenDeviceType LocDeviceType;
-
- public void Init(ScreenDeviceType DeviceType)
- {
- MqttPushs.Name = DeviceType;
- LocDeviceType = DeviceType;
- mQTTProxy.Connected = new Action(() =>
- {
- mQTTProxy.Subscrib(ScreenTOPIC.GetInstance.GetTopic(DeviceType)) ;
-
- });
- mQTTProxy.Connect("admin", "public", "10.2.1.254", 1883, $"{DeviceType}_设备监听数据{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
- }
-
- public void Start()
- {
-
- }
-
-
-
- /// <summary>
- /// MQTT消息推送
- /// </summary>
- /// <param name="topic">主题</param>
- /// <param name="message">消息体</param>
- public void Publish(object message)
- {
- MqttPushs.Value = message;
- mQTTProxy.Publish(ScreenTOPIC.GetInstance.GetTopic(LocDeviceType), JsonConvert.SerializeObject(MqttPushs));
- }
- }
- }
|