From 1e4c2db7163ce0c038a03b856694ac2cd78f128a Mon Sep 17 00:00:00 2001 From: BlubbFish Date: Tue, 31 Aug 2021 23:00:47 +0200 Subject: [PATCH] otify other Objects when Settings are changed. --- Lora-Map/Model/Admin/AdminModel.cs | 4 ++- Lora-Map/Model/Position/PositionItem.cs | 47 +++++++++++++++---------- Lora-Map/Model/Settings.cs | 6 +++- 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/Lora-Map/Model/Admin/AdminModel.cs b/Lora-Map/Model/Admin/AdminModel.cs index 8acaf8f..b234489 100644 --- a/Lora-Map/Model/Admin/AdminModel.cs +++ b/Lora-Map/Model/Admin/AdminModel.cs @@ -14,7 +14,9 @@ using LitJson; namespace Fraunhofer.Fit.IoT.LoraMap.Model.Admin { class AdminModel { public delegate void AdminEvent(Object sender, EventArgs e); - #pragma warning disable 0067 + + //Supress never used warning (cause of reflection) + #pragma warning disable 0067 public event AdminEvent NamesUpdate; public event AdminEvent GeoUpdate; public event AdminEvent SettingsUpdate; diff --git a/Lora-Map/Model/Position/PositionItem.cs b/Lora-Map/Model/Position/PositionItem.cs index de3ecf9..8c9218c 100644 --- a/Lora-Map/Model/Position/PositionItem.cs +++ b/Lora-Map/Model/Position/PositionItem.cs @@ -12,6 +12,8 @@ namespace Fraunhofer.Fit.IoT.LoraMap.Model.Position { private readonly SortedDictionary _history = new SortedDictionary(); private String _lastHash = ""; private Boolean _isdublicate = false; + private Double _historycounter = 0; + private readonly Object lockHistory = new Object(); public Double Rssi { get; private set; } public Double Snr { get; private set; } @@ -35,6 +37,7 @@ namespace Fraunhofer.Fit.IoT.LoraMap.Model.Position { public PositionItem(LoraData data, NamesModel marker) { this.Update(data); this.UpdateMarker(marker, data.Name); + Settings.Instance.SettingsUpdate += this.UpdateSettings; } public void UpdateMarker(NamesModel marker, String id) { @@ -104,28 +107,36 @@ namespace Fraunhofer.Fit.IoT.LoraMap.Model.Position { } private void StoreHistory() { - if(Settings.Instance.Internal.History.Enabled) { - if(Settings.Instance.Internal.History.Amount != 0 && this._history.Count > Settings.Instance.Internal.History.Amount) { - _ = this._history.Remove(this._history.Keys.ToList().First()); - } - if(Settings.Instance.Internal.History.Time != 0) { - List removeCandidates = new List(); - DateTime now = DateTime.UtcNow; - foreach(KeyValuePair item in this._history) { - if((now - item.Key).TotalSeconds > Settings.Instance.Internal.History.Time) { - removeCandidates.Add(item.Key); + lock(this.lockHistory) { + if(Settings.Instance.Internal.History.Enabled) { + if(Settings.Instance.Internal.History.Amount != 0 && this._history.Count > Settings.Instance.Internal.History.Amount) { + _ = this._history.Remove(this._history.Keys.ToList().First()); + } + if(Settings.Instance.Internal.History.Time != 0) { + List removeCandidates = new List(); + DateTime now = DateTime.UtcNow; + foreach(KeyValuePair item in this._history) { + if((now - item.Key).TotalSeconds > Settings.Instance.Internal.History.Time) { + removeCandidates.Add(item.Key); + } + } + if(removeCandidates.Count > 0) { + foreach(DateTime item in removeCandidates) { + _ = this._history.Remove(item); + } } } - if(removeCandidates.Count > 0) { - foreach(DateTime item in removeCandidates) { - _ = this._history.Remove(item); - } + if(!this._history.ContainsKey(this.Recievedtime)) { + this._history.Add(this.Recievedtime, new Double[] { this.Latitude, this.Longitude, this._historycounter++ }); } + } else { + this._history.Clear(); } - if(!this._history.ContainsKey(this.Recievedtime)) { - this._history.Add(this.Recievedtime, new Double[] { this.Latitude, this.Longitude }); - } - } else { + } + } + + private void UpdateSettings(Object sender, EventArgs e) { + lock(this.lockHistory) { this._history.Clear(); } } diff --git a/Lora-Map/Model/Settings.cs b/Lora-Map/Model/Settings.cs index b42e1a5..992c737 100644 --- a/Lora-Map/Model/Settings.cs +++ b/Lora-Map/Model/Settings.cs @@ -8,9 +8,12 @@ using System.IO; namespace Fraunhofer.Fit.IoT.LoraMap.Model { public class Settings : OwnSingeton { - private Int32 gridradius; + public delegate void SettingsEvent(Object sender, EventArgs e); + public event SettingsEvent SettingsUpdate; + + public PublicSettings External { get; set; } @@ -139,6 +142,7 @@ namespace Fraunhofer.Fit.IoT.LoraMap.Model { this.gridradius = json.ContainsKey("GridRadius") && json["GridRadius"].IsInt && this.External.Startloclat != 0 && this.External.Startloclon != 0 ? (Int32)json["GridRadius"] : 0; this.GenerateGrid(); this.FindMapLayer(); + this.SettingsUpdate?.Invoke(this, new EventArgs()); } private void ParseGeoJson() {