Lora-Map/mqtt-map/Googlelocation.cs

119 lines
5.2 KiB
C#
Raw Normal View History

2018-08-12 01:10:30 +02:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading;
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 LitJson;
namespace Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls_broken {
class Botclient {
2018-09-06 22:24:02 +02:00
public Botclient(Int32 paketrssi, Int32 rssi, Double snr, String updatetime, Double lat, Double lon, Double hdop, Int32 battery, Boolean fix) {
2018-08-12 01:10:30 +02:00
this.PacketRssi = paketrssi;
this.Rssi = rssi;
this.Snr = snr;
this.Upatedtime = updatetime;
this.Latitude = lat;
this.Longitude = lon;
this.Hdop = hdop;
2018-09-06 22:24:02 +02:00
this.Battery = battery;
2018-08-12 01:10:30 +02:00
this.Fix = fix;
}
public Int32 PacketRssi { get; private set; }
public Int32 Rssi { get; private set; }
public Double Snr { get; private set; }
public String Upatedtime { get; private set; }
public Double Latitude { get; private set; }
public Double Longitude { get; private set; }
public Double Hdop { get; private set; }
2018-09-06 22:24:02 +02:00
public Int32 Battery { get; private set; }
2018-08-12 01:10:30 +02:00
public Boolean Fix { get; private set; }
public virtual Dictionary<String, Object> ToDictionary() {
Dictionary<String, Object> dictionary = new Dictionary<String, Object>();
foreach (PropertyInfo item in this.GetType().GetProperties()) {
if (item.CanRead && item.GetValue(this) != null) {
if (item.GetValue(this).GetType().GetMethod("ToDictionary") != null) {
dictionary.Add(item.Name, item.GetValue(this).GetType().GetMethod("ToDictionary").Invoke(item.GetValue(this), null));
} else if (item.GetValue(this).GetType().HasInterface(typeof(IDictionary))) {
Dictionary<String, Object> subdict = new Dictionary<String, Object>();
foreach (DictionaryEntry subitem in (IDictionary)item.GetValue(this)) {
if (subitem.Value.GetType().GetMethod("ToDictionary") != null) {
subdict.Add(subitem.Key.ToString(), subitem.Value.GetType().GetMethod("ToDictionary").Invoke(subitem.Value, null));
}
}
dictionary.Add(item.Name, subdict);
} else if (item.GetValue(this).GetType().BaseType == typeof(Enum)) {
dictionary.Add(item.Name, Helper.GetEnumDescription((Enum)item.GetValue(this)));
} else {
dictionary.Add(item.Name, item.GetValue(this));
}
}
}
return dictionary;
}
}
2018-12-30 20:50:20 +01:00
class Googlelocation : Webserver
{
2018-08-12 01:10:30 +02:00
private readonly Dictionary<String, List<Botclient>> locations = new Dictionary<String, List<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);
if (d.ContainsKey("PacketRssi") && d["PacketRssi"].IsInt
&& d.ContainsKey("Rssi") && d["Rssi"].IsInt
&& d.ContainsKey("Snr") && d["Snr"].IsDouble
2018-09-06 22:24:02 +02:00
&& d.ContainsKey("Receivedtime") && d["Receivedtime"].IsString
&& d.ContainsKey("BatteryLevel") && d["BatteryLevel"].IsInt
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
2018-08-12 01:10:30 +02:00
&& d["Gps"].ContainsKey("Hdop") && d["Gps"]["Hdop"].IsDouble
&& d["Gps"].ContainsKey("Fix") && d["Gps"]["Fix"].IsBoolean
&& d.ContainsKey("Name") && d["Name"].IsString) {
String name = (String)d["Name"];
2018-09-06 22:24:02 +02:00
Botclient b = new Botclient((Int32)d["PacketRssi"], (Int32)d["Rssi"], (Double)d["Snr"], (String)d["Receivedtime"], (Double)d["Gps"]["Latitude"], (Double)d["Gps"]["Longitude"], (Double)d["Gps"]["Hdop"], (Int32)d["BatteryLevel"], (Boolean)d["Gps"]["Fix"]);
2018-08-12 01:10:30 +02:00
if (b.Fix) {
if (this.locations.ContainsKey(name)) {
this.locations[name].Add(b);
} else {
this.locations.Add(name, new List<Botclient> { b });
}
Console.WriteLine("Koodinate erhalten!");
} else {
Console.WriteLine("Daten erhalten! (Kein Fix)");
}
}
} catch { }
}
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
}
}
}