From f1636e90330e29f62d619576a18fd313406caf22 Mon Sep 17 00:00:00 2001 From: BlubbFish Date: Tue, 11 Sep 2018 07:59:10 +0000 Subject: [PATCH] Rewrite Bot-Utils so that handlers are threaded Tiny fix in INIReader --- Lora-Bot/Moduls/Mqtt.cs | 48 +++++---- Lora-Bot/Moduls/Scral.cs | 198 ++++++++++++++++++-------------------- Lora-Bot/Moduls/Txtout.cs | 24 +++-- Lora-Bot/Program.cs | 1 - 4 files changed, 137 insertions(+), 134 deletions(-) diff --git a/Lora-Bot/Moduls/Mqtt.cs b/Lora-Bot/Moduls/Mqtt.cs index ceffc8c..aa2883e 100644 --- a/Lora-Bot/Moduls/Mqtt.cs +++ b/Lora-Bot/Moduls/Mqtt.cs @@ -10,40 +10,50 @@ using Fraunhofer.Fit.Iot.Lora.Events; namespace Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls { class Mqtt : Mqtt { + private Boolean mqttconnect = false; + public override event ModulEvent Update; public Mqtt(LoraController lib, InIReader settings) : base(lib, settings) { } protected override void Connect() { this.mqtt = ABackend.GetInstance(this.config["settings"], ABackend.BackendType.Data); - //this.mqtt.MessageIncomming += this.EventInput; - this.library.Update += this.EventOutput; - Console.WriteLine("Connect!"); + Console.WriteLine("Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls.Mqtt.Connect()"); + this.mqttconnect = true; } protected override void Disconnect() { - this.library.Update -= this.EventOutput; + this.mqttconnect = false; if (this.mqtt != null) { this.mqtt.Dispose(); } this.mqtt = null; - Console.WriteLine("Disconnect!"); + Console.WriteLine("Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls.Mqtt.Disconnect()"); } - protected virtual void EventOutput(Object sender, DeviceUpdateEvent e) { - String topic = ""; - String data = ""; - if (e.Parent.GetType().HasInterface(typeof(IMqtt))) { - IMqtt sensor = (IMqtt)e.Parent; - topic = "lora/" + sensor.MqttTopic(); - data = sensor.ToJson(); - } - Console.WriteLine(topic); - Console.WriteLine(data); - if (topic != "" && data != "") { - ((ADataBackend)this.mqtt).Send(topic, data); - this.Update?.Invoke(this, new MqttEvent(topic, data)); - } + public override void EventLibSetter() { + this.library.Update += this.HandleLibUpdate; } + + protected override void LibUpadteThread(Object state) { + try { + if (this.mqttconnect) { + DeviceUpdateEvent e = state as DeviceUpdateEvent; + String topic = ""; + String data = ""; + if (e.Parent.GetType().HasInterface(typeof(IMqtt))) { + IMqtt sensor = (IMqtt)e.Parent; + topic = "lora/" + sensor.MqttTopic(); + data = sensor.ToJson(); + } + Console.WriteLine(topic); + Console.WriteLine(data); + if (topic != "" && data != "") { + ((ADataBackend)this.mqtt).Send(topic, data); + this.Update?.Invoke(this, new MqttEvent(topic, data)); + } + } + } catch { } + } } } diff --git a/Lora-Bot/Moduls/Scral.cs b/Lora-Bot/Moduls/Scral.cs index f1dcb8e..e26ba80 100644 --- a/Lora-Bot/Moduls/Scral.cs +++ b/Lora-Bot/Moduls/Scral.cs @@ -1,130 +1,118 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Net; using System.Text; -using System.Threading.Tasks; using BlubbFish.Utils; using BlubbFish.Utils.IoT.Bots; using BlubbFish.Utils.IoT.Bots.Moduls; using Fraunhofer.Fit.Iot.Lora; using Fraunhofer.Fit.Iot.Lora.Devices; +using Fraunhofer.Fit.Iot.Lora.Events; using LitJson; -namespace Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls -{ - public class Scral : AModul - { - private readonly List nodes = new List(); - public override event ModulEvent Update; - private Object getLock = new Object(); - private String server = "https://portal.monica-cloud.eu/"; - public Scral(LoraController lib, InIReader settings) : base(lib, settings) - { - lib.Update += Lib_Update; - } - - private void Lib_Update(object sender, Iot.Lora.Events.DeviceUpdateEvent e) - { - LoraClient l = (LoraClient)e.Parent; - if (!nodes.Contains(l.Name)) - { - this.Register(l); - this.nodes.Add(l.Name); - } - this.SendUpdate(l); - - } +namespace Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls { + public class Scral : AModul { + private readonly List nodes = new List(); + public override event ModulEvent Update; + private readonly Object getLock = new Object(); + private readonly String server = "https://portal.monica-cloud.eu/"; + public Scral(LoraController lib, InIReader settings) : base(lib, settings) { } + + public override void EventLibSetter() { + this.library.Update += this.HandleLibUpdate; + } + + protected override void LibUpadteThread(Object state) { + try { + DeviceUpdateEvent e = state as DeviceUpdateEvent; + LoraClient l = (LoraClient)e.Parent; + if (!this.nodes.Contains(l.Name)) { + this.Register(l); + this.nodes.Add(l.Name); + } + this.SendUpdate(l); + } catch { } + } private void SendUpdate(LoraClient l) { if (l.Gps.Fix) { Dictionary d = new Dictionary { - { "type", "uwb"}, - { "tagId", l.Name}, - { "timestamp", DateTime.Now.ToString("o") }, - { "lat", l.Gps.Latitude}, - { "lon", l.Gps.Longitude}, - { "bearing", l.Rssi }, - { "herr",l.Gps.Hdop}, - { "battery_level",l.Snr} - }; - this.RequestString("scral/puetz/dexels/wearable/localization", JsonMapper.ToJson(d), false, RequestMethod.PUT); + { "type", "uwb" }, + { "tagId", l.Name }, + { "timestamp", DateTime.Now.ToString("o") }, + { "lat", l.Gps.Latitude }, + { "lon", l.Gps.Longitude }, + { "bearing", l.Rssi }, + { "herr", l.Gps.Hdop }, + { "battery_level", l.Snr } + }; + if(this.RequestString("scral/puetz/dexels/wearable/localization", JsonMapper.ToJson(d), false, RequestMethod.PUT) == null) { + this.Register(l); + } this.Update?.Invoke(this, new BlubbFish.Utils.IoT.Bots.Events.ModulEventArgs("scral/puetz/dexels/wearable/localization", "PUT", JsonMapper.ToJson(d), "SCRAL")); } } private void Register(LoraClient l) { Dictionary d = new Dictionary { - { "device", "wearable" }, - { "sensor", "tag" }, - { "type", "uwb" }, - { "tagId", l.Name}, - { "timestamp", DateTime.Now.ToString("o") }, - { "unitOfMeasurements", "meters" }, - { "observationType", "propietary" }, - { "state", "active" } - }; + { "device", "wearable" }, + { "sensor", "tag" }, + { "type", "uwb" }, + { "tagId", l.Name }, + { "timestamp", DateTime.Now.ToString("o") }, + { "unitOfMeasurements", "meters" }, + { "observationType", "propietary" }, + { "state", "active" } + }; this.RequestString("scral/puetz/dexels/wearable", JsonMapper.ToJson(d), false, RequestMethod.POST); this.Update?.Invoke(this, new BlubbFish.Utils.IoT.Bots.Events.ModulEventArgs("scral/puetz/dexels/wearable", "POST", JsonMapper.ToJson(d), "SCRAL")); - } - - public override void Dispose() - { - //throw new NotImplementedException(); - } - - protected override void UpdateConfig() { } - - private String RequestString(String address, String json = "", Boolean withoutput = true, RequestMethod method = RequestMethod.GET) - { - String ret = null; - lock (this.getLock) - { - HttpWebRequest request = WebRequest.CreateHttp(this.server + address); - request.Timeout = 5000; - if (method == RequestMethod.POST || method == RequestMethod.PUT) - { - Byte[] requestdata = Encoding.ASCII.GetBytes(json); - request.ContentLength = requestdata.Length; - request.Method = method.ToString(); - request.ContentType = "application/json"; - using (Stream stream = request.GetRequestStream()) - { - stream.Write(requestdata, 0, requestdata.Length); - } - } - try - { - using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) - { - if (response.StatusCode == HttpStatusCode.Unauthorized) - { - Console.Error.WriteLine("Benutzer oder Passwort falsch!"); - throw new Exception("Benutzer oder Passwort falsch!"); - } - if (withoutput) - { - StreamReader reader = new StreamReader(response.GetResponseStream()); - ret = reader.ReadToEnd(); - } - } - } - catch (Exception e) - { - Helper.WriteError("Konnte keine Verbindung zum Razzbery Server herstellen. Resource: \"" + this.server + address + "\" Fehler: " + e.Message); - return null; - //throw new Exceptions.ConnectionException("Konnte keine Verbindung zum Razzbery Server herstellen: " + e.Message); - } - } - return ret; - } - - private enum RequestMethod - { - GET, - POST, - PUT - } - } + } + + public override void Dispose() { } + + protected override void UpdateConfig() { } + + #region HTTP Request + private String RequestString(String address, String json = "", Boolean withoutput = true, RequestMethod method = RequestMethod.GET) { + String ret = null; + lock (this.getLock) { + HttpWebRequest request = WebRequest.CreateHttp(this.server + address); + request.Timeout = 5000; + if (method == RequestMethod.POST || method == RequestMethod.PUT) { + Byte[] requestdata = Encoding.ASCII.GetBytes(json); + request.ContentLength = requestdata.Length; + request.Method = method.ToString(); + request.ContentType = "application/json"; + using (Stream stream = request.GetRequestStream()) { + stream.Write(requestdata, 0, requestdata.Length); + } + } + try { + using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { + if (response.StatusCode == HttpStatusCode.Unauthorized) { + Console.Error.WriteLine("Benutzer oder Passwort falsch!"); + throw new Exception("Benutzer oder Passwort falsch!"); + } + if (withoutput) { + StreamReader reader = new StreamReader(response.GetResponseStream()); + ret = reader.ReadToEnd(); + } + } + } catch (Exception e) { + Helper.WriteError("Konnte keine Verbindung zum Razzbery Server herstellen. Resource: \"" + this.server + address + "\" Fehler: " + e.Message); + return null; + //throw new Exceptions.ConnectionException("Konnte keine Verbindung zum Razzbery Server herstellen: " + e.Message); + } + } + return ret; + } + + private enum RequestMethod { + GET, + POST, + PUT + } + #endregion + } } diff --git a/Lora-Bot/Moduls/Txtout.cs b/Lora-Bot/Moduls/Txtout.cs index 809cfba..ee8490d 100644 --- a/Lora-Bot/Moduls/Txtout.cs +++ b/Lora-Bot/Moduls/Txtout.cs @@ -17,18 +17,24 @@ namespace Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls { public Txtout(LoraController lib, InIReader settings) : base(lib, settings) { if (this.config.ContainsKey("general") && this.config["general"].ContainsKey("path")) { this.filename = this.config["general"]["path"]; - this.file = new StreamWriter(this.filename); - this.library.Update += this.Library_Update; + this.file = new StreamWriter(this.filename, true); } } - private void Library_Update(Object sender, DeviceUpdateEvent e) { - if (sender is LoraClient l) { - String s = l.Name + "," + l.Receivedtime.ToString("o") + "," + l.Gps.Latitude + "," + l.Gps.Longitude + ",https://www.google.de/maps?q=" + l.Gps.Latitude + "%2C" + l.Gps.Longitude + "," + l.Rssi + "," + l.PacketRssi + "," + l.Snr; - this.file.WriteLine(s); - this.file.Flush(); - this.Update?.Invoke(this, new ModulEventArgs(this.filename, "Line", s, "TXTOUT")); - } + public override void EventLibSetter() { + this.library.Update += this.HandleLibUpdate; + } + + protected override void LibUpadteThread(Object state) { + try { + DeviceUpdateEvent e = state as DeviceUpdateEvent; + if (e.Parent is LoraClient l) { + String s = l.Name + "," + l.Receivedtime.ToString("o") + "," + l.Gps.Latitude + "," + l.Gps.Longitude + ",https://www.google.de/maps?q=" + l.Gps.Latitude + "%2C" + l.Gps.Longitude + "," + l.Rssi + "," + l.PacketRssi + "," + l.Snr; + this.file.WriteLine(s); + this.file.Flush(); + this.Update?.Invoke(this, new ModulEventArgs(this.filename, "Line", s, "TXTOUT")); + } + } catch { } } public override void Dispose() { diff --git a/Lora-Bot/Program.cs b/Lora-Bot/Program.cs index 76e340f..64724c8 100644 --- a/Lora-Bot/Program.cs +++ b/Lora-Bot/Program.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using BlubbFish.Utils; using BlubbFish.Utils.IoT.Bots; -using BlubbFish.Utils.IoT.Interfaces; using Fraunhofer.Fit.Iot.Lora; namespace Fraunhofer.Fit.IoT.Bots.LoraBot {