Lora-Map/mqtt-map/Googlelocation.cs

73 lines
2.9 KiB
C#
Raw Normal View History

2018-08-12 01:10:30 +02:00
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
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;
using Fraunhofer.Fit.IoT.LoraMap.Model;
2018-08-12 01:10:30 +02:00
using LitJson;
namespace Fraunhofer.Fit.IoT.LoraMap {
2018-08-12 01:10:30 +02:00
2018-12-30 20:50:20 +01:00
class Googlelocation : Webserver
{
2019-01-29 18:09:45 +01:00
private readonly Dictionary<String, Botclient> locations = new Dictionary<String, Botclient>();
2018-12-30 20:50:20 +01:00
public Googlelocation(ADataBackend backend, Dictionary<String, String> settings, InIReader requests) : base(backend, settings, requests) { }
2018-08-12 01:10:30 +02:00
2018-12-30 20:50:20 +01:00
protected override void Backend_MessageIncomming(Object sender, BackendEvent e) {
2018-08-12 01:10:30 +02:00
try {
JsonData d = JsonMapper.ToObject(e.Message);
2019-03-07 14:58:41 +01:00
if (d.ContainsKey("Rssi") && d["Rssi"].IsDouble
2018-08-12 01:10:30 +02:00
&& d.ContainsKey("Snr") && d["Snr"].IsDouble
2018-09-06 22:24:02 +02:00
&& d.ContainsKey("Receivedtime") && d["Receivedtime"].IsString
2019-01-29 18:09:45 +01:00
&& d.ContainsKey("BatteryLevel") && d["BatteryLevel"].IsDouble
2018-08-12 01:10:30 +02:00
&& d.ContainsKey("Gps") && d["Gps"].IsObject
2018-09-06 22:24:02 +02:00
&& d["Gps"].ContainsKey("Latitude") && d["Gps"]["Latitude"].IsDouble
&& d["Gps"].ContainsKey("Longitude") && d["Gps"]["Longitude"].IsDouble
2019-03-07 14:58:41 +01:00
&& d["Gps"].ContainsKey("LastLatitude") && d["Gps"]["LastLatitude"].IsDouble
&& d["Gps"].ContainsKey("LastLongitude") && d["Gps"]["LastLongitude"].IsDouble
2018-08-12 01:10:30 +02:00
&& d["Gps"].ContainsKey("Hdop") && d["Gps"]["Hdop"].IsDouble
&& d["Gps"].ContainsKey("Fix") && d["Gps"]["Fix"].IsBoolean
2019-03-07 14:58:41 +01:00
&& d["Gps"].ContainsKey("Height") && d["Gps"]["Height"].IsDouble
2018-08-12 01:10:30 +02:00
&& d.ContainsKey("Name") && d["Name"].IsString) {
String name = (String)d["Name"];
2019-03-07 14:58:41 +01:00
Botclient b = new Botclient(d);
if (b.Fix || b.Longitude != 0 || b.Latitude != 0) {
2018-08-12 01:10:30 +02:00
if (this.locations.ContainsKey(name)) {
2019-01-29 18:09:45 +01:00
this.locations[name] = b;
2018-08-12 01:10:30 +02:00
} else {
2019-01-29 18:09:45 +01:00
this.locations.Add(name,b);
2018-08-12 01:10:30 +02:00
}
Console.WriteLine("Koodinate erhalten!");
} else {
Console.WriteLine("Daten erhalten! (Kein Fix)");
}
}
2019-03-07 14:58:41 +01:00
} catch(Exception ex) {
Helper.WriteError(ex.Message);
}
2018-08-12 01:10:30 +02:00
}
2018-12-30 20:50:20 +01:00
protected override void SendResponse(HttpListenerContext cont) {
if (cont.Request.Url.PathAndQuery.StartsWith("/loc")) {
2018-08-12 01:10:30 +02:00
try {
Dictionary<String, Object> ret = new Dictionary<String, Object>();
2018-12-30 20:50:20 +01:00
Byte[] buf = Encoding.UTF8.GetBytes(JsonMapper.ToJson(this.locations));
cont.Response.ContentLength64 = buf.Length;
cont.Response.OutputStream.Write(buf, 0, buf.Length);
Console.WriteLine("200 - " + cont.Request.Url.PathAndQuery);
return;
} catch (Exception e) {
Helper.WriteError("500 - " + e.Message);
cont.Response.StatusCode = 500;
return;
}
2018-08-12 01:10:30 +02:00
}
2018-12-30 20:50:20 +01:00
base.SendResponse(cont);
2018-08-12 01:10:30 +02:00
}
}
}