diff --git a/MonicaScral/MonicaScral.csproj b/MonicaScral/MonicaScral.csproj index 1027fd8..76c37fe 100644 --- a/MonicaScral/MonicaScral.csproj +++ b/MonicaScral/MonicaScral.csproj @@ -70,5 +70,18 @@ Utils + + + PreserveNewest + + + + + + + + + + \ No newline at end of file diff --git a/MonicaScral/Program.cs b/MonicaScral/Program.cs index 46af264..ad7b878 100644 --- a/MonicaScral/Program.cs +++ b/MonicaScral/Program.cs @@ -12,50 +12,50 @@ namespace Fraunhofer.Fit.IoT.MonicaScral { static void Main(String[] args) => new Program(args); public Program(String[] args) { - MqttListener m = new MqttListener(new Dictionary() { { "type", "mqtt" }, { "server", "10.100.0.20" }, { "topic", "lora/data/+;lora/panic/+" } }); - ScralPusher s = new ScralPusher(new Dictionary() { - { "server", "http://monappdwp3.monica-cloud.eu:8250" }, - { "register_addr", "/scral/v1.0/gps-tracker-gw/gps-tag" }, - { "register_method", "post" }, - { "update_addr", "/scral/v1.0/gps-tracker-gw/gps-tag/localization" }, - { "update_method", "put" }, - { "panic_addr", "/scral/v1.0/gps-tracker-gw/gps-tag/alert" }, - { "panic_method", "put" },}); + InIReader.SetSearchPath(new List() { "/etc/monicascral", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\monicascral" }); + if(!InIReader.ConfigExist("settings")) { + Helper.WriteError("No settings.ini found. Abord!"); + return; + } + InIReader settings = InIReader.GetInstance("settings"); + this.logger.SetPath(settings.GetValue("logging", "path")); + MqttListener m = new MqttListener(settings.GetSection("mqtt")); + ScralPusher s = new ScralPusher(settings.GetSection("scral")); m.Update += s.DataInput; this.WaitForShutdown(); m.Dispose(); s.Dispose(); } - protected void WaitForShutdown() { - if(Type.GetType("Mono.Runtime") != null) { - this.sig_thread = new Thread(delegate () { - Mono.Unix.UnixSignal[] signals = new Mono.Unix.UnixSignal[] { - new Mono.Unix.UnixSignal(Mono.Unix.Native.Signum.SIGTERM), - new Mono.Unix.UnixSignal(Mono.Unix.Native.Signum.SIGINT) - }; - Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Signalhandler Mono attached."); - while(true) { - Int32 i = Mono.Unix.UnixSignal.WaitAny(signals, -1); - Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Signalhandler Mono INT recieved " + i + "."); - this.RunningProcess = false; - break; - } - }); - this.sig_thread.Start(); - } else { - Console.CancelKeyPress += new ConsoleCancelEventHandler(this.SetupShutdown); - Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Signalhandler Windows attached."); - } - while(this.RunningProcess) { - Thread.Sleep(100); - } + protected void WaitForShutdown() { + if(Type.GetType("Mono.Runtime") != null) { + this.sig_thread = new Thread(delegate () { + Mono.Unix.UnixSignal[] signals = new Mono.Unix.UnixSignal[] { + new Mono.Unix.UnixSignal(Mono.Unix.Native.Signum.SIGTERM), + new Mono.Unix.UnixSignal(Mono.Unix.Native.Signum.SIGINT) + }; + Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Signalhandler Mono attached."); + while(true) { + Int32 i = Mono.Unix.UnixSignal.WaitAny(signals, -1); + Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Signalhandler Mono INT recieved " + i + "."); + this.RunningProcess = false; + break; + } + }); + this.sig_thread.Start(); + } else { + Console.CancelKeyPress += new ConsoleCancelEventHandler(this.SetupShutdown); + Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Signalhandler Windows attached."); + } + while(this.RunningProcess) { + Thread.Sleep(100); + } } - private void SetupShutdown(Object sender, ConsoleCancelEventArgs e) { - e.Cancel = true; - Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.SetupShutdown: Signalhandler Windows INT recieved."); - this.RunningProcess = false; + private void SetupShutdown(Object sender, ConsoleCancelEventArgs e) { + e.Cancel = true; + Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.SetupShutdown: Signalhandler Windows INT recieved."); + this.RunningProcess = false; } } } diff --git a/MonicaScral/Properties/AssemblyInfo.cs b/MonicaScral/Properties/AssemblyInfo.cs index 7f2220f..67cb3d0 100644 --- a/MonicaScral/Properties/AssemblyInfo.cs +++ b/MonicaScral/Properties/AssemblyInfo.cs @@ -1,18 +1,19 @@ using System.Reflection; -using System.Runtime.CompilerServices; +using System.Resources; using System.Runtime.InteropServices; // Allgemeine Informationen über eine Assembly werden über die folgenden // Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, // die einer Assembly zugeordnet sind. [assembly: AssemblyTitle("MonicaScral")] -[assembly: AssemblyDescription("")] +[assembly: AssemblyDescription("Pushes Loratracker data to Monica Scral endpoint")] [assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] +[assembly: AssemblyCompany("Fraunhofer FIT")] [assembly: AssemblyProduct("MonicaScral")] -[assembly: AssemblyCopyright("Copyright © 2019")] -[assembly: AssemblyTrademark("")] +[assembly: AssemblyCopyright("Copyright © 2019 - 17.04.2019")] +[assembly: AssemblyTrademark("Fraunhofer FIT, BlubbFish")] [assembly: AssemblyCulture("")] +[assembly: NeutralResourcesLanguage("de-DE")] // 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 @@ -32,5 +33,9 @@ using System.Runtime.InteropServices; // 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.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyVersion("1.0.0")] +[assembly: AssemblyFileVersion("1.0.0")] + +/* + * 1.0.0 First Version + */ diff --git a/MonicaScral/ScralPusher.cs b/MonicaScral/ScralPusher.cs index bb76cde..5bf56e3 100644 --- a/MonicaScral/ScralPusher.cs +++ b/MonicaScral/ScralPusher.cs @@ -117,21 +117,21 @@ namespace Fraunhofer.Fit.IoT.MonicaScral { 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["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 { + HttpWebRequest request = WebRequest.CreateHttp(this.config["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); + } + } using(HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { if(response.StatusCode == HttpStatusCode.Unauthorized) { Console.Error.WriteLine("Benutzer oder Passwort falsch!"); diff --git a/MonicaScral/config-example/settings.conf.example b/MonicaScral/config-example/settings.conf.example new file mode 100644 index 0000000..fbd0320 --- /dev/null +++ b/MonicaScral/config-example/settings.conf.example @@ -0,0 +1,16 @@ +[logging] +path=/var/log/monicascral.log + +[mqtt] +type=mqtt +server=127.0.01 +topic=lora/data/+;lora/panic/+ + +[scral] +server=http://example.org:8080 +register_addr=/gps-tag +register_method=post +update_addr=/gps-tag/localization +update_method=put +panic_addr=/gps-tag/alert +panic_method=put \ No newline at end of file diff --git a/MonicaScral/dpkg/control b/MonicaScral/dpkg/control new file mode 100644 index 0000000..26ebd04 --- /dev/null +++ b/MonicaScral/dpkg/control @@ -0,0 +1,9 @@ +Package: monicascral +Version: x.x-x +Section: base +Priority: optional +Architecture: any +Depends: mono-runtime (>= 5.18), libmono-posix4.0-cil (>= 5.18) +Maintainer: BlubbFish +Description: MonicaScral + Pushes Loratracker data to Monica Scral endpoint diff --git a/MonicaScral/dpkg/create-Builds.bat b/MonicaScral/dpkg/create-Builds.bat new file mode 100644 index 0000000..10b1e14 --- /dev/null +++ b/MonicaScral/dpkg/create-Builds.bat @@ -0,0 +1,2 @@ +bash.exe -c "./make-deb.sh amd64" +pause \ No newline at end of file diff --git a/MonicaScral/dpkg/make-deb.sh b/MonicaScral/dpkg/make-deb.sh new file mode 100644 index 0000000..345f493 --- /dev/null +++ b/MonicaScral/dpkg/make-deb.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +HOMEDIR=$HOME +ROOT="$HOMEDIR/deb" +OUTPUT="../bin/Release" + +EXEC="$ROOT/usr/local/bin/monicascral" +CONFIG="$ROOT/etc/monicascral" +SYSTEMD="$ROOT/lib/systemd/system" +LOGROTATE="$ROOT/etc/logrotate.d" + +DEBIAN="$ROOT/DEBIAN" +VMAJOR=$(grep -e "^\[assembly: AssemblyVersion(\"" ../Properties/AssemblyInfo.cs | cut -d'"' -f 2 | cut -d'.' -f 1) +VMINOR=$(grep -e "^\[assembly: AssemblyVersion(\"" ../Properties/AssemblyInfo.cs | cut -d'"' -f 2 | cut -d'.' -f 2) +VBUILD=$(grep -e "^\[assembly: AssemblyVersion(\"" ../Properties/AssemblyInfo.cs | cut -d'"' -f 2 | cut -d'.' -f 3) +ARCHT=$1 + +mkdir -p $EXEC +mkdir -p $CONFIG +mkdir -p $DEBIAN +mkdir -p $SYSTEMD +mkdir -p $LOGROTATE + +cp control $DEBIAN +cp preinst $DEBIAN +cp postinst $DEBIAN +cp prerm $DEBIAN +sed -i s/Version:\ x\.x-x/"Version: $VMAJOR.$VMINOR-$VBUILD"/ $DEBIAN/control +sed -i s/Architecture:\ any/"Architecture: $ARCHT"/ $DEBIAN/control +chmod 755 $DEBIAN -R + +cp monicascral.service $SYSTEMD +chmod 644 $SYSTEMD/monicascral.service + +cp $OUTPUT/*.exe $EXEC/ +find $OUTPUT -name \*.dll -exec cp {} $EXEC/ \; +chmod 644 $EXEC/* +chmod 755 $EXEC + +cp $OUTPUT/config-example/* $CONFIG +chmod 644 $CONFIG/* +chmod 755 $CONFIG + +cp monicascral-logrotate $LOGROTATE/monicascral +chmod 644 $LOGROTATE/* + +dpkg-deb --build $ROOT +mv $HOMEDIR/deb.deb ../../../Builds/"$ARCHT-monicascral_$VMAJOR.$VMINOR-$VBUILD.deb" +rm $HOMEDIR/deb -r \ No newline at end of file diff --git a/MonicaScral/dpkg/monicascral-logrotate b/MonicaScral/dpkg/monicascral-logrotate new file mode 100644 index 0000000..49d30ec --- /dev/null +++ b/MonicaScral/dpkg/monicascral-logrotate @@ -0,0 +1,10 @@ +/var/log/monicascral.log { + compress + copytruncate + daily + delaycompress + missingok + notifempty + rotate 4 + size=10M +} \ No newline at end of file diff --git a/MonicaScral/dpkg/monicascral.service b/MonicaScral/dpkg/monicascral.service new file mode 100644 index 0000000..8394972 --- /dev/null +++ b/MonicaScral/dpkg/monicascral.service @@ -0,0 +1,20 @@ +# If you modify this, please also make sure to edit init.sh + +[Unit] +Description=MonicaScral +After=network-online.target + +[Service] +User=root +Group=root +WorkingDirectory=/usr/local/bin/monicascral +ExecStart=/usr/bin/mono /usr/local/bin/monicascral/MonicaScral.exe +KillMode=control-group +TimeoutStopSec=5 +Restart=on-failure +StandardOutput=null +StandardError=syslog + +[Install] +WantedBy=multi-user.target +Alias=monicascral.service diff --git a/MonicaScral/dpkg/postinst b/MonicaScral/dpkg/postinst new file mode 100644 index 0000000..f58b1a6 --- /dev/null +++ b/MonicaScral/dpkg/postinst @@ -0,0 +1,9 @@ +#!/bin/bash + +systemctl enable monicascral +systemctl daemon-reload + +if [ -f /tmp/monicascral_service_runner ]; then + service monicascral start + rm /tmp/monicascral_service_runner +fi \ No newline at end of file diff --git a/MonicaScral/dpkg/preinst b/MonicaScral/dpkg/preinst new file mode 100644 index 0000000..05a7907 --- /dev/null +++ b/MonicaScral/dpkg/preinst @@ -0,0 +1,2 @@ +#!/bin/bash + diff --git a/MonicaScral/dpkg/prerm b/MonicaScral/dpkg/prerm new file mode 100644 index 0000000..6706749 --- /dev/null +++ b/MonicaScral/dpkg/prerm @@ -0,0 +1,7 @@ +#!/bin/bash + +if [[ $(systemctl is-active monicascral || true) == "active" ]] +then + touch /tmp/monicascral_service_runner + service monicascral stop +fi \ No newline at end of file