From bc19b63d95c3ec002c6e55a2f5995474d7d81725 Mon Sep 17 00:00:00 2001 From: Daniel Schick Date: Fri, 16 Dec 2022 08:53:51 +0100 Subject: [PATCH] =?UTF-8?q?Alarm=20Tabelle=20hinzugef=C3=BCgt,=20beim=20La?= =?UTF-8?q?den=20und=20Speichern=20angefangen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AIS/SQL/ais_initial.db | Bin 36864 -> 40960 bytes AIS/SQL/ais_initial.db.sql | 2 +- AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs | 69 ++++++++++++++++++- AIS/bsmd.AIS2Service/MonitorZone.cs | 34 +++++++-- AIS/bsmd.AIS2Service/Util.cs | 29 ++++++++ AIS/bsmd.AIS2Service/bsmd.AIS2Service.csproj | 1 + 6 files changed, 127 insertions(+), 8 deletions(-) create mode 100644 AIS/bsmd.AIS2Service/Util.cs diff --git a/AIS/SQL/ais_initial.db b/AIS/SQL/ais_initial.db index b3fca5d5b1be1e6a8e11278fcaf1281dc1a83671..844e6a3034f10d675b9c0041d3e2a29142488f41 100644 GIT binary patch delta 291 zcmZozz|?SnX@az%2m=EH7Z9@pF(VM0Ow=(}6k*VFU%|^+!obOPjG1pXpC?Zh_jHcy z?6vZhi4kWBqYl^#MpP@B1V!1!#l;z0AmWKR ziAA|!f@|_!wr*rI+&AA~*OB02=D)0=xNz#WV QuvtOi0sq7a3X2>J0RK%`JOBUy delta 177 zcmZoTz|^pSX@az%FarYvClJGc;Y1x{d0_@U_Z7UHB@CQwhne|i^Lg@AaZl&C&VGw) z5@!Y5;mv{qHf+kAg^UW~`udE`O(G)f;^N|rE$k(UNja&BIf+HNV1j+}1Ga8 /// 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 @@ +