diff --git a/Lora-Map/Model/AlarmItem.cs b/Lora-Map/Model/AlarmItem.cs index 36c3854..83c02fa 100644 --- a/Lora-Map/Model/AlarmItem.cs +++ b/Lora-Map/Model/AlarmItem.cs @@ -1,81 +1,32 @@ using System; using System.Collections.Generic; -using System.Globalization; using System.Linq; using LitJson; namespace Fraunhofer.Fit.IoT.LoraMap.Model { - class AlarmItem { - public Double Rssi { get; private set; } - public Double Snr { get; private set; } - public DateTime Lorarecievedtime { get; private set; } - public DateTime Recievedtime { get; private set; } - public Double Latitude { get; private set; } - public Double Longitude { get; private set; } - public UTMData UTM { get; private set; } - public Double Hdop { get; private set; } - public Boolean Fix { get; private set; } - public Double Height { get; private set; } - public List ButtonPressed => this.buttonhistory.Keys.ToList(); - + public class AlarmItem : PositionItem { private readonly SortedDictionary buttonhistory = new SortedDictionary(); - public AlarmItem(JsonData json) => this.Update(json); + public List ButtonPressed => this.buttonhistory.Keys.ToList(); - public void Update(JsonData json) { - this.Rssi = json["Rssi"].IsInt ? (Int32)json["Rssi"] : (Double)json["Rssi"]; - this.Snr = json["Snr"].IsInt ? (Int32)json["Snr"] : (Double)json["Snr"]; - if (DateTime.TryParse((String)json["Receivedtime"], DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AssumeUniversal, out DateTime updatetime)) { - this.Lorarecievedtime = updatetime.ToUniversalTime(); - } - this.Recievedtime = DateTime.UtcNow; - this.Latitude = json["Gps"]["Latitude"].IsInt ? (Int32)json["Gps"]["Latitude"] : (Double)json["Gps"]["Latitude"]; - this.Longitude = json["Gps"]["Longitude"].IsInt ? (Int32)json["Gps"]["Longitude"] : (Double)json["Gps"]["Longitude"]; - this.Fix = (Boolean)json["Gps"]["Fix"]; - if (!this.Fix) { - this.Latitude = json["Gps"]["LastLatitude"].IsInt ? (Int32)json["Gps"]["LastLatitude"] : (Double)json["Gps"]["LastLatitude"]; - this.Longitude = json["Gps"]["LastLongitude"].IsInt ? (Int32)json["Gps"]["LastLongitude"] : (Double)json["Gps"]["LastLongitude"]; - } - this.UTM = new UTMData(this.Latitude, this.Longitude); - this.Hdop = json["Gps"]["Hdop"].IsInt ? (Int32)json["Gps"]["Hdop"] : (Double)json["Gps"]["Hdop"]; - this.Height = json["Gps"]["Height"].IsInt ? (Int32)json["Gps"]["Height"] : (Double)json["Gps"]["Height"]; + public AlarmItem(JsonData json) : base(json, null) { + } + + public override void Update(JsonData json) { + base.Update(json); this.SetHistory(json); } private void SetHistory(JsonData json) { - String key = json["BatteryLevel"].ToString(); - key += "_" + json["Calculatedcrc"].ToString(); - key += "_" + json["Gps"]["Hdop"].ToString(); - key += "_" + json["Gps"]["Height"].ToString(); - key += "_" + json["Gps"]["Fix"].ToString(); - key += "_" + json["Gps"]["LastLatitude"].ToString(); - key += "_" + json["Gps"]["LastLongitude"].ToString(); - key += "_" + json["Gps"]["Time"].ToString(); - if(!this.buttonhistory.ContainsValue(key)) { - this.buttonhistory.Add(DateTime.UtcNow, key); - if(this.buttonhistory.Count > 10) { - this.buttonhistory.Remove(this.buttonhistory.Keys.ToList().First()); + if(json.ContainsKey("Hash") && json["Hash"].IsString) { + String key = json["Hash"].ToString(); + if(!this.buttonhistory.ContainsValue(key)) { + this.buttonhistory.Add(DateTime.UtcNow, key); + if(this.buttonhistory.Count > 10) { + _ = this.buttonhistory.Remove(this.buttonhistory.Keys.ToList().First()); + } } - } + } } - - public static String GetId(JsonData json) => (String)json["Name"]; - - public static Boolean CheckJson(JsonData json) => - json.ContainsKey("Rssi") && (json["Rssi"].IsDouble || json["Rssi"].IsInt) - && json.ContainsKey("Snr") && (json["Snr"].IsDouble || json["Snr"].IsInt) - && json.ContainsKey("Receivedtime") && json["Receivedtime"].IsString - && json.ContainsKey("BatteryLevel") && (json["BatteryLevel"].IsDouble || json["BatteryLevel"].IsInt) - && json.ContainsKey("Gps") && json["Gps"].IsObject - && json["Gps"].ContainsKey("Latitude") && (json["Gps"]["Latitude"].IsDouble || json["Gps"]["Latitude"].IsInt) - && json["Gps"].ContainsKey("Longitude") && (json["Gps"]["Longitude"].IsDouble || json["Gps"]["Longitude"].IsInt) - && json["Gps"].ContainsKey("LastLatitude") && (json["Gps"]["LastLatitude"].IsDouble || json["Gps"]["LastLatitude"].IsInt) - && json["Gps"].ContainsKey("LastLongitude") && (json["Gps"]["LastLongitude"].IsDouble || json["Gps"]["LastLongitude"].IsInt) - && json["Gps"].ContainsKey("LastGPSPos") && json["Gps"]["LastGPSPos"].IsString - && json["Gps"].ContainsKey("Hdop") && (json["Gps"]["Hdop"].IsDouble || json["Gps"]["Hdop"].IsInt) - && json["Gps"].ContainsKey("Fix") && json["Gps"]["Fix"].IsBoolean - && json["Gps"].ContainsKey("Height") && (json["Gps"]["Height"].IsDouble || json["Gps"]["Height"].IsInt) - && json.ContainsKey("Name") && json["Name"].IsString && - json.ContainsKey("Calculatedcrc") && json["Calculatedcrc"].IsInt; } -} \ No newline at end of file +} diff --git a/Lora-Map/Model/PositionItem.cs b/Lora-Map/Model/PositionItem.cs index 682708a..db742f0 100644 --- a/Lora-Map/Model/PositionItem.cs +++ b/Lora-Map/Model/PositionItem.cs @@ -3,7 +3,7 @@ using System.Globalization; using LitJson; namespace Fraunhofer.Fit.IoT.LoraMap.Model { - class PositionItem { + public class PositionItem { private Double _lastLat = 0; private Double _lastLon = 0; @@ -30,7 +30,7 @@ namespace Fraunhofer.Fit.IoT.LoraMap.Model { } public void UpdateMarker(JsonData marker, String id) { - if(marker.ContainsKey(id)) { + if(marker != null && marker.ContainsKey(id)) { this.Name = marker[id].ContainsKey("name") && marker[id]["name"].IsString ? (String)marker[id]["name"] : id; this.Icon = marker[id].ContainsKey("marker.svg") && marker[id]["marker.svg"].IsObject ? Marker.ParseMarkerConfig(marker[id]["marker.svg"], this.Name) : marker[id].ContainsKey("icon") && marker[id]["icon"].IsString ? (String)marker[id]["icon"] : null; this.Group = marker[id].ContainsKey("Group") && marker[id]["Group"].IsString ? (String)marker[id]["Group"] : "no"; @@ -51,7 +51,7 @@ namespace Fraunhofer.Fit.IoT.LoraMap.Model { public static String GetId(JsonData json) => (String)json["Name"]; - public void Update(JsonData json) { + public virtual void Update(JsonData json) { this.Rssi = json.ContainsKey("Rssi") && (json["Rssi"].IsDouble || json["Rssi"].IsInt) && Double.TryParse(json["Rssi"].ToString(), out Double rssi) ? rssi : 0; this.Snr = json.ContainsKey("Snr") && (json["Snr"].IsDouble || json["Snr"].IsInt) && Double.TryParse(json["Snr"].ToString(), out Double snr) ? snr : 0; this.Lorarecievedtime = json.ContainsKey("Receivedtime") && json["Receivedtime"].IsString && DateTime.TryParse((String)json["Receivedtime"], DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AssumeUniversal, out DateTime updatetime) ? updatetime.ToUniversalTime() : DateTime.UtcNow; diff --git a/Lora-Map/Server.cs b/Lora-Map/Server.cs index 0383c3c..3050714 100644 --- a/Lora-Map/Server.cs +++ b/Lora-Map/Server.cs @@ -71,7 +71,7 @@ namespace Fraunhofer.Fit.IoT.LoraMap { protected override void Backend_MessageIncomming(Object sender, BackendEvent mqtt) { try { JsonData d = JsonMapper.ToObject(mqtt.Message); - if(PositionItem.CheckJson(d) && ((String)mqtt.From).Contains("lora/data")) { + if(((String)mqtt.From).Contains("lora/data") && PositionItem.CheckJson(d)) { String name = PositionItem.GetId(d); lock(this.lockData) { if(this.positions.ContainsKey(name)) { @@ -81,8 +81,8 @@ namespace Fraunhofer.Fit.IoT.LoraMap { } } Console.WriteLine("Koordinate erhalten!"); - } else if(AlarmItem.CheckJson(d) && ((String)mqtt.From).Contains("lora/panic")) { - String name = AlarmItem.GetId(d); + } else if(((String)mqtt.From).Contains("lora/panic") && PositionItem.CheckJson(d)) { + String name = PositionItem.GetId(d); lock(this.lockPanic) { if(this.alarms.ContainsKey(name)) { this.alarms[name].Update(d); @@ -98,7 +98,7 @@ namespace Fraunhofer.Fit.IoT.LoraMap { } } Console.WriteLine("PANIC erhalten!"); - } else if(Camera.CheckJson(d) && ((String)mqtt.From).Contains("camera/count")) { + } else if(((String)mqtt.From).Contains("camera/count") && Camera.CheckJson(d)) { String cameraid = Camera.GetId(d); lock(this.lockCount) { if(this.counter.ContainsKey(cameraid)) {