ZONE MONITORING WIP 1

added classes for vertices, polygons (zones) and a monitoring vlass
This commit is contained in:
Daniel Schick 2022-10-25 09:54:51 +02:00
parent 6f39713366
commit 62a5f6beec
7 changed files with 182 additions and 2 deletions

View File

@ -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<int, AIS_Target> targets = await sqliteStorage.LoadTargets();

View File

@ -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<int, AIS_Target> _sitRepDict;
private Thread _thread;
private bool _stopFlag = false;
#endregion
#region Construction
public AISZoneMonitor(ConcurrentDictionary<int, AIS_Target> 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
}
}

View File

@ -61,6 +61,12 @@
<setting name="RestAPIBaseAddress" serializeAs="String">
<value>http://localhost:9050</value>
</setting>
<setting name="MonitorTestIntervalSecs" serializeAs="String">
<value>30</value>
</setting>
<setting name="MonitorTargetSaturationSecs" serializeAs="String">
<value>120</value>
</setting>
</bsmd.AIS2Service.Properties.Settings>
</applicationSettings>
<runtime>

View File

@ -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<GeoPoint> _vertices;
public MonitorZone(List<GeoPoint> 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; }
}
}

View File

@ -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"]));
}
}
}
}

View File

@ -29,5 +29,11 @@
<Setting Name="RestAPIBaseAddress" Type="System.String" Scope="Application">
<Value Profile="(Default)">http://localhost:9050</Value>
</Setting>
<Setting Name="MonitorTestIntervalSecs" Type="System.Int32" Scope="Application">
<Value Profile="(Default)">30</Value>
</Setting>
<Setting Name="MonitorTargetSaturationSecs" Type="System.Int32" Scope="Application">
<Value Profile="(Default)">120</Value>
</Setting>
</Settings>
</SettingsFile>

View File

@ -61,6 +61,7 @@
<HintPath>packages\Stub.System.Data.SQLite.Core.NetFramework.1.0.116.0\lib\net46\System.Data.SQLite.dll</HintPath>
</Reference>
<Reference Include="System.Management" />
<Reference Include="System.Drawing" />
<Reference Include="System.Net.Http.Formatting, Version=5.2.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.AspNet.WebApi.Client.5.2.9\lib\net45\System.Net.Http.Formatting.dll</HintPath>
</Reference>
@ -96,6 +97,7 @@
<Compile Include="AISController.cs" />
<Compile Include="AISDecoder.cs" />
<Compile Include="AISManager.cs" />
<Compile Include="AISZoneMonitor.cs" />
<Compile Include="AIS_BaseStation.cs" />
<Compile Include="AIS_BaseStationReport.cs" />
<Compile Include="AIS_ClassB.cs" />
@ -107,6 +109,7 @@
<Compile Include="AIS_Target.cs" />
<Compile Include="IAISThread.cs" />
<Compile Include="Lookup.cs" />
<Compile Include="MonitorZone.cs" />
<Compile Include="NMEA.cs" />
<Compile Include="NMEA_AIS_Sentence.cs" />
<Compile Include="NMEA_PNMLS_Sentence.cs" />