From d0a4430d0e2c427b903730f9a5f4d88ba3218c1f Mon Sep 17 00:00:00 2001 From: Philip Schell Date: Mon, 29 Jul 2019 14:42:42 +0200 Subject: [PATCH] add settings.json --- CHANGELOG | 2 +- Lora-Map/Model/Admin/AdminModel.cs | 178 ++++++++++++------------ Lora-Map/resources/admin/css/global.css | 11 ++ Lora-Map/resources/admin/index.html | 7 +- Lora-Map/resources/admin/js/menu.js | 133 +++++++++++++----- 5 files changed, 205 insertions(+), 126 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index e511b2a..c41bcea 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -6,7 +6,7 @@ ### Changes * Refactoring of all JS * Make only one request per second instead of four per AJAX -* Refactoring Adminpannel +* Refactoring Adminpannel and add settings.json ## 1.2.8 ### New Features diff --git a/Lora-Map/Model/Admin/AdminModel.cs b/Lora-Map/Model/Admin/AdminModel.cs index 81e1f03..3546f7a 100644 --- a/Lora-Map/Model/Admin/AdminModel.cs +++ b/Lora-Map/Model/Admin/AdminModel.cs @@ -1,37 +1,37 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Net; -using System.Text; -using BlubbFish.Utils; -using BlubbFish.Utils.IoT.Bots; -using LitJson; - -namespace Fraunhofer.Fit.IoT.LoraMap.Model.Admin { - class AdminModel { - public delegate void AdminEvent(Object sender, EventArgs e); - public event AdminEvent NamesUpdate; - public event AdminEvent GeoUpdate; - public event AdminEvent SettingsUpdate; - - private readonly Dictionary session = new Dictionary(); - private readonly Dictionary settings; - - public AdminModel(Dictionary settings) { - this.settings = settings; - if(!settings.ContainsKey("admin_user") || !settings.ContainsKey("admin_pass")) { - Helper.WriteError("Kann die Einstellungen [webserver] admin_user und admin_pass nicht laden!"); - throw new FileNotFoundException("Kann die Einstellungen [webserver] admin_user und admin_pass nicht laden!"); - } - } - +using System; +using System.Collections.Generic; +using System.IO; +using System.Net; +using System.Text; +using BlubbFish.Utils; +using BlubbFish.Utils.IoT.Bots; +using LitJson; + +namespace Fraunhofer.Fit.IoT.LoraMap.Model.Admin { + class AdminModel { + public delegate void AdminEvent(Object sender, EventArgs e); + public event AdminEvent NamesUpdate; + public event AdminEvent GeoUpdate; + public event AdminEvent SettingsUpdate; + + private readonly Dictionary session = new Dictionary(); + private readonly Dictionary settings; + + public AdminModel(Dictionary settings) { + this.settings = settings; + if(!settings.ContainsKey("admin_user") || !settings.ContainsKey("admin_pass")) { + Helper.WriteError("Kann die Einstellungen [webserver] admin_user und admin_pass nicht laden!"); + throw new FileNotFoundException("Kann die Einstellungen [webserver] admin_user und admin_pass nicht laden!"); + } + } + public Boolean ParseReuqest(HttpListenerContext cont) => cont.Request.Url.PathAndQuery == "/admin/login" ? this.Login(cont) : !this.CheckAuth(cont) ? false : cont.Request.Url.PathAndQuery.StartsWith("/admin/get_json_") ? this.GetJsonFiles(cont) : - cont.Request.Url.PathAndQuery.StartsWith("/admin/set_json_") ? this.SetJsonFiles(cont) : - Webserver.SendFileResponse(cont); - + cont.Request.Url.PathAndQuery.StartsWith("/admin/set_json_") ? this.SetJsonFiles(cont) : + Webserver.SendFileResponse(cont); + private Boolean SetJsonFiles(HttpListenerContext cont) => cont.Request.Url.PathAndQuery == "/admin/set_json_names" ? this.SetJsonFile(cont, "names.json", this.NamesUpdate) : cont.Request.Url.PathAndQuery == "/admin/set_json_geo" ? this.SetJsonFile(cont, "geo.json", this.GeoUpdate) : @@ -59,7 +59,7 @@ namespace Fraunhofer.Fit.IoT.LoraMap.Model.Admin { private Boolean GetJsonFiles(HttpListenerContext cont) => cont.Request.Url.PathAndQuery == "/admin/get_json_names" ? this.GetJsonFile(cont, "names.json") : cont.Request.Url.PathAndQuery == "/admin/get_json_geo" ? this.GetJsonFile(cont, "geo.json") : - cont.Request.Url.PathAndQuery == "/admin/get_json_settings" ? this.GetJsonFile(cont, "settings.json"); + cont.Request.Url.PathAndQuery == "/admin/get_json_settings" ? this.GetJsonFile(cont, "settings.json") : false; private Boolean GetJsonFile(HttpListenerContext cont, String filename) { @@ -71,61 +71,61 @@ namespace Fraunhofer.Fit.IoT.LoraMap.Model.Admin { return true; } - private Boolean Login(HttpListenerContext cont) { - Dictionary POST = Webserver.GetPostParams(cont.Request); - if(POST.ContainsKey("user") && POST["user"] == this.settings["admin_user"] && POST.ContainsKey("pass") && POST["pass"] == this.settings["admin_pass"]) { - Int64 sessionid = 0; - while(true) { - sessionid = AdminSession.GetRandomSessionid(); - if(!this.session.ContainsKey(sessionid)) { - break; - } - } - if(cont.Request.Cookies["loramapsession"] != null) { - if(Int64.TryParse(cont.Request.Cookies["loramapsession"].Value, out Int64 cookiesessionid)) { - if(this.session.ContainsKey(cookiesessionid)) { - if(!this.session[sessionid].IsLoggedin) { - sessionid = cookiesessionid; - } - } - } - } - if(!this.session.ContainsKey(sessionid)) { - this.session.Add(sessionid, new AdminSession()); - } - this.session[sessionid].IsLoggedin = true; - cont.Response.AppendCookie(new Cookie("loramapsession", sessionid.ToString()) { - Expires = DateTime.Now.AddYears(1) - }); - cont.Response.AddHeader("Location", "/admin"); - cont.Response.StatusCode = 307; - Console.WriteLine("200 - Login OK! " + cont.Request.Url.PathAndQuery); - return true; - } - cont.Response.AddHeader("Location", "/admin/login.html"); - cont.Response.StatusCode = 307; - Helper.WriteError("307 - Login WRONG! " + cont.Request.Url.PathAndQuery); - return false; - } - - private Boolean CheckAuth(HttpListenerContext cont) { - #if DEBUG - return true; - #endif - if(cont.Request.Url.PathAndQuery.StartsWith("/admin/login.html")) { - return true; - } else { - if(cont.Request.Cookies["loramapsession"] != null) { - if(Int64.TryParse(cont.Request.Cookies["loramapsession"].Value, out Int64 sessionid)) { - if(this.session.ContainsKey(sessionid)) { - return this.session[sessionid].IsLoggedin; - } - } - } - cont.Response.StatusCode = 403; - Helper.WriteError("403 - " + cont.Request.Url.PathAndQuery); - } - return false; - } - } -} + private Boolean Login(HttpListenerContext cont) { + Dictionary POST = Webserver.GetPostParams(cont.Request); + if(POST.ContainsKey("user") && POST["user"] == this.settings["admin_user"] && POST.ContainsKey("pass") && POST["pass"] == this.settings["admin_pass"]) { + Int64 sessionid = 0; + while(true) { + sessionid = AdminSession.GetRandomSessionid(); + if(!this.session.ContainsKey(sessionid)) { + break; + } + } + if(cont.Request.Cookies["loramapsession"] != null) { + if(Int64.TryParse(cont.Request.Cookies["loramapsession"].Value, out Int64 cookiesessionid)) { + if(this.session.ContainsKey(cookiesessionid)) { + if(!this.session[sessionid].IsLoggedin) { + sessionid = cookiesessionid; + } + } + } + } + if(!this.session.ContainsKey(sessionid)) { + this.session.Add(sessionid, new AdminSession()); + } + this.session[sessionid].IsLoggedin = true; + cont.Response.AppendCookie(new Cookie("loramapsession", sessionid.ToString()) { + Expires = DateTime.Now.AddYears(1) + }); + cont.Response.AddHeader("Location", "/admin"); + cont.Response.StatusCode = 307; + Console.WriteLine("200 - Login OK! " + cont.Request.Url.PathAndQuery); + return true; + } + cont.Response.AddHeader("Location", "/admin/login.html"); + cont.Response.StatusCode = 307; + Helper.WriteError("307 - Login WRONG! " + cont.Request.Url.PathAndQuery); + return false; + } + + private Boolean CheckAuth(HttpListenerContext cont) { + #if DEBUG + return true; + #endif + if(cont.Request.Url.PathAndQuery.StartsWith("/admin/login.html")) { + return true; + } else { + if(cont.Request.Cookies["loramapsession"] != null) { + if(Int64.TryParse(cont.Request.Cookies["loramapsession"].Value, out Int64 sessionid)) { + if(this.session.ContainsKey(sessionid)) { + return this.session[sessionid].IsLoggedin; + } + } + } + cont.Response.StatusCode = 403; + Helper.WriteError("403 - " + cont.Request.Url.PathAndQuery); + } + return false; + } + } +} diff --git a/Lora-Map/resources/admin/css/global.css b/Lora-Map/resources/admin/css/global.css index 2c0a105..404d8cb 100644 --- a/Lora-Map/resources/admin/css/global.css +++ b/Lora-Map/resources/admin/css/global.css @@ -113,4 +113,15 @@ #content #eximport .names textarea { height: 120px; width: 90%; +} + +#content #settingseditor .title { + margin-bottom: 20px; + font-weight: bold; +} + +#content #settingseditor .startloc, +#content #settingseditor .wetterwarnings, +#content #settingseditor .savesettings { + margin-left: 15px; } \ No newline at end of file diff --git a/Lora-Map/resources/admin/index.html b/Lora-Map/resources/admin/index.html index 5b876b5..1632311 100644 --- a/Lora-Map/resources/admin/index.html +++ b/Lora-Map/resources/admin/index.html @@ -11,9 +11,10 @@
Wilkommen im Adminbereich
diff --git a/Lora-Map/resources/admin/js/menu.js b/Lora-Map/resources/admin/js/menu.js index c94175d..bceff51 100644 --- a/Lora-Map/resources/admin/js/menu.js +++ b/Lora-Map/resources/admin/js/menu.js @@ -1,35 +1,55 @@ -function menu_names() { - var ajaxnames = new XMLHttpRequest(); - ajaxnames.onreadystatechange = function() { - if(ajaxnames.readyState === 4 && ajaxnames.status === 200) { - NamesEditor.ParseJson(ajaxnames.responseText); - } - }; - ajaxnames.open("GET", "/admin/get_json_names", true); - ajaxnames.send(); -} - -function menu_overlay() { - -} - -function menu_eximport() { - var ajaxnames = new XMLHttpRequest(); - ajaxnames.onreadystatechange = function () { - if (ajaxnames.readyState === 4 && ajaxnames.status === 200) { - var ajaxgeo = new XMLHttpRequest(); - ajaxgeo.onreadystatechange = function () { - if (ajaxgeo.readyState === 4 && ajaxgeo.status === 200) { - ExImport.ParseJson(ajaxnames.responseText, ajaxgeo.responseText); - } - }; - ajaxgeo.open("GET", "/admin/get_json_geo", true); - ajaxgeo.send(); - } - }; - ajaxnames.open("GET", "/admin/get_json_names", true); - ajaxnames.send(); -} +var AdminMenu = { + Names: function () { + var ajaxnames = new XMLHttpRequest(); + ajaxnames.onreadystatechange = function () { + if (ajaxnames.readyState === 4 && ajaxnames.status === 200) { + NamesEditor.ParseJson(ajaxnames.responseText); + } + }; + ajaxnames.open("GET", "/admin/get_json_names", true); + ajaxnames.send(); + return false; + }, + Overlay: function () { + return false; + }, + Settings: function () { + var ajaxsettings = new XMLHttpRequest(); + ajaxsettings.onreadystatechange = function () { + if (ajaxsettings.readyState === 4 && ajaxsettings.status === 200) { + Settings.ParseJson(JSON.parse(ajaxsettings.responseText)); + } + }; + ajaxsettings.open("GET", "/admin/get_json_settings", true); + ajaxsettings.send(); + return false; + }, + ExImport: function () { + var ajaxnames = new XMLHttpRequest(); + ajaxnames.onreadystatechange = function () { + if (ajaxnames.readyState === 4 && ajaxnames.status === 200) { + var ajaxgeo = new XMLHttpRequest(); + ajaxgeo.onreadystatechange = function () { + if (ajaxgeo.readyState === 4 && ajaxgeo.status === 200) { + var ajaxsettings = new XMLHttpRequest(); + ajaxsettings.onreadystatechange = function () { + if (ajaxsettings.readyState === 4 && ajaxsettings.status === 200) { + ExImport.ParseJson(ajaxnames.responseText, ajaxgeo.responseText, ajaxsettings.responseText); + } + }; + ajaxsettings.open("GET", "/admin/get_json_settings", true); + ajaxsettings.send(); + } + }; + ajaxgeo.open("GET", "/admin/get_json_geo", true); + ajaxgeo.send(); + } + }; + ajaxnames.open("GET", "/admin/get_json_names", true); + ajaxnames.send(); + return false; + } +}; var NamesEditor = { iconeditorcounter: 0, @@ -320,15 +340,48 @@ var NamesEditor = { } }; +var Settings = { + ParseJson: function (jsonsettings) { + var html = "
Einstellungen
"; + html += "
Startlocation: Lat, Lon
"; + html += "
CellId's für DWD-Wetterwarnungen: (Trennen durch \";\", cap_warncellids_csv)
"; + html += "
"; + document.getElementById("content").innerHTML = html + "
"; + }, + Save: function () { + var ret = {}; + ret.StartPos = {}; + 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(";"); + + var savesettings = new XMLHttpRequest(); + savesettings.onreadystatechange = function () { + if (savesettings.readyState === 4) { + if (savesettings.status === 200) { + alert("Änderungen gespeichert!"); + } else if (savesettings.status === 501) { + alert("Ein Fehler ist aufgetreten (invalid JSON)!"); + } + } + }; + savesettings.open("POST", "/admin/set_json_settings", true); + savesettings.send(JSON.stringify(ret)); + } +}; + var ExImport = { - ParseJson: function (jsonnames, jsongeo) { + ParseJson: function (jsonnames, jsongeo, jsonsettings) { html = "
Ex- und Import der Einstellungen
"; html += "
names.json (Namen und Icons)
"; html += "
geo.json (Layer on the MAP) Kml Konverter
"; + html += "
settings.json (Settings of the Map)
"; + html += "
"; document.getElementById("content").innerHTML = html; document.getElementById("ex_names").value = jsonnames; document.getElementById("ex_geo").value = jsongeo; + document.getElementById("ex_settings").value = jsonsettings; }, SaveNames: function () { var savenames = new XMLHttpRequest(); @@ -357,5 +410,19 @@ var ExImport = { }; savegeo.open("POST", "/admin/set_json_geo", true); savegeo.send(document.getElementById("ex_geo").value); + }, + SaveSettings: function () { + var savesettings = new XMLHttpRequest(); + savesettings.onreadystatechange = function () { + if (savesettings.readyState === 4) { + if (savesettings.status === 200) { + alert("Änderungen an settings.json gespeichert!"); + } else if (savesettings.status === 501) { + alert("Ein Fehler ist aufgetreten (invalid JSON)!"); + } + } + }; + savesettings.open("POST", "/admin/set_json_settings", true); + savesettings.send(document.getElementById("ex_settings").value); } }; \ No newline at end of file