[1.1.0] Rewrite Module to reconnect itselfs, so you dont need to watch over the the state of the connection

This commit is contained in:
BlubbFish 2019-05-27 17:24:54 +02:00
parent e8812fa80b
commit 4c4deb4a13
2 changed files with 180 additions and 119 deletions

View File

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using BlubbFish.Utils.IoT.Events;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
@ -8,27 +10,80 @@ using uPLibrary.Networking.M2Mqtt.Messages;
namespace BlubbFish.Utils.IoT.Connector.Data {
public class Mqtt : ADataBackend, IDisposable {
private MqttClient client;
private Thread connectionWatcher;
public Mqtt(Dictionary<String, String> settings) : base(settings) {
Console.WriteLine("BlubbFish.Utils.IoT.Connector.Data.Mqtt.Mqtt()");
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();
this.ConnectionWatcher();
}
#region ConectionManage
private void ConnectionWatcher() {
this.connectionWatcher = new Thread(this.ConnectionWatcherRunner);
this.connectionWatcher.Start();
}
private void ConnectionWatcherRunner() {
while(true) {
try {
if(!this.IsConnected) {
this.Reconnect();
}
Thread.Sleep(500);
} catch(Exception) { }
}
}
private void Reconnect() {
Console.WriteLine("BlubbFish.Utils.IoT.Connector.Data.Mqtt.Reconnect()");
if(this.IsConnected) {
this.Disconnect(true);
} else {
this.Disconnect(false);
}
this.Connect();
}
private void Disconnect(Boolean complete) {
Console.WriteLine("BlubbFish.Utils.IoT.Connector.Data.Mqtt.Disconnect()");
this.client.MqttMsgPublishReceived -= this.Client_MqttMsgPublishReceived;
this.Unsubscripe();
if(complete) {
this.client.Disconnect();
}
}
private void Connect() {
Console.WriteLine("BlubbFish.Utils.IoT.Connector.Data.Mqtt.Connect()");
this.client.MqttMsgPublishReceived += this.Client_MqttMsgPublishReceived;
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());
}
if (this.settings.ContainsKey("topic")) {
this.Subscripe();
}
#endregion
#region Subscription
private void Unsubscripe() {
if(this.settings.ContainsKey("topic")) {
this.client.Unsubscribe(this.settings["topic"].Split(';'));
} else {
this.client.Unsubscribe(new String[] { "#" });
}
}
private void Subscripe() {
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++) {
for(Int32 i = 0; i < qos.Length; i++) {
qos[i] = MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE;
}
this.client.Subscribe(this.settings["topic"].Split(';'), qos);
@ -36,10 +91,11 @@ namespace BlubbFish.Utils.IoT.Connector.Data {
this.client.Subscribe(new String[] { "#" }, new Byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE });
}
}
#endregion
private void Client_MqttMsgPublishReceived(Object sender, MqttMsgPublishEventArgs e) {
private async void Client_MqttMsgPublishReceived(Object sender, MqttMsgPublishEventArgs e) => await Task.Run(() => {
this.NotifyClientIncomming(new DataEvent(Encoding.UTF8.GetString(e.Message), e.Topic, DateTime.Now));
}
});
public override void Send(String topic, String data) {
this.client.Publish(topic, Encoding.UTF8.GetBytes(data));
@ -62,20 +118,20 @@ namespace BlubbFish.Utils.IoT.Connector.Data {
protected virtual void Dispose(Boolean disposing) {
if(!this.disposedValue) {
if(disposing) {try {
this.client.MqttMsgPublishReceived -= this.Client_MqttMsgPublishReceived;
this.client.Unsubscribe(new String[] { "#" });
this.client.Disconnect();
try {
this.connectionWatcher.Abort();
this.connectionWatcher = null;
} catch { }
this.Disconnect(true);
} catch (Exception) { }
}
this.client = null;
this.disposedValue = true;
}
}
public override void Dispose() {
Dispose(true);
this.Dispose(true);
GC.SuppressFinalize(this);
}
#endregion

View File

@ -1,18 +1,19 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Resources;
using System.Runtime.InteropServices;
// Allgemeine Informationen über eine Assembly werden über die folgenden
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
// die einer Assembly zugeordnet sind.
[assembly: AssemblyTitle("ConnectorDataMqtt")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyDescription("ADataBackend Connector that connects to mqtt using M2Mqtt")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyCompany("BlubbFish")]
[assembly: AssemblyProduct("ConnectorDataMqtt")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCopyright("Copyright © 2017 - 27.05.2019")]
[assembly: AssemblyTrademark("© BlubbFish")]
[assembly: AssemblyCulture("")]
[assembly: NeutralResourcesLanguage("de-DE")]
// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly
// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von
@ -32,5 +33,9 @@ using System.Runtime.InteropServices;
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
// indem Sie "*" wie unten gezeigt eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.0")]
[assembly: AssemblyFileVersion("1.1.0")]
/*
* 1.1.0 Rewrite Module to reconnect itselfs, so you dont need to watch over the the state of the connection
*/