using System; using System.Collections.Generic; using System.Text; using Npgsql; using System.Collections; using System.Linq; using System.ComponentModel; namespace SafeMobileLib { public class DBgatewaysManager : DBmanager { public DBgatewaysManager(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) { } #region gateways public List getAllGateways(int x) { try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); string command = $"UPDATE gateways SET active = false WHERE \"ID\" NOT IN (SELECT \"ID\" FROM gateways WHERE active = true and deleted = false ORDER BY \"ID\" limit {x} )"; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { cmd.ExecuteScalar(); } } } catch (Exception ex) { Console.WriteLine(ex.Message, ConsoleColor.Red); } return getAllGateways(); } public List getAllGateways() { List gwList = new List(); try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); string command = "SELECT \"ID\", \"IP\",peer_id, active FROM gateways where deleted = false "; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader Reader = cmd.ExecuteReader()) { while (Reader.Read()) { Gateway g = new Gateway() { Id = Reader.GetInt32(0), Ip = Reader.GetString(1), Peer_id = Reader.GetInt32(2), Active = Reader.GetBoolean(3) }; gwList.Add(g); } } } } } catch (Exception ee) { Console.WriteLine(ee.Message, ConsoleColor.Red); } return gwList; } public sqlResponse addGateway(string ip, Int32 peer_id, bool active) { string command = $"insert into \"gateways\" (\"IP\",peer_id, active) VALUES('{ip}',{peer_id},{active})"; return RunCommand(command); } public sqlResponse deleteGateway(int id) { string command = $"UPDATE \"gateways\" SET deleted = true WHERE \"ID\" = {id}"; RunCommand(command); command = $"UPDATE \"radio_gw\" SET deleted = true WHERE \"GW_ID\"={id}"; return RunCommand(command); } public sqlResponse deleteGatewayforUser(Int32 user_id) { string command = $"DELETE FROM user_radio WHERE user_id = {user_id}"; return RunCommand(command); } #endregion #region radio gateways public List gelAllRadioGateways(Int64 gw_id) { List gwList = new List(); try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); string command = "SELECT \"ID\", \"IP\",imei, voice_enabled, subnetwork, name, gw_type FROM radio_gw " + $" WHERE \"GW_ID\" = {gw_id} and deleted = false "; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader Reader = cmd.ExecuteReader()) { while (Reader.Read()) { RadioGateway rg = new RadioGateway() { Id = Reader.GetInt32(0), Ip = Reader.GetString(1), Imei = Reader.GetInt32(2), Gw_id = gw_id, Gw_voice = Reader.GetInt32(3), Subnetwork = Reader.GetInt32(4), Name = Reader.GetString(5), Type = Reader.GetInt32(6), TypeDesc = ((GatewayType)Reader.GetInt32(6)).ToString() }; gwList.Add(rg); } } } } } catch (Exception ee) { Console.WriteLine(ee.Message, ConsoleColor.Red); } return gwList; } public bool HasLinxGateway() { string command = $"SELECT COUNT(\"ID\") FROM radio_gw WHERE gw_type = {(int)GatewayType.Broadband} and deleted = false"; return (GetInt32Result(command) > 0); } public bool IsTheLinxGateway(int id) { string command = $"SELECT count(\"ID\") FROM radio_gw WHERE \"ID\" = {id} and gw_type={(int)GatewayType.Broadband} and deleted = false "; return (GetInt32Result(command) > 0); } public List gelAllRadioGateways(Int64 gw_id, int gw_type) { List gwList = new List(); try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); string command = $"SELECT \"ID\", \"IP\", imei, voice_enabled, subnetwork, name, gw_type FROM radio_gw WHERE \"GW_ID\" = {gw_id} and gw_type = {gw_type} and deleted = false"; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader Reader = cmd.ExecuteReader()) { while (Reader.Read()) { RadioGateway rg = new RadioGateway() { Id = Reader.GetInt32(0), Ip = Reader.GetString(1), Imei = Reader.GetInt32(2), Gw_id = gw_id, Gw_voice = Reader.GetInt32(3), Subnetwork = Reader.GetInt32(4), Name = Reader.GetString(5), Type = Reader.GetInt32(6), TypeDesc = ((GatewayType)Reader.GetInt32(6)).ToString() }; gwList.Add(rg); } } } } } catch (Exception ee) { Console.WriteLine(ee.Message, ConsoleColor.Red); } return gwList; } public List gelAllRadioGateways_forUser(int gw_id,int user_id) { List gwList = new List(); try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); string command = "SELECT r.\"ID\", r.\"IP\",r.imei, r.voice_enabled,r.subnetwork, r.name, r.gw_type FROM radio_gw as r, user_radio as u " + $"Where r.\"GW_ID\" = {gw_id} and r.\"ID\"=u.radio_id and u.user_id= {user_id} and r.deleted = false"; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader Reader = cmd.ExecuteReader()) { while (Reader.Read()) { RadioGateway rg = new RadioGateway() { Id = Reader.GetInt32(0), Ip = Reader.GetString(1), Imei = Reader.GetInt32(2), Gw_id = gw_id, Gw_voice = Reader.GetInt32(3), Subnetwork = Reader.GetInt32(4), Name = Reader.GetString(5), Type = Reader.GetInt32(6), TypeDesc = ((GatewayType)Reader.GetInt32(6)).ToString() }; gwList.Add(rg); } } } } } catch (Exception ee) { Console.WriteLine(ee.ToString()); } return gwList; } public List gelAllGatewaysAndRadioGateways() { List gwList = new List(); try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); string command = "SELECT r.\"ID\" as rid, r.\"GW_ID\" as gid, r.name as name, r.gw_type as type " + " FROM radio_gw as r JOIN gateways as g ON r.\"GW_ID\"=g.\"ID\"" + " WHERE r.deleted = false and g.deleted = false"; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader Reader = cmd.ExecuteReader()) { while (Reader.Read()) { RadioGateway rg = new RadioGateway() { Id = Reader.GetInt32(0), Gw_id = Reader.GetInt32(1), Name = Reader.GetString(2), Type = Reader.GetInt32(3) }; gwList.Add(rg); } } } } } catch (Exception ee) { Console.WriteLine(ee.ToString()); } return gwList; } public void gelAllGatewaysAndRadioGatewaysHT(ref Hashtable gwHT) { string command = "SELECT r.\"ID\" as rid, r.\"GW_ID\" as gid," + " r.name as name, r.gw_type as type, r.voice_enabled as voice_enabled" + " FROM radio_gw as r " + " JOIN gateways as g ON r.\"GW_ID\"=g.\"ID\"" + " WHERE r.deleted = false and g.deleted = false"; try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader Reader = cmd.ExecuteReader()) { while (Reader.Read()) { RadioGateway rg = new RadioGateway() { Id = Reader.GetInt32(0), Gw_id = Reader.GetInt32(1), Name = Reader.GetString(2), Type = Reader.GetInt32(3), Alive = false, Gw_voice = Reader.GetInt16(4) }; string key = rg.Gw_id.ToString() + "." + rg.Id.ToString(); if (!gwHT.ContainsKey(key)) gwHT.Add(key, rg); else { ((RadioGateway)gwHT[key]).Name = rg.Name; ((RadioGateway)gwHT[key]).Gw_voice = rg.Gw_voice; } } } } } } catch (Exception ee) { Utils.WriteLine("gelAllGatewaysAndRadioGatewaysHT error: " + ee.ToString(), ConsoleColor.Red); } } public Hashtable gelAllGatewaysAndRadioGatewaysHTByName() { Hashtable gwHT = new Hashtable(); string command = "SELECT r.\"ID\" as rid, r.\"GW_ID\" as gid," + " r.name as name, r.gw_type as type " + " FROM radio_gw as r " + " JOIN gateways as g ON r.\"GW_ID\"=g.\"ID\"" + " WHERE r.deleted = false and g.deleted = false"; try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { RadioGateway rg = new RadioGateway() { Id = reader.GetInt32(0), Gw_id = reader.GetInt32(1), Name = reader.GetString(2), Type = reader.GetInt32(3), Alive = false }; if (!gwHT.ContainsKey(rg.Name)) gwHT.Add(rg.Name, rg); else ((RadioGateway)gwHT[rg.Name]).Name = rg.Name; } } } } } catch (Exception ee) { Utils.WriteLine("gelAllGatewaysAndRadioGatewaysHTByName error: " + ee.ToString(), ConsoleColor.Red); } return gwHT; } public List gelAllGatewaysAndRadioGateways(string idlist) { List gwList = new List(); try { Hashtable gwHT = new Hashtable(); string command = "SELECT r.\"ID\" as rid, r.\"GW_ID\" as gid," + " r.name as name, r.gw_type as type " + " FROM radio_gw as r " + " JOIN gateways as g ON r.\"GW_ID\"=g.\"ID\"" + " JOIN subs_gateway as sgw ON r.\"ID\"=sgw.radio_gw_id" + " WHERE r.deleted = false and g.deleted = false " + $" AND sc_id in {idlist} "; using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { RadioGateway rg = new RadioGateway() { Id = reader.GetInt32(0), Gw_id = reader.GetInt32(1), Name = reader.GetString(2), Type = reader.GetInt32(3) }; if (!gwHT.Contains(rg.Name)) gwHT.Add(rg.Name, rg); } } } } gwList = gwHT.Values.Cast().ToList(); } catch (Exception ex) { Utils.WriteLine("gelAllGatewaysAndRadioGateways error: " + ex.ToString(), ConsoleColor.Red); } return gwList; } public sqlResponse addRadioGateway(string ip,int imei, int gw_id, int voice, int subnetID, string name, int gw_type) { sqlResponse resp; try { string command = "INSERT INTO \"radio_gw\" (\"IP\",\"GW_ID\",imei,voice_enabled,subnetwork,name, gw_type, deleted )" + $" VALUES('{ip}',{gw_id},{imei},{voice},{subnetID},'{name}',{gw_type}, false) RETURNING \"ID\""; using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); string radioGatewayId = string.Empty; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { radioGatewayId = cmd.ExecuteScalar().ToString(); } command = $"INSERT INTO sip_manager (sip_id, id, type) VALUES( (SELECT max(sip_id)+1 FROM sip_manager),{radioGatewayId},{(int)ContactType.RADIOGATEWAY})"; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { cmd.ExecuteNonQuery(); } if (gw_type == (int)GatewayType.Broadband) { List lincsi = getAllLinxRadios(); foreach (var sc_id in lincsi) { command = $"UPDATE groups set r_gw_id = {radioGatewayId} WHERE type = {(int)GatewayType.Broadband}; DELETE FROM subs_gateway WHERE sc_id = {sc_id}; INSERT INTO subs_gateway (sc_id,gateway_id,radio_gw_id) values({sc_id},{gw_id},{radioGatewayId});"; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { cmd.ExecuteNonQuery(); } } } resp = sqlResponse.done; } } catch (Exception ee) { Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source); resp = sqlResponse.SQLerror; } return resp; } public List getAllLinxRadios() { List vehList = new List(); try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); string command = "SELECT id FROM vehicle as veh where type = 1"; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { vehList.Add(reader.GetInt32(0)); } } } } } catch (Exception ee) { Console.WriteLine(ee.Message, ConsoleColor.Red ); } return vehList; } public sqlResponse editRadioGateway(string ip, int imei, int gw_id, int voice, int subnetID, Int32 DBID, string name, int gw_type) { String query = String.Format("UPDATE \"radio_gw\"" + " set \"IP\"='{0}', \"GW_ID\"={1}, imei={2}, voice_enabled={3}, subnetwork={4},name='{6}', gw_type ={7} where \"ID\"={5}", ip, gw_id, imei, voice, subnetID, DBID, name, gw_type); return RunCommand(query); } public sqlResponse editGateway(string ip, Int32 peer_id, Int32 ID, bool active) { string query = $"UPDATE \"gateways\" set \"IP\"='{ip}', peer_id={peer_id}, active = {active} where \"ID\"={ID}"; return RunCommand(query); } public sqlResponse add_a_default_Repeater_In_database() { sqlResponse resp; NpgsqlCommand cmd; ArrayList listUserID = new ArrayList(); resp = sqlResponse.done; try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); cmd = new NpgsqlCommand("INSERT INTO \"gateways\" " + "(\"IP\",peer_id) VALUES('127.0.0.1',4999)", connection); cmd.ExecuteNonQuery(); cmd = new NpgsqlCommand("INSERT INTO \"radio_gw\" " + "(\"IP\",\"GW_ID\",imei,voice_enabled,subnetwork)" + " VALUES('10.120.10.6',2,1,1,12)", connection); cmd.ExecuteNonQuery(); cmd = new NpgsqlCommand("SELECT max(\"ID\") FROM radio_gw ", connection); Int32 radio_id = Convert.ToInt32(cmd.ExecuteScalar().ToString()); cmd = new NpgsqlCommand("INSERT INTO \"radio_zones\" " + "(nr,\"name\",rgw_id) VALUES(1,'Repeater'," + radio_id + ")", connection); cmd.ExecuteNonQuery(); cmd = new NpgsqlCommand("SELECT max(id) FROM radio_zones ", connection); Int32 zone_id = Convert.ToInt32(cmd.ExecuteScalar().ToString()); cmd = new NpgsqlCommand("INSERT INTO \"radio_channels\" " + "(nr,name,zone_id) VALUES(1,'Repeater'," + zone_id + ")", connection); cmd.ExecuteNonQuery(); } } catch (Exception ee) { Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source); resp = sqlResponse.SQLerror; } return resp; } public sqlResponse add_Zone_and_Channel_for_Repeater() { sqlResponse resp; NpgsqlCommand cmd; ArrayList listUserID = new ArrayList(); resp = sqlResponse.done; try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); cmd = new NpgsqlCommand("SELECT max(\"ID\") FROM radio_gw ", connection); Int32 radio_id = Convert.ToInt32(cmd.ExecuteScalar().ToString()); cmd = new NpgsqlCommand("INSERT INTO \"radio_zones\" " + "(nr,\"name\",rgw_id) VALUES(1,'Repeater'," + radio_id + ")", connection); cmd.ExecuteNonQuery(); cmd = new NpgsqlCommand("SELECT max(id) FROM radio_zones ", connection); Int32 zone_id = Convert.ToInt32(cmd.ExecuteScalar().ToString()); cmd = new NpgsqlCommand("INSERT INTO \"radio_channels\" " + "(nr,name,zone_id) VALUES(1,'Repeater'," + zone_id + ")", connection); cmd.ExecuteNonQuery(); } } catch (Exception ee) { Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source, ConsoleColor.Red); resp = sqlResponse.SQLerror; } return resp; } public sqlResponse addNewGatewayToAllUser() { sqlResponse resp; NpgsqlCommand cmd; ArrayList listUserID = new ArrayList(); resp = sqlResponse.done; try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); cmd = new NpgsqlCommand("SELECT MAX(\"ID\") FROM radio_gw ", connection); Int32 radio_id = Convert.ToInt32(cmd.ExecuteScalar().ToString()); cmd = new NpgsqlCommand("SELECT userid FROM users WHERE user_type = 0", connection); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { listUserID.Add(reader.GetInt32(0)); } } foreach (Int32 user_id in listUserID) { cmd = new NpgsqlCommand("INSERT INTO user_radio " + $"(radio_id, user_id) VALUES({radio_id},{user_id})", connection); cmd.ExecuteNonQuery(); resp = sqlResponse.done; } } } catch (Exception ee) { Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source); resp = sqlResponse.SQLerror; } return resp; } public sqlResponse deleteRadioToAllUser(int id) { string command = $"DELETE FROM user_radio WHERE radio_id= {id}"; return RunCommand(command); } public sqlResponse deleteRadioGateway(int id) { string command = $"UPDATE radio_gw SET deleted = true where \"ID\"= {id}"; return RunCommand(command); } #endregion #region radio zones public List gelAllradioZones(int rgw_id) { List zoneList = new List(); try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); string command = $"SELECT id, nr, \"name\" FROM radio_zones Where rgw_id = {rgw_id}"; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader Reader = cmd.ExecuteReader()) { while (Reader.Read()) { string zoneName = ""; if (!Reader.IsDBNull(2)) zoneName = Reader.GetString(2); RadioZones rz = new RadioZones(Reader.GetInt32(0), Reader.GetInt32(1), zoneName, rgw_id); zoneList.Add(rz); } } } } } catch (Exception ee) { Console.WriteLine(ee.ToString()); } return zoneList; } public sqlResponse addRadioZone(int nr, string name, int rgw_id) { string command = "INSERT INTO \"radio_zones\" (nr,\"name\",rgw_id)" + $" VALUES({nr},'{name}',{rgw_id})"; return RunCommand(command); } public sqlResponse deleteRadioZone(int id) { string command = $"DELETE FROM \"radio_zones\" WHERE id = {id}"; return RunCommand(command); } #endregion #region radio channels public List gelAllradioChannels(int zone_id) { List channelList = new List(); try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); string command = $"SELECT id, nr ,\"name\" FROM radio_channels WHERE zone_id = {zone_id}"; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { RadioChannels ch = new RadioChannels(reader.GetInt32(0), reader.GetInt32(1), reader.GetString(2), zone_id); channelList.Add(ch); } } } } } catch (Exception ee) { Console.WriteLine(ee.ToString()); } return channelList; } public sqlResponse addRadioChannel(int nr, string name, int zone_id) { string command = "INSERT INTO \"radio_channels\" (nr,name,zone_id)" + $" VALUES({nr},'{name}',{zone_id})"; return RunCommand(command); } public sqlResponse deleteRadioChannel(int id) { string command = $"DELETE FROM \"radio_channels\" WHERE id = {id}"; return RunCommand(command); } #endregion public ArrayList GetAllUnits4GW_RGW(int gwID, string rgwIP) { ArrayList ret = new ArrayList(); try { string command = "SELECT s.imei FROM subs_gateway as s_gw " + "INNER JOIN subscriber as s ON (s_gw.sc_id =s.sc_id) " + "INNER JOIN radio_gw as r_gw ON (r_gw.\"ID\" = s_gw.radio_gw_id) " + $" WHERE s_gw.gateway_id = {gwID} AND r_gw.\"IP\" ='{rgwIP}'"; using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { ret.Add(reader.GetString(0)); } } } } } catch (Exception ee) { Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source, ConsoleColor.Red); } return ret; } public ArrayList get_all_GatewaywithIDandIP() { ArrayList ret = new ArrayList(); try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); string command = "select \"ID\",\"GW_ID\",\"IP\" from radio_gw WHERE deleted = false"; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) ret.Add(new RadioGateway(reader.GetInt32(0), reader.GetString(2), 0, reader.GetInt32(1), 0, 0)); } } } } catch (Exception ee) { Console.WriteLine(ee.ToString()); } return ret; } //get all vehicles for current user public List getAllGatewaysForCurrentUser(string _id) { GatewayForUser gw; List gwList = new List(); try { string command = "SELECT usrad.radio_id,radgw.\"IP\",gw.\"IP\"" + " FROM user_radio as usrad, radio_gw as radgw, gateways as gw" + $" WHERE usrad.user_id={_id} and usrad.radio_id=radgw.\"ID\" and radgw.voice_enabled= 1 and radgw.\"GW_ID\"=gw.\"ID\" " + " AND radgw.deleted = false and gw.deleted = false "; using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { gw = new GatewayForUser(Convert.ToInt32(reader.GetValue(0)), reader.GetValue(1).ToString(), reader.GetValue(2).ToString()); gwList.Add(gw); } } } } } catch (Exception ee) { Console.WriteLine(ee.ToString()); } return gwList; } //get all vehicles that are not assigned to current user public List getAllOtherGatewaysForCurrentUser(string _id) { GatewayForUser gw; ArrayList listIDtoRemove = new ArrayList(); List gwList = new List(); string command = "SELECT radgw.\"ID\",radgw.\"IP\",gw.\"IP\"" + " FROM radio_gw as radgw, gateways as gw" + " WHERE radgw.\"GW_ID\"=gw.\"ID\" and radgw.deleted = false and gw.deleted = false "; try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { gw = new GatewayForUser(Convert.ToInt32(reader.GetValue(0)), reader.GetValue(1).ToString(), reader.GetValue(2).ToString()); gwList.Add(gw); } } } using (NpgsqlCommand cmd = new NpgsqlCommand($"SELECT radio_id FROM user_radio Where user_id = {_id}", connection)) { using (NpgsqlDataReader Reader = cmd.ExecuteReader()) { while (Reader.Read()) { listIDtoRemove.Add(Convert.ToInt32(Reader.GetValue(0))); } } } } GatewayForUser stuffToRemove = null; foreach (Int32 obj in listIDtoRemove) { foreach (GatewayForUser obj2 in gwList) if (obj == obj2.Id) stuffToRemove = obj2; if (stuffToRemove != null) gwList.Remove(stuffToRemove); stuffToRemove = null; } } catch (Exception ee) { SM.Debug(command); Console.WriteLine(ee.Message, ConsoleColor.Red); } return gwList; } //get all vehicles that are not assigned to current user public List getAllGatewaysForNewUser() { GatewayForUser gw; List gwList = new List(); string command = "SELECT radgw.\"ID\",radgw.\"IP\",gw.\"IP\"" + " FROM radio_gw as radgw, gateways as gw" + " WHERE radgw.\"GW_ID\"=gw.\"ID\" " + " AND radgw.deleted = false AND gw.deleted = false "; try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { gw = new GatewayForUser(Convert.ToInt32(reader.GetValue(0)), reader.GetValue(1).ToString(), reader.GetValue(2).ToString()); gwList.Add(gw); } } } } } catch (Exception ee) { SM.Debug(command); Console.WriteLine(ee.ToString()); } return gwList; } // add connection between user and vehicle public vehResponse addGatewayUserConnection(string gatewayId, string userId) { vehResponse resp = vehResponse.done; try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); using (NpgsqlCommand cmd = new NpgsqlCommand($"INSERT INTO user_radio VALUES({gatewayId},{userId})", connection)) { cmd.ExecuteNonQuery(); } } } catch (Exception ee) { Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source, ConsoleColor.Red); resp = vehResponse.SQLerror; } return resp; } //remove connection between user and vehicle public vehResponse removeGatewayUserConnection(string gatewayId, string userId) { vehResponse resp = vehResponse.done; try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); using (NpgsqlCommand cmd = new NpgsqlCommand($"DELETE FROM user_radio WHERE radio_id = {gatewayId} and user_id = {userId}", connection)) { cmd.ExecuteNonQuery(); } } } catch (Exception ee) { Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source); resp = vehResponse.SQLerror; } return resp; } } public class Gateway { private int id; public int Id { get { return id; } set { id = value; } } private string ip; public string Ip { get { return ip; } set { ip = value; } } private int peer_id; public int Peer_id { get { return peer_id; } set { peer_id = value; } } private bool active; public bool Active { get { return active; } set { active = value; } } public Gateway(int id, string ip, Int32 peer_id) { this.id = id; this.ip = ip; this.peer_id = peer_id; } public Gateway() { } } public class GatewayForUser { private Int32 id; public Int32 Id { get { return id; } set { id = value; } } private string ip_radio; public string Ip_radio { get { return ip_radio; } set { ip_radio = value; } } private string ip_PC; public string Ip_PC { get { return ip_PC; } set { ip_PC = value; } } private string ip_display; public string Ip_Display { get { return ip_display; } set { ip_display = value; } } public GatewayForUser(int id, string ip_radio, string ip_PC) { this.id = id; this.ip_radio = ip_radio; this.ip_PC = ip_PC; this.ip_display = ip_radio + " (" +ip_PC+")"; } } public enum RadioState { IDLE, RX, TX, HANGTIME, OFFLINE } public class RadioGateway : INotifyPropertyChanged { public enum GwType { SMC = 4 }; private object _lock = new object(); private void NotifyPropertyChanged(string propertyName) { if (PropertyChanged == null) Utils.WriteLine("PropertyChanged is NULL", ConsoleColor.Red); //Raise PropertyChanged event PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; private RadioState radioState1; public RadioState State1 { get { return radioState1; } set { lock (_lock) { //The property changed event will get fired whenever //the value changes. The subscriber will do work if the value is //1. This way you can keep your business logic outside of the setter if (value != radioState1) { radioState1 = value; //Utils.WriteLine("RadioState1: " + radioState1); NotifyPropertyChanged("radioState"); } } } } private RadioState radioState2; public RadioState State2 { get { return radioState2; } set { lock (_lock) { //The property changed event will get fired whenever //the value changes. The subscriber will do work if the value is //1. This way you can keep your business logic outside of the setter if (value != radioState2) { radioState2 = value; //Utils.WriteLine("RadioState2: " + radioState2); NotifyPropertyChanged("radioState2"); } } } } private int id; public int Id { get { return id; } set { id = value; } } private string ip; public string Ip { get { return ip; } set { ip = value; } } private int imei; public int Imei { get { return imei; } set { imei = value; } } private Int64 gw_id; public Int64 Gw_id { get { return gw_id; } set { gw_id = value; } } private int gw_voice; public int Gw_voice { get { return gw_voice; } set { gw_voice = value; } } private int subnetwork; public int Subnetwork { get { return subnetwork; } set { subnetwork = value; } } private string interfaceIP; public string InterfaceIP { get { return interfaceIP; } set { interfaceIP = value; } } private string name; public string Name { get { return name; } set { name = value; } } private int type; public int Type { get { return type; } set { type = value; } } private string typeDesc; public string TypeDesc { get { return typeDesc; } set { typeDesc = value; } } private bool alive; public bool Alive { get { return alive; } set { alive = value; } } private DateTime lastUpdate; public DateTime LastUpdate { get { return lastUpdate; } set { lastUpdate = value; } } public bool InPTT { get; set; } = false; public bool InPatch { get; set; } = false; public String DisplayedMemeber => Name + " [" + Ip + "]"; public RadioGateway() { this.radioState1 = RadioState.OFFLINE; this.radioState2 = RadioState.OFFLINE; } public RadioGateway(int id, string ip, int imei, Int64 gw_id, int gw_voice, int subnetwork) { this.id = id; this.ip = ip; this.gw_id = gw_id; this.imei = imei; this.gw_voice = gw_voice; this.subnetwork = subnetwork; this.radioState1 = RadioState.OFFLINE; this.radioState2 = RadioState.OFFLINE; } } public class RadioZones { private int id; public int Id { get { return id; } set { id = value; } } private int nr; public int Nr { get { return nr; } set { nr = value; } } private string name; public string Name { get { return name; } set { name = value; } } private int rGWid; public int RGWid { get { return rGWid; } set { rGWid = value; } } public RadioZones(int id, int nr,string name, int rGWid) { this.id = id; this.nr = nr; this.rGWid = rGWid; this.name = name; } } public class RadioChannels { private int id; public int Id { get { return id; } set { id = value; } } private int nr; public int Nr { get { return nr; } set { nr = value; } } private string name; public string Name { get { return name; } set { name = value; } } private int zoneid; public int Zoneid { get { return zoneid; } set { zoneid = value; } } public RadioChannels(int id, int nr, string name, int zoneid) { this.id = id; this.nr = nr; this.name = name; this.zoneid = zoneid; } } }