2017-10-02 18:26:38 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using uPLibrary.Networking.M2Mqtt;
|
|
|
|
|
using uPLibrary.Networking.M2Mqtt.Messages;
|
|
|
|
|
|
2017-10-03 00:51:08 +02:00
|
|
|
|
namespace BlubbFish.Utils.IoT.Connector.Data {
|
2017-10-02 18:26:38 +02:00
|
|
|
|
class Mqtt : ADataBackend, IDisposable {
|
|
|
|
|
private MqttClient client;
|
|
|
|
|
|
|
|
|
|
public override event MqttMessage MessageIncomming;
|
|
|
|
|
public override event MqttMessage MessageSending;
|
|
|
|
|
|
|
|
|
|
public Mqtt(Dictionary<String, String> settings) {
|
|
|
|
|
if(settings.ContainsKey("port")) {
|
|
|
|
|
this.client = new MqttClient(settings["server"], Int32.Parse(settings["port"]), false, null, null, MqttSslProtocols.None);
|
|
|
|
|
} else {
|
|
|
|
|
this.client = new MqttClient(settings["server"]);
|
|
|
|
|
}
|
|
|
|
|
Connect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Connect() {
|
|
|
|
|
this.client.MqttMsgPublishReceived += this.Client_MqttMsgPublishReceived;
|
|
|
|
|
this.client.Connect(Guid.NewGuid().ToString());
|
|
|
|
|
this.client.Subscribe(new String[] { "#" }, new Byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Client_MqttMsgPublishReceived(Object sender, MqttMsgPublishEventArgs e) {
|
|
|
|
|
this.MessageIncomming?.Invoke(this, new MqttEventArgs(Encoding.UTF8.GetString(e.Message), e.Topic));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Send(String topic, String data) {
|
|
|
|
|
this.client.Publish(topic, Encoding.UTF8.GetBytes(data));
|
|
|
|
|
this.MessageSending?.Invoke(this, new MqttEventArgs(data, topic));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region IDisposable Support
|
|
|
|
|
private Boolean disposedValue = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void Dispose(Boolean disposing) {
|
|
|
|
|
if(!this.disposedValue) {
|
|
|
|
|
if(disposing) {
|
|
|
|
|
this.client.MqttMsgPublishReceived -= this.Client_MqttMsgPublishReceived;
|
|
|
|
|
this.client.Unsubscribe(new String[] { "#" });
|
|
|
|
|
this.client.Disconnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.client = null;
|
|
|
|
|
|
|
|
|
|
this.disposedValue = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
~Mqtt() {
|
|
|
|
|
Dispose(false);
|
|
|
|
|
}
|
|
|
|
|
public override void Dispose() {
|
|
|
|
|
Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|