SafeDispatch/SafeMobileLIB_DLL/DBmanagers/DBregistrationManager.cs

375 lines
14 KiB
C#
Raw Permalink Normal View History

2024-02-22 16:43:59 +00:00
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, has_sms = false, has_ticketing = false, has_reports = false, has_voice = false, has_recordings = false, has_zones = false, has_telemetry = false ";
2024-02-22 16:43:59 +00:00
cmd = new NpgsqlCommand(query, connection);
cmd.ExecuteNonQuery();
2024-02-22 16:43:59 +00:00
}
}
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";
2024-02-22 16:43:59 +00:00
return (uint)GetInt32Result(command);
}
public uint getVoiceCount()
{
string command = "SELECT COUNT (has_voice) FROM registration WHERE has_voice = true";
2024-02-22 16:43:59 +00:00
return (uint)GetInt32Result(command);
}
public uint getZonesCount()
{
string command = "SELECT COUNT (has_zones) FROM registration WHERE has_zones = true";
2024-02-22 16:43:59 +00:00
return (uint)GetInt32Result(command);
}
public uint getTelemetryCount()
{
string command = "SELECT COUNT (has_telemetry) FROM registration WHERE has_telemetry = true";
2024-02-22 16:43:59 +00:00
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["ip"].ToString();
reg.gps = Convert.ToBoolean(reader["has_gps"]);
reg.sms = Convert.ToBoolean(reader["has_sms"]);
reg.ticketing = Convert.ToBoolean(reader["has_ticketing"]);
reg.reports = Convert.ToBoolean(reader["has_reports"]);
reg.voice = Convert.ToBoolean(reader["has_voice"]);
reg.recordings = Convert.ToBoolean(reader["has_recordings"]);
reg.zones = Convert.ToBoolean(reader["has_zones"]);
reg.telemetry = Convert.ToBoolean(reader["has_telemetry"]);
reg.map_type = Convert.ToInt16(reader["map_type"]);
2024-02-22 16:43:59 +00:00
reg.MapTypeS = "None";
switch (reg.map_type)
2024-02-22 16:43:59 +00:00
{
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;
}
2024-02-22 16:43:59 +00:00
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;
}
}
}