SafeDispatch/SafeMobileLIB_DLL/DBmanagers/DBregistrationManager.cs
2024-02-22 18:43:59 +02:00

394 lines
15 KiB
C#

using System;
using Npgsql;
using System.Collections.Generic;
namespace SafeMobileLib
{
public class DBregistrationManager : DBmanager
{
public DBregistrationManager(string p_server, string p_dbname, string p_user, string p_password, string p_port)
: base(p_server, p_dbname, p_user, p_password, p_port)
{
}
public int insertSafeDispatchRegistration(SDRegistration regObj)
{
String command = "INSERT INTO registration(ip, has_gps, has_sms, has_ticketing, has_reports, has_voice, has_recordings, has_zones, has_telemetry, map_type) "
+ $" VALUES ('{regObj.ip}',{regObj.gps},{regObj.sms},{regObj.ticketing},{regObj.reports},{regObj.voice},{regObj.recordings},{regObj.zones},{regObj.telemetry},{regObj.map_type})";
return (RunCommand(command) == sqlResponse.done) ? 0 : -1;
}
public int removeAllGatewayRegistrations()
{
string command = "UPDATE gateways SET active = false";
return (RunCommand(command) == sqlResponse.done) ? 0 : -1;
}
public int removeSafeDispatchRegistration(String ip)
{
string command = $"delete from registration where ip='{ip}'";
return (RunCommand(command) == sqlResponse.done) ? 0 : -1;
}
public int gatewaysCount()
{
string command = "SELECT COUNT(\"IP\") FROM gateways WHERE active = true";
return GetInt32Result(command);
}
public int safeDispatchesCount()
{
string command = "SELECT COUNT(ip) FROM registration";
return GetInt32Result(command);
}
public int updateSafeDispatchRegistration(SDRegistration oldValue, SDRegistration newValue)
{
int toReturn = 0;
try
{
using (NpgsqlConnection connection = new NpgsqlConnection())
{
connection.ConnectionString = getConnectionString();
connection.Open();
String query = "update registration set ip = '" + newValue.ip + "', has_gps = " + newValue.gps + ", has_sms = "
+ newValue.sms + ", has_ticketing = " + newValue.ticketing + ", has_reports = " + newValue.reports + ", has_voice = "
+ newValue.voice + ", has_recordings = "
+ newValue.recordings + ", has_zones = " + newValue.zones + ", has_telemetry = " + newValue.telemetry + ", map_type = "
+ newValue.map_type + " where ip = '" + oldValue.ip + "'";
using (NpgsqlCommand cmd = new NpgsqlCommand(query, connection))
{
cmd.ExecuteNonQuery();
}
}
}
catch (Exception e)
{
Console.WriteLine("Exception in updateSafeDispatchRegistration: " + e.ToString());
return -1;
}
return toReturn;
}
public int resetSafeDispatchRegistrations()
{
int toReturn = 0;
NpgsqlCommand cmd;
try
{
using (NpgsqlConnection connection = new NpgsqlConnection())
{
connection.ConnectionString = getConnectionString();
connection.Open();
String query = "UPDATE registration SET has_gps = false WHERE true";
cmd = new NpgsqlCommand(query, connection);
cmd.ExecuteNonQuery();
query = "UPDATE registration SET has_sms = false WHERE true";
cmd = new NpgsqlCommand(query, connection);
cmd.ExecuteNonQuery();
query = "UPDATE registration SET has_ticketing = false WHERE true";
cmd = new NpgsqlCommand(query, connection);
cmd.ExecuteNonQuery();
query = "UPDATE registration SET has_reports = false WHERE true";
cmd = new NpgsqlCommand(query, connection);
cmd.ExecuteNonQuery();
query = "UPDATE registration SET has_voice = false WHERE true";
cmd = new NpgsqlCommand(query, connection);
cmd.ExecuteNonQuery();
query = "UPDATE registration SET has_recordings = false WHERE true";
cmd = new NpgsqlCommand(query, connection);
cmd.ExecuteNonQuery();
query = "UPDATE registration SET has_zones = false WHERE true";
cmd = new NpgsqlCommand(query, connection);
cmd.ExecuteNonQuery();
query = "UPDATE registration SET has_telemetry = false WHERE true";
cmd = new NpgsqlCommand(query, connection);
cmd.ExecuteNonQuery();
}
}
catch (Exception e)
{
Console.WriteLine("Exception in resetSafeDispatchRegistrations: " + e.ToString());
return -1;
}
return toReturn;
}
public List<GatewayRegistration> getAllGateways()
{
List<GatewayRegistration> gateways = new List<GatewayRegistration>();
String query = "SELECT \"IP\", \"ID\" FROM gateways WHERE active = true";
try
{
using (NpgsqlConnection connection = new NpgsqlConnection())
{
connection.ConnectionString = getConnectionString();
connection.Open();
using (NpgsqlCommand cmd = new NpgsqlCommand(query, connection))
{
using (NpgsqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
String ip = reader.GetValue(0).ToString();
String id = reader.GetValue(1).ToString();
GatewayRegistration gwReg = new GatewayRegistration()
{
ip = ip,
id = id
};
gateways.Add(gwReg);
}
}
}
}
}
catch (Exception e)
{
Console.WriteLine("Exception in DBRegistrationManager.getAllGateways: " + e.ToString());
}
return gateways;
}
public uint getGPSCount()
{
string command = "SELECT COUNT (has_gps) FROM registration WHERE has_gps=true";
return (uint)GetInt32Result(command);
}
public uint getSMSCount()
{
string command = "SELECT COUNT (has_sms) FROM registration WHERE has_sms=true";
return (uint)GetInt32Result(command);
}
public uint getTicketingCount()
{
string command = "SELECT COUNT(has_ticketing) FROM registration WHERE has_ticketing=true";
return (uint)GetInt32Result(command);
}
public uint getRecordingsCount()
{
string command = "SELECT COUNT(has_recordings) FROM registration WHERE has_recordings=true";
return (uint)GetInt32Result(command);
}
public uint getReportsCount()
{
string command = "SELECT count (has_reports) FROM registration WHERE has_reports=true";
return (uint)GetInt32Result(command);
}
public uint getVoiceCount()
{
string command = "SELECT COUNT (has_voice) FROM registration WHERE has_voice=true";
return (uint)GetInt32Result(command);
}
public uint getZonesCount()
{
string command = "SELECT COUNT (has_zones) FROM registration WHERE has_zones=true";
return (uint)GetInt32Result(command);
}
public uint getTelemetryCount()
{
string command = "SELECT COUNT (has_telemetry) FROM registration WHERE has_telemetry=true";
return (uint)GetInt32Result(command);
}
public uint getGoogleCount()
{
string command = "SELECT COUNT (map_type) FROM registration WHERE map_type=1";
return (uint)GetInt32Result(command);
}
public uint getMapsUSCount()
{
string command = "SELECT COUNT (map_type) FROM registration WHERE map_type=2";
return (uint)GetInt32Result(command);
}
public uint getOSMCount()
{
string command = "SELECT COUNT (map_type) FROM registration WHERE map_type=3";
return (uint)GetInt32Result(command);
}
public uint getMapsArcgisCount()
{
string command = "SELECT COUNT (map_type) FROM registration WHERE map_type=4";
return (uint)GetInt32Result(command);
}
public List<SDRegistration> getAllSafeDispatches()
{
List<SDRegistration> safeDispatches = new List<SDRegistration>();
String query = "SELECT ip, has_gps, has_sms, has_ticketing, has_reports, has_voice, has_recordings, has_zones, has_telemetry, map_type FROM registration";
try
{
using (NpgsqlConnection connection = new NpgsqlConnection())
{
connection.ConnectionString = getConnectionString();
connection.Open();
using (NpgsqlCommand cmd = new NpgsqlCommand(query, connection))
{
using (NpgsqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
SDRegistration reg = new SDRegistration();
reg.ip = reader.GetValue(0).ToString();
reg.gps = (Boolean)reader.GetValue(1);
reg.sms = (Boolean)reader.GetValue(2);
reg.ticketing = (Boolean)reader.GetValue(3);
reg.reports = (Boolean)reader.GetValue(4);
reg.voice = (Boolean)reader.GetValue(5);
reg.recordings = (Boolean)reader.GetValue(6);
reg.zones = (Boolean)reader.GetValue(7);
reg.telemetry = (Boolean)reader.GetValue(8);
Int16 mType = Int16.Parse(reader.GetValue(9).ToString());
reg.map_type = mType;
reg.MapTypeS = "None";
switch (mType)
{
case 1:
reg.MapTypeS = "Google";
break;
case 2:
reg.MapTypeS = "MapPoint US";
break;
case 3:
reg.MapTypeS = "OpenStreetMap";
break;
case 4:
reg.MapTypeS = "ArcGIS";
break;
}
safeDispatches.Add(reg);
}
}
}
}
}
catch (Exception e)
{
Console.WriteLine("Exception in DBRegistrationManager.getAllSafeDispatches: " + e.ToString());
}
return safeDispatches;
}
public Boolean getGateway(String ip)
{
Int32 count = 0;
String query = $"SELECT COUNT(*) FROM gateways WHERE \"IP\" = '{ip}' AND active = true";
try
{
using (NpgsqlConnection connection = new NpgsqlConnection())
{
connection.ConnectionString = getConnectionString();
connection.Open();
using (NpgsqlCommand cmd = new NpgsqlCommand(query, connection))
{
using (NpgsqlDataReader reader = cmd.ExecuteReader())
{
reader.Read();
count = Int32.Parse(reader.GetValue(0).ToString());
}
}
}
}
catch (Exception e)
{
Console.WriteLine("Exception in gatewaysCount: " + e.ToString());
}
return (count > 0);
}
public SDRegistration getSafeDispatchFeatures(String id)
{
SDRegistration reg = new SDRegistration();
reg.ip = "invalid";
String query = "SELECT ip, has_gps, has_sms, has_ticketing, has_reports, has_voice, has_recordings, has_zones, has_telemetry, map_type " +
$" FROM registration WHERE ip = '{id}'";
try
{
using (NpgsqlConnection connection = new NpgsqlConnection())
{
connection.ConnectionString = getConnectionString();
connection.Open();
using (NpgsqlCommand cmd = new NpgsqlCommand(query, connection))
{
using (NpgsqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
int i = 0;
reg.ip = reader.GetValue(i++).ToString();
reg.gps = (Boolean)reader.GetValue(i++);
reg.sms = (Boolean)reader.GetValue(i++);
reg.ticketing = (Boolean)reader.GetValue(i++);
reg.reports = (Boolean)reader.GetValue(i++);
reg.voice = (Boolean)reader.GetValue(i++);
reg.recordings = (Boolean)reader.GetValue(i++);
reg.zones = (Boolean)reader.GetValue(i++);
reg.telemetry = (Boolean)reader.GetValue(i++);
Int16 mType = Int16.Parse(reader.GetValue(i++).ToString());
reg.map_type = mType;
}
}
}
}
}
catch (Exception e)
{
Console.WriteLine($"getAllSafeDispatches: {e.Message}");
}
return reg;
}
}
}