diff --git a/AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs b/AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs
index 0cfb94a1..df5a93a6 100644
--- a/AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs
+++ b/AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs
@@ -8,7 +8,6 @@ using System.Threading;
using System.Data.SQLite;
using log4net;
using System.Collections.Concurrent;
-using System.Runtime.Remoting.Messaging;
namespace bsmd.AIS2Service
{
@@ -43,6 +42,8 @@ namespace bsmd.AIS2Service
#region public methods
+ #region MonitorZone
+
///
/// monitor zone loader func for the zone alarm watchdog (doesn't need groups or such)
///
@@ -53,7 +54,7 @@ namespace bsmd.AIS2Service
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";
+ string loadZoneString = "SELECT id, name, active, monitor_group_id, sequence FROM monitor_zone";
SQLiteCommand lzCmd = new SQLiteCommand(loadZoneString, _connection);
SQLiteDataReader reader = lzCmd.ExecuteReader();
if(reader.HasRows)
@@ -64,6 +65,8 @@ namespace bsmd.AIS2Service
string name = reader.GetString(1);
MonitorZone mz = new MonitorZone(id, name);
mz.Active = reader.GetInt32(2) == 1;
+ mz.MonitorGroupId = reader.GetInt32(3);
+ mz.Sequence = reader.GetInt32(4);
monitorZones.Add(mz);
}
}
@@ -119,6 +122,42 @@ namespace bsmd.AIS2Service
return monitorZones;
}
+ bool Save(MonitorZone mZone)
+ {
+ if (mZone == null) return false;
+
+ if(mZone.Id <= 0)
+ {
+ // insert
+ string insertZoneString = $"INSERT INTO monitor_zone (name, active, monitor_group_id, sequence) VALUES ('{mZone.Name}', {mZone.Active}, {mZone.MonitorGroupId}, {mZone.Sequence}";
+ SQLiteCommand cmd = new SQLiteCommand(insertZoneString, _connection);
+ int insertedRows = cmd.ExecuteNonQuery();
+ cmd.Dispose();
+ mZone.Id = GetLastInsertId();
+ return insertedRows == 1;
+ }
+ else
+ {
+ // update
+ string updateZoneString = $"UPDATE monitor_zone SET name = '{mZone.Name}', active = '{mZone.Active}', sequence = '{mZone.Sequence}' WHERE id = {mZone.Id}'";
+ SQLiteCommand cmd = new SQLiteCommand(updateZoneString, _connection);
+ int updatedRows = cmd.ExecuteNonQuery();
+ cmd.Dispose();
+ return updatedRows == 1;
+ }
+ }
+
+ bool Delete(MonitorZone mZone)
+ {
+ if (mZone == null) return false;
+ string deleteAlarmString = $"DELETE FROM monitor_zone WHERE id = {mZone.Id}";
+ SQLiteCommand cmd = new SQLiteCommand(deleteAlarmString, _connection);
+ int affectedRows = cmd.ExecuteNonQuery();
+ return (affectedRows == 1);
+ }
+
+ #endregion
+
#region Alarm
public List LoadAlarms(MonitorAssignment assignment)
@@ -161,22 +200,99 @@ namespace bsmd.AIS2Service
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})";
+ 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();
+ cmd.Dispose();
+ alarm.Id = GetLastInsertId();
return (insertedRows == 1);
}
else
{
// update
- // TODO
-
- return false;
+ string updateAlarmString = $"UPDATE alarm SET acknowledged = {alarm.Acknowledged} WHERE id = {alarm.Id}";
+ SQLiteCommand cmd = new SQLiteCommand(updateAlarmString, _connection);
+ int updatedRows = cmd.ExecuteNonQuery();
+ cmd.Dispose();
+ return (updatedRows == 1);
}
}
#endregion
+ #region MonitorGroup
+
+ public List LoadGroups()
+ {
+ List groups = new List();
+ string loadGroupsString = "SELECT id, name from monitor_group ORDER BY name";
+ SQLiteCommand laCmd = new SQLiteCommand(loadGroupsString, _connection);
+ SQLiteDataReader reader = laCmd.ExecuteReader();
+ if (reader.HasRows)
+ {
+ while (reader.Read())
+ {
+ int id = reader.GetInt32(0);
+ string name = "";
+ if(!reader.IsDBNull(1))
+ name = reader.GetString(1);
+ MonitorGroup mGroup = new MonitorGroup(id, name);
+ groups.Add(mGroup);
+ }
+ }
+ return groups;
+ }
+
+ public bool Delete(MonitorGroup mGroup)
+ {
+ if (mGroup == null) return false;
+ string deleteGroupString = $"DELETE FROM monitor_group WHERE id = {mGroup.Id}";
+ SQLiteCommand cmd = new SQLiteCommand(deleteGroupString, _connection);
+ int deletedGroups = cmd.ExecuteNonQuery();
+ cmd.Dispose();
+ return deletedGroups == 1;
+ }
+
+ public bool Save(MonitorGroup mGroup)
+ {
+ if(mGroup == null) return false;
+ if(mGroup.Id <= 0)
+ {
+ // insert
+ string saveGroupString = $"INSERT INTO monitor_group (name) VALUES ('{mGroup.Name}')";
+ SQLiteCommand cmd = new SQLiteCommand(saveGroupString, _connection);
+ int insertedRows = cmd.ExecuteNonQuery();
+ cmd.Dispose();
+ mGroup.Id = GetLastInsertId();
+ return (insertedRows == 1);
+ }
+ else
+ {
+ // update
+ string saveGroupString = $"UPDATE monitor_group SET name = '{mGroup.Name}' WHERE id = {mGroup.Id}";
+ SQLiteCommand cmd = new SQLiteCommand(saveGroupString, _connection);
+ int updatedRows = cmd.ExecuteNonQuery();
+ cmd.Dispose();
+ return (updatedRows == 1);
+ }
+ }
+
+ #endregion
+
+ #region ZoneVertex
+
+ public bool Save(GeoPoint geoPoint)
+ {
+ if (geoPoint == null) return false;
+ string saveGeoPointString = $"INSERT INTO zone_vertex (monitor_zone_id, latitude, longitude) VALUES ({geoPoint.MonitorZoneId}, {geoPoint.Lat}, {geoPoint.Lon})";
+ SQLiteCommand cmd = new SQLiteCommand(saveGeoPointString, _connection);
+ int insertedRows = cmd.ExecuteNonQuery();
+ cmd.Dispose();
+ return insertedRows == 1;
+ }
+
+ #endregion
+
#endregion
#region private methods
@@ -388,6 +504,14 @@ namespace bsmd.AIS2Service
}
}
+ public int GetLastInsertId()
+ {
+ string sql = "SELECT last_insert_rowid()";
+ SQLiteCommand cmd = new SQLiteCommand(sql, _connection);
+ int lastID = (Int32)cmd.ExecuteScalar();
+ return lastID;
+ }
+
#endregion
#region IAISThread implementation
diff --git a/AIS/bsmd.AIS2Service/MonitorZone.cs b/AIS/bsmd.AIS2Service/MonitorZone.cs
index 2cf6d456..b28437e2 100644
--- a/AIS/bsmd.AIS2Service/MonitorZone.cs
+++ b/AIS/bsmd.AIS2Service/MonitorZone.cs
@@ -13,13 +13,29 @@ using System.Threading.Tasks;
namespace bsmd.AIS2Service
{
+ #region DBEntity
+
+ public class DBEntity
+ {
+ protected int _id; // PK from database
+
+ public DBEntity(int id)
+ {
+ _id = id;
+ }
+
+ public int Id { get { return _id; } set { _id = value; } }
+
+ }
+
+ #endregion
+
#region class MonitorGroup
- public class MonitorGroup
+ public class MonitorGroup : DBEntity
{
#region fields
-
- private readonly int _id; // PK from database
+
private readonly string _name;
private readonly List _zones = new List();
@@ -27,18 +43,16 @@ namespace bsmd.AIS2Service
#region Construction
- public MonitorGroup(int id, string name)
+ public MonitorGroup(int id, string name) : base( id )
{
- _id = id; _name = name;
+ _name = name;
}
#endregion
#region Properties
- public List Zones { get { return _zones; } }
-
- public int Id { get { return _id; } }
+ public List Zones { get { return _zones; } }
public string Name { get { return _name; } }
@@ -49,12 +63,11 @@ namespace bsmd.AIS2Service
#region class MonitorZone
- public class MonitorZone
+ public class MonitorZone : DBEntity
{
#region fields
-
- private readonly int _id; // PK from database
+
private readonly List _vertices = new List();
private readonly List _assignments = new List();
private readonly string _name;
@@ -63,16 +76,14 @@ namespace bsmd.AIS2Service
#region Construction
- public MonitorZone(int id, string name)
+ public MonitorZone(int id, string name) : base (id)
{
- _id = id; _name = name;
+ _name = name;
}
#endregion
- #region Properties
-
- public int Id { get { return _id; } }
+ #region Properties
public string Name { get { return _name; } }
@@ -84,6 +95,8 @@ namespace bsmd.AIS2Service
public int Sequence { get; set; }
+ public int MonitorGroupId { get; set; }
+
#endregion
#region public methods
@@ -114,35 +127,27 @@ namespace bsmd.AIS2Service
#region class GeoPoint
- public class GeoPoint
- {
+ public class GeoPoint : DBEntity
+ {
- private readonly int _id;
-
- public GeoPoint(int id)
- {
- _id = id;
- }
-
- public int Id { get { return _id; } }
+ public GeoPoint(int id) : base (id)
+ {}
public double Lat { get; set; }
public double Lon { get; set; }
+
+ public int MonitorZoneId { get; set; }
}
#endregion
#region class MonitorAssignment
- public class MonitorAssignment
- {
+ public class MonitorAssignment : DBEntity
+ {
- private readonly int _id;
-
- public MonitorAssignment(int id)
- {
- _id = id;
- }
+ public MonitorAssignment(int id) : base(id)
+ {}
[Flags]
public enum ZoneMonitorType
@@ -166,14 +171,11 @@ namespace bsmd.AIS2Service
#region class Alarm
- public class Alarm
- {
- private readonly int _id;
+ public class Alarm : DBEntity
+ {
private readonly MonitorAssignment _assignment;
- public Alarm(int id, MonitorAssignment assignment) { _id = id; _assignment = assignment; }
-
- public int Id { get { return _id; } }
+ public Alarm(int id, MonitorAssignment assignment) : base(id) { _assignment = assignment; }
public MonitorAssignment Assignment { get { return _assignment; } }