#24 Implement Backendstuff

This commit is contained in:
BlubbFish 2019-08-19 19:57:42 +02:00
parent f65b81b369
commit c4a23751c1
4 changed files with 214 additions and 8 deletions

View File

@ -0,0 +1,62 @@
using System;
using System.IO;
using System.Net;
using BlubbFish.Utils;
using LitJson;
namespace Fraunhofer.Fit.IoT.LoraMap.Lib {
public class WebRequests {
private static readonly Object getLock = new Object();
public JsonData GetJson(String url) {
String text = null;
for(Int32 i = 0; i < 3; i++) {
try {
text = this.GetString(url);
break;
} catch(Exception e) {
Helper.WriteError(e.Message);
if(i == 2) {
throw;
}
System.Threading.Thread.Sleep(30000);
}
}
if(text == null) {
return new JsonData();
}
try {
return JsonMapper.ToObject(text);
} catch(Exception) {
return new JsonData();
}
}
private String GetString(String url, Boolean withoutput = true) {
String ret = null;
lock(getLock) {
HttpWebRequest request = WebRequest.CreateHttp(url);
request.Timeout = 10000;
//request.Headers.Add(HttpRequestHeader.Authorization, this.auth);
try {
using(HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {
if(response.StatusCode == HttpStatusCode.Unauthorized) {
Console.Error.WriteLine("Benutzer oder Passwort falsch!");
throw new Exception("Benutzer oder Passwort falsch!");
}
if(withoutput) {
StreamReader reader = new StreamReader(response.GetResponseStream());
ret = reader.ReadToEnd();
}
}
} catch(Exception e) {
//Helper.WriteError("Konnte keine Verbindung zum Razzbery Server herstellen. Resource: \"" + this.server + v + "\" Fehler: " + e.Message);
//return null;
throw new Exception("Konnte keine Verbindung zum Server herstellen: " + e.Message);
}
}
return ret;
}
}
}

View File

@ -45,6 +45,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Lib\WebRequests.cs" />
<Compile Include="Model\Admin\AdminModel.cs" />
<Compile Include="Model\Admin\AdminSession.cs" />
<Compile Include="Model\Camera.cs" />
@ -238,8 +239,6 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Folder Include="Helper\" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -117,5 +117,7 @@ namespace Fraunhofer.Fit.IoT.LoraMap.Model {
}
}
}
public List<Int32> GetWeatherCellIds() => this.weatherCellIDs;
}
}

View File

@ -1,21 +1,164 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using Fraunhofer.Fit.IoT.LoraMap.Lib;
using LitJson;
namespace Fraunhofer.Fit.IoT.LoraMap.Model {
public class WeatherWarnings {
private readonly Settings settings;
private Thread bgthread;
private readonly WebRequests webrequests = new WebRequests();
public WeatherWarnings(Settings settings) { this.settings = settings;
public List<Warning> Warnungen { get; private set; }
public WeatherWarnings(Settings settings) {
this.settings = settings;
this.StartBackgroundThread();
}
private void StartBackgroundThread() {
this.bgthread = new Thread(this.BackGroundRunner);
this.bgthread.Start();
}
internal void Dispose() {
private void BackGroundRunner() {
while(true) {
List<Warning> ret = new List<Warning>();
foreach(Int32 item in this.settings.GetWeatherCellIds()) {
JsonData json = this.webrequests.GetJson("https://maps.dwd.de/geoserver/wfs?SERVICE=WFS&VERSION=2.0.0&REQUEST=GetFeature&typeName=dwd:Warnungen_Gemeinden&outputFormat=application/json&cql_filter=WARNCELLID=" + item);
if(json.ContainsKey("features") && json["features"].IsArray && json["features"].Count > 0) {
foreach(JsonData warning in json["features"]) {
try {
ret.Add(new Warning(warning));
} catch { }
}
}
}
this.Warnungen = ret;
Thread.Sleep(60 * 1000);
}
}
internal void Dispose() => this.bgthread.Abort();
public class Warning {
public Warning(JsonData warning) {
this.Id = warning["id"].ToString();
this.From = warning["properties"]["SENT"].ToString();
this.Expires = warning["properties"]["EXPIRES"].ToString();
this.Location = warning["properties"]["NAME"].ToString();
this.Type = warning["properties"]["EVENT"].ToString();
this.Level = warning["properties"]["SEVERITY"].ToString();
this.Headline = warning["properties"]["HEADLINE"].ToString();
this.Body = warning["properties"]["DESCRIPTION"].ToString();
this.Instructions = warning["properties"]["INSTRUCTION"].ToString();
}
public String Id { get; }
public String From { get; }
public String Expires { get; }
public String Location { get; }
public String Type { get; }
public String Level { get; }
public String Headline { get; }
public String Body { get; }
public String Instructions { get; }
}
}
}
/* https://maps.dwd.de/geoserver/wfs?SERVICE=WFS&VERSION=2.0.0&REQUEST=GetFeature&typeName=dwd:Warnungen_Gemeinden&outputFormat=application/json&cql_filter=WARNCELLID=805314000
* {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"id": "Warnungen_Gemeinden.805314000.2.49.0.1.276.0.DWD.PVW.1565627520000.4edfa973-5fef-4b97-8990-7489828dbe5b.DEU",
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[ 7.2072, 50.7395 ],
[ 7.1534, 50.7599 ],
[ 7.1255, 50.7744 ],
[ 7.105, 50.7622 ],
[ 7.0768, 50.7679 ],
[ 7.0666, 50.7705 ],
[ 7.0378, 50.7558 ],
[ 7.0256, 50.7054 ],
[ 7.0385, 50.6886 ],
[ 7.0255, 50.665 ],
[ 7.0473, 50.6391 ],
[ 7.0639, 50.6309 ],
[ 7.1054, 50.6595 ],
[ 7.1278, 50.6472 ],
[ 7.1564, 50.6547 ],
[ 7.1954, 50.6434 ],
[ 7.2119, 50.649 ],
[ 7.1972, 50.6648 ],
[ 7.1679, 50.7035 ],
[ 7.1969, 50.7138 ],
[ 7.2072, 50.7395 ]
]
]
]
},
"geometry_name": "THE_GEOM",
"properties": {
"AREADESC": "Bonn",
"NAME": "Stadt Bonn",
"WARNCELLID": 805314000,
"IDENTIFIER": "2.49.0.1.276.0.DWD.PVW.1565627520000.4edfa973-5fef-4b97-8990-7489828dbe5b.DEU",
"SENDER": "CAP@dwd.de",
"SENT": "2019-08-12T16:32:00Z",
"STATUS": "Actual",
"MSGTYPE": "Update",
"SOURCE": "PVW",
"SCOPE": "Public",
"CODE": "id:2.49.0.1.276.0.DWD.PVW.1565627520000.4edfa973-5fef-4b97-8990-7489828dbe5b",
"LANGUAGE": "de-DE",
"CATEGORY": "Met",
"EVENT": "GEWITTER",
"RESPONSETYPE": "Prepare",
"URGENCY": "Immediate",
"SEVERITY": "Minor",
"CERTAINTY": "Likely",
"EC_PROFILE": "2.1",
"EC_LICENSE": "Geobasisdaten: Copyright Bundesamt für Kartographie und Geodäsie, Frankfurt am Main, 2017",
"EC_II": "31",
"EC_GROUP": "THUNDERSTORM;WIND",
"EC_AREA_COLOR": "255 255 0",
"EFFECTIVE": "2019-08-12T16:32:00Z",
"ONSET": "2019-08-12T16:32:00Z",
"EXPIRES": "2019-08-12T17:00:00Z",
"SENDERNAME": "DWD / Nationales Warnzentrum Offenbach",
"HEADLINE": "Amtliche WARNUNG vor GEWITTER",
"DESCRIPTION": "Von Südwesten ziehen einzelne Gewitter auf. Dabei gibt es Windböen mit Geschwindigkeiten um 60 km/h (17m/s, 33kn, Bft 7).",
"INSTRUCTION": "ACHTUNG! Hinweis auf mögliche Gefahren: Örtlich kann es Blitzschlag geben. Bei Blitzschlag besteht Lebensgefahr!",
"WEB": "http://www.wettergefahren.de",
"CONTACT": "Deutscher Wetterdienst",
"PARAMETERNAME": "Böen;Gewitter;Gewitteraufzugsrichtung",
"PARAMETERVALUE": "~60 [km/h];einzelne;SW",
"ALTITUDE": 0,
"CEILING": 9842.5197,
"bbox": [ 7.0255, 50.6309, 7.2119, 50.7744 ]
}
}
],
"totalFeatures": 1,
"numberMatched": 1,
"numberReturned": 1,
"timeStamp": "2019-08-12T16:42:20.743Z",
"crs": {
"type": "name",
"properties": { "name": "urn:ogc:def:crs:EPSG::4326" }
},
"bbox": [ 50.6309, 7.0255, 50.7744, 7.2119 ]
}
*/