Alarm Tabelle hinzugefügt, beim Laden und Speichern angefangen
This commit is contained in:
parent
17d7aa2538
commit
bc19b63d95
Binary file not shown.
@ -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)
|
||||
|
||||
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
internal class AIS_SQLiteStorage : IAISThread
|
||||
{
|
||||
@ -41,6 +43,10 @@ namespace bsmd.AIS2Service
|
||||
|
||||
#region public methods
|
||||
|
||||
/// <summary>
|
||||
/// monitor zone loader func for the zone alarm watchdog (doesn't need groups or such)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public List<MonitorZone> LoadMonitorZones()
|
||||
{
|
||||
List<MonitorZone> monitorZones = new List<MonitorZone>();
|
||||
@ -113,6 +119,64 @@ namespace bsmd.AIS2Service
|
||||
return monitorZones;
|
||||
}
|
||||
|
||||
#region Alarm
|
||||
|
||||
public List<Alarm> LoadAlarms(MonitorAssignment assignment)
|
||||
{
|
||||
List<Alarm> result = new List<Alarm>();
|
||||
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();
|
||||
|
||||
@ -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
|
||||
|
||||
}
|
||||
|
||||
29
AIS/bsmd.AIS2Service/Util.cs
Normal file
29
AIS/bsmd.AIS2Service/Util.cs
Normal file
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// To test if flag is set please use framework method Enum.HasFlag()
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="flags"></param>
|
||||
/// <param name="flag"></param>
|
||||
/// <param name="value"></param>
|
||||
public static void Set<T>(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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -129,6 +129,7 @@
|
||||
<Compile Include="SerialTCPReader.cs" />
|
||||
<Compile Include="SitRep.cs" />
|
||||
<Compile Include="StartupWebAPI.cs" />
|
||||
<Compile Include="Util.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="..\SQL\ais_initial.db">
|
||||
|
||||
Loading…
Reference in New Issue
Block a user