From b65b2e50df83335aff7ca8290f42ac704e3e4ee4 Mon Sep 17 00:00:00 2001 From: BlubbFish Date: Sun, 29 Oct 2017 19:32:18 +0000 Subject: [PATCH] [NF] Telegram Condition [NF] Zway Libarary --- IoT-Bot.sln | 6 ++++ IoT-Bot/Condition/ACondition.cs | 50 +++++++++++++++++++++++++++++++- IoT-Bot/Condition/Edge.cs | 8 +++++ IoT-Bot/Condition/TelegramBot.cs | 44 ++++++++++++++++++++++++++++ IoT-Bot/Condition/Trigger.cs | 27 +++++++++++++++++ IoT-Bot/ConditionWorker.cs | 13 +++++---- IoT-Bot/IoT-Bot.csproj | 10 +++++-- IoT-Bot/Program.cs | 14 +++++++-- 8 files changed, 161 insertions(+), 11 deletions(-) create mode 100644 IoT-Bot/Condition/TelegramBot.cs create mode 100644 IoT-Bot/Condition/Trigger.cs diff --git a/IoT-Bot.sln b/IoT-Bot.sln index 5983ec7..6e144f2 100644 --- a/IoT-Bot.sln +++ b/IoT-Bot.sln @@ -9,6 +9,8 @@ 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}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +29,10 @@ 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 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/IoT-Bot/Condition/ACondition.cs b/IoT-Bot/Condition/ACondition.cs index da7e8ce..e98bc56 100644 --- a/IoT-Bot/Condition/ACondition.cs +++ b/IoT-Bot/Condition/ACondition.cs @@ -7,20 +7,68 @@ namespace IoTBot.Condition { abstract class ACondition { protected ASensor sensor; protected Dictionary settings; + protected String name; protected ADataBackend data; protected AUserBackend user; protected ACondition(String name, Dictionary settings, ASensor sensor, ADataBackend data, AUserBackend user) { this.settings = settings; + this.name = name; this.data = data; this.user = user; this.sensor = sensor; } public void Attach() { - this.sensor.Update += this.Sensor_Update; + if(this.sensor != null) { + this.sensor.Update += this.Sensor_Update; + } + 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; + } } + protected 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; + } + } + + protected abstract void Data_Update(Object sender, MqttEventArgs e); + + protected abstract void User_Update(Object sender, UserMessageEventArgs e); + protected abstract void Sensor_Update(Object sender, EventArgs e); public static ACondition GetInstance(String name, Dictionary settings, ASensor sensor, ADataBackend data, AUserBackend user) { diff --git a/IoT-Bot/Condition/Edge.cs b/IoT-Bot/Condition/Edge.cs index b6165fd..88042de 100644 --- a/IoT-Bot/Condition/Edge.cs +++ b/IoT-Bot/Condition/Edge.cs @@ -9,6 +9,10 @@ namespace IoTBot.Condition { public Edge(String name, Dictionary settings, ASensor sensor, ADataBackend data, AUserBackend user) : base(name, settings, sensor, data, user) { } + protected override void Data_Update(Object sender, MqttEventArgs e) { + //throw new NotImplementedException(); + } + protected override void Sensor_Update(Object sender, EventArgs e) { if(this.sensor.Datatypes == ASensor.Types.Bool) { if(this.sensor.GetBool == Boolean.Parse(this.settings["sensor_value"]) && this.histBool != this.sensor.GetBool) { @@ -19,5 +23,9 @@ namespace IoTBot.Condition { } } } + + 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 new file mode 100644 index 0000000..bbb8afb --- /dev/null +++ b/IoT-Bot/Condition/TelegramBot.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using BlubbFish.IoT.Zway; +using BlubbFish.Utils.IoT.Connector; +using BlubbFish.Utils.IoT.Sensor; + +namespace IoTBot.Condition { + class Telegrambot : ACondition { + 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", ""); + } + + 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") { + this.Send("Hallo zurück! Ich kann aktuell die Befehle /licht"); + } + if (e.Message.StartsWith("/licht")) { + this.BotLicht(e.Message); + } + } + + private void BotLicht(String message) { + if (message == "/licht") { + this.user.Send("Was soll ich tun?", new String[] { "/licht einschalten", "/licht ausschalten", "/licht aufzaehlen" }); + } + if(message == "/licht aufzaehlen") { + this.user.Send("Ich habe folgende Lampen im Angebot:"); + } + } + } +} diff --git a/IoT-Bot/Condition/Trigger.cs b/IoT-Bot/Condition/Trigger.cs new file mode 100644 index 0000000..d680e90 --- /dev/null +++ b/IoT-Bot/Condition/Trigger.cs @@ -0,0 +1,27 @@ +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; + +namespace IoTBot.Condition { + class Trigger : ACondition { + public Trigger(String name, Dictionary settings, ASensor sensor, ADataBackend data, AUserBackend user) : base(name, settings, sensor, data, user) { } + + 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 == this.settings["trigger"]) { + this.Send(); + } + } + } +} diff --git a/IoT-Bot/ConditionWorker.cs b/IoT-Bot/ConditionWorker.cs index 41492ca..1331c09 100644 --- a/IoT-Bot/ConditionWorker.cs +++ b/IoT-Bot/ConditionWorker.cs @@ -16,13 +16,16 @@ namespace IoTBot { } public void SetCondition(String name, Dictionary settings) { - Dictionary sensor_settings = new Dictionary(); - foreach (KeyValuePair item in settings) { - if(item.Key.StartsWith("sensor_")) { - sensor_settings.Add(item.Key.Substring(7), item.Value); + ASensor sensor = null; + if (settings.ContainsKey("sensor_type")) { + Dictionary sensor_settings = new Dictionary(); + foreach (KeyValuePair item in settings) { + if (item.Key.StartsWith("sensor_")) { + sensor_settings.Add(item.Key.Substring(7), item.Value); + } } + sensor = ASensor.GetInstance(this.data, sensor_settings, name); } - ASensor sensor = ASensor.GetInstance(this.data, sensor_settings, name); this.conditions.Add(ACondition.GetInstance(name, settings, sensor, this.data, this.user)); } diff --git a/IoT-Bot/IoT-Bot.csproj b/IoT-Bot/IoT-Bot.csproj index aa4de7f..a58d3d9 100644 --- a/IoT-Bot/IoT-Bot.csproj +++ b/IoT-Bot/IoT-Bot.csproj @@ -7,8 +7,8 @@ {89077643-B472-419F-8EAB-56B9E2D13ABC} Exe Properties - MqttToTelegram - MqttToTelegram + IoTBot + IoTBot v4.6.2 512 true @@ -37,6 +37,8 @@ + + @@ -55,6 +57,10 @@ {fac8ce64-bf13-4ece-8097-aeb5dd060098} Utils + + {166258ed-cb3d-43f5-8e8d-3a993b64d022} + Zway +