From 4545c506fc8970e8474523b2a02cf02ffb825a54 Mon Sep 17 00:00:00 2001 From: BlubbFish Date: Mon, 2 Oct 2017 22:51:08 +0000 Subject: [PATCH] =?UTF-8?q?[NF]=20AUserBackend=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IoT-Bot/Condition/ACondition.cs | 12 ++++++++---- IoT-Bot/Condition/Edge.cs | 6 ++---- IoT-Bot/ConditionWorker.cs | 12 +++++------- IoT-Bot/Program.cs | 11 ++++++----- IoT-Bot/settings.ini.example | 3 ++- 5 files changed, 23 insertions(+), 21 deletions(-) diff --git a/IoT-Bot/Condition/ACondition.cs b/IoT-Bot/Condition/ACondition.cs index 68dc4c5..20f7e00 100644 --- a/IoT-Bot/Condition/ACondition.cs +++ b/IoT-Bot/Condition/ACondition.cs @@ -7,9 +7,13 @@ namespace IoTBot.Condition { abstract class ACondition { protected ASensor sensor; protected Dictionary settings; + protected ADataBackend data; + protected AUserBackend user; - public ACondition(Dictionary settings, ADataBackend backend) { + public ACondition(Dictionary settings, ADataBackend data, AUserBackend user) { this.settings = settings; + this.data = data; + this.user = user; Dictionary l = new Dictionary { { "topic", this.settings["sensor_topic"] }, { "type", this.settings["sensor"] } @@ -17,13 +21,13 @@ namespace IoTBot.Condition { if(settings.ContainsKey("sensor_polling")) { l.Add("polling", this.settings["sensor_polling"]); } - this.sensor = ASensor.GetInstance(backend, l, "testSensor"); + this.sensor = ASensor.GetInstance(data, 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) { + public static ACondition GetInstance(Dictionary settings, ADataBackend data, AUserBackend user) { String object_condition = "IoTBot.Condition." + Char.ToUpper(settings["type"][0]) + settings["type"].Substring(1).ToLower(); Type t = null; try { @@ -31,7 +35,7 @@ namespace IoTBot.Condition { } 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 }); + return (ACondition)t.GetConstructor(new Type[] { typeof(Dictionary), typeof(ADataBackend), typeof(AUserBackend) }).Invoke(new Object[] { settings, data, user }); } } } \ No newline at end of file diff --git a/IoT-Bot/Condition/Edge.cs b/IoT-Bot/Condition/Edge.cs index 36d0b80..79234a8 100644 --- a/IoT-Bot/Condition/Edge.cs +++ b/IoT-Bot/Condition/Edge.cs @@ -7,15 +7,13 @@ namespace IoTBot.Condition { class Edge : ACondition { private Boolean histBool; - public Edge(Dictionary settings, ADataBackend backend) : base(settings, backend) { - - } + public Edge(Dictionary settings, ADataBackend data, AUserBackend user) : base(settings, data, user) { } 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) { this.histBool = this.sensor.GetBool; - Telegram.Instance.Send("Jemand ist DA!"); + this.user.Send("Jemand ist DA!"); } else { this.histBool = this.sensor.GetBool; } diff --git a/IoT-Bot/ConditionWorker.cs b/IoT-Bot/ConditionWorker.cs index 313c08a..04d630a 100644 --- a/IoT-Bot/ConditionWorker.cs +++ b/IoT-Bot/ConditionWorker.cs @@ -9,25 +9,23 @@ namespace IoTBot { private InIReader ini; private static ConditionWorker instance; private List conditions; - private Telegram telegram; - public static ConditionWorker GetInstance(ADataBackend backend) { + public static ConditionWorker GetInstance(ADataBackend data, AUserBackend user) { if (instance == null) { - instance = new ConditionWorker(backend); + instance = new ConditionWorker(data, user); } return instance; } - internal void Run(Telegram telegram) { - this.telegram = telegram; + internal void Run() { } - private ConditionWorker(ADataBackend backend) { + private ConditionWorker(ADataBackend data, AUserBackend user) { 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)); + this.conditions.Add(ACondition.GetInstance(this.ini.GetSection(section), data, user)); } } } diff --git a/IoT-Bot/Program.cs b/IoT-Bot/Program.cs index 688ebaf..cf1c173 100644 --- a/IoT-Bot/Program.cs +++ b/IoT-Bot/Program.cs @@ -6,11 +6,12 @@ namespace IoTBot { class Program { static void Main(String[] args) { ADataBackend mqtt = ADataBackend.GetInstance(InIReader.GetInstance("settings.ini").GetSection("mqtt")); - ConditionWorker.GetInstance(mqtt).Run(Telegram.Instance); + AUserBackend telegram = AUserBackend.GetInstance(InIReader.GetInstance("settings.ini").GetSection("user")); + ConditionWorker.GetInstance(mqtt, telegram).Run(); mqtt.MessageIncomming += Mqtt_MessageIncomming; mqtt.MessageSending += Mqtt_MessageSending; - Telegram.Instance.MessageIncomming += Telegram_MessageIncomming; - Telegram.Instance.MessageSending += Telegram_MessageSending; + telegram.MessageIncomming += Telegram_MessageIncomming; + telegram.MessageSending += Telegram_MessageSending; while(true) { System.Threading.Thread.Sleep(100); @@ -21,11 +22,11 @@ namespace IoTBot { Console.WriteLine("-> [" + DateTime.Now.ToUniversalTime() + "] MQTT: " + e.Message + " on " + e.Topic); } - private static void Telegram_MessageSending(Object sender, TelegramEventArgs e) { + private static void Telegram_MessageSending(Object sender, UserMessageEventArgs e) { Console.WriteLine("-> [" + e.Date.ToUniversalTime() + "] Telegram: " + e.Message + " on " + e.UserId); } - private static void Telegram_MessageIncomming(Object sender, TelegramEventArgs e) { + private static void Telegram_MessageIncomming(Object sender, UserMessageEventArgs e) { Console.WriteLine("<- [" + e.Date.ToUniversalTime() + "] Telegram: " + e.Message + " on " + e.UserId); } diff --git a/IoT-Bot/settings.ini.example b/IoT-Bot/settings.ini.example index 09118dc..664ff45 100644 --- a/IoT-Bot/settings.ini.example +++ b/IoT-Bot/settings.ini.example @@ -1,4 +1,5 @@ -[general] +[user] +type=telegram telegram-key=ABCDEFGH chatid=1234