From 6c841e9b5ec35bafa780ac9a81ca99e4f535f1f5 Mon Sep 17 00:00:00 2001 From: BlubbFish Date: Tue, 8 May 2018 18:07:48 +0000 Subject: [PATCH] [NF] First usable Version (Trigger) --- IoT-Bot.sln | 24 +++-- IoT-Bot/Condition/ACondition.cs | 69 +++++--------- IoT-Bot/Condition/Edge.cs | 17 ++-- IoT-Bot/Condition/TelegramBot.cs | 37 ++++---- IoT-Bot/Condition/Trigger.cs | 20 ++-- IoT-Bot/ConditionWorker.cs | 24 +++-- IoT-Bot/Helper.cs | 11 +++ IoT-Bot/IoT-Bot.csproj | 35 +++++-- IoT-Bot/Program.cs | 88 ++++++++++++------ IoT-Bot/Properties/AssemblyInfo.cs | 20 ++-- IoT-Bot/bin/Release/ConnectorDataMqtt.dll | Bin 0 -> 6144 bytes IoT-Bot/bin/Release/IoTBot.exe | Bin 0 -> 11776 bytes IoT-Bot/bin/Release/M2Mqtt.dll | Bin 0 -> 50176 bytes IoT-Bot/bin/Release/Utils-IoT.dll | Bin 0 -> 13824 bytes IoT-Bot/bin/Release/Utils.dll | Bin 0 -> 25600 bytes .../config-example/condition.conf.example | 8 ++ .../Release/config-example/data.conf.example | 3 + .../Release/config-example/user.conf.example | 4 + IoT-Bot/bin/Release/litjson.dll | Bin 0 -> 50688 bytes IoT-Bot/bin/iotbot_1.0-1.deb | Bin 0 -> 51010 bytes IoT-Bot/config-example/condition.conf.example | 8 ++ IoT-Bot/config-example/data.conf.example | 3 + IoT-Bot/config-example/user.conf.example | 4 + IoT-Bot/dpkg/control | 10 ++ IoT-Bot/dpkg/create-dkpg.bat | 6 ++ IoT-Bot/dpkg/make-deb.sh | 25 +++++ 26 files changed, 264 insertions(+), 152 deletions(-) create mode 100644 IoT-Bot/Helper.cs create mode 100644 IoT-Bot/bin/Release/ConnectorDataMqtt.dll create mode 100644 IoT-Bot/bin/Release/IoTBot.exe create mode 100644 IoT-Bot/bin/Release/M2Mqtt.dll create mode 100644 IoT-Bot/bin/Release/Utils-IoT.dll create mode 100644 IoT-Bot/bin/Release/Utils.dll create mode 100644 IoT-Bot/bin/Release/config-example/condition.conf.example create mode 100644 IoT-Bot/bin/Release/config-example/data.conf.example create mode 100644 IoT-Bot/bin/Release/config-example/user.conf.example create mode 100644 IoT-Bot/bin/Release/litjson.dll create mode 100644 IoT-Bot/bin/iotbot_1.0-1.deb create mode 100644 IoT-Bot/config-example/condition.conf.example create mode 100644 IoT-Bot/config-example/data.conf.example create mode 100644 IoT-Bot/config-example/user.conf.example create mode 100644 IoT-Bot/dpkg/control create mode 100644 IoT-Bot/dpkg/create-dkpg.bat create mode 100644 IoT-Bot/dpkg/make-deb.sh diff --git a/IoT-Bot.sln b/IoT-Bot.sln index 6e144f2..388a8ce 100644 --- a/IoT-Bot.sln +++ b/IoT-Bot.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26730.16 +VisualStudioVersion = 15.0.27004.2010 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IoT-Bot", "IoT-Bot\IoT-Bot.csproj", "{89077643-B472-419F-8EAB-56B9E2D13ABC}" EndProject @@ -9,7 +9,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils-IoT", "..\Utils\IoT\U EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils", "..\Utils\Utils\Utils.csproj", "{FAC8CE64-BF13-4ECE-8097-AEB5DD060098}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Zway", "..\Zway\Zway\Zway.csproj", "{166258ED-CB3D-43F5-8E8D-3A993B64D022}" +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_4.7.1", "..\Librarys\mqtt\M2Mqtt\M2Mqtt_4.7.1.csproj", "{A11AEF5A-B246-4FE8-8330-06DB73CC8074}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConnectorDataMqtt", "..\Utils\IoT\Connector\Data\Mqtt\ConnectorDataMqtt.csproj", "{EE6C8F68-ED46-4C1C-ABDD-CFCDF75104F2}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -29,10 +33,18 @@ Global {FAC8CE64-BF13-4ECE-8097-AEB5DD060098}.Debug|Any CPU.Build.0 = Debug|Any CPU {FAC8CE64-BF13-4ECE-8097-AEB5DD060098}.Release|Any CPU.ActiveCfg = Release|Any CPU {FAC8CE64-BF13-4ECE-8097-AEB5DD060098}.Release|Any CPU.Build.0 = Release|Any CPU - {166258ED-CB3D-43F5-8E8D-3A993B64D022}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {166258ED-CB3D-43F5-8E8D-3A993B64D022}.Debug|Any CPU.Build.0 = Debug|Any CPU - {166258ED-CB3D-43F5-8E8D-3A993B64D022}.Release|Any CPU.ActiveCfg = Release|Any CPU - {166258ED-CB3D-43F5-8E8D-3A993B64D022}.Release|Any CPU.Build.0 = Release|Any CPU + {91A14CD2-2940-4500-8193-56D37EDDDBAA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {91A14CD2-2940-4500-8193-56D37EDDDBAA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {91A14CD2-2940-4500-8193-56D37EDDDBAA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {91A14CD2-2940-4500-8193-56D37EDDDBAA}.Release|Any CPU.Build.0 = Release|Any CPU + {A11AEF5A-B246-4FE8-8330-06DB73CC8074}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {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 + {EE6C8F68-ED46-4C1C-ABDD-CFCDF75104F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EE6C8F68-ED46-4C1C-ABDD-CFCDF75104F2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EE6C8F68-ED46-4C1C-ABDD-CFCDF75104F2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EE6C8F68-ED46-4C1C-ABDD-CFCDF75104F2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/IoT-Bot/Condition/ACondition.cs b/IoT-Bot/Condition/ACondition.cs index e98bc56..e1e1311 100644 --- a/IoT-Bot/Condition/ACondition.cs +++ b/IoT-Bot/Condition/ACondition.cs @@ -1,77 +1,58 @@ using System; using System.Collections.Generic; using BlubbFish.Utils.IoT.Connector; -using BlubbFish.Utils.IoT.Sensor; +using BlubbFish.Utils.IoT.Events; +using BlubbFish.Utils.IoT.JsonSensor; namespace IoTBot.Condition { abstract class ACondition { - protected ASensor sensor; + protected AJsonSensor sensor; protected Dictionary settings; protected String name; - protected ADataBackend data; - protected AUserBackend user; + protected Dictionary backends; + protected ABackend target_backend; - protected ACondition(String name, Dictionary settings, ASensor sensor, ADataBackend data, AUserBackend user) { + protected ACondition(String name, Dictionary settings, AJsonSensor sensor, Dictionary backends) { this.settings = settings; this.name = name; - this.data = data; - this.user = user; + this.backends = backends; this.sensor = sensor; } public void Attach() { if(this.sensor != null) { - this.sensor.Update += this.Sensor_Update; + this.sensor.Update += this.SensorUpdate; } - switch (this.settings["source"].ToLower()) { - case "user": - if(this.user != null) { - this.user.MessageIncomming += this.User_Update; - } - break; - case "data": - if(this.data != null) { - this.data.MessageIncomming += this.Data_Update; - } - break; - case "both": - if (this.user != null) { - this.user.MessageIncomming += this.User_Update; - } - if (this.data != null) { - this.data.MessageIncomming += this.Data_Update; - } - break; + if(this.settings.ContainsKey("source") && this.backends.ContainsKey(this.settings["source"])) { + this.backends[this.settings["source"]].MessageIncomming += this.BackendUpdate; + } + if (this.settings.ContainsKey("target") && this.backends.ContainsKey(this.settings["target"])) { + this.target_backend = this.backends[this.settings["target"]]; } } - protected void Send(String message = "", String topic = "") { + protected virtual void Send(String message = "", String topic = "") { if(message == "") { message = this.settings["target_message"]; } if(topic == "") { topic = this.settings["target_topic"]; } - switch (this.settings["target"].ToLower()) { - case "user": - this.user.Send(message); - break; - case "data": - this.data.Send(topic, message); - break; - case "sensor": - //this.sensor.Set(message); - break; + if(this.target_backend != null) { + if(this.target_backend is ADataBackend) { + ((ADataBackend)this.target_backend).Send(topic, message); + } + if(this.target_backend is AUserBackend) { + ((AUserBackend)this.target_backend).Send(message); + } } } - protected abstract void Data_Update(Object sender, MqttEventArgs e); + protected abstract void BackendUpdate(Object sender, BackendEvent e); - protected abstract void User_Update(Object sender, UserMessageEventArgs e); + protected abstract void SensorUpdate(Object sender, EventArgs e); - protected abstract void Sensor_Update(Object sender, EventArgs e); - - public static ACondition GetInstance(String name, Dictionary settings, ASensor sensor, ADataBackend data, AUserBackend user) { + public static ACondition GetInstance(String name, Dictionary settings, AJsonSensor sensor, Dictionary backends) { String object_condition = "IoTBot.Condition." + Char.ToUpper(settings["type"][0]) + settings["type"].Substring(1).ToLower(); Type t = null; try { @@ -79,7 +60,7 @@ namespace IoTBot.Condition { } catch(TypeLoadException) { throw new ArgumentException("condition.ini: " + settings["type"] + " is not a Sensor"); } - return (ACondition)t.GetConstructor(new Type[] { typeof(String), typeof(Dictionary), typeof(ASensor), typeof(ADataBackend), typeof(AUserBackend) }).Invoke(new Object[] { name, settings, sensor, data, user }); + return (ACondition)t.GetConstructor(new Type[] { typeof(String), typeof(Dictionary), typeof(AJsonSensor), typeof(Dictionary) }).Invoke(new Object[] { name, settings, sensor, backends }); } } } \ No newline at end of file diff --git a/IoT-Bot/Condition/Edge.cs b/IoT-Bot/Condition/Edge.cs index 88042de..48d5ba2 100644 --- a/IoT-Bot/Condition/Edge.cs +++ b/IoT-Bot/Condition/Edge.cs @@ -1,31 +1,28 @@ using System; using System.Collections.Generic; using BlubbFish.Utils.IoT.Connector; -using BlubbFish.Utils.IoT.Sensor; +using BlubbFish.Utils.IoT.Events; +using BlubbFish.Utils.IoT.JsonSensor; namespace IoTBot.Condition { class Edge : ACondition { private Boolean histBool; - public Edge(String name, Dictionary settings, ASensor sensor, ADataBackend data, AUserBackend user) : base(name, settings, sensor, data, user) { } + public Edge(String name, Dictionary settings, AJsonSensor sensor, Dictionary backends) : base(name, settings, sensor, backends) { } - protected override void Data_Update(Object sender, MqttEventArgs e) { + protected override void BackendUpdate(Object sender, BackendEvent e) { //throw new NotImplementedException(); } - protected override void Sensor_Update(Object sender, EventArgs e) { - if(this.sensor.Datatypes == ASensor.Types.Bool) { + protected override void SensorUpdate(Object sender, EventArgs e) { + if(this.sensor.Datatypes == AJsonSensor.Types.Bool) { if(this.sensor.GetBool == Boolean.Parse(this.settings["sensor_value"]) && this.histBool != this.sensor.GetBool) { this.histBool = this.sensor.GetBool; - this.user.Send("Jemand ist DA!"); + //this.user.Send("Jemand ist DA!"); } else { this.histBool = this.sensor.GetBool; } } } - - protected override void User_Update(Object sender, UserMessageEventArgs e) { - //throw new NotImplementedException(); - } } } diff --git a/IoT-Bot/Condition/TelegramBot.cs b/IoT-Bot/Condition/TelegramBot.cs index 45fdd83..e408d3e 100644 --- a/IoT-Bot/Condition/TelegramBot.cs +++ b/IoT-Bot/Condition/TelegramBot.cs @@ -1,33 +1,24 @@ using System; using System.Collections.Generic; -using BlubbFish.IoT.Zway; -using BlubbFish.IoT.Zway.Devices.CommandClasses; using BlubbFish.Utils.IoT.Connector; -using BlubbFish.Utils.IoT.Sensor; +using BlubbFish.Utils.IoT.Events; +using BlubbFish.Utils.IoT.JsonSensor; namespace IoTBot.Condition { class Telegrambot : ACondition { - private ZwayController zw; + //private ZwayController zw; - public Telegrambot(String name, Dictionary settings, ASensor sensor, ADataBackend data, AUserBackend user) : base(name, settings, sensor, data, user) { - this.zw = new ZwayController("10.100.0.214", "admin", ""); - this.zw.Update += this.Zw_Update; + public Telegrambot(String name, Dictionary settings, AJsonSensor sensor, Dictionary backends) : base(name, settings, sensor, backends) { + //this.zw = new ZwayController("10.100.0.214", "admin", ""); + //this.zw.Update += this.Zw_Update; } - private void Zw_Update(Object sender, BlubbFish.IoT.Zway.Events.DeviceUpdateEvent e) { + /*private void Zw_Update(Object sender, BlubbFish.IoT.Zway.Events.DeviceUpdateEvent e) { Console.WriteLine("-> ZW: "+sender.ToString()); - } + }*/ - protected override void Data_Update(Object sender, MqttEventArgs e) { - //throw new NotImplementedException(); - } - - protected override void Sensor_Update(Object sender, EventArgs e) { - //throw new NotImplementedException(); - } - - protected override void User_Update(Object sender, UserMessageEventArgs e) { - if(e.Message == "/start") { + protected override void BackendUpdate(Object sender, BackendEvent e) { + if (e.Message == "/start") { this.Send("Hallo zurück! Ich kann aktuell die Befehle /schalter"); } if (e.Message.StartsWith("/schalter")) { @@ -35,8 +26,12 @@ namespace IoTBot.Condition { } } + protected override void SensorUpdate(Object sender, EventArgs e) { + //throw new NotImplementedException(); + } + private void BotSchalter(String message) { - if (message == "/schalter") { + /*if (message == "/schalter") { this.user.Send("Was soll ich tun?", new String[] { "/schalter einschalten", "/schalter ausschalten", "/schalter status" }); } if(message == "/schalter status") { @@ -87,7 +82,7 @@ namespace IoTBot.Condition { this.user.Send("Ausschalten von " + item.Name + " " + (item.Level ? "nicht " : "") + "erfolgreich"); } } - } + }*/ } } } diff --git a/IoT-Bot/Condition/Trigger.cs b/IoT-Bot/Condition/Trigger.cs index d680e90..b0057bb 100644 --- a/IoT-Bot/Condition/Trigger.cs +++ b/IoT-Bot/Condition/Trigger.cs @@ -1,27 +1,19 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using BlubbFish.Utils.IoT.Connector; -using BlubbFish.Utils.IoT.Sensor; +using BlubbFish.Utils.IoT.Events; +using BlubbFish.Utils.IoT.JsonSensor; namespace IoTBot.Condition { class Trigger : ACondition { - public Trigger(String name, Dictionary settings, ASensor sensor, ADataBackend data, AUserBackend user) : base(name, settings, sensor, data, user) { } + public Trigger(String name, Dictionary settings, AJsonSensor sensor, Dictionary backends) : base(name, settings, sensor, backends) { } - protected override void Data_Update(Object sender, MqttEventArgs e) { - //throw new NotImplementedException(); + protected override void BackendUpdate(Object sender, BackendEvent e) { + } - protected override void Sensor_Update(Object sender, EventArgs e) { - //throw new NotImplementedException(); - } - - protected override void User_Update(Object sender, UserMessageEventArgs e) { - if (e.Message == this.settings["trigger"]) { + protected override void SensorUpdate(Object sender, EventArgs e) { this.Send(); - } } } } diff --git a/IoT-Bot/ConditionWorker.cs b/IoT-Bot/ConditionWorker.cs index 1331c09..fc8ed42 100644 --- a/IoT-Bot/ConditionWorker.cs +++ b/IoT-Bot/ConditionWorker.cs @@ -1,22 +1,20 @@ using System; using System.Collections.Generic; using BlubbFish.Utils.IoT.Connector; -using BlubbFish.Utils.IoT.Sensor; +using BlubbFish.Utils.IoT.JsonSensor; using IoTBot.Condition; namespace IoTBot { class ConditionWorker { private List conditions = new List(); - private readonly ADataBackend data; - private readonly AUserBackend user; + private readonly Dictionary backends; - public ConditionWorker(ADataBackend data, AUserBackend user) { - this.data = data; - this.user = user; + public ConditionWorker(Dictionary backends) { + this.backends = backends; } public void SetCondition(String name, Dictionary settings) { - ASensor sensor = null; + AJsonSensor sensor = null; if (settings.ContainsKey("sensor_type")) { Dictionary sensor_settings = new Dictionary(); foreach (KeyValuePair item in settings) { @@ -24,9 +22,9 @@ namespace IoTBot { sensor_settings.Add(item.Key.Substring(7), item.Value); } } - sensor = ASensor.GetInstance(this.data, sensor_settings, name); + sensor = AJsonSensor.GetInstance(this.backends, sensor_settings, name); } - this.conditions.Add(ACondition.GetInstance(name, settings, sensor, this.data, this.user)); + this.conditions.Add(ACondition.GetInstance(name, settings, sensor, this.backends)); } public void Run() { @@ -34,5 +32,13 @@ namespace IoTBot { item.Attach(); } } + + internal void Stop() { + //throw new NotImplementedException(); + } + + internal void Dispose() { + //throw new NotImplementedException(); + } } } diff --git a/IoT-Bot/Helper.cs b/IoT-Bot/Helper.cs new file mode 100644 index 0000000..f028293 --- /dev/null +++ b/IoT-Bot/Helper.cs @@ -0,0 +1,11 @@ +using System; + +namespace IoTBot { + class Helper { + internal static void WriteError(String text) { + Console.ForegroundColor = ConsoleColor.Red; + Console.Error.WriteLine("ERROR: " + text); + Console.ResetColor(); + } + } +} \ No newline at end of file diff --git a/IoT-Bot/IoT-Bot.csproj b/IoT-Bot/IoT-Bot.csproj index a58d3d9..e55a558 100644 --- a/IoT-Bot/IoT-Bot.csproj +++ b/IoT-Bot/IoT-Bot.csproj @@ -9,7 +9,7 @@ Properties IoTBot IoTBot - v4.6.2 + v4.7.1 512 true @@ -39,16 +39,33 @@ + + + + - - - - + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + {91a14cd2-2940-4500-8193-56d37edddbaa} + litjson_4.7.1 + + + {ee6c8f68-ed46-4c1c-abdd-cfcdf75104f2} + ConnectorDataMqtt + {b870e4d5-6806-4a0b-b233-8907eedc5afc} Utils-IoT @@ -57,12 +74,12 @@ {fac8ce64-bf13-4ece-8097-aeb5dd060098} Utils - - {166258ed-cb3d-43f5-8e8d-3a993b64d022} - Zway - + + + +