ZONE MONITORING WIP2

This commit is contained in:
Daniel Schick 2022-10-25 12:06:19 +02:00
parent 62a5f6beec
commit 6712b17096
6 changed files with 181 additions and 10 deletions

Binary file not shown.

View File

@ -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) Anmeldeinformationen auf 192.168.2.3 (SMWET01)
user id "shipinfo" PW "baffi123" DB "schiffsmeldedienst" user id "shipinfo" PW "baffi123" DB "schiffsmeldedienst"

View File

@ -40,7 +40,7 @@ namespace bsmd.AIS2Service
_tasks.Add(new SitRep(_decodedClasses, _sitRepList, _dbSaveTargets)); _tasks.Add(new SitRep(_decodedClasses, _sitRepList, _dbSaveTargets));
AIS_SQLiteStorage sqliteStorage = new AIS_SQLiteStorage(_dbSaveTargets); AIS_SQLiteStorage sqliteStorage = new AIS_SQLiteStorage(_dbSaveTargets);
_tasks.Add(sqliteStorage); _tasks.Add(sqliteStorage);
_tasks.Add(new AISZoneMonitor(_sitRepList)); _tasks.Add(new AISZoneMonitor(_sitRepList, sqliteStorage));
// preload sit rep // preload sit rep
Dictionary<int, AIS_Target> targets = await sqliteStorage.LoadTargets(); Dictionary<int, AIS_Target> targets = await sqliteStorage.LoadTargets();

View File

@ -20,6 +20,7 @@ namespace bsmd.AIS2Service
#region Fields #region Fields
ConcurrentDictionary<int, AIS_Target> _sitRepDict; ConcurrentDictionary<int, AIS_Target> _sitRepDict;
AIS_SQLiteStorage _storage;
private Thread _thread; private Thread _thread;
private bool _stopFlag = false; private bool _stopFlag = false;
@ -27,9 +28,10 @@ namespace bsmd.AIS2Service
#region Construction #region Construction
public AISZoneMonitor(ConcurrentDictionary<int, AIS_Target> sitRepDict) public AISZoneMonitor(ConcurrentDictionary<int, AIS_Target> sitRepDict, AIS_SQLiteStorage sqliteStorage)
{ {
_sitRepDict = sitRepDict; _sitRepDict = sitRepDict;
_storage = sqliteStorage;
} }
#endregion #endregion

View File

@ -37,7 +37,83 @@ namespace bsmd.AIS2Service
_connection.Open(); _connection.Open();
} }
#endregion #endregion
#region public methods
public List<MonitorZone> LoadMonitorZones()
{
List<MonitorZone> monitorZones = new List<MonitorZone>();
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 #region private methods

View File

@ -12,15 +12,46 @@ using System.Threading.Tasks;
namespace bsmd.AIS2Service namespace bsmd.AIS2Service
{ {
#region class MonitorZone
internal class MonitorZone internal class MonitorZone
{ {
private List<GeoPoint> _vertices;
public MonitorZone(List<GeoPoint> vertices) #region fields
private int _id; // PK from database
private List<GeoPoint> _vertices = new List<GeoPoint>();
private List<MonitorAssignment> _assignments = new List<MonitorAssignment>();
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<GeoPoint> Vertices { get { return _vertices; } }
public List<MonitorAssignment> Assignments { get { return _assignments; } }
public bool Active { get; set; }
#endregion
#region public methods
public bool IsPointInPolygon4(GeoPoint testPoint) public bool IsPointInPolygon4(GeoPoint testPoint)
{ {
bool result = false; bool result = false;
@ -38,17 +69,62 @@ namespace bsmd.AIS2Service
} }
return result; return result;
} }
#endregion
} }
#endregion
#region class GeoPoint
internal 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 int Id { get { return _id; } }
public double Lon { get; private set; }
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
} }