using System; using System.Collections.Generic; using System.Net; using System.Text; using BlubbFish.Utils; using BlubbFish.Utils.IoT.Bots; using BlubbFish.Utils.IoT.Connector; using BlubbFish.Utils.IoT.Events; using Fraunhofer.Fit.IoT.MqttMap.Model; using LitJson; namespace Fraunhofer.Fit.IoT.MqttMap { class Googlelocation : Webserver { private readonly Dictionary locations = new Dictionary(); public Googlelocation(ADataBackend backend, Dictionary settings, InIReader requests) : base(backend, settings, requests) { } protected override void Backend_MessageIncomming(Object sender, BackendEvent e) { 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 && d.ContainsKey("Receivedtime") && d["Receivedtime"].IsString && d.ContainsKey("BatteryLevel") && d["BatteryLevel"].IsDouble && d.ContainsKey("Gps") && d["Gps"].IsObject && d["Gps"].ContainsKey("Latitude") && d["Gps"]["Latitude"].IsDouble && d["Gps"].ContainsKey("Longitude") && d["Gps"]["Longitude"].IsDouble && 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"]; 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"], (Double)d["BatteryLevel"], (Boolean)d["Gps"]["Fix"]); if (b.Fix) { if (this.locations.ContainsKey(name)) { this.locations[name] = b; } else { this.locations.Add(name,b); } Console.WriteLine("Koodinate erhalten!"); } else { Console.WriteLine("Daten erhalten! (Kein Fix)"); } } } catch { } } protected override void SendResponse(HttpListenerContext cont) { if (cont.Request.Url.PathAndQuery.StartsWith("/loc")) { try { Dictionary ret = new Dictionary(); 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; } } base.SendResponse(cont); } } }