diff --git a/AIS/SQL/ais_initial.db b/AIS/SQL/ais_initial.db index a1c3c778..2d4f6cf0 100644 Binary files a/AIS/SQL/ais_initial.db and b/AIS/SQL/ais_initial.db differ 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 + }