Speicherung etc. fortlaufend
This commit is contained in:
parent
bc19b63d95
commit
02d347b592
@ -8,7 +8,6 @@ using System.Threading;
|
|||||||
using System.Data.SQLite;
|
using System.Data.SQLite;
|
||||||
using log4net;
|
using log4net;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Runtime.Remoting.Messaging;
|
|
||||||
|
|
||||||
namespace bsmd.AIS2Service
|
namespace bsmd.AIS2Service
|
||||||
{
|
{
|
||||||
@ -43,6 +42,8 @@ namespace bsmd.AIS2Service
|
|||||||
|
|
||||||
#region public methods
|
#region public methods
|
||||||
|
|
||||||
|
#region MonitorZone
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// monitor zone loader func for the zone alarm watchdog (doesn't need groups or such)
|
/// monitor zone loader func for the zone alarm watchdog (doesn't need groups or such)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -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
|
if ((_connection == null) || (_connection.State != ConnectionState.Open)) return monitorZones; // can't load but return nothing in a friendly way
|
||||||
|
|
||||||
// load zones
|
// 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);
|
SQLiteCommand lzCmd = new SQLiteCommand(loadZoneString, _connection);
|
||||||
SQLiteDataReader reader = lzCmd.ExecuteReader();
|
SQLiteDataReader reader = lzCmd.ExecuteReader();
|
||||||
if(reader.HasRows)
|
if(reader.HasRows)
|
||||||
@ -64,6 +65,8 @@ namespace bsmd.AIS2Service
|
|||||||
string name = reader.GetString(1);
|
string name = reader.GetString(1);
|
||||||
MonitorZone mz = new MonitorZone(id, name);
|
MonitorZone mz = new MonitorZone(id, name);
|
||||||
mz.Active = reader.GetInt32(2) == 1;
|
mz.Active = reader.GetInt32(2) == 1;
|
||||||
|
mz.MonitorGroupId = reader.GetInt32(3);
|
||||||
|
mz.Sequence = reader.GetInt32(4);
|
||||||
monitorZones.Add(mz);
|
monitorZones.Add(mz);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -119,6 +122,42 @@ namespace bsmd.AIS2Service
|
|||||||
return monitorZones;
|
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
|
#region Alarm
|
||||||
|
|
||||||
public List<Alarm> LoadAlarms(MonitorAssignment assignment)
|
public List<Alarm> LoadAlarms(MonitorAssignment assignment)
|
||||||
@ -161,22 +200,99 @@ namespace bsmd.AIS2Service
|
|||||||
if (alarm.Id <= 0)
|
if (alarm.Id <= 0)
|
||||||
{
|
{
|
||||||
// insert
|
// 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);
|
SQLiteCommand cmd = new SQLiteCommand(saveAlarmString, _connection);
|
||||||
int insertedRows = cmd.ExecuteNonQuery();
|
int insertedRows = cmd.ExecuteNonQuery();
|
||||||
|
cmd.Dispose();
|
||||||
|
alarm.Id = GetLastInsertId();
|
||||||
return (insertedRows == 1);
|
return (insertedRows == 1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// update
|
// update
|
||||||
// TODO
|
string updateAlarmString = $"UPDATE alarm SET acknowledged = {alarm.Acknowledged} WHERE id = {alarm.Id}";
|
||||||
|
SQLiteCommand cmd = new SQLiteCommand(updateAlarmString, _connection);
|
||||||
return false;
|
int updatedRows = cmd.ExecuteNonQuery();
|
||||||
|
cmd.Dispose();
|
||||||
|
return (updatedRows == 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region MonitorGroup
|
||||||
|
|
||||||
|
public List<MonitorGroup> LoadGroups()
|
||||||
|
{
|
||||||
|
List<MonitorGroup> groups = new List<MonitorGroup>();
|
||||||
|
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
|
#endregion
|
||||||
|
|
||||||
#region private methods
|
#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
|
#endregion
|
||||||
|
|
||||||
#region IAISThread implementation
|
#region IAISThread implementation
|
||||||
|
|||||||
@ -13,13 +13,29 @@ using System.Threading.Tasks;
|
|||||||
namespace bsmd.AIS2Service
|
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
|
#region class MonitorGroup
|
||||||
|
|
||||||
public class MonitorGroup
|
public class MonitorGroup : DBEntity
|
||||||
{
|
{
|
||||||
#region fields
|
#region fields
|
||||||
|
|
||||||
private readonly int _id; // PK from database
|
|
||||||
private readonly string _name;
|
private readonly string _name;
|
||||||
private readonly List<MonitorZone> _zones = new List<MonitorZone>();
|
private readonly List<MonitorZone> _zones = new List<MonitorZone>();
|
||||||
|
|
||||||
@ -27,9 +43,9 @@ namespace bsmd.AIS2Service
|
|||||||
|
|
||||||
#region Construction
|
#region Construction
|
||||||
|
|
||||||
public MonitorGroup(int id, string name)
|
public MonitorGroup(int id, string name) : base( id )
|
||||||
{
|
{
|
||||||
_id = id; _name = name;
|
_name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -38,8 +54,6 @@ namespace bsmd.AIS2Service
|
|||||||
|
|
||||||
public List<MonitorZone> Zones { get { return _zones; } }
|
public List<MonitorZone> Zones { get { return _zones; } }
|
||||||
|
|
||||||
public int Id { get { return _id; } }
|
|
||||||
|
|
||||||
public string Name { get { return _name; } }
|
public string Name { get { return _name; } }
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -49,12 +63,11 @@ namespace bsmd.AIS2Service
|
|||||||
|
|
||||||
#region class MonitorZone
|
#region class MonitorZone
|
||||||
|
|
||||||
public class MonitorZone
|
public class MonitorZone : DBEntity
|
||||||
{
|
{
|
||||||
|
|
||||||
#region fields
|
#region fields
|
||||||
|
|
||||||
private readonly int _id; // PK from database
|
|
||||||
private readonly List<GeoPoint> _vertices = new List<GeoPoint>();
|
private readonly List<GeoPoint> _vertices = new List<GeoPoint>();
|
||||||
private readonly List<MonitorAssignment> _assignments = new List<MonitorAssignment>();
|
private readonly List<MonitorAssignment> _assignments = new List<MonitorAssignment>();
|
||||||
private readonly string _name;
|
private readonly string _name;
|
||||||
@ -63,17 +76,15 @@ namespace bsmd.AIS2Service
|
|||||||
|
|
||||||
#region Construction
|
#region Construction
|
||||||
|
|
||||||
public MonitorZone(int id, string name)
|
public MonitorZone(int id, string name) : base (id)
|
||||||
{
|
{
|
||||||
_id = id; _name = name;
|
_name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
|
|
||||||
public int Id { get { return _id; } }
|
|
||||||
|
|
||||||
public string Name { get { return _name; } }
|
public string Name { get { return _name; } }
|
||||||
|
|
||||||
public List<GeoPoint> Vertices { get { return _vertices; } }
|
public List<GeoPoint> Vertices { get { return _vertices; } }
|
||||||
@ -84,6 +95,8 @@ namespace bsmd.AIS2Service
|
|||||||
|
|
||||||
public int Sequence { get; set; }
|
public int Sequence { get; set; }
|
||||||
|
|
||||||
|
public int MonitorGroupId { get; set; }
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region public methods
|
#region public methods
|
||||||
@ -114,35 +127,27 @@ namespace bsmd.AIS2Service
|
|||||||
|
|
||||||
#region class GeoPoint
|
#region class GeoPoint
|
||||||
|
|
||||||
public class GeoPoint
|
public class GeoPoint : DBEntity
|
||||||
{
|
{
|
||||||
|
|
||||||
private readonly int _id;
|
public GeoPoint(int id) : base (id)
|
||||||
|
{}
|
||||||
public GeoPoint(int id)
|
|
||||||
{
|
|
||||||
_id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int Id { get { return _id; } }
|
|
||||||
|
|
||||||
public double Lat { get; set; }
|
public double Lat { get; set; }
|
||||||
public double Lon { get; set; }
|
public double Lon { get; set; }
|
||||||
|
|
||||||
|
public int MonitorZoneId { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region class MonitorAssignment
|
#region class MonitorAssignment
|
||||||
|
|
||||||
public class MonitorAssignment
|
public class MonitorAssignment : DBEntity
|
||||||
{
|
{
|
||||||
|
|
||||||
private readonly int _id;
|
public MonitorAssignment(int id) : base(id)
|
||||||
|
{}
|
||||||
public MonitorAssignment(int id)
|
|
||||||
{
|
|
||||||
_id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
[Flags]
|
[Flags]
|
||||||
public enum ZoneMonitorType
|
public enum ZoneMonitorType
|
||||||
@ -166,14 +171,11 @@ namespace bsmd.AIS2Service
|
|||||||
|
|
||||||
#region class Alarm
|
#region class Alarm
|
||||||
|
|
||||||
public class Alarm
|
public class Alarm : DBEntity
|
||||||
{
|
{
|
||||||
private readonly int _id;
|
|
||||||
private readonly MonitorAssignment _assignment;
|
private readonly MonitorAssignment _assignment;
|
||||||
|
|
||||||
public Alarm(int id, MonitorAssignment assignment) { _id = id; _assignment = assignment; }
|
public Alarm(int id, MonitorAssignment assignment) : base(id) { _assignment = assignment; }
|
||||||
|
|
||||||
public int Id { get { return _id; } }
|
|
||||||
|
|
||||||
public MonitorAssignment Assignment { get { return _assignment; } }
|
public MonitorAssignment Assignment { get { return _assignment; } }
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user