Rewrite Bot-Utils so that handlers are threaded

Tiny fix in INIReader
This commit is contained in:
BlubbFish 2018-09-11 07:59:10 +00:00
parent 8381319596
commit f1636e9033
4 changed files with 137 additions and 134 deletions

View File

@ -10,40 +10,50 @@ using Fraunhofer.Fit.Iot.Lora.Events;
namespace Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls { namespace Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls {
class Mqtt : Mqtt<LoraController> { class Mqtt : Mqtt<LoraController> {
private Boolean mqttconnect = false;
public override event ModulEvent Update; public override event ModulEvent Update;
public Mqtt(LoraController lib, InIReader settings) : base(lib, settings) { } public Mqtt(LoraController lib, InIReader settings) : base(lib, settings) { }
protected override void Connect() { protected override void Connect() {
this.mqtt = ABackend.GetInstance(this.config["settings"], ABackend.BackendType.Data); this.mqtt = ABackend.GetInstance(this.config["settings"], ABackend.BackendType.Data);
//this.mqtt.MessageIncomming += this.EventInput; Console.WriteLine("Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls.Mqtt.Connect()");
this.library.Update += this.EventOutput; this.mqttconnect = true;
Console.WriteLine("Connect!");
} }
protected override void Disconnect() { protected override void Disconnect() {
this.library.Update -= this.EventOutput; this.mqttconnect = false;
if (this.mqtt != null) { if (this.mqtt != null) {
this.mqtt.Dispose(); this.mqtt.Dispose();
} }
this.mqtt = null; this.mqtt = null;
Console.WriteLine("Disconnect!"); Console.WriteLine("Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls.Mqtt.Disconnect()");
} }
protected virtual void EventOutput(Object sender, DeviceUpdateEvent e) { public override void EventLibSetter() {
String topic = ""; this.library.Update += this.HandleLibUpdate;
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));
}
} }
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 { }
}
} }
} }

View File

@ -1,130 +1,118 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Net; using System.Net;
using System.Text; using System.Text;
using System.Threading.Tasks;
using BlubbFish.Utils; using BlubbFish.Utils;
using BlubbFish.Utils.IoT.Bots; using BlubbFish.Utils.IoT.Bots;
using BlubbFish.Utils.IoT.Bots.Moduls; using BlubbFish.Utils.IoT.Bots.Moduls;
using Fraunhofer.Fit.Iot.Lora; using Fraunhofer.Fit.Iot.Lora;
using Fraunhofer.Fit.Iot.Lora.Devices; using Fraunhofer.Fit.Iot.Lora.Devices;
using Fraunhofer.Fit.Iot.Lora.Events;
using LitJson; using LitJson;
namespace Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls namespace Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls {
{ public class Scral : AModul<LoraController> {
public class Scral : AModul<LoraController> private readonly List<String> nodes = new List<String>();
{ public override event ModulEvent Update;
private readonly List<String> nodes = new List<String>(); private readonly Object getLock = new Object();
public override event ModulEvent Update; private readonly String server = "https://portal.monica-cloud.eu/";
private Object getLock = new Object(); public Scral(LoraController lib, InIReader settings) : base(lib, settings) { }
private 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;
lib.Update += Lib_Update; }
}
protected override void LibUpadteThread(Object state) {
private void Lib_Update(object sender, Iot.Lora.Events.DeviceUpdateEvent e) try {
{ DeviceUpdateEvent e = state as DeviceUpdateEvent;
LoraClient l = (LoraClient)e.Parent; LoraClient l = (LoraClient)e.Parent;
if (!nodes.Contains(l.Name)) if (!this.nodes.Contains(l.Name)) {
{ this.Register(l);
this.Register(l); this.nodes.Add(l.Name);
this.nodes.Add(l.Name); }
} this.SendUpdate(l);
this.SendUpdate(l); } catch { }
}
}
private void SendUpdate(LoraClient l) { private void SendUpdate(LoraClient l) {
if (l.Gps.Fix) { if (l.Gps.Fix) {
Dictionary<String, Object> d = new Dictionary<String, Object> { Dictionary<String, Object> d = new Dictionary<String, Object> {
{ "type", "uwb"}, { "type", "uwb" },
{ "tagId", l.Name}, { "tagId", l.Name },
{ "timestamp", DateTime.Now.ToString("o") }, { "timestamp", DateTime.Now.ToString("o") },
{ "lat", l.Gps.Latitude}, { "lat", l.Gps.Latitude },
{ "lon", l.Gps.Longitude}, { "lon", l.Gps.Longitude },
{ "bearing", l.Rssi }, { "bearing", l.Rssi },
{ "herr",l.Gps.Hdop}, { "herr", l.Gps.Hdop },
{ "battery_level",l.Snr} { "battery_level", l.Snr }
}; };
this.RequestString("scral/puetz/dexels/wearable/localization", JsonMapper.ToJson(d), false, RequestMethod.PUT); 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")); 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) { private void Register(LoraClient l) {
Dictionary<String, Object> d = new Dictionary<String, Object> { Dictionary<String, Object> d = new Dictionary<String, Object> {
{ "device", "wearable" }, { "device", "wearable" },
{ "sensor", "tag" }, { "sensor", "tag" },
{ "type", "uwb" }, { "type", "uwb" },
{ "tagId", l.Name}, { "tagId", l.Name },
{ "timestamp", DateTime.Now.ToString("o") }, { "timestamp", DateTime.Now.ToString("o") },
{ "unitOfMeasurements", "meters" }, { "unitOfMeasurements", "meters" },
{ "observationType", "propietary" }, { "observationType", "propietary" },
{ "state", "active" } { "state", "active" }
}; };
this.RequestString("scral/puetz/dexels/wearable", JsonMapper.ToJson(d), false, RequestMethod.POST); 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")); this.Update?.Invoke(this, new BlubbFish.Utils.IoT.Bots.Events.ModulEventArgs("scral/puetz/dexels/wearable", "POST", JsonMapper.ToJson(d), "SCRAL"));
} }
public override void Dispose() public override void Dispose() { }
{
//throw new NotImplementedException(); protected override void UpdateConfig() { }
}
#region HTTP Request
protected override void UpdateConfig() { } private String RequestString(String address, String json = "", Boolean withoutput = true, RequestMethod method = RequestMethod.GET) {
String ret = null;
private String RequestString(String address, String json = "", Boolean withoutput = true, RequestMethod method = RequestMethod.GET) lock (this.getLock) {
{ HttpWebRequest request = WebRequest.CreateHttp(this.server + address);
String ret = null; request.Timeout = 5000;
lock (this.getLock) if (method == RequestMethod.POST || method == RequestMethod.PUT) {
{ Byte[] requestdata = Encoding.ASCII.GetBytes(json);
HttpWebRequest request = WebRequest.CreateHttp(this.server + address); request.ContentLength = requestdata.Length;
request.Timeout = 5000; request.Method = method.ToString();
if (method == RequestMethod.POST || method == RequestMethod.PUT) request.ContentType = "application/json";
{ using (Stream stream = request.GetRequestStream()) {
Byte[] requestdata = Encoding.ASCII.GetBytes(json); stream.Write(requestdata, 0, requestdata.Length);
request.ContentLength = requestdata.Length; }
request.Method = method.ToString(); }
request.ContentType = "application/json"; try {
using (Stream stream = request.GetRequestStream()) using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {
{ if (response.StatusCode == HttpStatusCode.Unauthorized) {
stream.Write(requestdata, 0, requestdata.Length); Console.Error.WriteLine("Benutzer oder Passwort falsch!");
} throw new Exception("Benutzer oder Passwort falsch!");
} }
try if (withoutput) {
{ StreamReader reader = new StreamReader(response.GetResponseStream());
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) ret = reader.ReadToEnd();
{ }
if (response.StatusCode == HttpStatusCode.Unauthorized) }
{ } catch (Exception e) {
Console.Error.WriteLine("Benutzer oder Passwort falsch!"); Helper.WriteError("Konnte keine Verbindung zum Razzbery Server herstellen. Resource: \"" + this.server + address + "\" Fehler: " + e.Message);
throw new Exception("Benutzer oder Passwort falsch!"); return null;
} //throw new Exceptions.ConnectionException("Konnte keine Verbindung zum Razzbery Server herstellen: " + e.Message);
if (withoutput) }
{ }
StreamReader reader = new StreamReader(response.GetResponseStream()); return ret;
ret = reader.ReadToEnd(); }
}
} private enum RequestMethod {
} GET,
catch (Exception e) POST,
{ PUT
Helper.WriteError("Konnte keine Verbindung zum Razzbery Server herstellen. Resource: \"" + this.server + address + "\" Fehler: " + e.Message); }
return null; #endregion
//throw new Exceptions.ConnectionException("Konnte keine Verbindung zum Razzbery Server herstellen: " + e.Message); }
}
}
return ret;
}
private enum RequestMethod
{
GET,
POST,
PUT
}
}
} }

View File

@ -17,18 +17,24 @@ namespace Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls {
public Txtout(LoraController lib, InIReader settings) : base(lib, settings) { public Txtout(LoraController lib, InIReader settings) : base(lib, settings) {
if (this.config.ContainsKey("general") && this.config["general"].ContainsKey("path")) { if (this.config.ContainsKey("general") && this.config["general"].ContainsKey("path")) {
this.filename = this.config["general"]["path"]; this.filename = this.config["general"]["path"];
this.file = new StreamWriter(this.filename); this.file = new StreamWriter(this.filename, true);
this.library.Update += this.Library_Update;
} }
} }
private void Library_Update(Object sender, DeviceUpdateEvent e) { public override void EventLibSetter() {
if (sender is LoraClient l) { this.library.Update += this.HandleLibUpdate;
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(); protected override void LibUpadteThread(Object state) {
this.Update?.Invoke(this, new ModulEventArgs(this.filename, "Line", s, "TXTOUT")); 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() { public override void Dispose() {

View File

@ -2,7 +2,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using BlubbFish.Utils; using BlubbFish.Utils;
using BlubbFish.Utils.IoT.Bots; using BlubbFish.Utils.IoT.Bots;
using BlubbFish.Utils.IoT.Interfaces;
using Fraunhofer.Fit.Iot.Lora; using Fraunhofer.Fit.Iot.Lora;
namespace Fraunhofer.Fit.IoT.Bots.LoraBot { namespace Fraunhofer.Fit.IoT.Bots.LoraBot {