From 47adcbacf138c240bfa388a2731df37beeb73873 Mon Sep 17 00:00:00 2001 From: Daniel Schick Date: Tue, 25 Oct 2022 09:54:51 +0200 Subject: [PATCH] ZONE MONITORING WIP 1 added classes for vertices, polygons (zones) and a monitoring vlass --- AIS/bsmd.AIS2Service/AISManager.cs | 8 +- AIS/bsmd.AIS2Service/AISZoneMonitor.cs | 87 +++++++++++++++++++ AIS/bsmd.AIS2Service/App.config | 6 ++ AIS/bsmd.AIS2Service/MonitorZone.cs | 54 ++++++++++++ .../Properties/Settings.Designer.cs | 20 ++++- .../Properties/Settings.settings | 6 ++ AIS/bsmd.AIS2Service/bsmd.AIS2Service.csproj | 3 + 7 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 AIS/bsmd.AIS2Service/AISZoneMonitor.cs create mode 100644 AIS/bsmd.AIS2Service/MonitorZone.cs diff --git a/AIS/bsmd.AIS2Service/AISManager.cs b/AIS/bsmd.AIS2Service/AISManager.cs index 821fc8de..f4a9ec18 100644 --- a/AIS/bsmd.AIS2Service/AISManager.cs +++ b/AIS/bsmd.AIS2Service/AISManager.cs @@ -1,4 +1,9 @@ -using log4net; +// Copyright (c) 2022 - schick Informatik +// bsmd.AIS2Service [AISManager.cs]: %UserDisplayName% +// Description: Manager class that holds references to all concurrant threads +// and memory storage entities + +using log4net; using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -35,6 +40,7 @@ namespace bsmd.AIS2Service _tasks.Add(new SitRep(_decodedClasses, _sitRepList, _dbSaveTargets)); AIS_SQLiteStorage sqliteStorage = new AIS_SQLiteStorage(_dbSaveTargets); _tasks.Add(sqliteStorage); + _tasks.Add(new AISZoneMonitor(_sitRepList)); // preload sit rep Dictionary targets = await sqliteStorage.LoadTargets(); diff --git a/AIS/bsmd.AIS2Service/AISZoneMonitor.cs b/AIS/bsmd.AIS2Service/AISZoneMonitor.cs new file mode 100644 index 00000000..57098963 --- /dev/null +++ b/AIS/bsmd.AIS2Service/AISZoneMonitor.cs @@ -0,0 +1,87 @@ +// Copyright (c) 2022 - schick Informatik +// bsmd.AIS2Service [AISZoneMonitor.cs]: %UserDisplayName% +// Description: Background Thread Controller class for comparing AIS Targets +// to monitor zones +// + +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace bsmd.AIS2Service +{ + internal class AISZoneMonitor : IAISThread + { + + #region Fields + + ConcurrentDictionary _sitRepDict; + private Thread _thread; + private bool _stopFlag = false; + + #endregion + + #region Construction + + public AISZoneMonitor(ConcurrentDictionary sitRepDict) + { + _sitRepDict = sitRepDict; + } + + #endregion + + #region private methods + + private void Monitor() + { + // load zones from storage + + // loop + + while(!_stopFlag) + { + // check all "current" AIS Targets against the zones + int currentSaturationSecs = 0; // (DateTime.Now - alarm.FirstDetected).TotalSeconds; + if(currentSaturationSecs > Properties.Settings.Default.MonitorTargetSaturationSecs) + { + // trigger alarm, this thing was "hot" long enough + + } + + Thread.Sleep(Properties.Settings.Default.MonitorTestIntervalSecs * 1000); + } + } + + #endregion + + #region IAISThread implementation + + public string Name { get { return "Zone monitor"; } } + + public event EventHandler FatalErrorOccurred; + + public void Start() + { + if (_thread != null) return; // may not run twice + ThreadStart runReader = new ThreadStart(this.Monitor); + _thread = new Thread(runReader); + _thread.Start(); + } + + public void Stop() + { + if (_thread == null) return; + _stopFlag = true; + _thread.Join(); + _thread = null; + } + + #endregion + + + } +} diff --git a/AIS/bsmd.AIS2Service/App.config b/AIS/bsmd.AIS2Service/App.config index 4018ac42..353c1b8d 100644 --- a/AIS/bsmd.AIS2Service/App.config +++ b/AIS/bsmd.AIS2Service/App.config @@ -61,6 +61,12 @@ http://localhost:9050 + + 30 + + + 120 + diff --git a/AIS/bsmd.AIS2Service/MonitorZone.cs b/AIS/bsmd.AIS2Service/MonitorZone.cs new file mode 100644 index 00000000..f78cb562 --- /dev/null +++ b/AIS/bsmd.AIS2Service/MonitorZone.cs @@ -0,0 +1,54 @@ +// Copyright (c) 2022 - schick Informatik +// bsmd.AIS2Service [MonitorZone.cs]: %UserDisplayName% +// Description: Represents a geographical area that should be checked against +// ship positions + +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace bsmd.AIS2Service +{ + internal class MonitorZone + { + private List _vertices; + + public MonitorZone(List vertices) + { + _vertices = vertices; + } + + public bool IsPointInPolygon4(GeoPoint testPoint) + { + bool result = false; + int j = _vertices.Count() - 1; + for (int i = 0; i < _vertices.Count(); i++) + { + if (_vertices[i].Lat < testPoint.Lat && _vertices[j].Lat >= testPoint.Lat || _vertices[j].Lat < testPoint.Lat && _vertices[i].Lat >= testPoint.Lat) + { + if (_vertices[i].Lon + (testPoint.Lat - _vertices[i].Lat) / (_vertices[j].Lat - _vertices[i].Lat) * (_vertices[j].Lon - _vertices[i].Lon) < testPoint.Lon) + { + result = !result; + } + } + j = i; + } + return result; + } + } + + internal class GeoPoint + { + public GeoPoint(double lat, double lon) + { + Lat = lat; Lon = lon; + } + + public double Lat { get; private set; } + public double Lon { get; private set; } + } + +} diff --git a/AIS/bsmd.AIS2Service/Properties/Settings.Designer.cs b/AIS/bsmd.AIS2Service/Properties/Settings.Designer.cs index ae8c55a7..5a6caab3 100644 --- a/AIS/bsmd.AIS2Service/Properties/Settings.Designer.cs +++ b/AIS/bsmd.AIS2Service/Properties/Settings.Designer.cs @@ -12,7 +12,7 @@ namespace bsmd.AIS2Service.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.3.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.2.0.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); @@ -103,5 +103,23 @@ namespace bsmd.AIS2Service.Properties { return ((string)(this["RestAPIBaseAddress"])); } } + + [global::System.Configuration.ApplicationScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("30")] + public int MonitorTestIntervalSecs { + get { + return ((int)(this["MonitorTestIntervalSecs"])); + } + } + + [global::System.Configuration.ApplicationScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("120")] + public int MonitorTargetSaturationSecs { + get { + return ((int)(this["MonitorTargetSaturationSecs"])); + } + } } } diff --git a/AIS/bsmd.AIS2Service/Properties/Settings.settings b/AIS/bsmd.AIS2Service/Properties/Settings.settings index 4d561b1d..c1f0c7de 100644 --- a/AIS/bsmd.AIS2Service/Properties/Settings.settings +++ b/AIS/bsmd.AIS2Service/Properties/Settings.settings @@ -29,5 +29,11 @@ http://localhost:9050 + + 30 + + + 120 + \ No newline at end of file diff --git a/AIS/bsmd.AIS2Service/bsmd.AIS2Service.csproj b/AIS/bsmd.AIS2Service/bsmd.AIS2Service.csproj index 6631d4ad..98fee541 100644 --- a/AIS/bsmd.AIS2Service/bsmd.AIS2Service.csproj +++ b/AIS/bsmd.AIS2Service/bsmd.AIS2Service.csproj @@ -61,6 +61,7 @@ packages\Stub.System.Data.SQLite.Core.NetFramework.1.0.116.0\lib\net46\System.Data.SQLite.dll + packages\Microsoft.AspNet.WebApi.Client.5.2.9\lib\net45\System.Net.Http.Formatting.dll @@ -96,6 +97,7 @@ + @@ -107,6 +109,7 @@ +