using System; using System.Collections.Generic; using System.Text; using Npgsql; using System.Collections; using System.Data; namespace SafeMobileLib { public class DBReportingManager : DBmanager { public DBReportingManager(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 List get_ALLAlarm(String glwhere, Boolean is24hours, Boolean isInMile, Boolean DayFirst, Hashtable VehIDHash, Hashtable ZoneIDHash, Hashtable LandIDHash, Hashtable vehicleHT, string milesh, string kmh) { String tmpglwhere = glwhere; String TimeVal = ""; List HistDataReport = new List(); try { //get all energ alarm using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); string command = $"SELECT sc_id, timeGMT FROM emergalarm {glwhere} ORDER BY timeGMT desc"; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { TimeVal = dr.GetInt32(1).GetRegionalFormat(is24hours, DayFirst); var tmp = new StopData { Location = TimeVal, Duration = "", Time = "Emergency", Data = (VehIDHash[dr.GetInt32(0)] != null) ? (String)VehIDHash[dr.GetInt32(0)] : "", timeGMT = dr.GetInt32(1) }; if (tmp.Data != "") HistDataReport.Add(tmp); } } } command = $"SELECT sc_id,zone_id,action,timeGMT,type FROM geozoneinout {glwhere} ORDER BY timeGMT desc"; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { string descStr = ((uint)dr.GetInt32(2) == 1) ? "OUT " : "IN "; if (dr.GetInt32(4) == 1) { if (ZoneIDHash[dr.GetInt32(1)] != null) descStr = descStr + ((ZoneClass)ZoneIDHash[dr.GetInt32(1)]).Name; } else { if (LandIDHash[dr.GetInt32(1)] != null) descStr = descStr + ((String)LandIDHash[dr.GetInt32(1)]); } TimeVal = dr.GetInt32(3).GetRegionalFormat(is24hours, DayFirst); var tmp = new StopData { Location = TimeVal, Duration = descStr, Time = "Geo-Fence", Data = (VehIDHash[dr.GetInt32(0)] != null) ? (String)VehIDHash[dr.GetInt32(0)] : "", timeGMT = dr.GetInt32(3) }; if (tmp.Data != "") HistDataReport.Add(tmp); } } } command = $"SELECT timeGMT,sc_id,speed FROM speedalarm {glwhere} ORDER BY timeGMT desc"; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { TimeVal = dr.GetInt32(0).GetRegionalFormat(is24hours, DayFirst); var tmp = new StopData { Location = TimeVal, Duration = (isInMile) ? ((int)(dr.GetInt32(2) * 0.621371192)).ToString() + " " + milesh : dr.GetInt32(2).ToString() + " " + kmh, Time = "Speeding", Data = (VehIDHash[dr.GetInt32(1)] != null) ? (String)VehIDHash[dr.GetInt32(1)] : "", timeGMT = dr.GetInt32(0) }; if (tmp.Data != "") HistDataReport.Add(tmp); } } } if (tmpglwhere != "") tmpglwhere = tmpglwhere + " and alarm =1"; else tmpglwhere = " where alarm = 1"; command = $"SELECT sc_id,timeGMT,telemetry_id FROM telemetry_history {tmpglwhere} ORDER BY timeGMT desc"; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { String actionName = ""; if (VehIDHash[dr.GetInt32(0)] != null) { foreach (TelemetryObj obj in ((VehicleForReports)vehicleHT[(String)VehIDHash[dr.GetInt32(0)]]).telemList) if (obj.Id == dr.GetInt32(2)) { actionName = obj.Name; break; } } TimeVal = dr.GetInt32(1).GetRegionalFormat(is24hours, DayFirst); var tmp = new StopData { Location = TimeVal, Duration = actionName, Time = "Telemetry", Data = (VehIDHash[dr.GetInt32(0)] != null) ? (String)VehIDHash[dr.GetInt32(0)] : "", timeGMT = dr.GetInt32(1) }; if (tmp.Data != "") HistDataReport.Add(tmp); } } } } HistDataReport.Sort(delegate (StopData li1, StopData li2) { // -1 is for descending order return (-1) * (li1.timeGMT.CompareTo(li2.timeGMT)); }); } catch (Exception ex) { Console.WriteLine(ex.Message, ConsoleColor.Red); } return HistDataReport; } public List get_Speeding(String glwhere, Boolean is24hours, Boolean isInMile, Hashtable VehIDHash) { List SpeedingList = new List(); Int32 count50000 = 0; try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); string command = $"SELECT timeGMT,sc_id,speed,address,lat,lng FROM speedalarm {glwhere} ORDER BY timeGMT desc"; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { count50000++; var tmp = new SpeedData { Name = (VehIDHash[dr.GetInt32(1)] != null) ? (String)VehIDHash[dr.GetInt32(1)] : "", Speed = (isInMile) ? ((int)(dr.GetInt32(2) * 0.621371192)).ToString() : dr.GetInt32(2).ToString(), Time = dr.GetInt32(0).TimeOfDayHHMMLocal(!is24hours), Data = dr.GetInt32(0).GetDTLocalFromSeconds().Date.ToShortDateString(), Address = dr.GetString(3), sc_id = dr.GetInt32(1), timeGMT = dr.GetInt32(0), lat = (dr.IsDBNull(4) ? 0 : dr.GetDouble(4)), lng = (dr.IsDBNull(5) ? 0 : dr.GetDouble(5)) }; if (tmp.Name != "") SpeedingList.Add(tmp); if (count50000 > 50000) break; } } } } } catch (Exception ex) { Console.WriteLine(ex.Message, ConsoleColor.Red); } return SpeedingList; } public List get_LANDANDZONE(String glwhere, rep_type typ, Boolean is24hours, Boolean DayFirst, Hashtable VehIDHash, Hashtable ZoneIDHash, Hashtable LandIDHash) { List LandList = new List(); String newglwhere = glwhere; String ToAdd = " type=2 "; if (typ == rep_type.GEOFENC) ToAdd = " type=1 "; if (newglwhere.Length > 1) newglwhere = newglwhere + " and " + ToAdd; else newglwhere = " where " + ToAdd; try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); string command = $"SELECT sc_id,zone_id,action,timeGMT FROM geozoneinout {newglwhere} ORDER BY timeGMT desc"; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { LandData tmp = new LandData(); if (VehIDHash[dr.GetInt32(0)] != null) tmp.Name = (String)VehIDHash[dr.GetInt32(0)]; else tmp.Name = ""; tmp.LandType = ((uint)dr.GetInt32(2) == 1) ? "OUT" : "IN"; tmp.Time = dr.GetInt32(3).GetRegionalFormat(is24hours, DayFirst); tmp.LandName = dr.GetInt32(0).TimeOfDayHHMMLocal(); if (typ == rep_type.GEOFENC) { if (ZoneIDHash[dr.GetInt32(1)] != null) tmp.LandName = ((ZoneClass)ZoneIDHash[dr.GetInt32(1)]).Name; } else { SM.Debug("LandID" + dr.GetInt32(1)); if (LandIDHash[dr.GetInt32(1)] != null) tmp.LandName = (String)LandIDHash[dr.GetInt32(1)]; } if (tmp.Name != "") LandList.Add(tmp); } } } } } catch (Exception ex) { Console.WriteLine(ex.Message, ConsoleColor.Red); } return LandList; } public List get_log_view_all(String glwhere, Boolean is24hours, Boolean DayFirst, Hashtable VehIMEIHash) { String tmpname = ""; String TimeVal = ""; string Stat = ""; List ONOFFList = new List(); try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); string command = $"SELECT imei,timeGMT,status FROM logmototurbo {glwhere} ORDER BY timeGMT desc"; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { NpgsqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { if (VehIMEIHash[dr.GetString(0)] != null) tmpname = (VehIMEIHash[dr.GetString(0)] as String); else tmpname = ""; if (tmpname != "") { TimeVal = dr.GetInt32(1).GetRegionalFormat(is24hours, DayFirst); switch (dr.GetInt32(2)) { case 0: Stat = "OFF"; break; case 1: Stat = "ON"; break; case 9: Stat = "MADE OFF"; break; case 10: Stat = "MADE ON"; break; } var tmp = new ONOFFData { Name = tmpname, Time = TimeVal, Status = Stat }; ONOFFList.Add(tmp); } } } } } catch (Exception ex) { Console.WriteLine(ex.Message, ConsoleColor.Red); } return ONOFFList; } public List get_EMERG(String glwhere, Boolean is24hours, Hashtable VehIDHash) { List ONOFFList = new List(); try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); string command = $"SELECT timeGMT,sc_id,address,lat,lng FROM emergalarm {glwhere} ORDER BY timeGMT desc"; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { var tmp = new ONOFFData { Name = (VehIDHash[dr.GetInt32(1)] != null) ? (String)VehIDHash[dr.GetInt32(1)] : "", Time = (is24hours) ? (dr.GetInt32(0).GetDTLocalFromSeconds()).ToString("MM/dd/yyyy HH:mm:ss") : (dr.GetInt32(0).GetDTLocalFromSeconds()).ToString("MM/dd/yyyy hh:mm:ss tt"), Status = dr.GetString(2), sc_id = dr.GetInt32(1), lat = dr.GetDouble(3), lng = dr.GetDouble(4), timeGMT = dr.GetInt32(0) }; if (tmp.Name != "") ONOFFList.Add(tmp); } } } } } catch (Exception ex) { Console.WriteLine(ex.Message, ConsoleColor.Red); } return ONOFFList; } public List get_History(String glwhere, Boolean compAddress, Boolean forceLATLNG, Boolean is24hours, Boolean isInMile, ref int cntaddr, ref Queue AddrGISQueue, ref Hashtable IdReportHS) { List HistDataReport = new List(); string latlng = ""; try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); string command = "SELECT timeGMT,address,speed,lat,lng,scevtime, sc_id, W.name, W.driver_id FROM messages " + $" INNER JOIN vehicle W on sc_id = W.id {glwhere} ORDER BY timeGMT DESC"; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { String Address = ""; latlng = "LAT:" + Math.Round(dr.GetDouble(3), 5).ToString() + " , LNG:" + Math.Round(dr.GetDouble(4), 5).ToString(); if (!forceLATLNG) { if (!dr.IsDBNull(1)) Address = dr.GetString(1); if ((dr.GetDouble(3) == 0) || (dr.GetDouble(4) == 0)) Address = "NO GPS FIX"; else if ((Address == "") || (Address == " ")) { if (compAddress) { cntaddr++; IdReportHS[cntaddr.ToString()] = string.Format("no address ({0})", latlng); AddrGISQueue.Enqueue(new AddrAndID(cntaddr, dr.GetDouble(3), dr.GetDouble(4))); Address = cntaddr.ToString(); } else Address = latlng; } } else Address = "LAT:" + Math.Round(dr.GetDouble(3), 5).ToString() + " , LNG:" + Math.Round(dr.GetDouble(4), 5).ToString(); var tmp = new StopData { Location = Address, Duration = (isInMile) ? ((int)(dr.GetInt32(2) * 0.621371192)).ToString() : dr.GetInt32(2).ToString(), Time = dr.GetInt32(0).TimeOfDayHHMMSSLocal(!is24hours), Data = (dr.GetInt32(0).GetDTLocalFromSeconds()).Date.ToShortDateString(), unit_sc_id = dr.GetInt32(6), unit_name = dr.GetString(7), unique_id = dr.GetDouble(5) }; HistDataReport.Add(tmp); } } } } } catch (Exception ex) { Console.WriteLine(ex.Message, ConsoleColor.Red); } return HistDataReport; } public List get_TelemHist_alarm_event(String glwhere, Hashtable VehIDHash, Hashtable vehicleHT) { List SpeedingList = new List(); try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); string command = $"SELECT timegmt,sc_id,telemetry_id,address,lat,lng FROM telemetry_history {glwhere} ORDER BY timeGMT desc"; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { String actionName = ""; if (VehIDHash[dr.GetInt32(1)] != null) { foreach (TelemetryObj obj in ((VehicleForReports)vehicleHT[(String)VehIDHash[dr.GetInt32(1)]]).telemList) if (obj.Id == dr.GetInt32(2)) { actionName = obj.Name; break; } } var tmp = new SpeedData { Name = (VehIDHash[dr.GetInt32(1)] != null) ? (String)VehIDHash[dr.GetInt32(1)] : "", Speed = actionName, Time = dr.GetInt32(0).TimeOfDayHHMMSSLocal(false), Data = (dr.GetInt32(0).GetDTLocalFromSeconds()).Date.ToShortDateString(), Address = dr.GetString(3), sc_id = dr.GetInt32(1), timeGMT = dr.GetInt32(0), lat = dr.GetDouble(4), lng = dr.GetDouble(5) }; if (tmp.Name != "") SpeedingList.Add(tmp); } } } } } catch (Exception ex) { Console.WriteLine(ex.Message, ConsoleColor.Red); } return SpeedingList; } public List get_SMS_Location(String glwhere, Boolean is24hours, Hashtable VehIDHash) { List SMSLocList = new List(); try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); string command = $"SELECT timeGMT,sc_id_sour,mess,address,lat,lng FROM sms {glwhere} ORDER BY timeGMT desc"; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { while (dr.Read()) { var tmp = new SMS_Location { Name = (VehIDHash[dr.GetInt32(1)] != null) ? (String)VehIDHash[dr.GetInt32(1)] : "", Text_mess = dr.GetString(2), Time = dr.GetInt32(0).TimeOfDayHHMMLocal(!is24hours), Data = (dr.GetInt32(0).GetDTLocalFromSeconds()).Date.ToShortDateString(), Address = dr.GetString(3), sc_id = dr.GetInt32(1), timeGMT = dr.GetInt32(0), lat = dr.GetDouble(4), lng = dr.GetDouble(5) }; if (tmp.Name != "") SMSLocList.Add(tmp); } } } } } catch (Exception ex) { Console.WriteLine(ex.Message, ConsoleColor.Red); } return SMSLocList; } public List get_Ticketing(String glwhere, Boolean is24hours) { List SpeedingList = new List(); try { string command = "select veh.name, jt.ticket_id, jts.status, jt.start_time, jt.end_time " + "from jobtickets jt " + "inner join " + "( " + "select ticket_id, max(modified_date) as modified_date,job_Status,sc_id from jobtickets_log " + "group by ticket_id,sc_id,job_status " + ") jtl on jtl.ticket_id = jt.ticket_id and jtl.job_status = jt.job_status and jtl.sc_id = jt.sc_id " + "inner join jobticketstatusesset jts on jts.status_id = jt.job_status " + "inner join vehicle veh on veh.id = jt.sc_id " + glwhere + " order by jt.ticket_id desc "; using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { var tmp = new SpeedData { Name = dr.GetString(0), Speed = dr.GetInt32(1).ToString(), Time = dr.GetString(2), Data = (dr.GetValue(3).ToString() == "") ? "" : dr.GetInt32(3).GetDTLocalFromSeconds().Date.ToShortDateString(), Address = (dr.GetValue(4).ToString() == "") ? "" : dr.GetInt32(4).GetDTLocalFromSeconds().Date.ToShortDateString(), }; if (tmp.Name != "") SpeedingList.Add(tmp); } } } } } catch (Exception ex) { Console.WriteLine(ex.Message, ConsoleColor.Red); } return SpeedingList; } public List getUserReportTime() { List UserReportTimeList = new List(); try { string command = "SELECT user_id, value, login FROM \"userSettings\"" + " INNER JOIN " + " ( " + "SELECT DISTINCT(userid) AS uid FROM reports " + " ) r ON r.uid = user_id " + " INNER JOIN users u on u.userid = user_id " + " WHERE key='reportTime'"; using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { NpgsqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); while (dr.Read()) { int seconds = 0; string value = dr.GetString(1); Int32.TryParse(value, out seconds); DateTime DT = DateTime.Today.AddSeconds(seconds); var tmp = new UserReportTime { UserID = dr.GetInt32(0), UserName = dr.GetString(2), HH = DT.Hour, MM = DT.Minute, ReportExecuted = false }; UserReportTimeList.Add(tmp); } } } } catch (Exception ex) { Console.WriteLine(ex.Message, ConsoleColor.Red); } return UserReportTimeList; } public ArrayList get_position_for_report(Int32 timeMIN, Int32 timeMAX, Int32 sc_id) { ArrayList ret = new ArrayList(); try { Double scevtimemin = 0, scevtimemax = 0; scevtimemin = sc_id * 10000000000 + timeMIN; scevtimemax = sc_id * 10000000000 + timeMAX; using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); string command = String.Format("SELECT timeGMT , address , lat , lng , di , speed FROM messages WHERE scevtime >{0} and scevtime <{1} ORDER by scevtime", scevtimemin, scevtimemax); using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { using (NpgsqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { string address = dr.IsDBNull(1) ? "" : dr.GetString(1); if ((dr.GetDouble(2) != 0) && (dr.GetDouble(3) != 0)) ret.Add(new PositionData(dr.GetDouble(2), dr.GetDouble(3), address, dr.GetInt32(0), car_state_e.CAR_RUNNING, dr.GetInt32(5))); } } } } } catch (Exception ex) { Console.WriteLine(ex.Message, ConsoleColor.Red); } return ret; } } }