2017-10-02 18:26:38 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
2018-05-07 18:52:24 +02:00
|
|
|
|
using BlubbFish.Utils.IoT.Events;
|
2017-10-02 18:26:38 +02:00
|
|
|
|
using uPLibrary.Networking.M2Mqtt;
|
|
|
|
|
using uPLibrary.Networking.M2Mqtt.Messages;
|
|
|
|
|
|
2017-10-03 00:51:08 +02:00
|
|
|
|
namespace BlubbFish.Utils.IoT.Connector.Data {
|
2017-12-03 12:23:19 +01:00
|
|
|
|
public class Mqtt : ADataBackend, IDisposable {
|
2017-10-02 18:26:38 +02:00
|
|
|
|
private MqttClient client;
|
|
|
|
|
|
2018-05-07 18:52:24 +02:00
|
|
|
|
public Mqtt(Dictionary<String, String> settings) : base(settings) {
|
2018-06-05 18:46:56 +02:00
|
|
|
|
Int32 port = 1883;
|
|
|
|
|
if(this.settings.ContainsKey("port")) {
|
|
|
|
|
port = Int32.Parse(this.settings["port"]);
|
2017-10-02 18:26:38 +02:00
|
|
|
|
}
|
2018-06-05 18:46:56 +02:00
|
|
|
|
this.client = new MqttClient(this.settings["server"], port, false, null, null, MqttSslProtocols.None);
|
2017-10-02 18:26:38 +02:00
|
|
|
|
Connect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Connect() {
|
|
|
|
|
this.client.MqttMsgPublishReceived += this.Client_MqttMsgPublishReceived;
|
2018-06-05 18:46:56 +02:00
|
|
|
|
if (this.settings.ContainsKey("user") && this.settings.ContainsKey("pass")) {
|
|
|
|
|
this.client.Connect(Guid.NewGuid().ToString(), this.settings["user"], this.settings["pass"]);
|
|
|
|
|
} else {
|
|
|
|
|
this.client.Connect(Guid.NewGuid().ToString());
|
|
|
|
|
}
|
2018-09-06 22:26:38 +02:00
|
|
|
|
if (this.settings.ContainsKey("topic")) {
|
|
|
|
|
Int32 l = this.settings["topic"].Split(';').Length;
|
|
|
|
|
Byte[] qos = new Byte[l];
|
|
|
|
|
for (Int32 i = 0; i < qos.Length; i++) {
|
|
|
|
|
qos[i] = MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE;
|
|
|
|
|
}
|
|
|
|
|
this.client.Subscribe(this.settings["topic"].Split(';'), qos);
|
|
|
|
|
} else {
|
|
|
|
|
this.client.Subscribe(new String[] { "#" }, new Byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE });
|
|
|
|
|
}
|
2017-10-02 18:26:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Client_MqttMsgPublishReceived(Object sender, MqttMsgPublishEventArgs e) {
|
2018-05-07 18:52:24 +02:00
|
|
|
|
this.NotifyClientIncomming(new DataEvent(Encoding.UTF8.GetString(e.Message), e.Topic, DateTime.Now));
|
2017-10-02 18:26:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Send(String topic, String data) {
|
|
|
|
|
this.client.Publish(topic, Encoding.UTF8.GetBytes(data));
|
2018-05-07 18:52:24 +02:00
|
|
|
|
this.NotifyClientSending(new DataEvent(data, topic, DateTime.Now));
|
2017-10-02 18:26:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region IDisposable Support
|
|
|
|
|
private Boolean disposedValue = false;
|
|
|
|
|
|
2018-06-05 18:46:56 +02:00
|
|
|
|
public override Boolean IsConnected {
|
|
|
|
|
get {
|
|
|
|
|
if(this.client != null) {
|
|
|
|
|
return this.client.IsConnected;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-02 18:26:38 +02:00
|
|
|
|
|
|
|
|
|
protected virtual void Dispose(Boolean disposing) {
|
|
|
|
|
if(!this.disposedValue) {
|
2017-12-17 21:00:13 +01:00
|
|
|
|
if(disposing) {try {
|
|
|
|
|
this.client.MqttMsgPublishReceived -= this.Client_MqttMsgPublishReceived;
|
|
|
|
|
this.client.Unsubscribe(new String[] { "#" });
|
|
|
|
|
this.client.Disconnect();
|
|
|
|
|
} catch (Exception) { }
|
2017-10-02 18:26:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.client = null;
|
|
|
|
|
|
|
|
|
|
this.disposedValue = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-02 19:14:42 +02:00
|
|
|
|
|
2017-10-02 18:26:38 +02:00
|
|
|
|
public override void Dispose() {
|
|
|
|
|
Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|