From 6712b17096e8c7814d153774db9171e1638738d2 Mon Sep 17 00:00:00 2001 From: Daniel Schick Date: Tue, 25 Oct 2022 12:06:19 +0200 Subject: [PATCH] ZONE MONITORING WIP2 --- AIS/SQL/ais_initial.db | Bin 20480 -> 32768 bytes AIS/SQL/readme.txt | 17 ++++ AIS/bsmd.AIS2Service/AISManager.cs | 2 +- AIS/bsmd.AIS2Service/AISZoneMonitor.cs | 4 +- AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs | 78 ++++++++++++++++++- AIS/bsmd.AIS2Service/MonitorZone.cs | 90 ++++++++++++++++++++-- 6 files changed, 181 insertions(+), 10 deletions(-) diff --git a/AIS/SQL/ais_initial.db b/AIS/SQL/ais_initial.db index a1c3c778e7b8713db6397001e224bb86a693c64f..2d4f6cf0e56f0a5dc355a718458aee451afc7344 100644 GIT binary patch delta 843 zcmZozz}V2hG(lRBmw|zS1Bh9Hm=TDTCh8c=@-pZdSb=2NISU#1X7hRSRB=z|a^fuH z`p0o{W1|8G2jdEHeSOBJ@X4#WRO`9^F{}^=5{p4%jrk>sNja&-g*i#_C8@au@nCMe zDTV-tbC9cJh^s<~qmz%Tf>LgNUS>&tQG8W?UaFFU1{bGNW{MK0r(cMxyK9hwpMQvg zU#O3dLO_tGuVYZ8g12j=f@5fizo(ybeUPiKt6zu?P)lB7ZmJSzh-(CxmzZ3VSq9`o z4dK#k^y6k1*Vbli3I;n0WL11(adBpPUT$h$2^yCj)qQBPNKVuw!i^9oPJYKHSC8z` zcx-+Id8DMW0L9^!9PHxC%8X5x2#1%Y7L}w{fSmww7^6iV^K~9J@*y7KpqSCbOyfJe4ad2+|xG;3g~lj zFs~5T*Jo@ioVyeRY9q^FefR#BsI4nz9cazCpF$^@;08O To2PTH_#?kq!9V~2-me~n diff --git a/AIS/SQL/readme.txt b/AIS/SQL/readme.txt index d65ebc37..cce0d791 100644 --- a/AIS/SQL/readme.txt +++ b/AIS/SQL/readme.txt @@ -1,3 +1,20 @@ +# AIS Datenhaltung + +## Dateien + +### Neuer Stand (AIS2) + +Initiale, "leere" SQLite Datenbank: +ais_initial.db + +### Alter Stand (Portierung der MySQL basierten Version von DP07) + +Create Skripte für SQL Server: +create_AIS_DB.sql +CreateTablels.sql + +## Übertragung AIS Positionen nach WETRIS + Anmeldeinformationen auf 192.168.2.3 (SMWET01) user id "shipinfo" PW "baffi123" DB "schiffsmeldedienst" diff --git a/AIS/bsmd.AIS2Service/AISManager.cs b/AIS/bsmd.AIS2Service/AISManager.cs index f4a9ec18..ce040ef8 100644 --- a/AIS/bsmd.AIS2Service/AISManager.cs +++ b/AIS/bsmd.AIS2Service/AISManager.cs @@ -40,7 +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)); + _tasks.Add(new AISZoneMonitor(_sitRepList, sqliteStorage)); // preload sit rep Dictionary targets = await sqliteStorage.LoadTargets(); diff --git a/AIS/bsmd.AIS2Service/AISZoneMonitor.cs b/AIS/bsmd.AIS2Service/AISZoneMonitor.cs index 57098963..376a3603 100644 --- a/AIS/bsmd.AIS2Service/AISZoneMonitor.cs +++ b/AIS/bsmd.AIS2Service/AISZoneMonitor.cs @@ -20,6 +20,7 @@ namespace bsmd.AIS2Service #region Fields ConcurrentDictionary _sitRepDict; + AIS_SQLiteStorage _storage; private Thread _thread; private bool _stopFlag = false; @@ -27,9 +28,10 @@ namespace bsmd.AIS2Service #region Construction - public AISZoneMonitor(ConcurrentDictionary sitRepDict) + public AISZoneMonitor(ConcurrentDictionary sitRepDict, AIS_SQLiteStorage sqliteStorage) { _sitRepDict = sitRepDict; + _storage = sqliteStorage; } #endregion diff --git a/AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs b/AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs index 707f8805..c9074636 100644 --- a/AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs +++ b/AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs @@ -37,7 +37,83 @@ namespace bsmd.AIS2Service _connection.Open(); } - #endregion + #endregion + + #region public methods + + public List LoadMonitorZones() + { + List monitorZones = new List(); + if ((_connection == null) || (_connection.State != ConnectionState.Open)) return monitorZones; // can't load but return nothing in a friendly way + + // load zones + string loadZoneString = "SELECT id, name, active FROM monitor_zone"; + SQLiteCommand lzCmd = new SQLiteCommand(loadZoneString, _connection); + SQLiteDataReader reader = lzCmd.ExecuteReader(); + if(reader.HasRows) + { + while(reader.Read()) + { + int id = reader.GetInt32(0); + string name = reader.GetString(1); + MonitorZone mz = new MonitorZone(id, name); + mz.Active = reader.GetInt32(2) == 1; + monitorZones.Add(mz); + } + } + reader.Close(); + lzCmd.Dispose(); + + // load vertices for each zone + string loadVertexString = "SELECT Id, latitude, longitude FROM zone_vertex WHERE monitor_zone_id = @ID"; + SQLiteCommand lvCmd = new SQLiteCommand(loadVertexString, _connection); + foreach(MonitorZone mz in monitorZones) + { + lvCmd.Parameters.Clear(); + lvCmd.Parameters.AddWithValue("@ID", mz.Id); + SQLiteDataReader lvReader = lvCmd.ExecuteReader(); + if(reader.HasRows) + { + while(reader.Read()) + { + int id = reader.GetInt32(0); + GeoPoint gp = new GeoPoint(id); + gp.Lat = reader.GetDouble(1); + gp.Lon = reader.GetDouble(2); + mz.Vertices.Add(gp); + } + } + lvReader.Close(); + } + lvCmd.Dispose(); + + // load mmsi / zone assignments for each zone + string loadAssignmentsString = "SELECT id, mmsi, type FROM zone_assignment WHERE monitor_zone_id = @ID"; + SQLiteCommand laCmd = new SQLiteCommand(loadAssignmentsString, _connection); + foreach (MonitorZone mz in monitorZones) + { + lvCmd.Parameters.Clear(); + lvCmd.Parameters.AddWithValue("@ID", mz.Id); + SQLiteDataReader lvReader = laCmd.ExecuteReader(); + if (reader.HasRows) + { + while (reader.Read()) + { + int id = reader.GetInt32(0); + MonitorAssignment ma = new MonitorAssignment(id); + ma.MMSI = reader.GetInt32(1); + ma.MonitorType = (MonitorAssignment.ZoneMonitorType)reader.GetInt32(2); + mz.Assignments.Add(ma); + } + } + lvReader.Close(); + } + laCmd.Dispose(); + + return monitorZones; + } + + #endregion #region private methods diff --git a/AIS/bsmd.AIS2Service/MonitorZone.cs b/AIS/bsmd.AIS2Service/MonitorZone.cs index f78cb562..17568a21 100644 --- a/AIS/bsmd.AIS2Service/MonitorZone.cs +++ b/AIS/bsmd.AIS2Service/MonitorZone.cs @@ -12,15 +12,46 @@ using System.Threading.Tasks; namespace bsmd.AIS2Service { + + #region class MonitorZone + internal class MonitorZone { - private List _vertices; - public MonitorZone(List vertices) + #region fields + + private int _id; // PK from database + private List _vertices = new List(); + private List _assignments = new List(); + private string _name; + + #endregion + + #region Construction + + public MonitorZone(int id, string name) { - _vertices = vertices; + _id = id; _name = name; } + #endregion + + #region Properties + + public int Id { get { return _id; } } + + public string Name { get { return _name; } } + + public List Vertices { get { return _vertices; } } + + public List Assignments { get { return _assignments; } } + + public bool Active { get; set; } + + #endregion + + #region public methods + public bool IsPointInPolygon4(GeoPoint testPoint) { bool result = false; @@ -38,17 +69,62 @@ namespace bsmd.AIS2Service } return result; } + + #endregion + } + #endregion + + #region class GeoPoint + internal class GeoPoint { - public GeoPoint(double lat, double lon) + + private int _id; + + public GeoPoint(int id) { - Lat = lat; Lon = lon; + _id = id; } - public double Lat { get; private set; } - public double Lon { get; private set; } + public int Id { get { return _id; } } + + public double Lat { get; set; } + public double Lon { get; set; } } + #endregion + + #region class MonitorAssignment + + internal class MonitorAssignment + { + + private int _id; + + public MonitorAssignment(int id) + { + _id = id; + } + + public enum ZoneMonitorType + { + INACTIVE, + ENTER, + EXIT, + PASSTHROUGH, // outside - enter - inside - exit - outside + LEAVE_AND_RETURN // inside - exit - outside - enter - insider + } + + public int Id { get { return _id; } } + + public int MMSI { get; set; } + + public ZoneMonitorType MonitorType { get; set; } = ZoneMonitorType.INACTIVE; + + } + + #endregion + }