[NF] Thread that checks if a MQTT Connection exists and if not reopen it

[NF] ConnectorDataMqtt now supports user and password
This commit is contained in:
2018-06-05 16:46:56 +00:00
parent 109850474e
commit 022d1b8c2c
9 changed files with 103 additions and 6 deletions
+2
View File
@@ -17,6 +17,8 @@ namespace BlubbFish.Utils.IoT.Connector {
protected Dictionary<String, String> settings;
public abstract Boolean IsConnected { get; }
public ABackend(Dictionary<String, String> settings) {
this.settings = settings;
}
+18 -6
View File
@@ -10,17 +10,21 @@ namespace BlubbFish.Utils.IoT.Connector.Data {
private MqttClient client;
public Mqtt(Dictionary<String, String> settings) : base(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"]);
Int32 port = 1883;
if(this.settings.ContainsKey("port")) {
port = Int32.Parse(this.settings["port"]);
}
this.client = new MqttClient(this.settings["server"], port, false, null, null, MqttSslProtocols.None);
Connect();
}
private void Connect() {
this.client.MqttMsgPublishReceived += this.Client_MqttMsgPublishReceived;
this.client.Connect(Guid.NewGuid().ToString());
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());
}
this.client.Subscribe(new String[] { "#" }, new Byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE });
}
@@ -36,7 +40,15 @@ namespace BlubbFish.Utils.IoT.Connector.Data {
#region IDisposable Support
private Boolean disposedValue = false;
public override Boolean IsConnected {
get {
if(this.client != null) {
return this.client.IsConnected;
} else {
return false;
}
}
}
protected virtual void Dispose(Boolean disposing) {
if(!this.disposedValue) {
Binary file not shown.
Binary file not shown.