commit 4889d3769bb1b24325d028b308766820b458dac7 Author: BlubbFish Date: Sun Oct 1 21:56:00 2017 +0000 Projekt Umbenannt und Ausführbar gemacht diff --git a/IoT-Bot.sln b/IoT-Bot.sln new file mode 100644 index 0000000..86d09fb --- /dev/null +++ b/IoT-Bot.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.25420.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IoT-Bot", "IoT-Bot\IoT-Bot.csproj", "{89077643-B472-419F-8EAB-56B9E2D13ABC}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {89077643-B472-419F-8EAB-56B9E2D13ABC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {89077643-B472-419F-8EAB-56B9E2D13ABC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {89077643-B472-419F-8EAB-56B9E2D13ABC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {89077643-B472-419F-8EAB-56B9E2D13ABC}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/IoT-Bot/App.config b/IoT-Bot/App.config new file mode 100644 index 0000000..d740e88 --- /dev/null +++ b/IoT-Bot/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/IoT-Bot/Condition/ACondition.cs b/IoT-Bot/Condition/ACondition.cs new file mode 100644 index 0000000..a53f0e6 --- /dev/null +++ b/IoT-Bot/Condition/ACondition.cs @@ -0,0 +1,37 @@ +using Dashboard.Sensor; +using System; +using System.Collections.Generic; +using Dashboard.Connector; + +namespace MqttToTelegram.Condition { + abstract class ACondition { + protected ASensor sensor; + protected Dictionary settings; + + public ACondition(Dictionary settings, ADataBackend backend) { + this.settings = settings; + Dictionary l = new Dictionary { + { "topic", this.settings["sensor_topic"] }, + { "type", this.settings["sensor"] } + }; + if(settings.ContainsKey("sensor_polling")) { + l.Add("polling", this.settings["sensor_polling"]); + } + this.sensor = ASensor.GetInstance(backend, l, "testSensor"); + this.sensor.Update += this.Sensor_Update; + } + + protected abstract void Sensor_Update(Object sender, EventArgs e); + + public static ACondition GetInstance(Dictionary settings, ADataBackend backend) { + String object_condition = "MqttToTelegram.Condition." + Char.ToUpper(settings["type"][0]) + settings["type"].Substring(1).ToLower(); + Type t = null; + try { + t = Type.GetType(object_condition, true); + } catch(TypeLoadException) { + throw new ArgumentException("condition.ini: " + settings["type"] + " is not a Sensor"); + } + return (ACondition)t.GetConstructor(new Type[] { typeof(Dictionary), typeof(ADataBackend) }).Invoke(new Object[] { settings, backend }); + } + } +} \ No newline at end of file diff --git a/IoT-Bot/Condition/Edge.cs b/IoT-Bot/Condition/Edge.cs new file mode 100644 index 0000000..35955f1 --- /dev/null +++ b/IoT-Bot/Condition/Edge.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using Dashboard.Connector; + +namespace MqttToTelegram.Condition { + class Edge : ACondition { + private Boolean histBool; + + public Edge(Dictionary settings, ADataBackend backend) : base(settings, backend) { + + } + + protected override void Sensor_Update(Object sender, EventArgs e) { + if(this.sensor.Datatypes == Dashboard.Sensor.ASensor.Types.Bool) { + if(this.sensor.GetBool == Boolean.Parse(this.settings["sensor_value"]) && this.histBool != this.sensor.GetBool) { + this.histBool = this.sensor.GetBool; + Telegram.Instance.Send("Jemand ist DA!"); + } else { + this.histBool = this.sensor.GetBool; + } + } + } + } +} diff --git a/IoT-Bot/ConditionWorker.cs b/IoT-Bot/ConditionWorker.cs new file mode 100644 index 0000000..3d679de --- /dev/null +++ b/IoT-Bot/ConditionWorker.cs @@ -0,0 +1,34 @@ +using BlubbFish.Utils; +using System; +using System.Collections.Generic; +using Dashboard.Connector; +using MqttToTelegram.Condition; + +namespace MqttToTelegram { + class ConditionWorker { + private InIReader ini; + private static ConditionWorker instance; + private List conditions; + private Telegram telegram; + + public static ConditionWorker GetInstance(ADataBackend backend) { + if (instance == null) { + instance = new ConditionWorker(backend); + } + return instance; + } + + internal void Run(Telegram telegram) { + this.telegram = telegram; + } + + private ConditionWorker(ADataBackend backend) { + this.ini = InIReader.GetInstance("condition.ini"); + this.conditions = new List(); + List sections = this.ini.GetSections(); + foreach(String section in sections) { + this.conditions.Add(ACondition.GetInstance(this.ini.GetSection(section), backend)); + } + } + } +} diff --git a/IoT-Bot/IoT-Bot.csproj b/IoT-Bot/IoT-Bot.csproj new file mode 100644 index 0000000..800a082 --- /dev/null +++ b/IoT-Bot/IoT-Bot.csproj @@ -0,0 +1,100 @@ + + + + + Debug + AnyCPU + {89077643-B472-419F-8EAB-56B9E2D13ABC} + Exe + Properties + MqttToTelegram + MqttToTelegram + v4.5.2 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\M2Mqtt.4.3.0.0\lib\net45\M2Mqtt.Net.dll + True + + + ..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll + True + + + + + + + + ..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll + True + + + + + + + + + ..\packages\Telegram.Bot.13.1.0\lib\netstandard1.1\Telegram.Bot.dll + True + + + + + + + + + + + + + + + + + + + + + + + + + + {fac8ce64-bf13-4ece-8097-aeb5dd060098} + Utils + + + + + + \ No newline at end of file diff --git a/IoT-Bot/Program.cs b/IoT-Bot/Program.cs new file mode 100644 index 0000000..8a27a76 --- /dev/null +++ b/IoT-Bot/Program.cs @@ -0,0 +1,36 @@ +using System; +using BlubbFish.Utils; +using Dashboard.Connector; + +namespace MqttToTelegram { + class Program { + static void Main(String[] args) { + ADataBackend mqtt = ADataBackend.GetInstance(InIReader.GetInstance("settings.ini").GetSection("mqtt")); + ConditionWorker.GetInstance(mqtt).Run(Telegram.Instance); + mqtt.MessageIncomming += Mqtt_MessageIncomming; + mqtt.MessageSending += Mqtt_MessageSending; + Telegram.Instance.MessageIncomming += Telegram_MessageIncomming; + Telegram.Instance.MessageSending += Telegram_MessageSending; + + while(true) { + System.Threading.Thread.Sleep(100); + } + } + + private static void Mqtt_MessageSending(ADataBackend sender, MqttEventArgs e) { + Console.WriteLine("-> [" + DateTime.Now.ToUniversalTime() + "] MQTT: " + e.Message + " on " + e.Topic); + } + + private static void Telegram_MessageSending(Object sender, global::Telegram.Bot.Types.Message e) { + Console.WriteLine("-> [" + e.Date.ToUniversalTime() + "] Telegram: " + e.Text + " on " + e.Chat.Id); + } + + private static void Telegram_MessageIncomming(Object sender, global::Telegram.Bot.Types.Message e) { + Console.WriteLine("<- [" + e.Date.ToUniversalTime() + "] Telegram: " + e.Text + " on " + e.Chat.Id); + } + + private static void Mqtt_MessageIncomming(ADataBackend sender, MqttEventArgs e) { + Console.WriteLine("<- [" + DateTime.Now.ToUniversalTime() + "] MQTT: " + e.Message + " on " + e.Topic); + } + } +} diff --git a/IoT-Bot/Properties/AssemblyInfo.cs b/IoT-Bot/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..007d1fb --- /dev/null +++ b/IoT-Bot/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +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("MqttToTelegram")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("MqttToTelegram")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar +// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von +// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen. +[assembly: ComVisible(false)] + +// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird +[assembly: Guid("89077643-b472-419f-8eab-56b9e2d13abc")] + +// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: +// +// Hauptversion +// Nebenversion +// Buildnummer +// Revision +// +// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern +// übernehmen, indem Sie "*" eingeben: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/IoT-Bot/packages.config b/IoT-Bot/packages.config new file mode 100644 index 0000000..c4b422a --- /dev/null +++ b/IoT-Bot/packages.config @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/IoT-Bot/settings.ini.example b/IoT-Bot/settings.ini.example new file mode 100644 index 0000000..09118dc --- /dev/null +++ b/IoT-Bot/settings.ini.example @@ -0,0 +1,7 @@ +[general] +telegram-key=ABCDEFGH +chatid=1234 + +[mqtt] +server=localhost +type=mqtt \ No newline at end of file