diff --git a/AIS/SQL/ais_initial.db b/AIS/SQL/ais_initial.db index b3fca5d5..844e6a30 100644 Binary files a/AIS/SQL/ais_initial.db and b/AIS/SQL/ais_initial.db differ diff --git a/AIS/SQL/ais_initial.db.sql b/AIS/SQL/ais_initial.db.sql index aa9271f1..17b3c11d 100644 --- a/AIS/SQL/ais_initial.db.sql +++ b/AIS/SQL/ais_initial.db.sql @@ -75,7 +75,7 @@ CREATE TABLE IF NOT EXISTS "alarm" ( "id" INTEGER NOT NULL, "zone_assignment_id" INTEGER NOT NULL, "timestamp" TEXT, - "type" INTEGER, + "type" TEXT, "acknowledged" INTEGER, FOREIGN KEY("zone_assignment_id") REFERENCES "zone_assignment"("id"), PRIMARY KEY("id" AUTOINCREMENT) diff --git a/AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs b/AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs index c9074636..0cfb94a1 100644 --- a/AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs +++ b/AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs @@ -8,12 +8,14 @@ using System.Threading; using System.Data.SQLite; using log4net; using System.Collections.Concurrent; +using System.Runtime.Remoting.Messaging; namespace bsmd.AIS2Service { /// /// This class handles the (short-time) storage of AIS targets with a limited /// past track. It is just intended to function as a "saving the state" of the AIS situation. + /// Attention: Alarm zones / alarms are also stored here. This might or might not be such a great idea. /// internal class AIS_SQLiteStorage : IAISThread { @@ -41,6 +43,10 @@ namespace bsmd.AIS2Service #region public methods + /// + /// monitor zone loader func for the zone alarm watchdog (doesn't need groups or such) + /// + /// public List LoadMonitorZones() { List monitorZones = new List(); @@ -113,6 +119,64 @@ namespace bsmd.AIS2Service return monitorZones; } + #region Alarm + + public List LoadAlarms(MonitorAssignment assignment) + { + List result = new List(); + string loadAlarmString = "SELECT id, timestamp, type, acknowledged FROM alarm WHERE zone_assignment_id = @ID"; + SQLiteCommand laCmd = new SQLiteCommand(loadAlarmString, _connection); + laCmd.Parameters.AddWithValue("@ID", assignment.Id); + SQLiteDataReader reader = laCmd.ExecuteReader(); + if (reader.HasRows) + { + while (reader.Read()) + { + int id = reader.GetInt32(0); + Alarm alarm = new Alarm(id, assignment); + alarm.Timestamp = reader.GetDateTime(1); + alarm.ZoneMonitorType = (MonitorAssignment.ZoneMonitorType)reader.GetInt32(2); + if(!reader.IsDBNull(3)) + alarm.Acknowledged = reader.GetDateTime(3); + result.Add(alarm); + } + reader.Close(); + } + laCmd.Dispose(); + return result; + } + + public bool Delete(Alarm alarm) + { + if (alarm == null) return false; + string deleteAlarmString = $"DELETE FROM alarm WHERE id = {alarm.Id}"; + SQLiteCommand cmd = new SQLiteCommand(deleteAlarmString, _connection); + int affectedRows = cmd.ExecuteNonQuery(); + return (affectedRows == 1); + } + + public bool Save(Alarm alarm) + { + if (alarm == null) return false; + if (alarm.Id <= 0) + { + // insert + string saveAlarmString = $"INSERT INTO alarm (zone_assignment_id, timestamp, type, acknowledged) VALUES ({alarm.Assignment.Id}, {alarm.Timestamp}, {alarm.ZoneMonitorType}, {alarm.Acknowledged})"; + SQLiteCommand cmd = new SQLiteCommand(saveAlarmString, _connection); + int insertedRows = cmd.ExecuteNonQuery(); + return (insertedRows == 1); + } + else + { + // update + // TODO + + return false; + } + } + + #endregion + #endregion #region private methods @@ -334,7 +398,8 @@ namespace bsmd.AIS2Service public void Start() { - if (_thread != null) return; // may not run twice + if (_thread != null) return; // may not run twice + if (_inputQueue == null) return; // run thread only if we have a queue ThreadStart runReader = new ThreadStart(this.ReadQueue); _thread = new Thread(runReader); _thread.Start(); @@ -343,7 +408,7 @@ namespace bsmd.AIS2Service public void Stop() { if (_thread == null) return; - _stopFlag = true; + _stopFlag = true; _thread.Join(); _thread = null; _connection.Close(); diff --git a/AIS/bsmd.AIS2Service/MonitorZone.cs b/AIS/bsmd.AIS2Service/MonitorZone.cs index 6767ca67..2cf6d456 100644 --- a/AIS/bsmd.AIS2Service/MonitorZone.cs +++ b/AIS/bsmd.AIS2Service/MonitorZone.cs @@ -144,13 +144,14 @@ namespace bsmd.AIS2Service _id = id; } + [Flags] public enum ZoneMonitorType { - INACTIVE, - ENTER, - EXIT, - PASSTHROUGH, // outside - enter - inside - exit - outside - LEAVE_AND_RETURN // inside - exit - outside - enter - insider + INACTIVE = 0, + ENTER = 1, + EXIT = 2, + PASSTHROUGH = 4, // outside - enter - inside - exit - outside + LEAVE_AND_RETURN = 8 // inside - exit - outside - enter - inside } public int Id { get { return _id; } } @@ -163,4 +164,27 @@ namespace bsmd.AIS2Service #endregion + #region class Alarm + + public class Alarm + { + private readonly int _id; + private readonly MonitorAssignment _assignment; + + public Alarm(int id, MonitorAssignment assignment) { _id = id; _assignment = assignment; } + + public int Id { get { return _id; } } + + public MonitorAssignment Assignment { get { return _assignment; } } + + public DateTime Timestamp { get; set; } + + public DateTime? Acknowledged { get; set; } + + public MonitorAssignment.ZoneMonitorType ZoneMonitorType { get; set; } + + } + + #endregion + } diff --git a/AIS/bsmd.AIS2Service/Util.cs b/AIS/bsmd.AIS2Service/Util.cs new file mode 100644 index 00000000..ee98981a --- /dev/null +++ b/AIS/bsmd.AIS2Service/Util.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace bsmd.AIS2Service +{ + public static class Util + { + /// + /// To test if flag is set please use framework method Enum.HasFlag() + /// + /// + /// + /// + /// + public static void Set(ref T flags, T flag, bool value) where T : struct + { + int flagsValue = (int)(object)flags; + int flagValue = (int)(object)flag; + + if(value) + flags = (T)(object)(flagsValue | flagValue); + else + flags = (T)(object)(flagsValue & (~flagValue)); + } + } +} diff --git a/AIS/bsmd.AIS2Service/bsmd.AIS2Service.csproj b/AIS/bsmd.AIS2Service/bsmd.AIS2Service.csproj index 98fee541..8506f25b 100644 --- a/AIS/bsmd.AIS2Service/bsmd.AIS2Service.csproj +++ b/AIS/bsmd.AIS2Service/bsmd.AIS2Service.csproj @@ -129,6 +129,7 @@ +