|
|
|
@@ -11,9 +11,10 @@ namespace BlubbFish.Utils.IoT.Connector.Data {
|
|
|
|
|
public class Mqtt : ADataBackend, IDisposable {
|
|
|
|
|
private MqttClient client;
|
|
|
|
|
private Thread connectionWatcher;
|
|
|
|
|
private Boolean connectionWatcherRunning;
|
|
|
|
|
|
|
|
|
|
public Mqtt(Dictionary<String, String> settings) : base(settings) {
|
|
|
|
|
Console.WriteLine("BlubbFish.Utils.IoT.Connector.Data.Mqtt.Mqtt()");
|
|
|
|
|
Console.WriteLine("BlubbFish.Utils.IoT.Connector.Data.Mqtt(" + this.ToString()+")");
|
|
|
|
|
Int32 port = 1883;
|
|
|
|
|
if(this.settings.ContainsKey("port")) {
|
|
|
|
|
port = Int32.Parse(this.settings["port"]);
|
|
|
|
@@ -22,25 +23,45 @@ namespace BlubbFish.Utils.IoT.Connector.Data {
|
|
|
|
|
this.ConnectionWatcher();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override String ToString() {
|
|
|
|
|
String ret = "mqtt://";
|
|
|
|
|
if (this.settings.ContainsKey("user")) {
|
|
|
|
|
ret += this.settings["user"];
|
|
|
|
|
if (this.settings.ContainsKey("pass")) {
|
|
|
|
|
ret += ":" + this.settings["pass"];
|
|
|
|
|
}
|
|
|
|
|
ret += "@";
|
|
|
|
|
}
|
|
|
|
|
ret += this.settings["server"];
|
|
|
|
|
if (this.settings.ContainsKey("port")) {
|
|
|
|
|
ret += ":" + this.settings["port"];
|
|
|
|
|
}
|
|
|
|
|
if (this.settings.ContainsKey("topic")) {
|
|
|
|
|
ret += "/" + this.settings["topic"];
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region ConectionManage
|
|
|
|
|
private void ConnectionWatcher() {
|
|
|
|
|
this.connectionWatcher = new Thread(this.ConnectionWatcherRunner);
|
|
|
|
|
this.connectionWatcherRunning = true;
|
|
|
|
|
this.connectionWatcher.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ConnectionWatcherRunner() {
|
|
|
|
|
while(true) {
|
|
|
|
|
while(this.connectionWatcherRunning) {
|
|
|
|
|
try {
|
|
|
|
|
if(!this.IsConnected) {
|
|
|
|
|
this.Reconnect();
|
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
}
|
|
|
|
|
Thread.Sleep(500);
|
|
|
|
|
Thread.Sleep(10);
|
|
|
|
|
} catch(Exception) { }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Reconnect() {
|
|
|
|
|
Console.WriteLine("BlubbFish.Utils.IoT.Connector.Data.Mqtt.Reconnect()");
|
|
|
|
|
if(this.IsConnected) {
|
|
|
|
|
this.Disconnect(true);
|
|
|
|
|
} else {
|
|
|
|
@@ -50,7 +71,7 @@ namespace BlubbFish.Utils.IoT.Connector.Data {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Disconnect(Boolean complete) {
|
|
|
|
|
Console.WriteLine("BlubbFish.Utils.IoT.Connector.Data.Mqtt.Disconnect()");
|
|
|
|
|
Console.WriteLine("BlubbFish.Utils.IoT.Connector.Data.Disconnect(" + this.ToString() + ")");
|
|
|
|
|
this.client.MqttMsgPublishReceived -= this.Client_MqttMsgPublishReceived;
|
|
|
|
|
this.Unsubscripe();
|
|
|
|
|
if(complete) {
|
|
|
|
@@ -59,7 +80,7 @@ namespace BlubbFish.Utils.IoT.Connector.Data {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Connect() {
|
|
|
|
|
Console.WriteLine("BlubbFish.Utils.IoT.Connector.Data.Mqtt.Connect()");
|
|
|
|
|
Console.WriteLine("BlubbFish.Utils.IoT.Connector.Data.Connect(" + this.ToString() + ")");
|
|
|
|
|
this.client.MqttMsgPublishReceived += this.Client_MqttMsgPublishReceived;
|
|
|
|
|
_ = this.settings.ContainsKey("user") && this.settings.ContainsKey("pass")
|
|
|
|
|
? this.client.Connect(Guid.NewGuid().ToString(), this.settings["user"], this.settings["pass"])
|
|
|
|
@@ -106,11 +127,13 @@ namespace BlubbFish.Utils.IoT.Connector.Data {
|
|
|
|
|
|
|
|
|
|
protected virtual void Dispose(Boolean disposing) {
|
|
|
|
|
if(!this.disposedValue) {
|
|
|
|
|
if(disposing) {try {
|
|
|
|
|
if(disposing) {
|
|
|
|
|
try {
|
|
|
|
|
this.connectionWatcher.Abort();
|
|
|
|
|
this.connectionWatcherRunning = false;
|
|
|
|
|
while(this.connectionWatcher.IsAlive) {
|
|
|
|
|
Thread.Sleep(10);
|
|
|
|
|
}
|
|
|
|
|
this.connectionWatcher = null;
|
|
|
|
|
} catch { }
|
|
|
|
|
this.Disconnect(true);
|
|
|
|
|
} catch (Exception) { }
|
|
|
|
|
}
|
|
|
|
|