using BPA.Message;
using BPA.Communication;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BPA.Communication.Interfaces;
using BPA.Helper;
namespace BPASmartClient.Business
{
public class LocalMqtt
{
private volatile static LocalMqtt _Instance;
public static LocalMqtt GetInstance => _Instance ?? (_Instance = new LocalMqtt());
private LocalMqtt() { }
///
///
///
SendScreenDataModel MqttPushs = new SendScreenDataModel();
//MqttHelper mQTTProxy = new MqttHelper();
ICommMqtt mQTTProxy = CommHelper.MQTT;
ScreenDeviceType LocDeviceType;
string MQTT_IP;
string MQTT_PORT;
string MQTT_USERNAME;
string MQTT_PASSWORD;
public void Init(ScreenDeviceType DeviceType)
{
MqttPushs.Name = DeviceType;
LocDeviceType = DeviceType;
//mQTTProxy.Connected = new Action(() =>
//{
// mQTTProxy.Subscrib(ScreenTOPIC.GetInstance.GetTopic(DeviceType));
//});
MQTT_IP = System.Configuration.ConfigurationManager.AppSettings["MQTT_DevieScreem_IP"].ToString();
MQTT_PORT = System.Configuration.ConfigurationManager.AppSettings["MQTT_DevieScreem_Port"].ToString();
MQTT_USERNAME = System.Configuration.ConfigurationManager.AppSettings["MQTT_DevieScreem_User"].ToString();
MQTT_PASSWORD = System.Configuration.ConfigurationManager.AppSettings["MQTT_DevieScreem_Password"].ToString();
string ClientId = $"{DeviceType}_设备监听数据{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}";
CommHelper.CreateMqtt(MQTT_IP, MQTT_USERNAME, MQTT_PASSWORD, ClientId, int.Parse(MQTT_PORT)).OnSuccess((s) =>
{
mQTTProxy = s.Content;
mQTTProxy.Subscrib(ScreenTOPIC.GetInstance.GetTopic(DeviceType));
});
//mQTTProxy.Connect(new BPA.Communication.Base.ConfigurationOptions()
//{
// UserName = MQTT_USERNAME,
// Password = MQTT_PASSWORD,
// IpAddress = MQTT_IP,
// Port = int.Parse(MQTT_PORT),
// ClientId = $"{DeviceType}_设备监听数据{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"
//});
}
public void Start()
{
}
///
/// MQTT消息推送
///
/// 主题
/// 消息体
public void Publish(object message)
{
MqttPushs.Value = message;
mQTTProxy.Publish(ScreenTOPIC.GetInstance.GetTopic(LocDeviceType), JsonConvert.SerializeObject(MqttPushs));
}
}
}