Rewrite Bot-Utils so that handlers are threaded
Tiny fix in INIReader
This commit is contained in:
parent
8381319596
commit
f1636e9033
@ -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;
|
protected override void LibUpadteThread(Object state) {
|
||||||
topic = "lora/" + sensor.MqttTopic();
|
try {
|
||||||
data = sensor.ToJson();
|
if (this.mqttconnect) {
|
||||||
}
|
DeviceUpdateEvent e = state as DeviceUpdateEvent;
|
||||||
Console.WriteLine(topic);
|
String topic = "";
|
||||||
Console.WriteLine(data);
|
String data = "";
|
||||||
if (topic != "" && data != "") {
|
if (e.Parent.GetType().HasInterface(typeof(IMqtt))) {
|
||||||
((ADataBackend)this.mqtt).Send(topic, data);
|
IMqtt sensor = (IMqtt)e.Parent;
|
||||||
this.Update?.Invoke(this, new MqttEvent(topic, data));
|
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 { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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)
|
|
||||||
{
|
|
||||||
lib.Update += Lib_Update;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Lib_Update(object sender, Iot.Lora.Events.DeviceUpdateEvent e)
|
public override void EventLibSetter() {
|
||||||
{
|
this.library.Update += this.HandleLibUpdate;
|
||||||
LoraClient l = (LoraClient)e.Parent;
|
}
|
||||||
if (!nodes.Contains(l.Name))
|
|
||||||
{
|
|
||||||
this.Register(l);
|
|
||||||
this.nodes.Add(l.Name);
|
|
||||||
}
|
|
||||||
this.SendUpdate(l);
|
|
||||||
|
|
||||||
|
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) {
|
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
|
||||||
|
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 {
|
||||||
protected override void UpdateConfig() { }
|
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {
|
||||||
|
if (response.StatusCode == HttpStatusCode.Unauthorized) {
|
||||||
private String RequestString(String address, String json = "", Boolean withoutput = true, RequestMethod method = RequestMethod.GET)
|
Console.Error.WriteLine("Benutzer oder Passwort falsch!");
|
||||||
{
|
throw new Exception("Benutzer oder Passwort falsch!");
|
||||||
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;
|
if (withoutput) {
|
||||||
}
|
StreamReader reader = new StreamReader(response.GetResponseStream());
|
||||||
|
ret = reader.ReadToEnd();
|
||||||
private enum RequestMethod
|
}
|
||||||
{
|
}
|
||||||
GET,
|
} catch (Exception e) {
|
||||||
POST,
|
Helper.WriteError("Konnte keine Verbindung zum Razzbery Server herstellen. Resource: \"" + this.server + address + "\" Fehler: " + e.Message);
|
||||||
PUT
|
return null;
|
||||||
|
//throw new Exceptions.ConnectionException("Konnte keine Verbindung zum Razzbery Server herstellen: " + e.Message);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private enum RequestMethod {
|
||||||
|
GET,
|
||||||
|
POST,
|
||||||
|
PUT
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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() {
|
||||||
|
@ -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 {
|
||||||
|
Reference in New Issue
Block a user