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,27 +10,35 @@ 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() {
this.library.Update += this.HandleLibUpdate;
}
protected override void LibUpadteThread(Object state) {
try {
if (this.mqttconnect) {
DeviceUpdateEvent e = state as DeviceUpdateEvent;
String topic = ""; String topic = "";
String data = ""; String data = "";
if (e.Parent.GetType().HasInterface(typeof(IMqtt))) { if (e.Parent.GetType().HasInterface(typeof(IMqtt))) {
@ -45,5 +53,7 @@ namespace Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls {
this.Update?.Invoke(this, new MqttEvent(topic, data)); this.Update?.Invoke(this, new MqttEvent(topic, data));
} }
} }
} catch { }
}
} }
} }

View File

@ -1,55 +1,55 @@
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>(); private readonly List<String> nodes = new List<String>();
public override event ModulEvent Update; public override event ModulEvent Update;
private Object getLock = new Object(); private readonly Object getLock = new Object();
private String server = "https://portal.monica-cloud.eu/"; private readonly String server = "https://portal.monica-cloud.eu/";
public Scral(LoraController lib, InIReader settings) : base(lib, settings) public Scral(LoraController lib, InIReader settings) : base(lib, settings) { }
{
lib.Update += Lib_Update; public override void EventLibSetter() {
this.library.Update += this.HandleLibUpdate;
} }
private void Lib_Update(object sender, Iot.Lora.Events.DeviceUpdateEvent e) protected override void LibUpadteThread(Object state) {
{ 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"));
} }
} }
@ -59,7 +59,7 @@ namespace Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls
{ "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" },
@ -69,49 +69,37 @@ namespace Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls
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() { } protected override void UpdateConfig() { }
private String RequestString(String address, String json = "", Boolean withoutput = true, RequestMethod method = RequestMethod.GET) #region HTTP Request
{ private String RequestString(String address, String json = "", Boolean withoutput = true, RequestMethod method = RequestMethod.GET) {
String ret = null; String ret = null;
lock (this.getLock) lock (this.getLock) {
{
HttpWebRequest request = WebRequest.CreateHttp(this.server + address); HttpWebRequest request = WebRequest.CreateHttp(this.server + address);
request.Timeout = 5000; request.Timeout = 5000;
if (method == RequestMethod.POST || method == RequestMethod.PUT) if (method == RequestMethod.POST || method == RequestMethod.PUT) {
{
Byte[] requestdata = Encoding.ASCII.GetBytes(json); Byte[] requestdata = Encoding.ASCII.GetBytes(json);
request.ContentLength = requestdata.Length; request.ContentLength = requestdata.Length;
request.Method = method.ToString(); request.Method = method.ToString();
request.ContentType = "application/json"; request.ContentType = "application/json";
using (Stream stream = request.GetRequestStream()) using (Stream stream = request.GetRequestStream()) {
{
stream.Write(requestdata, 0, requestdata.Length); stream.Write(requestdata, 0, requestdata.Length);
} }
} }
try try {
{ using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) if (response.StatusCode == HttpStatusCode.Unauthorized) {
{
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
Console.Error.WriteLine("Benutzer oder Passwort falsch!"); Console.Error.WriteLine("Benutzer oder Passwort falsch!");
throw new Exception("Benutzer oder Passwort falsch!"); throw new Exception("Benutzer oder Passwort falsch!");
} }
if (withoutput) if (withoutput) {
{
StreamReader reader = new StreamReader(response.GetResponseStream()); StreamReader reader = new StreamReader(response.GetResponseStream());
ret = reader.ReadToEnd(); ret = reader.ReadToEnd();
} }
} }
} } catch (Exception e) {
catch (Exception e)
{
Helper.WriteError("Konnte keine Verbindung zum Razzbery Server herstellen. Resource: \"" + this.server + address + "\" Fehler: " + e.Message); Helper.WriteError("Konnte keine Verbindung zum Razzbery Server herstellen. Resource: \"" + this.server + address + "\" Fehler: " + e.Message);
return null; return null;
//throw new Exceptions.ConnectionException("Konnte keine Verbindung zum Razzbery Server herstellen: " + e.Message); //throw new Exceptions.ConnectionException("Konnte keine Verbindung zum Razzbery Server herstellen: " + e.Message);
@ -120,11 +108,11 @@ namespace Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls
return ret; return ret;
} }
private enum RequestMethod private enum RequestMethod {
{
GET, GET,
POST, POST,
PUT PUT
} }
#endregion
} }
} }

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;
}
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; 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.WriteLine(s);
this.file.Flush(); this.file.Flush();
this.Update?.Invoke(this, new ModulEventArgs(this.filename, "Line", s, "TXTOUT")); 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 {