add settings.json

This commit is contained in:
Philip Schell 2019-07-29 14:42:42 +02:00
parent 82d51c5dc1
commit d0a4430d0e
5 changed files with 205 additions and 126 deletions

View File

@ -6,7 +6,7 @@
### Changes ### Changes
* Refactoring of all JS * Refactoring of all JS
* Make only one request per second instead of four per AJAX * Make only one request per second instead of four per AJAX
* Refactoring Adminpannel * Refactoring Adminpannel and add settings.json
## 1.2.8 ## 1.2.8
### New Features ### New Features

View File

@ -1,37 +1,37 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Text; using System.Text;
using BlubbFish.Utils; using BlubbFish.Utils;
using BlubbFish.Utils.IoT.Bots; using BlubbFish.Utils.IoT.Bots;
using LitJson; using LitJson;
namespace Fraunhofer.Fit.IoT.LoraMap.Model.Admin { namespace Fraunhofer.Fit.IoT.LoraMap.Model.Admin {
class AdminModel { class AdminModel {
public delegate void AdminEvent(Object sender, EventArgs e); public delegate void AdminEvent(Object sender, EventArgs e);
public event AdminEvent NamesUpdate; public event AdminEvent NamesUpdate;
public event AdminEvent GeoUpdate; public event AdminEvent GeoUpdate;
public event AdminEvent SettingsUpdate; public event AdminEvent SettingsUpdate;
private readonly Dictionary<Int64, AdminSession> session = new Dictionary<Int64, AdminSession>(); private readonly Dictionary<Int64, AdminSession> session = new Dictionary<Int64, AdminSession>();
private readonly Dictionary<String, String> settings; private readonly Dictionary<String, String> settings;
public AdminModel(Dictionary<String, String> settings) { public AdminModel(Dictionary<String, String> settings) {
this.settings = settings; this.settings = settings;
if(!settings.ContainsKey("admin_user") || !settings.ContainsKey("admin_pass")) { if(!settings.ContainsKey("admin_user") || !settings.ContainsKey("admin_pass")) {
Helper.WriteError("Kann die Einstellungen [webserver] admin_user und admin_pass nicht laden!"); 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!"); throw new FileNotFoundException("Kann die Einstellungen [webserver] admin_user und admin_pass nicht laden!");
} }
} }
public Boolean ParseReuqest(HttpListenerContext cont) => public Boolean ParseReuqest(HttpListenerContext cont) =>
cont.Request.Url.PathAndQuery == "/admin/login" ? this.Login(cont) : cont.Request.Url.PathAndQuery == "/admin/login" ? this.Login(cont) :
!this.CheckAuth(cont) ? false : !this.CheckAuth(cont) ? false :
cont.Request.Url.PathAndQuery.StartsWith("/admin/get_json_") ? this.GetJsonFiles(cont) : cont.Request.Url.PathAndQuery.StartsWith("/admin/get_json_") ? this.GetJsonFiles(cont) :
cont.Request.Url.PathAndQuery.StartsWith("/admin/set_json_") ? this.SetJsonFiles(cont) : cont.Request.Url.PathAndQuery.StartsWith("/admin/set_json_") ? this.SetJsonFiles(cont) :
Webserver.SendFileResponse(cont); Webserver.SendFileResponse(cont);
private Boolean SetJsonFiles(HttpListenerContext 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_names" ? this.SetJsonFile(cont, "names.json", this.NamesUpdate) :
cont.Request.Url.PathAndQuery == "/admin/set_json_geo" ? this.SetJsonFile(cont, "geo.json", this.GeoUpdate) : 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) => 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_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_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; false;
private Boolean GetJsonFile(HttpListenerContext cont, String filename) { private Boolean GetJsonFile(HttpListenerContext cont, String filename) {
@ -71,61 +71,61 @@ namespace Fraunhofer.Fit.IoT.LoraMap.Model.Admin {
return true; return true;
} }
private Boolean Login(HttpListenerContext cont) { private Boolean Login(HttpListenerContext cont) {
Dictionary<String, String> POST = Webserver.GetPostParams(cont.Request); Dictionary<String, String> POST = Webserver.GetPostParams(cont.Request);
if(POST.ContainsKey("user") && POST["user"] == this.settings["admin_user"] && POST.ContainsKey("pass") && POST["pass"] == this.settings["admin_pass"]) { if(POST.ContainsKey("user") && POST["user"] == this.settings["admin_user"] && POST.ContainsKey("pass") && POST["pass"] == this.settings["admin_pass"]) {
Int64 sessionid = 0; Int64 sessionid = 0;
while(true) { while(true) {
sessionid = AdminSession.GetRandomSessionid(); sessionid = AdminSession.GetRandomSessionid();
if(!this.session.ContainsKey(sessionid)) { if(!this.session.ContainsKey(sessionid)) {
break; break;
} }
} }
if(cont.Request.Cookies["loramapsession"] != null) { if(cont.Request.Cookies["loramapsession"] != null) {
if(Int64.TryParse(cont.Request.Cookies["loramapsession"].Value, out Int64 cookiesessionid)) { if(Int64.TryParse(cont.Request.Cookies["loramapsession"].Value, out Int64 cookiesessionid)) {
if(this.session.ContainsKey(cookiesessionid)) { if(this.session.ContainsKey(cookiesessionid)) {
if(!this.session[sessionid].IsLoggedin) { if(!this.session[sessionid].IsLoggedin) {
sessionid = cookiesessionid; sessionid = cookiesessionid;
} }
} }
} }
} }
if(!this.session.ContainsKey(sessionid)) { if(!this.session.ContainsKey(sessionid)) {
this.session.Add(sessionid, new AdminSession()); this.session.Add(sessionid, new AdminSession());
} }
this.session[sessionid].IsLoggedin = true; this.session[sessionid].IsLoggedin = true;
cont.Response.AppendCookie(new Cookie("loramapsession", sessionid.ToString()) { cont.Response.AppendCookie(new Cookie("loramapsession", sessionid.ToString()) {
Expires = DateTime.Now.AddYears(1) Expires = DateTime.Now.AddYears(1)
}); });
cont.Response.AddHeader("Location", "/admin"); cont.Response.AddHeader("Location", "/admin");
cont.Response.StatusCode = 307; cont.Response.StatusCode = 307;
Console.WriteLine("200 - Login OK! " + cont.Request.Url.PathAndQuery); Console.WriteLine("200 - Login OK! " + cont.Request.Url.PathAndQuery);
return true; return true;
} }
cont.Response.AddHeader("Location", "/admin/login.html"); cont.Response.AddHeader("Location", "/admin/login.html");
cont.Response.StatusCode = 307; cont.Response.StatusCode = 307;
Helper.WriteError("307 - Login WRONG! " + cont.Request.Url.PathAndQuery); Helper.WriteError("307 - Login WRONG! " + cont.Request.Url.PathAndQuery);
return false; return false;
} }
private Boolean CheckAuth(HttpListenerContext cont) { private Boolean CheckAuth(HttpListenerContext cont) {
#if DEBUG #if DEBUG
return true; return true;
#endif #endif
if(cont.Request.Url.PathAndQuery.StartsWith("/admin/login.html")) { if(cont.Request.Url.PathAndQuery.StartsWith("/admin/login.html")) {
return true; return true;
} else { } else {
if(cont.Request.Cookies["loramapsession"] != null) { if(cont.Request.Cookies["loramapsession"] != null) {
if(Int64.TryParse(cont.Request.Cookies["loramapsession"].Value, out Int64 sessionid)) { if(Int64.TryParse(cont.Request.Cookies["loramapsession"].Value, out Int64 sessionid)) {
if(this.session.ContainsKey(sessionid)) { if(this.session.ContainsKey(sessionid)) {
return this.session[sessionid].IsLoggedin; return this.session[sessionid].IsLoggedin;
} }
} }
} }
cont.Response.StatusCode = 403; cont.Response.StatusCode = 403;
Helper.WriteError("403 - " + cont.Request.Url.PathAndQuery); Helper.WriteError("403 - " + cont.Request.Url.PathAndQuery);
} }
return false; return false;
} }
} }
} }

View File

@ -113,4 +113,15 @@
#content #eximport .names textarea { #content #eximport .names textarea {
height: 120px; height: 120px;
width: 90%; width: 90%;
}
#content #settingseditor .title {
margin-bottom: 20px;
font-weight: bold;
}
#content #settingseditor .startloc,
#content #settingseditor .wetterwarnings,
#content #settingseditor .savesettings {
margin-left: 15px;
} }

View File

@ -11,9 +11,10 @@
<div id="header">Adminpannel Lora-Map</div> <div id="header">Adminpannel Lora-Map</div>
<div id="menu"> <div id="menu">
<ul> <ul>
<li><a onclick="menu_names(); return false;" href="#">Namen und Icons</a></li> <li><a onclick="return AdminMenu.Names();" href="#">Namen und Icons</a></li>
<li><a onclick="menu_overlay(); return false;" href="#">Karteneditor</a></li> <!-- <li><a onclick="return AdminMenu.Overlay();" href="#">Karteneditor</a></li> -->
<li><a onclick="menu_eximport(); return false;" href="#">Ex- und Import</a></li> <li><a onclick="return AdminMenu.Settings();" href="#">Einstellungen</a></li>
<li><a onclick="return AdminMenu.ExImport();" href="#">Ex- und Import</a></li>
</ul> </ul>
</div> </div>
<div id="content">Wilkommen im Adminbereich</div> <div id="content">Wilkommen im Adminbereich</div>

View File

@ -1,35 +1,55 @@
function menu_names() { var AdminMenu = {
var ajaxnames = new XMLHttpRequest(); Names: function () {
ajaxnames.onreadystatechange = function() { var ajaxnames = new XMLHttpRequest();
if(ajaxnames.readyState === 4 && ajaxnames.status === 200) { ajaxnames.onreadystatechange = function () {
NamesEditor.ParseJson(ajaxnames.responseText); if (ajaxnames.readyState === 4 && ajaxnames.status === 200) {
} NamesEditor.ParseJson(ajaxnames.responseText);
}; }
ajaxnames.open("GET", "/admin/get_json_names", true); };
ajaxnames.send(); ajaxnames.open("GET", "/admin/get_json_names", true);
} ajaxnames.send();
return false;
function menu_overlay() { },
Overlay: function () {
} return false;
},
function menu_eximport() { Settings: function () {
var ajaxnames = new XMLHttpRequest(); var ajaxsettings = new XMLHttpRequest();
ajaxnames.onreadystatechange = function () { ajaxsettings.onreadystatechange = function () {
if (ajaxnames.readyState === 4 && ajaxnames.status === 200) { if (ajaxsettings.readyState === 4 && ajaxsettings.status === 200) {
var ajaxgeo = new XMLHttpRequest(); Settings.ParseJson(JSON.parse(ajaxsettings.responseText));
ajaxgeo.onreadystatechange = function () { }
if (ajaxgeo.readyState === 4 && ajaxgeo.status === 200) { };
ExImport.ParseJson(ajaxnames.responseText, ajaxgeo.responseText); ajaxsettings.open("GET", "/admin/get_json_settings", true);
} ajaxsettings.send();
}; return false;
ajaxgeo.open("GET", "/admin/get_json_geo", true); },
ajaxgeo.send(); ExImport: function () {
} var ajaxnames = new XMLHttpRequest();
}; ajaxnames.onreadystatechange = function () {
ajaxnames.open("GET", "/admin/get_json_names", true); if (ajaxnames.readyState === 4 && ajaxnames.status === 200) {
ajaxnames.send(); 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 = { var NamesEditor = {
iconeditorcounter: 0, iconeditorcounter: 0,
@ -320,15 +340,48 @@ var NamesEditor = {
} }
}; };
var Settings = {
ParseJson: function (jsonsettings) {
var html = "<div id='settingseditor'><div class='title'>Einstellungen</div>";
html += "<div class='startloc'>Startlocation: <input value='" + jsonsettings.StartPos.lat + "' id='startlat'> Lat, <input value='" + jsonsettings.StartPos.lon + "' id='startlon'> Lon</div>";
html += "<div class='wetterwarnings'>CellId's für DWD-Wetterwarnungen: <input value='" + jsonsettings.CellIds.join(";") + "' id='wetterids'> (Trennen durch \";\", <a href='https://www.dwd.de/DE/leistungen/opendata/help/warnungen/cap_warncellids_csv.html'>cap_warncellids_csv</a>)</div>";
html += "<div class='savesettings'><img src='../icons/general/save.png' onclick='Settings.Save()' class='pointer'></div>";
document.getElementById("content").innerHTML = html + "</div>";
},
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 = { var ExImport = {
ParseJson: function (jsonnames, jsongeo) { ParseJson: function (jsonnames, jsongeo, jsonsettings) {
html = "<div id='eximport'><div class='title'>Ex- und Import der Einstellungen</div>"; html = "<div id='eximport'><div class='title'>Ex- und Import der Einstellungen</div>";
html += "<div class='names'>names.json (Namen und Icons)<br/><textarea id='ex_names'></textarea> <img src='../icons/general/save.png' onclick='ExImport.SaveNames()' class='pointer'></div>"; html += "<div class='names'>names.json (Namen und Icons)<br/><textarea id='ex_names'></textarea> <img src='../icons/general/save.png' onclick='ExImport.SaveNames()' class='pointer'></div>";
html += "<div class='names'>geo.json (Layer on the MAP) <a href='https://mapbox.github.io/togeojson/'>Kml Konverter</a><br/><textarea id='ex_geo'></textarea> <img src='../icons/general/save.png' onclick='ExImport.SaveGeo()' class='pointer'></div>"; html += "<div class='names'>geo.json (Layer on the MAP) <a href='https://mapbox.github.io/togeojson/'>Kml Konverter</a><br/><textarea id='ex_geo'></textarea> <img src='../icons/general/save.png' onclick='ExImport.SaveGeo()' class='pointer'></div>";
html += "<div class='names'>settings.json (Settings of the Map)<br/><textarea id='ex_settings'></textarea> <img src='../icons/general/save.png' onclick='ExImport.SaveSettings()' class='pointer'></div>";
html += "</div>"; html += "</div>";
document.getElementById("content").innerHTML = html; document.getElementById("content").innerHTML = html;
document.getElementById("ex_names").value = jsonnames; document.getElementById("ex_names").value = jsonnames;
document.getElementById("ex_geo").value = jsongeo; document.getElementById("ex_geo").value = jsongeo;
document.getElementById("ex_settings").value = jsonsettings;
}, },
SaveNames: function () { SaveNames: function () {
var savenames = new XMLHttpRequest(); var savenames = new XMLHttpRequest();
@ -357,5 +410,19 @@ var ExImport = {
}; };
savegeo.open("POST", "/admin/set_json_geo", true); savegeo.open("POST", "/admin/set_json_geo", true);
savegeo.send(document.getElementById("ex_geo").value); 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);
} }
}; };