diff --git a/CHANGELOG b/CHANGELOG index c41bcea..ef0aee7 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,11 +2,13 @@ ## 1.2.9 ### New Features +* Add setting model to code +* #19 grid automatisch generieren ### Bugfixes ### Changes * Refactoring of all JS * Make only one request per second instead of four per AJAX -* Refactoring Adminpannel and add settings.json +* Refactoring adminpannel and add settings.json ## 1.2.8 ### New Features diff --git a/Lora-Map/Lora-Map.csproj b/Lora-Map/Lora-Map.csproj index 8f6e897..c086843 100644 --- a/Lora-Map/Lora-Map.csproj +++ b/Lora-Map/Lora-Map.csproj @@ -51,6 +51,7 @@ + diff --git a/Lora-Map/Model/Settings.cs b/Lora-Map/Model/Settings.cs new file mode 100644 index 0000000..742527b --- /dev/null +++ b/Lora-Map/Model/Settings.cs @@ -0,0 +1,121 @@ +using CoordinateSharp; +using LitJson; +using System; +using System.Collections.Generic; +using System.IO; + +namespace Fraunhofer.Fit.IoT.LoraMap.Model { + public class Settings { + private readonly List weatherCellIDs = new List(); + private Int32 gridradius; + + public Double Startloclat { get; private set; } + public Double Startloclon { get; private set; } + public Dictionary>>> Grid { get; private set; } + + public Settings() => this.ParseJson(); + + public void AdminModelUpdateSettings(Object sender, EventArgs e) => this.ParseJson(); + + private void ParseJson() { + JsonData json = JsonMapper.ToObject(File.ReadAllText("json/settings.json")); + if(json.ContainsKey("StartPos") && json["StartPos"].IsObject && + json["StartPos"].ContainsKey("lat") && json["StartPos"]["lat"].IsDouble && + json["StartPos"].ContainsKey("lon") && json["StartPos"]["lon"].IsDouble) { + this.Startloclat = (Double)json["StartPos"]["lat"]; + this.Startloclon = (Double)json["StartPos"]["lon"]; + } else { + this.Startloclat = 0; + this.Startloclon = 0; + } + this.weatherCellIDs.Clear(); + if (json.ContainsKey("CellIds") && json["CellIds"].IsArray && json["CellIds"].Count > 0) { + foreach (JsonData item in json["CellIds"]) { + if(Int32.TryParse(item.ToString(), out Int32 cellid)) { + this.weatherCellIDs.Add(cellid); + } + } + } + this.gridradius = json.ContainsKey("GridRadius") && json["GridRadius"].IsInt && this.Startloclat != 0 && this.Startloclon != 0 ? (Int32)json["GridRadius"] : 0; + this.GenerateGrid(); + } + + private void GenerateGrid() { + if(this.Startloclat == 0 || this.Startloclon == 0 || this.gridradius == 0) { + return; + } + MilitaryGridReferenceSystem start = new Coordinate(this.Startloclat, this.Startloclon).MGRS; + + Double left = start.Easting - this.gridradius - ((start.Easting - this.gridradius) % 100); + Double bottom = start.Northing - this.gridradius - ((start.Northing - this.gridradius) % 100); + Double right = start.Easting + this.gridradius + (100 - ((start.Easting + this.gridradius) % 100)); + Double top = start.Northing + this.gridradius + (100 - ((start.Northing + this.gridradius) % 100)); + this.Grid = new Dictionary>>> { + { "Major", new List>>() }, + { "Minor", new List>>() } + }; + for (Double i = left; i <= right; i = i + 50) { + Coordinate TopLeft = MilitaryGridReferenceSystem.MGRStoLatLong(new MilitaryGridReferenceSystem(start.LatZone, start.LongZone, start.Digraph, i, top)); + Coordinate BottomLeft = MilitaryGridReferenceSystem.MGRStoLatLong(new MilitaryGridReferenceSystem(start.LatZone, start.LongZone, start.Digraph, i, bottom)); + if(i%100 == 0) { + this.Grid["Major"].Add(new Dictionary> { + { "from", new List { + TopLeft.Latitude.DecimalDegree, + TopLeft.Longitude.DecimalDegree + } + }, + { "to", new List { + BottomLeft.Latitude.DecimalDegree, + BottomLeft.Longitude.DecimalDegree + } + } + }); + } else { + this.Grid["Minor"].Add(new Dictionary> { + { "from", new List { + TopLeft.Latitude.DecimalDegree, + TopLeft.Longitude.DecimalDegree + } + }, + { "to", new List { + BottomLeft.Latitude.DecimalDegree, + BottomLeft.Longitude.DecimalDegree + } + } + }); + } + } + for (Double i = bottom; i <= top; i = i + 50) { + Coordinate BottomLeft = MilitaryGridReferenceSystem.MGRStoLatLong(new MilitaryGridReferenceSystem(start.LatZone, start.LongZone, start.Digraph, left, i)); + Coordinate BottomRight = MilitaryGridReferenceSystem.MGRStoLatLong(new MilitaryGridReferenceSystem(start.LatZone, start.LongZone, start.Digraph, right, i)); + if (i % 100 == 0) { + this.Grid["Major"].Add(new Dictionary> { + { "from", new List { + BottomLeft.Latitude.DecimalDegree, + BottomLeft.Longitude.DecimalDegree + } + }, + { "to", new List { + BottomRight.Latitude.DecimalDegree, + BottomRight.Longitude.DecimalDegree + } + } + }); + } else { + this.Grid["Minor"].Add(new Dictionary> { + { "from", new List { + BottomLeft.Latitude.DecimalDegree, + BottomLeft.Longitude.DecimalDegree + } + }, + { "to", new List { + BottomRight.Latitude.DecimalDegree, + BottomRight.Longitude.DecimalDegree + } + } + }); + } + } + } + } +} diff --git a/Lora-Map/Server.cs b/Lora-Map/Server.cs index 3bff571..96d7361 100644 --- a/Lora-Map/Server.cs +++ b/Lora-Map/Server.cs @@ -18,6 +18,7 @@ namespace Fraunhofer.Fit.IoT.LoraMap { private readonly SortedDictionary cameras = new SortedDictionary(); private readonly SortedDictionary crowds = new SortedDictionary(); private JsonData marker; + private Settings settings; private readonly Dictionary markertable = new Dictionary(); private readonly AdminModel admin; private readonly Object lockData = new Object(); @@ -29,6 +30,8 @@ namespace Fraunhofer.Fit.IoT.LoraMap { this.admin = new AdminModel(settings); this.marker = JsonMapper.ToObject(File.ReadAllText("json/names.json")); this.admin.NamesUpdate += this.AdminModelUpdateNames; + this.settings = new Settings(); + this.admin.SettingsUpdate += this.settings.AdminModelUpdateSettings; this.StartListen(); } @@ -123,7 +126,8 @@ namespace Fraunhofer.Fit.IoT.LoraMap { } else if (cont.Request.Url.PathAndQuery.StartsWith("/getonce")) { return SendJsonResponse(new Dictionary() { { "getlayer", this.FindMapLayer(cont.Request) }, - { "getgeo", JsonMapper.ToObject(File.ReadAllText("json/geo.json")) } + { "getgeo", JsonMapper.ToObject(File.ReadAllText("json/geo.json")) }, + { "startup", this.settings } }, cont); } 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); diff --git a/Lora-Map/resources/admin/css/global.css b/Lora-Map/resources/admin/css/global.css index 404d8cb..2d30b04 100644 --- a/Lora-Map/resources/admin/css/global.css +++ b/Lora-Map/resources/admin/css/global.css @@ -122,6 +122,7 @@ #content #settingseditor .startloc, #content #settingseditor .wetterwarnings, -#content #settingseditor .savesettings { +#content #settingseditor .savesettings, +#content #settingseditor .gridradius { margin-left: 15px; } \ No newline at end of file diff --git a/Lora-Map/resources/admin/js/menu.js b/Lora-Map/resources/admin/js/menu.js index bceff51..9004aab 100644 --- a/Lora-Map/resources/admin/js/menu.js +++ b/Lora-Map/resources/admin/js/menu.js @@ -343,8 +343,9 @@ var NamesEditor = { var Settings = { ParseJson: function (jsonsettings) { var html = "
Einstellungen
"; - html += "
Startlocation: Lat, Lon
"; + html += "
Startpunkt: Lat, Lon
"; html += "
CellId's für DWD-Wetterwarnungen: (Trennen durch \";\", cap_warncellids_csv)
"; + html += "
Radius für das Grid um den Startpunkt: m
"; html += "
"; document.getElementById("content").innerHTML = html + "
"; }, @@ -354,6 +355,7 @@ var Settings = { ret.StartPos.lat = parseFloat(document.getElementById("startlat").value.replace(",", ".")); ret.StartPos.lon = parseFloat(document.getElementById("startlon").value.replace(",", ".")); ret.CellIds = document.getElementById("wetterids").value.split(";"); + ret.GridRadius = parseInt(document.getElementById("gridrad").value); var savesettings = new XMLHttpRequest(); savesettings.onreadystatechange = function () {