SafeDispatch/SafeMobileLIB_DLL/DBmanagers/DBReportingManager.cs

972 lines
42 KiB
C#
Raw Permalink Normal View History

2024-02-22 16:43:59 +00:00
using System;
using System.Linq;
2024-02-22 16:43:59 +00:00
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<StopData> get_ALLAlarm(String glwhere, Boolean is24hours, Boolean isInMile, Boolean DayFirst, Hashtable VehIDHash, Hashtable ZoneIDHash, Hashtable LandIDHash, Hashtable vehicleHT, string milesh, string kmh)
{
int sc_id = 0, timeGMT = 0, zone_id = 0, speed = 0, telemetry_id = 0, action = 0, type = 0;
2024-02-22 16:43:59 +00:00
String tmpglwhere = glwhere;
2024-02-22 16:43:59 +00:00
List<StopData> HistDataReport = new List<StopData>();
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())
{
2024-02-22 16:43:59 +00:00
while (dr.Read())
{
sc_id = Convert.ToInt32(dr["sc_id"]);
if (VehIDHash.ContainsKey(sc_id) && (String)VehIDHash[sc_id] != "")
{
timeGMT = Convert.ToInt32(dr["timeGMT"]);
2024-02-22 16:43:59 +00:00
HistDataReport.Add(new StopData
{
Location = timeGMT.GetRegionalFormat(is24hours, DayFirst),
Duration = "",
Time = "Emergency",
Data = (String)VehIDHash[sc_id],
timeGMT = timeGMT
});
}
2024-02-22 16:43:59 +00:00
}
}
}
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())
{
sc_id = Convert.ToInt32(dr["sc_id"]);
2024-02-22 16:43:59 +00:00
if (VehIDHash.ContainsKey(sc_id) && (String)VehIDHash[sc_id] != "")
2024-02-22 16:43:59 +00:00
{
timeGMT = Convert.ToInt32(dr["timeGMT"]);
zone_id = Convert.ToInt32(dr["zone_id"]);
action = Convert.ToInt32(dr["action"]);
type = Convert.ToInt32(dr["type"]);
2024-02-22 16:43:59 +00:00
string descStr = ((uint)action == 1) ? "OUT " : "IN ";
if (type == 1)
{
if (ZoneIDHash[zone_id] != null)
descStr = descStr + ((ZoneClass)ZoneIDHash[zone_id]).Name;
}
else
{
if (LandIDHash[zone_id] != null)
descStr = descStr + ((String)LandIDHash[zone_id]);
}
HistDataReport.Add(new StopData
{
Location = timeGMT.GetRegionalFormat(is24hours, DayFirst),
Duration = descStr,
Time = "Geo-Fence",
Data = (String)VehIDHash[sc_id],
timeGMT = timeGMT
});
}
2024-02-22 16:43:59 +00:00
}
}
}
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())
{
sc_id = Convert.ToInt32(dr["sc_id"]);
if (VehIDHash.ContainsKey(sc_id) && (String)VehIDHash[sc_id] != "")
2024-02-22 16:43:59 +00:00
{
timeGMT = Convert.ToInt32(dr["timeGMT"]);
speed = Convert.ToInt32(dr["speed"]);
HistDataReport.Add(new StopData
{
Location = timeGMT.GetRegionalFormat(is24hours, DayFirst),
Duration = (isInMile) ? ((int)(speed * 0.621371192)).ToString() + " " + milesh : speed.ToString() + " " + kmh,
Time = "Speeding",
Data = (String)VehIDHash[sc_id],
timeGMT = timeGMT
});
}
2024-02-22 16:43:59 +00:00
}
}
}
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())
{
2024-02-22 16:43:59 +00:00
while (dr.Read())
{
sc_id = Convert.ToInt32(dr["sc_id"]);
2024-02-22 16:43:59 +00:00
if (VehIDHash.ContainsKey(sc_id) && (String)VehIDHash[sc_id] != "")
2024-02-22 16:43:59 +00:00
{
telemetry_id = Convert.ToInt32(dr["telemetry_id"]);
timeGMT = Convert.ToInt32(dr["timeGMT"]);
// get action name
var list = ((VehicleForReports)vehicleHT[(String)VehIDHash[sc_id]]).telemList;
string actionName = list.Where(x => x.Id == telemetry_id).Select(x => x.Name).FirstOrDefault();
HistDataReport.Add(new StopData
{
Location = timeGMT.GetRegionalFormat(is24hours, DayFirst),
Duration = actionName,
Time = "Telemetry",
Data = (String)VehIDHash[sc_id],
timeGMT = timeGMT
});
}
2024-02-22 16:43:59 +00:00
}
}
}
}
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<SpeedData> get_Speeding(String glwhere, Boolean is24hours, Boolean isInMile, Hashtable VehIDHash)
{
List<SpeedData> SpeedingList = new List<SpeedData>();
Int32 count50000 = 0;
Int32 sc_id = 0;
2024-02-22 16:43:59 +00:00
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++;
sc_id = Convert.ToInt32(dr["sc_id"]);
2024-02-22 16:43:59 +00:00
if (VehIDHash.ContainsKey(sc_id) && (String)VehIDHash[sc_id] != "")
{
SpeedingList.Add(new SpeedData
{
Name = (String)VehIDHash[sc_id],
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 = sc_id,
timeGMT = dr.GetInt32(0),
lat = (dr.IsDBNull(4) ? 0 : dr.GetDouble(4)),
lng = (dr.IsDBNull(5) ? 0 : dr.GetDouble(5))
});
if (count50000 > 50000)
break;
}
2024-02-22 16:43:59 +00:00
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message, ConsoleColor.Red);
}
return SpeedingList;
}
public List<LandData> get_LANDANDZONE(String glwhere, rep_type typ, Boolean is24hours, Boolean DayFirst, Hashtable VehIDHash, Hashtable ZoneIDHash, Hashtable LandIDHash)
{
List<LandData> LandList = new List<LandData>();
String newglwhere = glwhere;
String ToAdd = " type=2 ";
2024-02-22 16:43:59 +00:00
if (typ == rep_type.GEOFENC) ToAdd = " type=1 ";
if (newglwhere.Length > 1)
newglwhere = newglwhere + " and " + ToAdd;
else
newglwhere = " where " + ToAdd;
2024-02-22 16:43:59 +00:00
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))
{
int sc_id = 0, zone_id = 0;
2024-02-22 16:43:59 +00:00
using (NpgsqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
sc_id = Convert.ToInt32(dr["sc_id"]);
zone_id = Convert.ToInt32(dr["zone_id"]);
2024-02-22 16:43:59 +00:00
if (VehIDHash.ContainsKey(sc_id) && (String)VehIDHash[sc_id] != "")
2024-02-22 16:43:59 +00:00
{
LandData tmp = new LandData();
tmp.Name = (String)VehIDHash[sc_id];
tmp.LandType = Convert.ToInt32(dr["action"]) == 1 ? "OUT" : "IN";
tmp.Time = Convert.ToInt32(dr["timeGMT"]).GetRegionalFormat(is24hours, DayFirst);
tmp.LandName = sc_id.TimeOfDayHHMMLocal();
if (typ == rep_type.GEOFENC)
{
if (ZoneIDHash[zone_id] != null)
tmp.LandName = ((ZoneClass)ZoneIDHash[zone_id]).Name;
}
else
{
if (LandIDHash[zone_id] != null)
tmp.LandName = (String)LandIDHash[zone_id];
}
2024-02-22 16:43:59 +00:00
LandList.Add(tmp);
}
2024-02-22 16:43:59 +00:00
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message, ConsoleColor.Red);
}
return LandList;
}
public List<ONOFFData> get_log_view_all(String glwhere, Boolean is24hours, Boolean DayFirst, Hashtable VehIMEIHash)
{
String tmpname = "";
string Stat = "";
string imei = "";
int timeGMT = 0;
2024-02-22 16:43:59 +00:00
List<ONOFFData> ONOFFList = new List<ONOFFData>();
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())
{
imei = dr["imei"].ToString();
timeGMT = Convert.ToInt32(dr["timeGMT"]);
tmpname = "";
if (VehIMEIHash[imei] != null)
tmpname = (VehIMEIHash[imei] as String);
2024-02-22 16:43:59 +00:00
if (tmpname != "")
{
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;
2024-02-22 16:43:59 +00:00
}
ONOFFList.Add( new ONOFFData
{
Name = tmpname,
Time = timeGMT.GetRegionalFormat(is24hours, DayFirst),
Status = Stat
});
2024-02-22 16:43:59 +00:00
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message, ConsoleColor.Red);
}
return ONOFFList;
}
public List<ONOFFData> get_EMERG(String glwhere, Boolean is24hours, Hashtable VehIDHash)
{
List<ONOFFData> ONOFFList = new List<ONOFFData>();
int timeGMT = 0, sc_id = 0;
2024-02-22 16:43:59 +00:00
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())
{
string formatDateTime = is24hours ? "MM/dd/yyyy HH:mm:ss" : "MM/dd/yyyy hh:mm:ss tt";
2024-02-22 16:43:59 +00:00
while (dr.Read())
{
sc_id = Convert.ToInt32(dr["sc_id"]);
if (VehIDHash.ContainsKey(sc_id) && (String)VehIDHash[sc_id] != "")
2024-02-22 16:43:59 +00:00
{
timeGMT = Convert.ToInt32(dr["timegmt"]);
ONOFFList.Add( new ONOFFData
{
Name = (String)VehIDHash[sc_id],
Time = timeGMT.GetDTLocalFromSeconds().ToString(formatDateTime),
Status = dr["address"].ToString(),
sc_id = sc_id,
lat = Convert.ToDouble(dr["lat"]),
lng = Convert.ToDouble(dr["lng"]),
timeGMT = timeGMT
});
}
2024-02-22 16:43:59 +00:00
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message, ConsoleColor.Red);
}
return ONOFFList;
}
private BatchHistoryData GetData(NpgsqlDataReader dr, bool isInMile, bool is24hours, bool getAddress = false)
{
int timegmt = 0;
double lat = 0, lng = 0;
BatchHistoryData batchHistObj = null;
try
{
lat = Convert.ToDouble(dr["lat"]);
lng = Convert.ToDouble(dr["lng"]);
timegmt = Convert.ToInt32(dr["timegmt"]);
batchHistObj = new BatchHistoryData
{
Department = dr["department"].ToString(),
Unit = dr["unit"].ToString(),
Location = ((getAddress && dr["address"] != DBNull.Value) ? dr["address"].ToString() : "LAT:" + Math.Round(lat, 5).ToString() + " , LNG:" + Math.Round(lat, 5).ToString()),
Speed = (isInMile) ? ((int)(Convert.ToInt32(dr["speed"]) * 0.621371192)).ToString() : Convert.ToInt32(dr["speed"]).ToString(),
Lat = lat,
Lng = lng,
Address = dr["address"].ToString(),
Time = timegmt.TimeOfDayHHMMSSLocal(!is24hours),
Date = timegmt.GetDTLocalFromSeconds().Date.ToShortDateString()
};
}
catch (Exception ex)
{
Console.WriteLine(ex.Message, ConsoleColor.Red);
}
return batchHistObj;
}
private string FormatCommandBatchHistory(string sc_ids, int startdate, int enddate, bool isInMile, bool is24hours, int limit = Int32.MaxValue, bool getAddress = false, bool getOnlyValidPositions = false)
{
string address = "";
string join_getiing_address = "";
string onlyValidPositions = "";
if (getAddress)
{
address = ",a.address";
join_getiing_address = "LEFT JOIN address a ON a.lat = ROUND(z.lat::numeric, 4) AND a.lng = ROUND(z.lng::numeric, 4) ";
}
if (getOnlyValidPositions)
{
onlyValidPositions = " AND m.lat <> 0.0 AND m.lng <> 0.0 ";
}
string command = $" SELECT x.grp_name as department, x.veh_name as unit, z.lat, z.lng, z.timegmt, z.speed {address} " +
" FROM " +
" (SELECT m.sc_id, m.timegmt, m.lat, m.lng, m.speed " +
" FROM messages m " +
$" WHERE m.timegmt BETWEEN {startdate} AND {enddate} " +
$" {onlyValidPositions}" +
$" AND m.sc_id IN ({sc_ids}) { (limit != Int32.MaxValue ? $"LIMIT {limit}" : "")} " +
" )z " +
" INNER JOIN " +
" ( " +
" SELECT sh.sc_id, v.name as veh_name, g.name as grp_name " +
$" FROM (SELECT * FROM subscriber_history WHERE sc_id IN ({sc_ids})) sh " +
" INNER JOIN vehicle v ON v.id = sh.veh_id " +
" INNER JOIN vehicle_group vg ON sh.sc_id = vg.sc_id " +
" INNER JOIN groups g ON vg.grp_ip = g.id " +
" ) x ON z.sc_id = x.sc_id " +
$"{join_getiing_address}" +
" ORDER BY x.grp_name, x.veh_name, z.timegmt ";
return command;
}
public List<BatchHistoryData> getBatchHistory(string sc_ids, int startdate, int enddate , bool isInMile, bool is24hours, int limit = Int32.MaxValue, bool getAddress = false, bool getOnlyValidPositions = false)
{
var list = new List<BatchHistoryData>();
using (NpgsqlConnection connection = new NpgsqlConnection())
{
connection.ConnectionString = getConnectionString();
connection.Open();
string command = FormatCommandBatchHistory(sc_ids, startdate, enddate, isInMile, is24hours, limit, getAddress, getOnlyValidPositions);
using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection))
{
cmd.CommandTimeout = 1200;
using (NpgsqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
BatchHistoryData data = GetData(dr, isInMile, is24hours, getAddress);
list.Add(data);
}
}
}
}
return list;
}
public void saveBatchHistoryToCsv(string filePath, List<string> excludeColumns, string sc_ids, int startdate, int enddate, bool isInMile, bool is24hours, int limit = Int32.MaxValue, bool getAddress = false, bool getOnlyValidPositions = false)
{
using (NpgsqlConnection connection = new NpgsqlConnection())
{
connection.ConnectionString = getConnectionString();
connection.Open();
string command = FormatCommandBatchHistory(sc_ids, startdate, enddate, isInMile, is24hours, limit, getAddress, getOnlyValidPositions);
using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection))
{
cmd.CommandTimeout = 1200;
using (NpgsqlDataReader dr = cmd.ExecuteReader())
{
// Being a huge report we get record by record and save them into file (without keep them).
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(filePath, false))
{
//================
// write header
//================
Utils.HeaderToCsv<BatchHistoryData>(sw, excludeColumns);
while (dr.Read())
{
//================
// write rows
//================
BatchHistoryData data = GetData(dr, isInMile, is24hours, getAddress);
Utils.RowToCsv<BatchHistoryData>(sw, data, excludeColumns);
}
}
}
}
}
}
2024-02-22 16:43:59 +00:00
public List<StopData> get_History(String glwhere, Boolean compAddress, Boolean forceLATLNG, Boolean is24hours, Boolean isInMile, ref int cntaddr, ref Queue AddrGISQueue, ref Hashtable IdReportHS)
{
List<StopData> HistDataReport = new List<StopData>();
string latlng = "";
try
{
using (NpgsqlConnection connection = new NpgsqlConnection())
{
2024-02-22 16:43:59 +00:00
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";
2024-02-22 16:43:59 +00:00
using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection))
{
cmd.CommandTimeout = 1200;
2024-02-22 16:43:59 +00:00
using (NpgsqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
double lat = Convert.ToDouble(dr["lat"]);
double lng = Convert.ToDouble(dr["lng"]);
2024-02-22 16:43:59 +00:00
String Address = "";
latlng = "LAT:" + Math.Round(lat, 5).ToString() + " , LNG:" + Math.Round(lat, 5).ToString();
2024-02-22 16:43:59 +00:00
if (!forceLATLNG)
{
if (!dr.IsDBNull(1))
Address = dr["address"].ToString();
if ((lat == 0.0) || (lng == 0.0))
Address = "NO GPS FIX";
2024-02-22 16:43:59 +00:00
else if ((Address == "") || (Address == " "))
{
if (compAddress)
{
cntaddr++;
IdReportHS[cntaddr.ToString()] = string.Format("no address ({0})", latlng);
AddrGISQueue.Enqueue(new AddrAndID(cntaddr, lat, lng));
2024-02-22 16:43:59 +00:00
Address = cntaddr.ToString();
}
else Address = latlng;
}
}
else
Address = latlng;
HistDataReport.Add(new StopData
{
Location = Address,
Duration = (isInMile) ? ((int)(Convert.ToInt32(dr["speed"]) * 0.621371192)).ToString() : Convert.ToInt32(dr["speed"]).ToString(),
Time = Convert.ToInt32(dr["timeGMT"]).TimeOfDayHHMMSSLocal(!is24hours),
Data = (Convert.ToInt32(dr["timeGMT"]).GetDTLocalFromSeconds()).Date.ToShortDateString(),
unit_sc_id = Convert.ToInt32(dr["sc_id"]),
unit_name = dr["name"].ToString(),
unique_id = Convert.ToDouble(dr["scevtime"])
});
2024-02-22 16:43:59 +00:00
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message, ConsoleColor.Red);
}
return HistDataReport;
}
2024-02-22 16:43:59 +00:00
public List<SpeedData> get_TelemHist_alarm_event(String glwhere, Hashtable VehIDHash, Hashtable vehicleHT)
{
List<SpeedData> SpeedingList = new List<SpeedData>();
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())
{
int timeGMT = 0, sc_id = 0, telemetry_id = 0;
2024-02-22 16:43:59 +00:00
while (dr.Read())
{
sc_id = Convert.ToInt32(dr["sc_id"]);
if (VehIDHash.ContainsKey(sc_id) && (String)VehIDHash[sc_id] != "")
2024-02-22 16:43:59 +00:00
{
timeGMT = Convert.ToInt32(dr["timegmt"]);
telemetry_id = Convert.ToInt32(dr["telemetry_id"]);
// get action name
var list = ((VehicleForReports)vehicleHT[(String)VehIDHash[sc_id]]).telemList;
string actionName = list.Where(x => x.Id == telemetry_id).Select(x => x.Name).FirstOrDefault();
SpeedingList.Add(new SpeedData
{
Name = (String)VehIDHash[sc_id],
Speed = actionName,
Time = timeGMT.TimeOfDayHHMMSSLocal(false),
Data = (timeGMT.GetDTLocalFromSeconds()).Date.ToShortDateString(),
Address = dr["address"].ToString(),
sc_id = sc_id,
timeGMT = timeGMT,
lat = Convert.ToDouble(dr["lat"]),
lng = Convert.ToDouble(dr["lng"])
});
}
2024-02-22 16:43:59 +00:00
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message, ConsoleColor.Red);
}
return SpeedingList;
}
public List<SMS_Location> get_SMS_Location(String glwhere, Boolean is24hours, Hashtable VehIDHash)
{
List<SMS_Location> SMSLocList = new List<SMS_Location>();
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))
{
int sc_id = 0, timeGMT = 0;
2024-02-22 16:43:59 +00:00
while (dr.Read())
{
sc_id = Convert.ToInt32(dr["sc_id_sour"]);
if (VehIDHash.ContainsKey(sc_id) && (String)VehIDHash[sc_id] != "")
2024-02-22 16:43:59 +00:00
{
timeGMT = Convert.ToInt32(dr["timeGMT"]);
SMSLocList.Add(new SMS_Location
{
Name = (String)VehIDHash[sc_id],
Text_mess = dr["mess"].ToString(),
Time = timeGMT.TimeOfDayHHMMLocal(!is24hours),
Data = (timeGMT.GetDTLocalFromSeconds()).Date.ToShortDateString(),
Address = dr["address"].ToString(),
sc_id = sc_id,
timeGMT = timeGMT,
lat = Convert.ToDouble(dr["lat"]),
lng = Convert.ToDouble(dr["lng"])
});
}
2024-02-22 16:43:59 +00:00
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message, ConsoleColor.Red);
}
return SMSLocList;
}
public List<SpeedData> get_Ticketing(String glwhere, Boolean is24hours)
{
List<SpeedData> SpeedingList = new List<SpeedData>();
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["name"].ToString(),
Speed = dr["ticket_id"].ToString(),
Time = dr["status"].ToString(),
Data = (dr["start_time"].ToString() == "") ? "" : Convert.ToInt32(dr["start_time"]).GetDTLocalFromSeconds().Date.ToShortDateString(),
Address = (dr["end_time"].ToString() == "") ? "" : Convert.ToInt32(dr["end_time"]).GetDTLocalFromSeconds().Date.ToShortDateString(),
2024-02-22 16:43:59 +00:00
};
2024-02-22 16:43:59 +00:00
if (tmp.Name != "")
SpeedingList.Add(tmp);
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message, ConsoleColor.Red);
}
return SpeedingList;
}
public List<UserReportTime> getUserReportTime()
{
List<UserReportTime> UserReportTimeList = new List<UserReportTime>();
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["value"].ToString();
2024-02-22 16:43:59 +00:00
Int32.TryParse(value, out seconds);
DateTime DT = DateTime.Today.AddSeconds(seconds);
UserReportTimeList.Add(new UserReportTime
{
UserID = Convert.ToInt32(dr["user_id"]),
UserName = dr["login"].ToString(),
HH = DT.Hour,
MM = DT.Minute,
ReportExecuted = false
});
2024-02-22 16:43:59 +00:00
}
}
}
}
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())
{
double lat = Convert.ToDouble(dr["lat"]);
double lng = Convert.ToDouble(dr["lng"]);
string address = dr.IsDBNull(1) ? "" : dr["address"].ToString();
int speed = Convert.ToInt32(dr["speed"]);
int timeGMT = Convert.ToInt32(dr["timeGMT"]);
if (lat != 0 && lng != 0)
ret.Add(new PositionData(lat, lng, address, timeGMT, car_state_e.CAR_RUNNING, speed));
2024-02-22 16:43:59 +00:00
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message, ConsoleColor.Red);
}
return ret;
}
}
}