Skip to content
Snippets Groups Projects
Commit 997a0508 authored by philip.schell's avatar philip.schell
Browse files

[1.8.3] Update to changed ConnectorDataMqtt and remove Scral from code,...

[1.8.3] Update to changed ConnectorDataMqtt and remove Scral from code, because its an own project now, that uses the mqtt-backend
parent 17cd18ae
No related branches found
No related tags found
No related merge requests found
...@@ -44,7 +44,6 @@ ...@@ -44,7 +44,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Moduls\Mqtt.cs" /> <Compile Include="Moduls\Mqtt.cs" />
<Compile Include="Moduls\Scral.cs" />
<Compile Include="Moduls\Txtout.cs" /> <Compile Include="Moduls\Txtout.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
...@@ -80,9 +79,6 @@ ...@@ -80,9 +79,6 @@
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="config-example\scral.conf.example">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="config-example\mqtt.conf.example"> <None Include="config-example\mqtt.conf.example">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
......
...@@ -8,27 +8,10 @@ using Fraunhofer.Fit.Iot.Lora; ...@@ -8,27 +8,10 @@ using Fraunhofer.Fit.Iot.Lora;
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() {
this.mqtt = ABackend.GetInstance(this.config["settings"], ABackend.BackendType.Data);
Console.WriteLine("Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls.Mqtt.Connect");
this.mqttconnect = true;
}
protected override void Disconnect() {
this.mqttconnect = false;
if (this.mqtt != null) {
this.mqtt.Dispose();
}
this.mqtt = null;
Console.WriteLine("Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls.Mqtt.Disconnect");
}
public override void EventLibSetter() { public override void EventLibSetter() {
this.library.DataUpdate += this.HandleLibUpdate; this.library.DataUpdate += this.HandleLibUpdate;
this.library.PanicUpdate += this.HandleLibUpdate; this.library.PanicUpdate += this.HandleLibUpdate;
...@@ -37,7 +20,7 @@ namespace Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls { ...@@ -37,7 +20,7 @@ namespace Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls {
protected override void LibUpadteThread(Object state) { protected override void LibUpadteThread(Object state) {
try { try {
if (this.mqttconnect) { if (this.mqtt.IsConnected) {
if(state.GetType().HasInterface(typeof(IMqtt))) { if(state.GetType().HasInterface(typeof(IMqtt))) {
IMqtt sensor = state as IMqtt; IMqtt sensor = state as IMqtt;
((ADataBackend)this.mqtt).Send("lora/" + sensor.MqttTopic(), sensor.ToJson()); ((ADataBackend)this.mqtt).Send("lora/" + sensor.MqttTopic(), sensor.ToJson());
......
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using BlubbFish.Utils;
using BlubbFish.Utils.IoT.Bots.Moduls;
using Fraunhofer.Fit.Iot.Lora;
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 readonly Object getLock = new Object();
private readonly Boolean authRequired;
private readonly String auth;
public Scral(LoraController lib, InIReader settings) : base(lib, settings) {
if (!this.config.ContainsKey("general")) {
throw new ArgumentException("Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls.Scral: Config section [general] not exist");
}
if (!this.config["general"].ContainsKey("server")) {
throw new ArgumentException("Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls.Scral: In config section [general] value server not exist");
}
if (!this.config["general"].ContainsKey("user") && !this.config["general"].ContainsKey("pass")) {
this.authRequired = false;
} else if (!this.config["general"].ContainsKey("user")) {
throw new ArgumentException("Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls.Scral: In config section [general] value user not exist");
} else if (!this.config["general"].ContainsKey("pass")) {
throw new ArgumentException("Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls.Scral: In config section [general] value pass not exist");
} else {
this.auth = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(this.config["general"]["user"] + ":" + this.config["general"]["pass"]));
this.authRequired = true;
}
if (!this.config.ContainsKey("update")) {
throw new ArgumentException("Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls.Scral: Config section [update] not exist");
}
if (!this.config["update"].ContainsKey("addr")) {
throw new ArgumentException("Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls.Scral: In config section [update] value addr not exist");
}
if (!this.config["update"].ContainsKey("method")) {
throw new ArgumentException("Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls.Scral: In config section [update] value method not exist");
}
if (!this.config.ContainsKey("register")) {
throw new ArgumentException("Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls.Scral: Config section [register] not exist");
}
if (!this.config["register"].ContainsKey("addr")) {
throw new ArgumentException("Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls.Scral: In config section [register] value addr not exist");
}
if (!this.config["register"].ContainsKey("method")) {
throw new ArgumentException("Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls.Scral: In config section [register] value method not exist");
}
}
public override void EventLibSetter() {
this.library.DataUpdate += this.HandleLibUpdate;
}
protected override void LibUpadteThread(Object state) {
try {
if (state is DataUpdateEvent data) {
if (!this.nodes.Contains(data.Name)) {
this.SendRegister(data);
this.nodes.Add(data.Name);
}
this.SendUpdate(data);
}
} catch (Exception e) {
Helper.WriteError("Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls.Scral.LibUpadteThread: " + e.Message);
}
}
private void SendUpdate(DataUpdateEvent data) {
if (data.Gps.Fix) {
Dictionary<String, Object> d = new Dictionary<String, Object> {
{ "type", "uwb" },
{ "tagId", data.Name },
{ "timestamp", DateTime.Now.ToString("o") },
{ "lat", data.Gps.Latitude },
{ "lon", data.Gps.Longitude },
{ "bearing", data.Rssi },
{ "herr", data.Gps.Hdop },
{ "battery_level", data.Snr }
};
try {
String addr = this.config["update"]["addr"];
if (Enum.TryParse(this.config["update"]["method"], true, out RequestMethod meth)) {
this.RequestString(addr, JsonMapper.ToJson(d), false, meth);
this.Update?.Invoke(this, new BlubbFish.Utils.IoT.Bots.Events.ModulEventArgs(addr, meth.ToString(), JsonMapper.ToJson(d), "SCRAL"));
}
} catch (Exception e) {
Helper.WriteError("Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls.Scral.SendUpdate: " + e.Message);
this.SendRegister(data);
}
}
}
private void SendRegister(DataUpdateEvent data) {
Dictionary<String, Object> d = new Dictionary<String, Object> {
{ "device", "wearable" },
{ "sensor", "tag" },
{ "type", "uwb" },
{ "tagId", data.Name },
{ "timestamp", DateTime.Now.ToString("o") },
{ "unitOfMeasurements", "meters" },
{ "observationType", "propietary" },
{ "state", "active" }
};
try {
String addr = this.config["register"]["addr"];
if (Enum.TryParse(this.config["register"]["method"], true, out RequestMethod meth)) {
this.RequestString(addr, JsonMapper.ToJson(d), false, meth);
this.Update?.Invoke(this, new BlubbFish.Utils.IoT.Bots.Events.ModulEventArgs(addr, meth.ToString(), JsonMapper.ToJson(d), "SCRAL"));
}
} catch (Exception e) {
Helper.WriteError("Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls.Scral.SendRegister: " + e.Message);
}
}
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.config["general"]["server"] + address);
request.Timeout = 2000;
if (this.authRequired) {
request.Headers.Add(HttpRequestHeader.Authorization, this.auth);
}
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) {
throw new WebException("Error while uploading to Scal. Resource: \"" + this.config["general"]["server"] + address + "\" Method: " + method + " Data: " + json + " Fehler: " + e.Message);
}
}
return ret;
}
private enum RequestMethod {
GET,
POST,
PUT
}
#endregion
}
}
...@@ -24,9 +24,7 @@ namespace Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls { ...@@ -24,9 +24,7 @@ namespace Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls {
} }
public override void EventLibSetter() { public override void EventLibSetter() => this.library.DataUpdate += this.HandleLibUpdate;
this.library.DataUpdate += this.HandleLibUpdate;
}
protected override void LibUpadteThread(Object state) { protected override void LibUpadteThread(Object state) {
try { try {
......
...@@ -29,6 +29,7 @@ namespace Fraunhofer.Fit.IoT.Bots.LoraBot { ...@@ -29,6 +29,7 @@ namespace Fraunhofer.Fit.IoT.Bots.LoraBot {
this.ModulEvents(); this.ModulEvents();
lora.DataUpdate += this.LoraDataUpdate; lora.DataUpdate += this.LoraDataUpdate;
lora.StatusUpdate += this.LoraStatusUpdate; lora.StatusUpdate += this.LoraStatusUpdate;
lora.PanicUpdate += this.LoraPanicUpdate;
this.WaitForShutdown(); this.WaitForShutdown();
Console.WriteLine("after wait"); Console.WriteLine("after wait");
this.ModulDispose(); this.ModulDispose();
...@@ -47,13 +48,11 @@ namespace Fraunhofer.Fit.IoT.Bots.LoraBot { ...@@ -47,13 +48,11 @@ namespace Fraunhofer.Fit.IoT.Bots.LoraBot {
} }
} }
private void LoraStatusUpdate(Object sender, StatusUpdateEvent e) { private void LoraPanicUpdate(Object sender, PanicUpdateEvent e) => Console.WriteLine("-> Lora-Panic: " + e.ToString());
Console.WriteLine("-> Lora-Status: " + e.ToString());
}
private void LoraDataUpdate(Object sender, DataUpdateEvent e) { private void LoraStatusUpdate(Object sender, StatusUpdateEvent e) => Console.WriteLine("-> Lora-Status: " + e.ToString());
Console.WriteLine("-> Lora-Data: " + e.ToString());
} private void LoraDataUpdate(Object sender, DataUpdateEvent e) => Console.WriteLine("-> Lora-Data: " + e.ToString());
} }
} }
using System.Reflection; using System.Reflection;
using System.Resources;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
// Allgemeine Informationen über eine Assembly werden über die folgenden // Allgemeine Informationen über eine Assembly werden über die folgenden
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, // Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
// die einer Assembly zugeordnet sind. // die einer Assembly zugeordnet sind.
[assembly: AssemblyTitle("Lora-Bot")] [assembly: AssemblyTitle("Lora-Bot")]
[assembly: AssemblyDescription("")] [assembly: AssemblyDescription("Program that runs on a device and process the Lora traffic from the Lora library")]
[assembly: AssemblyConfiguration("")] [assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")] [assembly: AssemblyCompany("Fraunhofer FIT")]
[assembly: AssemblyProduct("Lora-Bot")] [assembly: AssemblyProduct("Lora-Bot")]
[assembly: AssemblyCopyright("Copyright © 2018 - 22.04.2019")] [assembly: AssemblyCopyright("Copyright © 2018 - 31.05.2019")]
[assembly: AssemblyTrademark("")] [assembly: AssemblyTrademark("Fraunhofer FIT, BlubbFish")]
[assembly: AssemblyCulture("")] [assembly: AssemblyCulture("")]
[assembly: NeutralResourcesLanguage("de-DE")]
// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly // Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly
// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von // für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von
...@@ -31,8 +33,8 @@ using System.Runtime.InteropServices; ...@@ -31,8 +33,8 @@ using System.Runtime.InteropServices;
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden, // Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
// übernehmen, indem Sie "*" eingeben: // übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.8.2")] [assembly: AssemblyVersion("1.8.3")]
[assembly: AssemblyFileVersion("1.8.2")] [assembly: AssemblyFileVersion("1.8.3")]
/* /*
* 1.1.0 Update Scral addresses * 1.1.0 Update Scral addresses
* 1.2.0 Run Module Events in threads so that one Module can not block others, TXTOut now appends to the logfile * 1.2.0 Run Module Events in threads so that one Module can not block others, TXTOut now appends to the logfile
...@@ -51,4 +53,5 @@ using System.Runtime.InteropServices; ...@@ -51,4 +53,5 @@ using System.Runtime.InteropServices;
* 1.8.0 Add field that indicates when the last gps position was recieved, change all times to UTC * 1.8.0 Add field that indicates when the last gps position was recieved, change all times to UTC
* 1.8.1 Add Hostname to MQTT, so you can see from witch device the data is recieved * 1.8.1 Add Hostname to MQTT, so you can see from witch device the data is recieved
* 1.8.2 Bugfix, create also an event for sending normal loradata when update panic * 1.8.2 Bugfix, create also an event for sending normal loradata when update panic
* 1.8.3 Update to changed ConnectorDataMqtt and remove Scral from code, because its an own project now, that uses the mqtt-backend
*/ */
[modul]
config=private
[general]
server=https://portal.monica-cloud.eu/
[update]
addr=scral/puetz/dexels/wearable/localization
method=put
[register]
addr=scral/puetz/dexels/wearable
method=post
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment