Skip to content

Commit

Permalink
Lora-Bot 1.3.0 Scral now get its config from configfile, make the GW …
Browse files Browse the repository at this point in the history
…more stable

Lora 1.1.0 Now awaiing Battery as Double and fix the sending twise issue
  • Loading branch information
blubbfish committed Sep 13, 2018
1 parent c3ab7b8 commit 64cbe25
Show file tree
Hide file tree
Showing 25 changed files with 130 additions and 84 deletions.
3 changes: 0 additions & 3 deletions Lora-Bot/Lora-Bot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@
<None Include="libwiringPi.so.2.44">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="lora.sh">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
10 changes: 5 additions & 5 deletions Lora-Bot/Moduls/Mqtt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ 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()");
Console.WriteLine("Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls.Mqtt.Connect");
this.mqttconnect = true;
}

Expand All @@ -28,7 +28,7 @@ protected override void Disconnect() {
this.mqtt.Dispose();
}
this.mqtt = null;
Console.WriteLine("Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls.Mqtt.Disconnect()");
Console.WriteLine("Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls.Mqtt.Disconnect");
}

public override void EventLibSetter() {
Expand All @@ -46,14 +46,14 @@ protected override void LibUpadteThread(Object state) {
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 { }
} catch (Exception e) {
Helper.WriteError("Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls.Mqtt.LibUpadteThread: " + e.Message);
}
}
}
}
67 changes: 52 additions & 15 deletions Lora-Bot/Moduls/Scral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,32 @@ 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 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.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.Update += this.HandleLibUpdate;
Expand All @@ -28,11 +52,13 @@ protected override void LibUpadteThread(Object state) {
DeviceUpdateEvent e = state as DeviceUpdateEvent;
LoraClient l = (LoraClient)e.Parent;
if (!this.nodes.Contains(l.Name)) {
this.Register(l);
this.SendRegister(l);
this.nodes.Add(l.Name);
}
this.SendUpdate(l);
} catch { }
} catch (Exception e) {
Helper.WriteError("Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls.Scral.LibUpadteThread: " + e.Message);
}
}

private void SendUpdate(LoraClient l) {
Expand All @@ -47,14 +73,20 @@ private void SendUpdate(LoraClient l) {
{ "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);
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(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) {
private void SendRegister(LoraClient l) {
Dictionary<String, Object> d = new Dictionary<String, Object> {
{ "device", "wearable" },
{ "sensor", "tag" },
Expand All @@ -65,8 +97,15 @@ private void Register(LoraClient l) {
{ "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"));
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() { }
Expand All @@ -77,8 +116,8 @@ 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;
HttpWebRequest request = WebRequest.CreateHttp(this.config["general"]["server"] + address);
request.Timeout = 2000;
if (method == RequestMethod.POST || method == RequestMethod.PUT) {
Byte[] requestdata = Encoding.ASCII.GetBytes(json);
request.ContentLength = requestdata.Length;
Expand All @@ -100,9 +139,7 @@ private String RequestString(String address, String json = "", Boolean withoutpu
}
}
} 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);
throw new WebException("Error while uploading to Scal. Resource: \"" + this.config["general"]["server"] + address + "\" Method: " + method + " Data: " + json + " Fehler: " + e.Message);
}
}
return ret;
Expand Down
67 changes: 35 additions & 32 deletions Lora-Bot/Moduls/Txtout.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
using System;
using System.IO;
using BlubbFish.Utils;
using BlubbFish.Utils.IoT.Bots.Events;
using BlubbFish.Utils.IoT.Bots.Moduls;
using Fraunhofer.Fit.Iot.Lora;
using Fraunhofer.Fit.Iot.Lora.Devices;
using Fraunhofer.Fit.Iot.Lora.Events;

namespace Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls {
public class Txtout : AModul<LoraController> {
public override event ModulEvent Update;

private readonly String filename;
private readonly StreamWriter file;

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, true);
}
}

using System;
using System.IO;
using BlubbFish.Utils;
using BlubbFish.Utils.IoT.Bots.Events;
using BlubbFish.Utils.IoT.Bots.Moduls;
using Fraunhofer.Fit.Iot.Lora;
using Fraunhofer.Fit.Iot.Lora.Devices;
using Fraunhofer.Fit.Iot.Lora.Events;

namespace Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls {
public class Txtout : AModul<LoraController> {
public override event ModulEvent Update;

private readonly String filename;
private readonly StreamWriter file;

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, true);
} else {
throw new ArgumentException("Setting section [general] is missing or its value path");
}

}

public override void EventLibSetter() {
this.library.Update += this.HandleLibUpdate;
}
Expand All @@ -35,13 +38,13 @@ protected override void LibUpadteThread(Object state) {
this.Update?.Invoke(this, new ModulEventArgs(this.filename, "Line", s, "TXTOUT"));
}
} catch { }
}

public override void Dispose() {
this.file.Flush();
this.file.Close();
}

protected override void UpdateConfig() {}
}
}

public override void Dispose() {
this.file.Flush();
this.file.Close();
}

protected override void UpdateConfig() {}
}
}
2 changes: 1 addition & 1 deletion Lora-Bot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Program(String[] args) {
}

private void LoraDataUpdate(Object sender, Iot.Lora.Events.DeviceUpdateEvent e) {
Console.WriteLine("-> Lora [" + e.UpdateTime + "]: " + e.Parent.ToString());
Console.WriteLine("-> Lora " + e.Parent.ToString());
}

}
Expand Down
7 changes: 4 additions & 3 deletions Lora-Bot/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
// übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.0")]
[assembly: AssemblyFileVersion("1.2.0")]
[assembly: AssemblyVersion("1.3.0")]
[assembly: AssemblyFileVersion("1.3.0")]
/*
* 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.3.0 Scral now get its config from configfile, lora now want to get battery as [0-9].[0-9]{2} value
*/
Binary file modified Lora-Bot/bin/Release/Bot-Utils.dll
Binary file not shown.
Binary file modified Lora-Bot/bin/Release/ConnectorDataMqtt.dll
Binary file not shown.
Binary file modified Lora-Bot/bin/Release/Iot-Interfaces.dll
Binary file not shown.
Binary file modified Lora-Bot/bin/Release/Lora-Bot.exe
Binary file not shown.
Binary file modified Lora-Bot/bin/Release/Lora.dll
Binary file not shown.
Binary file modified Lora-Bot/bin/Release/M2Mqtt.dll
Binary file not shown.
Binary file modified Lora-Bot/bin/Release/Utils-IoT.dll
Binary file not shown.
Binary file modified Lora-Bot/bin/Release/Utils.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion Lora-Bot/bin/Release/config-example/mqtt.conf.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[modul]
[modul]
config=private

[settings]
Expand Down
15 changes: 11 additions & 4 deletions Lora-Bot/bin/Release/config-example/scral.conf.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
[modul]
[modul]
config=private

[settings]
type=mqtt
server=localhost
[general]
server=https://portal.monica-cloud.eu/

[update]
addr=scral/puetz/dexels/wearable/localization
method=put

[register]
addr=scral/puetz/dexels/wearable
method=post
4 changes: 2 additions & 2 deletions Lora-Bot/bin/Release/config-example/settings.conf.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[logging]
[logging]
path=/var/log/lorabot.log

[lora]
frequency=868100000
spreadingfactor=8
signalbandwith=125000
codingrate=6
codingrate=6
4 changes: 2 additions & 2 deletions Lora-Bot/bin/Release/config-example/txtout.conf.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[general]
path=/home/pi/coords.txt
[general]
path=/home/pi/coords.txt
Binary file modified Lora-Bot/bin/Release/litjson.dll
Binary file not shown.
5 changes: 0 additions & 5 deletions Lora-Bot/bin/Release/lora.sh

This file was deleted.

2 changes: 1 addition & 1 deletion Lora-Bot/config-example/mqtt.conf.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[modul]
[modul]
config=private

[settings]
Expand Down
13 changes: 12 additions & 1 deletion Lora-Bot/config-example/scral.conf.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
[modul]
[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
4 changes: 2 additions & 2 deletions Lora-Bot/config-example/settings.conf.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[logging]
[logging]
path=/var/log/lorabot.log

[lora]
frequency=868100000
spreadingfactor=8
signalbandwith=125000
codingrate=6
codingrate=6
4 changes: 2 additions & 2 deletions Lora-Bot/config-example/txtout.conf.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[general]
path=/home/pi/coords.txt
[general]
path=/home/pi/coords.txt
5 changes: 0 additions & 5 deletions Lora-Bot/lora.sh

This file was deleted.

0 comments on commit 64cbe25

Please sign in to comment.