From c90a3113202285f2ff44c4da0d20667982e60b4d Mon Sep 17 00:00:00 2001 From: BlubbFish Date: Wed, 27 Mar 2019 19:36:45 +0100 Subject: [PATCH] add Panicclient and rewrite Botclient so that is less code add the possiblity to ask the Server for Panic button presses remove the version box and put that to the global version-info pannel --- Lora-Map/Lora-Map.csproj | 1 + Lora-Map/Model/Botclient.cs | 143 ++++++++++++------------------ Lora-Map/Model/Panicclient.cs | 52 +++++++++++ Lora-Map/Server.cs | 42 ++++----- Lora-Map/resources/css/global.css | 59 +++++++----- Lora-Map/resources/index.html | 20 ++++- Lora-Map/resources/js/menu.js | 2 +- 7 files changed, 183 insertions(+), 136 deletions(-) create mode 100644 Lora-Map/Model/Panicclient.cs diff --git a/Lora-Map/Lora-Map.csproj b/Lora-Map/Lora-Map.csproj index f0baca8..a34852e 100644 --- a/Lora-Map/Lora-Map.csproj +++ b/Lora-Map/Lora-Map.csproj @@ -46,6 +46,7 @@ + diff --git a/Lora-Map/Model/Botclient.cs b/Lora-Map/Model/Botclient.cs index c587ea0..5ea3609 100644 --- a/Lora-Map/Model/Botclient.cs +++ b/Lora-Map/Model/Botclient.cs @@ -1,63 +1,25 @@ using System; -using System.Collections; -using System.Collections.Generic; using System.Globalization; -using System.Reflection; -using BlubbFish.Utils; using LitJson; namespace Fraunhofer.Fit.IoT.LoraMap.Model { class Botclient { + public Double Rssi { get; private set; } + public Double Snr { get; private set; } + public DateTime Upatedtime { get; private set; } + public Double Latitude { get; private set; } + public Double Longitude { get; private set; } + public Double Hdop { get; private set; } + public Double Battery { get; private set; } + public Int32 Batterysimple { get; private set; } + public Boolean Fix { get; private set; } + public Double Height { get; private set; } + public String Name { get; private set; } + public String Icon { get; private set; } - public Botclient(String id, JsonData json, JsonData marker) { - if (json.ContainsKey("Rssi") && json["Rssi"].IsDouble) { - this.Rssi = (Double)json["Rssi"]; - } - if (json.ContainsKey("Snr") && json["Snr"].IsDouble) { - this.Snr = (Double)json["Snr"]; - } - if (json.ContainsKey("Receivedtime") && json["Receivedtime"].IsString) { - if (DateTime.TryParse((String)json["Receivedtime"], DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AssumeUniversal, out DateTime updatetime)) { - this.Upatedtime = updatetime; - } - } - if (json.ContainsKey("BatteryLevel") && json["BatteryLevel"].IsDouble) { - this.Battery = Math.Round((Double)json["BatteryLevel"], 2); - if(this.Battery < 3) { - this.Batterysimple = 0; - } else if(this.Battery < 3.2) { - this.Batterysimple = 1; - } else if(this.Battery < 3.5) { - this.Batterysimple = 2; - } else if(this.Battery < 3.8) { - this.Batterysimple = 3; - } else { - this.Batterysimple = 4; - } - } - if (json.ContainsKey("Gps") && json["Gps"].IsObject) { - if (json["Gps"].ContainsKey("Latitude") && json["Gps"]["Latitude"].IsDouble) { - this.Latitude = (Double)json["Gps"]["Latitude"]; - } - if (json["Gps"].ContainsKey("Longitude") && json["Gps"]["Longitude"].IsDouble) { - this.Longitude = (Double)json["Gps"]["Longitude"]; - } - if (json["Gps"].ContainsKey("Fix") && json["Gps"]["Fix"].IsBoolean) { - this.Fix = (Boolean)json["Gps"]["Fix"]; - } - if (json["Gps"].ContainsKey("LastLatitude") && json["Gps"]["LastLatitude"].IsDouble && !this.Fix) { - this.Latitude = (Double)json["Gps"]["LastLatitude"]; - } - if (json["Gps"].ContainsKey("LastLongitude") && json["Gps"]["LastLongitude"].IsDouble && !this.Fix) { - this.Longitude = (Double)json["Gps"]["LastLongitude"]; - } - if (json["Gps"].ContainsKey("Hdop") && json["Gps"]["Hdop"].IsDouble) { - this.Hdop = (Double)json["Gps"]["Hdop"]; - } - if (json["Gps"].ContainsKey("Height") && json["Gps"]["Height"].IsDouble) { - this.Height = (Double)json["Gps"]["Height"]; - } - } + public Botclient(JsonData json, JsonData marker) { + this.Update(json); + String id = GetId(json); if(marker.ContainsKey(id)) { if(marker[id].ContainsKey("name") && marker[id]["name"].IsString) { this.Name = (String)marker[id]["name"]; @@ -77,41 +39,50 @@ namespace Fraunhofer.Fit.IoT.LoraMap.Model { } } - public Double Rssi { get; private set; } - public Double Snr { get; private set; } - public DateTime Upatedtime { get; private set; } - public Double Latitude { get; private set; } - public Double Longitude { get; private set; } - public Double Hdop { get; private set; } - public Double Battery { get; private set; } - public Int32 Batterysimple { get; private set; } - public Boolean Fix { get; private set; } - public Double Height { get; private set; } - public String Name { get; private set; } - public String Icon { get; private set; } + public static Boolean CheckJson(JsonData json) => json.ContainsKey("Rssi") && json["Rssi"].IsDouble + && json.ContainsKey("Snr") && json["Snr"].IsDouble + && json.ContainsKey("Receivedtime") && json["Receivedtime"].IsString + && json.ContainsKey("BatteryLevel") && json["BatteryLevel"].IsDouble + && json.ContainsKey("Gps") && json["Gps"].IsObject + && json["Gps"].ContainsKey("Latitude") && json["Gps"]["Latitude"].IsDouble + && json["Gps"].ContainsKey("Longitude") && json["Gps"]["Longitude"].IsDouble + && json["Gps"].ContainsKey("LastLatitude") && json["Gps"]["LastLatitude"].IsDouble + && json["Gps"].ContainsKey("LastLongitude") && json["Gps"]["LastLongitude"].IsDouble + && json["Gps"].ContainsKey("Hdop") && json["Gps"]["Hdop"].IsDouble + && json["Gps"].ContainsKey("Fix") && json["Gps"]["Fix"].IsBoolean + && json["Gps"].ContainsKey("Height") && json["Gps"]["Height"].IsDouble + && json.ContainsKey("Name") && json["Name"].IsString; - public virtual Dictionary ToDictionary() { - Dictionary dictionary = new Dictionary(); - foreach (PropertyInfo item in this.GetType().GetProperties()) { - if (item.CanRead && item.GetValue(this) != null) { - if (item.GetValue(this).GetType().GetMethod("ToDictionary") != null) { - dictionary.Add(item.Name, item.GetValue(this).GetType().GetMethod("ToDictionary").Invoke(item.GetValue(this), null)); - } else if (item.GetValue(this).GetType().HasInterface(typeof(IDictionary))) { - Dictionary subdict = new Dictionary(); - foreach (DictionaryEntry subitem in (IDictionary)item.GetValue(this)) { - if (subitem.Value.GetType().GetMethod("ToDictionary") != null) { - subdict.Add(subitem.Key.ToString(), subitem.Value.GetType().GetMethod("ToDictionary").Invoke(subitem.Value, null)); - } - } - dictionary.Add(item.Name, subdict); - } else if (item.GetValue(this).GetType().BaseType == typeof(Enum)) { - dictionary.Add(item.Name, Helper.GetEnumDescription((Enum)item.GetValue(this))); - } else { - dictionary.Add(item.Name, item.GetValue(this)); - } - } + public static String GetId(JsonData json) => (String)json["Name"]; + public void Update(JsonData json) { + this.Rssi = (Double)json["Rssi"]; + this.Snr = (Double)json["Snr"]; + if(DateTime.TryParse((String)json["Receivedtime"], DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AssumeUniversal, out DateTime updatetime)) { + this.Upatedtime = updatetime; } - return dictionary; + this.Battery = Math.Round((Double)json["BatteryLevel"], 2); + if(this.Battery < 3) { + this.Batterysimple = 0; + } else if(this.Battery < 3.2) { + this.Batterysimple = 1; + } else if(this.Battery < 3.5) { + this.Batterysimple = 2; + } else if(this.Battery < 3.8) { + this.Batterysimple = 3; + } else { + this.Batterysimple = 4; + } + this.Latitude = (Double)json["Gps"]["Latitude"]; + this.Longitude = (Double)json["Gps"]["Longitude"]; + this.Fix = (Boolean)json["Gps"]["Fix"]; + if(!this.Fix) { + this.Latitude = (Double)json["Gps"]["LastLatitude"]; + this.Longitude = (Double)json["Gps"]["LastLongitude"]; + } + this.Hdop = (Double)json["Gps"]["Hdop"]; + this.Height = (Double)json["Gps"]["Height"]; } + + } } diff --git a/Lora-Map/Model/Panicclient.cs b/Lora-Map/Model/Panicclient.cs new file mode 100644 index 0000000..344e64c --- /dev/null +++ b/Lora-Map/Model/Panicclient.cs @@ -0,0 +1,52 @@ +using System; +using System.Globalization; +using LitJson; + +namespace Fraunhofer.Fit.IoT.LoraMap.Model { + class Panicclient { + public Double Rssi { get; private set; } + public Double Snr { get; private set; } + public DateTime Upatedtime { get; private set; } + public Double Latitude { get; private set; } + public Double Longitude { get; private set; } + public Double Hdop { get; private set; } + public Boolean Fix { get; private set; } + public Double Height { get; private set; } + public DateTime Triggerdtime { get; private set; } + + public Panicclient(JsonData json) => this.Update(json); + + public void Update(JsonData json) { + this.Triggerdtime = DateTime.Now; + this.Rssi = (Double)json["Rssi"]; + this.Snr = (Double)json["Snr"]; + if(DateTime.TryParse((String)json["Receivedtime"], DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AssumeUniversal, out DateTime updatetime)) { + this.Upatedtime = updatetime; + } + this.Latitude = (Double)json["Gps"]["Latitude"]; + this.Longitude = (Double)json["Gps"]["Longitude"]; + this.Fix = (Boolean)json["Gps"]["Fix"]; + if(!this.Fix) { + this.Latitude = (Double)json["Gps"]["LastLatitude"]; + this.Longitude = (Double)json["Gps"]["LastLongitude"]; + } + this.Hdop = (Double)json["Gps"]["Hdop"]; + this.Height = (Double)json["Gps"]["Height"]; + } + + public static String GetId(JsonData json) => (String)json["Name"]; + + public static Boolean CheckJson(JsonData json) => json.ContainsKey("Rssi") && json["Rssi"].IsDouble + && json.ContainsKey("Snr") && json["Snr"].IsDouble + && json.ContainsKey("Receivedtime") && json["Receivedtime"].IsString + && json.ContainsKey("Gps") && json["Gps"].IsObject + && json["Gps"].ContainsKey("Latitude") && json["Gps"]["Latitude"].IsDouble + && json["Gps"].ContainsKey("Longitude") && json["Gps"]["Longitude"].IsDouble + && json["Gps"].ContainsKey("LastLatitude") && json["Gps"]["LastLatitude"].IsDouble + && json["Gps"].ContainsKey("LastLongitude") && json["Gps"]["LastLongitude"].IsDouble + && json["Gps"].ContainsKey("Hdop") && json["Gps"]["Hdop"].IsDouble + && json["Gps"].ContainsKey("Fix") && json["Gps"]["Fix"].IsBoolean + && json["Gps"].ContainsKey("Height") && json["Gps"]["Height"].IsDouble + && json.ContainsKey("Name") && json["Name"].IsString; + } +} \ No newline at end of file diff --git a/Lora-Map/Server.cs b/Lora-Map/Server.cs index cbf1d31..bfda7b3 100644 --- a/Lora-Map/Server.cs +++ b/Lora-Map/Server.cs @@ -14,6 +14,7 @@ namespace Fraunhofer.Fit.IoT.LoraMap { class Server : Webserver { private readonly SortedDictionary locations = new SortedDictionary(); + private readonly SortedDictionary panics = new SortedDictionary(); private readonly JsonData marker; private readonly Dictionary markertable = new Dictionary(); @@ -22,27 +23,22 @@ namespace Fraunhofer.Fit.IoT.LoraMap { protected override void Backend_MessageIncomming(Object sender, BackendEvent e) { try { JsonData d = JsonMapper.ToObject(e.Message); - if (d.ContainsKey("Rssi") && d["Rssi"].IsDouble - && d.ContainsKey("Snr") && d["Snr"].IsDouble - && d.ContainsKey("Receivedtime") && d["Receivedtime"].IsString - && d.ContainsKey("BatteryLevel") && d["BatteryLevel"].IsDouble - && d.ContainsKey("Gps") && d["Gps"].IsObject - && d["Gps"].ContainsKey("Latitude") && d["Gps"]["Latitude"].IsDouble - && d["Gps"].ContainsKey("Longitude") && d["Gps"]["Longitude"].IsDouble - && d["Gps"].ContainsKey("LastLatitude") && d["Gps"]["LastLatitude"].IsDouble - && d["Gps"].ContainsKey("LastLongitude") && d["Gps"]["LastLongitude"].IsDouble - && d["Gps"].ContainsKey("Hdop") && d["Gps"]["Hdop"].IsDouble - && d["Gps"].ContainsKey("Fix") && d["Gps"]["Fix"].IsBoolean - && d["Gps"].ContainsKey("Height") && d["Gps"]["Height"].IsDouble - && d.ContainsKey("Name") && d["Name"].IsString) { - String name = (String)d["Name"]; - Botclient b = new Botclient(name, d, this.marker); + if (Botclient.CheckJson(d) && ((String)e.From).Contains("lora/data")) { + String name = Botclient.GetId(d); if (this.locations.ContainsKey(name)) { - this.locations[name] = b; + this.locations[name].Update(d); } else { - this.locations.Add(name, b); + this.locations.Add(name, new Botclient(d, this.marker)); } Console.WriteLine("Koordinate erhalten!"); + } else if(Panicclient.CheckJson(d) && ((String)e.From).Contains("lora/panic")) { + String name = Panicclient.GetId(d); + if(this.panics.ContainsKey(name)) { + this.panics[name].Update(d); + } else { + this.panics.Add(name, new Panicclient(d)); + } + Console.WriteLine("PANIC erhalten!"); } } catch (Exception ex) { Helper.WriteError(ex.Message); @@ -52,14 +48,12 @@ namespace Fraunhofer.Fit.IoT.LoraMap { protected override void SendResponse(HttpListenerContext cont) { try { if (cont.Request.Url.PathAndQuery.StartsWith("/loc")) { - Dictionary ret = new Dictionary(); - Byte[] buf = Encoding.UTF8.GetBytes(JsonMapper.ToJson(this.locations)); - cont.Response.ContentLength64 = buf.Length; - cont.Response.OutputStream.Write(buf, 0, buf.Length); - Console.WriteLine("200 - " + cont.Request.Url.PathAndQuery); + this.SendJsonResponse(this.locations, cont); return; - } - if (cont.Request.Url.PathAndQuery.StartsWith("/icons/marker/Marker.svg") && cont.Request.Url.PathAndQuery.Contains("?")) { + } else if(cont.Request.Url.PathAndQuery.StartsWith("/panic")) { + this.SendJsonResponse(this.panics, cont); + return; + } else if (cont.Request.Url.PathAndQuery.StartsWith("/icons/marker/Marker.svg") && cont.Request.Url.PathAndQuery.Contains("?")) { String hash = cont.Request.Url.PathAndQuery.Substring(cont.Request.Url.PathAndQuery.IndexOf('?') + 1); if (!this.markertable.ContainsKey(hash)) { this.markertable.Add(hash, new Marker(hash)); diff --git a/Lora-Map/resources/css/global.css b/Lora-Map/resources/css/global.css index 2698765..958e741 100644 --- a/Lora-Map/resources/css/global.css +++ b/Lora-Map/resources/css/global.css @@ -15,17 +15,23 @@ object { pointer-events: none; } +.leaflet-touch .leaflet-bar a { + width: 38px; +} + #menucollumn { width: 32px; background-color: white; + background-clip: padding-box; position: absolute; top: 85px; - bottom: 35px; + bottom: 10px; left: 10px; z-index: 5000; border: #707070 2px solid; - border: rgba(0,0,0,0.4) 2px solid; - border-radius: 4px + border: rgba(0,0,0,0.2) 2px solid; + border-radius: 4px; + padding: 3px; } #menucollumn span { display: block; @@ -42,33 +48,18 @@ object { #menucollumn .info { background-image: url("icons/information.png"); } -/*
Icons made by Smashicons from www.flaticon.com is licensed by CC 3.0 BY
*/ -/*
Icons made by Freepik from www.flaticon.com is licensed by CC 3.0 BY
*/ -/**
Icons made by Freepik from www.flaticon.com is licensed by CC 3.0 BY
*/ - -#version { - position: absolute; - bottom: 0; - left: 0; - z-index: 50000; - background-color: white; - border-radius: 5px; - height: 20px; - width: 40px; - border: #707070 2px solid; - border: rgba(0,0,0,0.4) 2px solid; -} #pannels { position: absolute; top: 85px; - left: 47px; - bottom: 35px; + left: 55px; + bottom: 10px; width: 250px; z-index: 50000; background-color: white; + background-clip: padding-box; border: #707070 2px solid; - border: rgba(0,0,0,0.4) 2px solid; + border: rgba(0,0,0,0.2) 2px solid; border-radius: 4px; display: none; font-size: 11px; @@ -132,4 +123,28 @@ object { } #pannels #pannels_info .update { margin-bottom: 10px; +} + +#pannels #pannels_version { + padding: 5px; + display: none; +} +#pannels #pannels_version .versionstring { + font-weight: bold; +} +#pannels #pannels_version .versionstring #version { + font-weight: normal; + font-family: monospace; + display: inline; +} +#pannels #pannels_version .cicons { + font-weight: bold; +} +#pannels #pannels_version .cicons ul { + margin: 0; + padding: 0; + padding-left: 15px; +} +#pannels #pannels_version .cicons ul li { + font-weight: normal; } \ No newline at end of file diff --git a/Lora-Map/resources/index.html b/Lora-Map/resources/index.html index 8817893..034f227 100644 --- a/Lora-Map/resources/index.html +++ b/Lora-Map/resources/index.html @@ -12,13 +12,27 @@ -
vx.x.x
-
+
+ +
+
+
+ Version: +
vx.x.x
+
+
+ Icons: + +
+
diff --git a/Lora-Map/resources/js/menu.js b/Lora-Map/resources/js/menu.js index e886ce3..662b226 100644 --- a/Lora-Map/resources/js/menu.js +++ b/Lora-Map/resources/js/menu.js @@ -56,7 +56,7 @@ function updateStatus() { for (var id in serverLocation) { if (serverLocation.hasOwnProperty(id)) { var markeritem = serverLocation[id]; - if (typeof (overviewStatus[id]) == "undefined") { + if (typeof overviewStatus[id] === "undefined") { overviewStatus[id] = createOverviewElement(markeritem, id); document.getElementById("pannels_pos").appendChild(overviewStatus[id]); }