add settings.json
This commit is contained in:
parent
82d51c5dc1
commit
d0a4430d0e
@ -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
|
||||
|
@ -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<Int64, AdminSession> session = new Dictionary<Int64, AdminSession>();
|
||||
private readonly Dictionary<String, String> settings;
|
||||
|
||||
public AdminModel(Dictionary<String, String> 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<Int64, AdminSession> session = new Dictionary<Int64, AdminSession>();
|
||||
private readonly Dictionary<String, String> settings;
|
||||
|
||||
public AdminModel(Dictionary<String, String> 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<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"]) {
|
||||
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<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"]) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
@ -11,9 +11,10 @@
|
||||
<div id="header">Adminpannel Lora-Map</div>
|
||||
<div id="menu">
|
||||
<ul>
|
||||
<li><a onclick="menu_names(); return false;" href="#">Namen und Icons</a></li>
|
||||
<li><a onclick="menu_overlay(); return false;" href="#">Karteneditor</a></li>
|
||||
<li><a onclick="menu_eximport(); return false;" href="#">Ex- und Import</a></li>
|
||||
<li><a onclick="return AdminMenu.Names();" href="#">Namen und Icons</a></li>
|
||||
<!-- <li><a onclick="return AdminMenu.Overlay();" href="#">Karteneditor</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>
|
||||
</div>
|
||||
<div id="content">Wilkommen im Adminbereich</div>
|
||||
|
@ -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 = "<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 = {
|
||||
ParseJson: function (jsonnames, jsongeo) {
|
||||
ParseJson: function (jsonnames, jsongeo, jsonsettings) {
|
||||
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'>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>";
|
||||
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);
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user