45 lines
1.3 KiB
C#
45 lines
1.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using uPLibrary.Networking.M2Mqtt;
|
|
using System.Net;
|
|
using uPLibrary.Networking.M2Mqtt.Messages;
|
|
|
|
namespace Common
|
|
{
|
|
public class BLWMQTT
|
|
{
|
|
private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(BLWMQTT));
|
|
public static MqttClient MqttClientGlobal { get; set; }
|
|
public static void StartMqtt()
|
|
{
|
|
try
|
|
{
|
|
// create client instance
|
|
MqttClientGlobal = new MqttClient(IPAddress.Parse("120.24.73.62"));
|
|
string clientId = Guid.NewGuid().ToString();
|
|
MqttClientGlobal.Connect(clientId, "admin", "blw@1234");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.Error("mqtt:" + ex.Message);
|
|
}
|
|
}
|
|
public static void MQTTPublishData(string topic, string payload)
|
|
{
|
|
try
|
|
{
|
|
if (MqttClientGlobal.IsConnected)
|
|
{
|
|
MqttClientGlobal.Publish(topic, Encoding.UTF8.GetBytes(payload), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, false);
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|