diff --git a/Zway-Bot.sln b/Zway-Bot.sln index 2469399..73851cb 100644 --- a/Zway-Bot.sln +++ b/Zway-Bot.sln @@ -17,7 +17,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Iot-Interfaces", "..\Utils\ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "litjson_4.7.1", "..\Librarys\litjson\litjson\litjson_4.7.1.csproj", "{91A14CD2-2940-4500-8193-56D37EDDDBAA}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "M2Mqtt", "..\Librarys\mqtt\M2Mqtt\M2Mqtt_4.7.1.csproj", "{A11AEF5A-B246-4FE8-8330-06DB73CC8074}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "M2Mqtt_4.7.1", "..\Librarys\mqtt\M2Mqtt\M2Mqtt_4.7.1.csproj", "{A11AEF5A-B246-4FE8-8330-06DB73CC8074}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bot-Utils", "..\Utils\Bot-Utils\Bot-Utils.csproj", "{BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -57,6 +59,10 @@ Global {A11AEF5A-B246-4FE8-8330-06DB73CC8074}.Debug|Any CPU.Build.0 = Debug|Any CPU {A11AEF5A-B246-4FE8-8330-06DB73CC8074}.Release|Any CPU.ActiveCfg = Release|Any CPU {A11AEF5A-B246-4FE8-8330-06DB73CC8074}.Release|Any CPU.Build.0 = Release|Any CPU + {BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Zway-Bot/Moduls/Mqtt.cs b/Zway-Bot/Moduls/Mqtt.cs index c060d74..f5c35e9 100644 --- a/Zway-Bot/Moduls/Mqtt.cs +++ b/Zway-Bot/Moduls/Mqtt.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text.RegularExpressions; +using System.Threading; using BlubbFish.IoT.Zway; using BlubbFish.IoT.Zway.Events; using BlubbFish.IoT.Zway.Interfaces; @@ -11,19 +12,52 @@ using LitJson; namespace ZwayBot.Moduls { class Mqtt : AModul, IDisposable { - private readonly ABackend mqtt; + private ABackend mqtt; + private readonly Thread connectionWatcher; private Dictionary modules; public override event ModulEvent Update; public Mqtt(ZwayController zway, InIReader settings) : base(zway, settings) { if (this.config.ContainsKey("settings")) { - this.mqtt = ABackend.GetInstance(this.config["settings"], ABackend.BackendType.Data); - this.mqtt.MessageIncomming += this.Mqtt_MessageIncomming; - this.zw.Update += this.ZwayEvent; + this.connectionWatcher = new Thread(this.ConnectionWatcherRunner); + this.connectionWatcher.Start(); } } + private void ConnectionWatcherRunner() { + while (true) { + try { + if (this.mqtt == null || !this.mqtt.IsConnected) { + this.Reconnect(); + } + Thread.Sleep(10000); + } catch (Exception) { } + } + } + + private void Reconnect() { + this.Disconnect(); + this.Connect(); + } + + private void Disconnect() { + if (this.mqtt != null) { + this.mqtt.MessageIncomming -= this.Mqtt_MessageIncomming; + } + this.zw.Update -= this.ZwayEvent; + if (this.mqtt != null) { + this.mqtt.Dispose(); + } + this.mqtt = null; + } + + private void Connect() { + this.mqtt = ABackend.GetInstance(this.config["settings"], ABackend.BackendType.Data); + this.mqtt.MessageIncomming += this.Mqtt_MessageIncomming; + this.zw.Update += this.ZwayEvent; + } + private void ZwayEvent(Object sender, DeviceUpdateEvent e) { String topic = ""; String data = ""; @@ -119,7 +153,9 @@ namespace ZwayBot.Moduls { protected virtual void Dispose(Boolean disposing) { if (!this.disposedValue) { if (disposing) { - this.mqtt.Dispose(); + this.connectionWatcher.Abort(); + while(this.connectionWatcher.ThreadState == ThreadState.Running) { Thread.Sleep(10); } + this.Disconnect(); } this.disposedValue = true; } diff --git a/Zway-Bot/Properties/AssemblyInfo.cs b/Zway-Bot/Properties/AssemblyInfo.cs index 26035e3..f0edd53 100644 --- a/Zway-Bot/Properties/AssemblyInfo.cs +++ b/Zway-Bot/Properties/AssemblyInfo.cs @@ -10,7 +10,7 @@ using System.Runtime.InteropServices; [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("BlubbFish")] [assembly: AssemblyProduct("Zway-Bot")] -[assembly: AssemblyCopyright("Copyright © 2017 - 15.05.2018")] +[assembly: AssemblyCopyright("Copyright © 2017 - 05.06.2018")] [assembly: AssemblyTrademark("BlubbFish")] [assembly: AssemblyCulture("")] @@ -31,8 +31,8 @@ using System.Runtime.InteropServices; // Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden, // übernehmen, indem Sie "*" eingeben: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.5.7")] -[assembly: AssemblyFileVersion("1.5.7")] +[assembly: AssemblyVersion("1.6.0")] +[assembly: AssemblyFileVersion("1.6.0")] [assembly: NeutralResourcesLanguage("de-DE")] // “Internet Of Things” icon by By Michael Wohlwend, US, from thenounproject.com. diff --git a/Zway-Bot/bin/Release/ConnectorDataMqtt.dll b/Zway-Bot/bin/Release/ConnectorDataMqtt.dll index aac3839..6021d22 100644 Binary files a/Zway-Bot/bin/Release/ConnectorDataMqtt.dll and b/Zway-Bot/bin/Release/ConnectorDataMqtt.dll differ diff --git a/Zway-Bot/bin/Release/Iot-Interfaces.dll b/Zway-Bot/bin/Release/Iot-Interfaces.dll index fd1334f..455c1bf 100644 Binary files a/Zway-Bot/bin/Release/Iot-Interfaces.dll and b/Zway-Bot/bin/Release/Iot-Interfaces.dll differ diff --git a/Zway-Bot/bin/Release/M2Mqtt.dll b/Zway-Bot/bin/Release/M2Mqtt.dll index 1d88de0..e21d585 100644 Binary files a/Zway-Bot/bin/Release/M2Mqtt.dll and b/Zway-Bot/bin/Release/M2Mqtt.dll differ diff --git a/Zway-Bot/bin/Release/Utils-IoT.dll b/Zway-Bot/bin/Release/Utils-IoT.dll index b8d13d4..eac5354 100644 Binary files a/Zway-Bot/bin/Release/Utils-IoT.dll and b/Zway-Bot/bin/Release/Utils-IoT.dll differ diff --git a/Zway-Bot/bin/Release/Utils.dll b/Zway-Bot/bin/Release/Utils.dll index c3dd46a..4ff48ed 100644 Binary files a/Zway-Bot/bin/Release/Utils.dll and b/Zway-Bot/bin/Release/Utils.dll differ diff --git a/Zway-Bot/bin/Release/Zway-Bot.exe b/Zway-Bot/bin/Release/Zway-Bot.exe index d14ba95..31b630f 100644 Binary files a/Zway-Bot/bin/Release/Zway-Bot.exe and b/Zway-Bot/bin/Release/Zway-Bot.exe differ diff --git a/Zway-Bot/bin/Release/Zway.dll b/Zway-Bot/bin/Release/Zway.dll index 74599f3..4cc2056 100644 Binary files a/Zway-Bot/bin/Release/Zway.dll and b/Zway-Bot/bin/Release/Zway.dll differ diff --git a/Zway-Bot/bin/Release/litjson.dll b/Zway-Bot/bin/Release/litjson.dll index 1cf991f..7697337 100644 Binary files a/Zway-Bot/bin/Release/litjson.dll and b/Zway-Bot/bin/Release/litjson.dll differ