2018-08-12 01:10:30 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Net;
|
2021-08-29 00:49:00 +02:00
|
|
|
|
using System.Text;
|
2021-04-10 20:32:24 +02:00
|
|
|
|
|
2018-12-30 20:50:20 +01:00
|
|
|
|
using BlubbFish.Utils;
|
2018-08-12 01:10:30 +02:00
|
|
|
|
using BlubbFish.Utils.IoT.Bots;
|
|
|
|
|
using BlubbFish.Utils.IoT.Connector;
|
|
|
|
|
using BlubbFish.Utils.IoT.Events;
|
2021-04-10 20:32:24 +02:00
|
|
|
|
|
2019-03-08 08:50:05 +01:00
|
|
|
|
using Fraunhofer.Fit.IoT.LoraMap.Model;
|
2019-04-01 18:15:50 +02:00
|
|
|
|
using Fraunhofer.Fit.IoT.LoraMap.Model.Admin;
|
2021-04-10 20:32:24 +02:00
|
|
|
|
using Fraunhofer.Fit.IoT.LoraMap.Model.Camera;
|
|
|
|
|
using Fraunhofer.Fit.IoT.LoraMap.Model.Position;
|
|
|
|
|
using Fraunhofer.Fit.IoT.LoraMap.Model.Sensor;
|
|
|
|
|
using Fraunhofer.Fit.IoT.LoraMap.Model.Svg;
|
|
|
|
|
|
2018-08-12 01:10:30 +02:00
|
|
|
|
using LitJson;
|
|
|
|
|
|
2019-03-08 08:50:05 +01:00
|
|
|
|
namespace Fraunhofer.Fit.IoT.LoraMap {
|
2021-08-29 00:49:00 +02:00
|
|
|
|
class Server : AWebserverDataBackend {
|
2021-04-10 20:32:24 +02:00
|
|
|
|
private readonly SortedDictionary<String, Object> jsonapi = new SortedDictionary<String, Object>() {
|
|
|
|
|
{ "camera", CameraModel.Instance },
|
|
|
|
|
{ "position", PositionModel.Instance },
|
|
|
|
|
{ "sensor", SensorModel.Instance },
|
|
|
|
|
{ "settings", Settings.Instance.External },
|
|
|
|
|
};
|
2019-04-15 21:19:29 +02:00
|
|
|
|
private readonly AdminModel admin;
|
2019-03-10 13:06:15 +01:00
|
|
|
|
|
2021-08-29 00:49:00 +02:00
|
|
|
|
public Server(ADataBackend backend, Dictionary<String, String> settings) : base(backend, settings) {
|
2019-04-15 21:19:29 +02:00
|
|
|
|
this.logger.SetPath(settings["loggingpath"]);
|
|
|
|
|
this.admin = new AdminModel(settings);
|
2021-04-10 20:32:24 +02:00
|
|
|
|
this.admin.SettingsUpdate += Settings.Instance.ReloadSettings;
|
|
|
|
|
this.admin.GeoUpdate += Settings.Instance.ReloadGeo;
|
|
|
|
|
this.admin.NamesUpdate += PositionModel.Instance.ReloadNames;
|
2021-08-29 00:49:00 +02:00
|
|
|
|
this.StartDataBackend();
|
2019-04-14 17:25:37 +02:00
|
|
|
|
this.StartListen();
|
2019-08-02 19:04:51 +02:00
|
|
|
|
this.WaitForShutdown();
|
|
|
|
|
this.Dispose();
|
2019-04-14 17:25:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-09 20:41:51 +02:00
|
|
|
|
protected override void Backend_MessageIncomming(Object sender, BackendEvent mqtt) {
|
2018-08-12 01:10:30 +02:00
|
|
|
|
try {
|
2019-07-09 20:41:51 +02:00
|
|
|
|
JsonData d = JsonMapper.ToObject(mqtt.Message);
|
2021-04-10 20:32:24 +02:00
|
|
|
|
PositionModel.Instance.ParseMqttJson(d, (String)mqtt.From);
|
|
|
|
|
CameraModel.Instance.ParseMqttJson(d, (String)mqtt.From);
|
|
|
|
|
SensorModel.Instance.ParseMqttJson(d, (String)mqtt.From);
|
2019-07-09 20:41:51 +02:00
|
|
|
|
} catch(Exception e) {
|
|
|
|
|
Helper.WriteError("Backend_MessageIncomming(): "+e.Message + "\n\n" + e.StackTrace);
|
2019-03-07 14:58:41 +01:00
|
|
|
|
}
|
2018-08-12 01:10:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-01 18:15:50 +02:00
|
|
|
|
protected override Boolean SendWebserverResponse(HttpListenerContext cont) {
|
2019-03-27 15:45:11 +01:00
|
|
|
|
try {
|
2021-04-10 20:32:24 +02:00
|
|
|
|
if(cont.Request.Url.AbsolutePath.StartsWith("/api/json/")) {
|
|
|
|
|
if(cont.Request.Url.AbsolutePath.Length > 10) {
|
|
|
|
|
String parts = cont.Request.Url.AbsolutePath[10..];
|
|
|
|
|
Dictionary<String, Object> ret = new Dictionary<String, Object>();
|
|
|
|
|
foreach(String part in parts.Split(",")) {
|
|
|
|
|
if(this.jsonapi.ContainsKey(part)) {
|
|
|
|
|
ret.Add(part, this.jsonapi[part]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-29 00:49:00 +02:00
|
|
|
|
return cont.SendStringResponse(JsonMapper.ToJson(ret));
|
2019-03-27 15:45:11 +01:00
|
|
|
|
}
|
2021-04-10 20:32:24 +02:00
|
|
|
|
} else if(cont.Request.Url.AbsolutePath.StartsWith("/api/time")) {
|
2021-08-29 00:49:00 +02:00
|
|
|
|
return cont.SendStringResponse(JsonMapper.ToJson(new Dictionary<String, DateTime>() { { "utc", DateTime.UtcNow } }));
|
2021-04-10 20:32:24 +02:00
|
|
|
|
} else if(cont.Request.Url.AbsolutePath.StartsWith("/api/svg/")) {
|
|
|
|
|
return SvgModel.Instance.ParseRequest(cont);
|
|
|
|
|
} else if(cont.Request.Url.PathAndQuery.StartsWith("/admin/")) {
|
2019-04-01 18:15:50 +02:00
|
|
|
|
return this.admin.ParseReuqest(cont);
|
2021-04-10 20:32:24 +02:00
|
|
|
|
} else if(cont.Request.Url.PathAndQuery.StartsWith("/maps/")) {
|
2021-08-29 00:49:00 +02:00
|
|
|
|
return cont.SendFileResponse("resources", false);
|
2019-07-25 11:14:04 +02:00
|
|
|
|
}
|
2019-04-21 15:29:00 +02:00
|
|
|
|
} catch(Exception e) {
|
2019-07-09 20:41:51 +02:00
|
|
|
|
Helper.WriteError("SendWebserverResponse(): 500 - " + e.Message + "\n\n" + e.StackTrace);
|
2019-03-27 15:45:11 +01:00
|
|
|
|
cont.Response.StatusCode = 500;
|
2019-04-01 18:15:50 +02:00
|
|
|
|
return false;
|
2019-03-13 00:18:21 +01:00
|
|
|
|
}
|
2021-08-29 00:49:00 +02:00
|
|
|
|
return cont.SendFileResponse();
|
2018-08-12 01:10:30 +02:00
|
|
|
|
}
|
2019-04-21 15:29:00 +02:00
|
|
|
|
|
2019-08-02 19:04:51 +02:00
|
|
|
|
public override void Dispose() {
|
2021-04-10 20:32:24 +02:00
|
|
|
|
SensorModel.Instance.Dispose();
|
2019-08-02 19:04:51 +02:00
|
|
|
|
base.Dispose();
|
|
|
|
|
}
|
2018-08-12 01:10:30 +02:00
|
|
|
|
}
|
|
|
|
|
}
|