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 {
class Mqtt : Mqtt<LoraController> {
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 { }
}
}
}

View File

@ -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<LoraController>
{
private readonly List<String> nodes = new List<String>();
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<LoraController> {
private readonly List<String> nodes = new List<String>();
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<String, Object> d = new Dictionary<String, Object> {
{ "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<String, Object> d = new Dictionary<String, Object> {
{ "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
}
}

View File

@ -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() {

View File

@ -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 {