SafeDispatch/ReportsLibrary/Reports/ClassForReports.cs

708 lines
27 KiB
C#
Raw Permalink Normal View History

2024-02-22 16:43:59 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections;
//using MapGoogle;
using System.IO;
using System.Threading;
//using Telerik.WinControls.UI;
using System.Windows.Forms;
using SafeMobileLib;
using System.Xml;
namespace ReportsLibrary
{
[DataObject]
public class ONOFFDataObj
{
[DataObjectMethod(DataObjectMethodType.Select)]
public List<ONOFFData> GetALL()
{
return StaticMembers.ONOFFList;
}
}
[DataObject]
public class SpeedDataObj
{
[DataObjectMethod(DataObjectMethodType.Select)]
public List<SpeedData> GetALL()
{
return StaticMembers.SpeedingList;
}
}
[DataObject]
public class LandDataObj
{
[DataObjectMethod(DataObjectMethodType.Select)]
public List<LandData> GetALL()
{
return StaticMembers.LandList;
}
}
[DataObject]
public class EndOfDayDataObj
{
public static String ReverseGeoOSM = "127.0.0.1";
[DataObjectMethod(DataObjectMethodType.Select)]
public List<EndOfDayData> GetALL()
{
CalcStops calcStop = new CalcStops(true);//mapControl
calcStop.get_position(StaticMembers.PositionList);
calcStop.calculate_zone();
calcStop.calc_zone_with_delta2();
try
{
calcStop.create_stop_list();
}
catch (Exception ex)
{
// throw new ArgumentException("Database connection failed");
SM.Debug("Error create stop list: " + ex.ToString());
}
if (StaticMembers.StartStop)
{
calcStop.StartStopCalc();
calcStop.StartStopCalcPosition();
}
// calcul distanta
ArrayList DriveList = new ArrayList();
Boolean first = true;
Double distance = .01f;
distance = 0;
//Double LatStart = 0, LngStart = 0, LatStop = 0, LngStop = 0;
Int32 timeStart = 0, timeStop = 0, oldStop = 0;
Int32 totaltime = 0, timeforStartCalc = 0;
SM.Debug("Stop Count:" + calcStop.StopsList.Count);
if (calcStop.StopsList.Count > 1)
{
Stops prevObj = null;
foreach (Stops obj in calcStop.StopsList)
{
if (prevObj != null)
{
if (!((prevObj.type == car_state_e.CAR_STOP) && (obj.type == car_state_e.CAR_START)))
{
if (first)
{
timeStart = prevObj.stop;
first = false;
}
else timeStart = oldStop;
timeStop = obj.start;
oldStop = obj.stop;
distance = calcStop.calcDistance(timeStart, timeStop);
DriveList.Add(distance);
}
else { first = true; }
}
prevObj = obj;
}
}
//loaddata
Int32 RowNumber = 0;
Int32 count3 = -1;
Double difidx = .01f;
Boolean lastStopIsStop = false;
Int32 lastStopTimeStop = 0;
Double total = 0;
String lastStopNameStop = "";
ArrayList numbers = new ArrayList();
ReverseGeocoding volltmpGo = new ReverseGeocoding(ReverseGeoOSM);
//calcul addressa Google
if (!StaticMembers.LATLNGforDinamicReports)
{
try
{
if ((StaticMembers.MapType == MapTYPE.ArcGis) || (StaticMembers.MapType == MapTYPE.Google))
{
foreach (Stops obj3 in calcStop.StopsList)
{
StaticMembers.AddrGISQueue.Enqueue(new AddrAndID(Convert.ToInt32(obj3.name), obj3.lat, obj3.lng));
if (StaticMembers.ShowCoordinates)
StaticMembers.IdReportHS.Add(obj3.name, "LAT:" + Math.Round(obj3.lat, 5).ToString() + " , LNG:" + Math.Round(obj3.lng, 5).ToString());
else
StaticMembers.IdReportHS.Add(obj3.name, obj3.address != "" ? obj3.address : volltmpGo.getAddressLATLNG(obj3.lat, obj3.lng));
}
bool retrynew = true;
int cntmax = 0;
foreach (String obj in StaticMembers.IdReportHS.Keys)
numbers.Add(obj);
while (retrynew)
{
cntmax++;
retrynew = false;
foreach (String obj in numbers)
{
if ((String)StaticMembers.IdReportHS[obj] == "no address")
{
SM.Debug("Sta 240 msecunda");
Thread.Sleep(240);
retrynew = true;
}
}
if (cntmax > 10) retrynew = false;
}
}
}
catch (Exception ex)
{
SM.Debug("Erorr on compute adress for report:" + ex.ToString());
}
}
//Load list for reports
var Ret = new List<EndOfDayData>();
Boolean jumpstop = true;
EndOfDayData tmpData = null;
foreach (Stops obj3 in calcStop.StopsList)
{
if (obj3.type == car_state_e.CAR_START)
{
try
{
tmpData = new EndOfDayData();
RowNumber++;
tmpData.Trip = Convert.ToString(RowNumber);
string dateTimeFormat = StaticMembers.is24hours ? "MM/dd/yyyy HH:mm:ss" : "MM/dd/yyyy hh:mm:ss tt";
tmpData.StartTime = ((obj3.stop).GetDTLocalFromSeconds()).ToString(dateTimeFormat);
2024-02-22 16:43:59 +00:00
timeforStartCalc = obj3.stop;
if (!StaticMembers.LATLNGforDinamicReports)
{
if ((StaticMembers.MapType == MapTYPE.Google) || (StaticMembers.MapType == MapTYPE.ArcGis))
tmpData.StartAddress = StaticMembers.IdReportHS[obj3.name].ToString();
}
else
{
tmpData.StartAddress = "LAT:" + Math.Round(obj3.lat, 5).ToString() + " , LNG:" + Math.Round(obj3.lng, 5).ToString();
}
lastStopIsStop = false;
jumpstop = false;
}
catch (Exception ex)
{
SM.Debug("Address null exceptie la getlist:" + ex.ToString());
}
}
else
{
if (!jumpstop)
{
try
{
if (lastStopIsStop)
{
tmpData = new EndOfDayData();
RowNumber++;
tmpData.Trip = Convert.ToString(RowNumber);
2024-02-22 16:43:59 +00:00
string dateTimeFormat = StaticMembers.is24hours ? "MM/dd/yyyy HH:mm:ss" : "MM/dd/yyyy hh:mm:ss tt";
tmpData.StartTime = ((lastStopTimeStop).GetDTLocalFromSeconds()).ToString(dateTimeFormat);
2024-02-22 16:43:59 +00:00
timeforStartCalc = lastStopTimeStop;
tmpData.StartAddress = lastStopNameStop;
}
count3++;
if (tmpData != null)
{
totaltime += (obj3.start - timeforStartCalc);
tmpData.StopTime = ((obj3.start).GetDTLocalFromSeconds()).ToString();
2024-02-22 16:43:59 +00:00
if (!StaticMembers.LATLNGforDinamicReports)
{
if ((StaticMembers.MapType == MapTYPE.Google) || (StaticMembers.MapType == MapTYPE.ArcGis))
tmpData.StopAddress = StaticMembers.IdReportHS[obj3.name].ToString();
}
else
tmpData.StopAddress = "LAT:" + Math.Round(obj3.lat, 5).ToString() + " , LNG:" + Math.Round(obj3.lng, 5).ToString();
2024-02-22 16:43:59 +00:00
difidx = (Double)DriveList[count3];
total += difidx;
if (StaticMembers.isInMile)
tmpData.Distance = (Math.Round(((difidx * 0.621371192) * 1000)) / 1000).ToString();
else
tmpData.Distance = difidx.ToString();
if (tmpData != null)
Ret.Add(tmpData);
2024-02-22 16:43:59 +00:00
}
lastStopIsStop = true;
lastStopTimeStop = obj3.stop;
2024-02-22 16:43:59 +00:00
if (!StaticMembers.LATLNGforDinamicReports)
{
if ((StaticMembers.MapType == MapTYPE.Google) || (StaticMembers.MapType == MapTYPE.ArcGis))
lastStopNameStop = StaticMembers.IdReportHS[obj3.name].ToString();
}
else
lastStopNameStop = "LAT:" + Math.Round(obj3.lat, 5).ToString() + " , LNG:" + Math.Round(obj3.lng, 5).ToString();
2024-02-22 16:43:59 +00:00
}
catch (Exception ex)
{
SM.Debug("eroare DriverList= " + ex.ToString());
}
}
jumpstop = false;
}
}
if (RowNumber > 0)
{
tmpData = new EndOfDayData();
tmpData.Trip = "";
tmpData.StartTime = "Total time:";
SM.Debug("Total time is:" + totaltime);
tmpData.StartAddress = String.Format("{0:00}", (int)(totaltime / 3600)) + ":" + String.Format("{0:00}", ((int)(((int)(totaltime % 3600)) / 60))) + ":" + String.Format("{0:00}", ((int)(totaltime % 60)));
tmpData.StopTime = "";
tmpData.StopAddress = "Total distance:";
if (StaticMembers.isInMile)
tmpData.Distance = (Math.Round(((total * 0.621371192) * 1000)) / 1000).ToString();
else
tmpData.Distance = total.ToString();
2024-02-22 16:43:59 +00:00
Ret.Add(tmpData);
}
//de la google address
if ((StaticMembers.MapType == MapTYPE.Google) || (StaticMembers.MapType == MapTYPE.ArcGis))
{
foreach (String obj in numbers)
StaticMembers.IdReportHS.Remove(obj);
numbers.Clear();
}
return Ret;
}
}
[DataObject]
public class StopDataObj
{
public static String ReverseGeoOSM = "127.0.0.1";
[DataObjectMethod(DataObjectMethodType.Select)]
public List<StopData> GetALL()
{
if (StaticMembers.IsHistroyNotStop)
return StaticMembers.HistDataReport;
//if (!StaticMembers.IsHistroyNotStop) // aici prelucrez date cand e raport de stop sau IDLE
CalcStops calcStop = new CalcStops(true);//mapControl
calcStop.get_position(StaticMembers.PositionList);
try
2024-02-22 16:43:59 +00:00
{
if (!StaticMembers.IsIdle)
2024-02-22 16:43:59 +00:00
{
calcStop.calculate_zone();
calcStop.calc_zone_with_delta2();
calcStop.create_stop_list();
2024-02-22 16:43:59 +00:00
}
else
2024-02-22 16:43:59 +00:00
{
calcStop.create_stop_list_EON(StaticMembers.IDLETime);
2024-02-22 16:43:59 +00:00
}
}
catch (Exception ex)
{
SM.Debug("Database connection failed:" + ex.ToString());
}
2024-02-22 16:43:59 +00:00
if (StaticMembers.StartStop)
{
calcStop.StartStopCalc();
calcStop.StartStopCalcPosition();
}
ArrayList numbers = new ArrayList();
ReverseGeocoding volltmpGo = new ReverseGeocoding(ReverseGeoOSM);
//calcul addressa Google
if (!StaticMembers.LATLNGforDinamicReports)
{
try
2024-02-22 16:43:59 +00:00
{
if ((StaticMembers.MapType == MapTYPE.ArcGis) || (StaticMembers.MapType == MapTYPE.Google))
2024-02-22 16:43:59 +00:00
{
foreach (Stops obj3 in calcStop.StopsList)
2024-02-22 16:43:59 +00:00
{
StaticMembers.AddrGISQueue.Enqueue(new AddrAndID(Convert.ToInt32(obj3.name), obj3.lat, obj3.lng));
if (StaticMembers.ShowCoordinates)
StaticMembers.IdReportHS.Add(obj3.name, "LAT:" + Math.Round(obj3.lat, 5).ToString() + " , LNG:" + Math.Round(obj3.lng, 5).ToString());
else
StaticMembers.IdReportHS.Add(obj3.name, obj3.address != "" ? obj3.address : volltmpGo.getAddressLATLNG(obj3.lat, obj3.lng));
}
if (!StaticMembers.ShowCoordinates)
{
bool retrynew = true;
int cntmax = 0;
foreach (String obj in StaticMembers.IdReportHS.Keys)
numbers.Add(obj);
while (retrynew)
2024-02-22 16:43:59 +00:00
{
cntmax++;
retrynew = false;
2024-02-22 16:43:59 +00:00
foreach (String obj in numbers)
2024-02-22 16:43:59 +00:00
{
if ((String)StaticMembers.IdReportHS[obj] == "no address")
2024-02-22 16:43:59 +00:00
{
SM.Debug("Sta 180 msec");
Thread.Sleep(180);
retrynew = true;
2024-02-22 16:43:59 +00:00
}
}
if (cntmax > 10)
retrynew = false;
2024-02-22 16:43:59 +00:00
}
}
}
}
catch (Exception ex)
{
SM.Debug("Erorr on compute adress for report:" + ex.ToString());
}
}
int countStops = -1;
long stoptime, starttime;
StopData tmpData = null;
Int32 RowNumber = 0;
var list = new List<StopData>();
2024-02-22 16:43:59 +00:00
foreach (Stops obj3 in calcStop.StopsList)
{
countStops++;
if (obj3.type == car_state_e.CAR_STOP)
2024-02-22 16:43:59 +00:00
{
// Populate StopData object
tmpData = new StopData();
try
2024-02-22 16:43:59 +00:00
{
RowNumber++;
2024-02-22 16:43:59 +00:00
tmpData.Data = ((obj3.start).GetDTLocalFromSeconds()).Date.ToShortDateString();
tmpData.Time = ((Int32)obj3.start).TimeOfDayHHMMLocal(!StaticMembers.is24hours);
2024-02-22 16:43:59 +00:00
// get location
if (!StaticMembers.LATLNGforDinamicReports)
{
if ((StaticMembers.MapType == MapTYPE.Google) || (StaticMembers.MapType == MapTYPE.ArcGis))
tmpData.Location = StaticMembers.IdReportHS[obj3.name].ToString();
2024-02-22 16:43:59 +00:00
}
else
tmpData.Location = "LAT:" + Math.Round(obj3.lat, 5).ToString() + " , LNG:" + Math.Round(obj3.lng, 5).ToString();
// calculate duration
stoptime = obj3.stop;
starttime = obj3.start;
if (countStops < (calcStop.StopsList.Count - 1))
2024-02-22 16:43:59 +00:00
{
if (((Stops)(calcStop.StopsList[countStops + 1])).type == car_state_e.CAR_START)
stoptime = ((Stops)(calcStop.StopsList[countStops + 1])).stop;
2024-02-22 16:43:59 +00:00
}
tmpData.Duration = FormatDuration(starttime, stoptime);
}
catch (Exception ex)
{
SM.Debug("EX:" + ex.ToString());
2024-02-22 16:43:59 +00:00
}
// add to list
list.Add(tmpData);
2024-02-22 16:43:59 +00:00
}
}
//de la google address
if ((StaticMembers.MapType == MapTYPE.Google) || (StaticMembers.MapType == MapTYPE.ArcGis))
2024-02-22 16:43:59 +00:00
{
foreach (String obj in numbers)
StaticMembers.IdReportHS.Remove(obj);
numbers.Clear();
2024-02-22 16:43:59 +00:00
}
return list;
}
private string FormatDuration(long starttime, long stoptime)
{
Int64 diffTime = stoptime - starttime;
Int32 Hour = (Int32)(diffTime / 3600);
Int32 Minute = (Int32)((diffTime - Hour * 3600) / 60);
return Hour.ToString("D2") + ":" + Minute.ToString("D2");
2024-02-22 16:43:59 +00:00
}
}
[DataObject]
public class BatchHistoryDataObj
{
[DataObjectMethod(DataObjectMethodType.Select)]
public List<BatchHistoryData> GetALL()
{
return StaticMembers.BatchHistoryReport;
}
}
2024-02-22 16:43:59 +00:00
[DataObject]
public class FleetDataObj
{
[DataObjectMethod(DataObjectMethodType.Select)]
public List<FleetData> GetALL()
{
var fleetDataList = new List<FleetData>();
2024-02-22 16:43:59 +00:00
if (!StaticMembers.IsSMSLocationNotFleet)
{
foreach (FleetIdandPos veh in StaticMembers.FleetPosList)
{
CalcStops calcStop = new CalcStops(true);//mapControl
calcStop.get_position(veh.Position);
try
{
StaticMembers.NameReport = veh.Name;
calcStop.calculate_zone();
calcStop.calc_zone_with_delta2();
calcStop.create_stop_list();
}
catch (Exception ex)
{
SM.Debug("Database connection failed" + ex.ToString());
}
if (StaticMembers.StartStop)
{
calcStop.StartStopCalc();
calcStop.StartStopCalcPosition();
}
// populate FleetData
2024-02-22 16:43:59 +00:00
FleetData tmpData = new FleetData();
tmpData.Name = veh.Name;
tmpData.Distance = calculatetDistance(calcStop);
tmpData.Trips = "0";
if (calcStop.StopsList.Count > 1)
tmpData.Trips = calculateTrips(calcStop);
2024-02-22 16:43:59 +00:00
tmpData.Start = "";
tmpData.Stop = "";
2024-02-22 16:43:59 +00:00
if ((calcStop.StopsList.Count != 0) && (calcStop.StopsList.Count != 1))
{
string formatDate = StaticMembers.is24hours ? "MM/dd/yyyy HH:mm:ss" : "MM/dd/yyyy HH:mm:ss tt";
tmpData.Start = ((((Stops)(calcStop.StopsList[0])).stop).GetDTLocalFromSeconds()).ToString(formatDate);
tmpData.Stop = ((((Stops)(calcStop.StopsList[calcStop.StopsList.Count - 1])).start).GetDTLocalFromSeconds()).ToString(formatDate);
2024-02-22 16:43:59 +00:00
}
// add to list
fleetDataList.Add(tmpData);
2024-02-22 16:43:59 +00:00
}
}
else
{
foreach (SMS_Location sms_obj in StaticMembers.SMSLocList)
{
fleetDataList.Add( new FleetData()
{
Name = sms_obj.Name,
Start = sms_obj.Data,
Stop = sms_obj.Address,
Trips = sms_obj.Time,
Distance = sms_obj.Text_mess
});
2024-02-22 16:43:59 +00:00
}
}
return fleetDataList;
}
private string calculateTrips(CalcStops calcStop)
{
int startnr = 0;
Stops prevStopObj = null;
foreach (Stops obj in calcStop.StopsList)
{
if (obj.type == car_state_e.CAR_START)
startnr++;
if (prevStopObj != null)
if ((prevStopObj.type == car_state_e.CAR_STOP) && (obj.type == car_state_e.CAR_STOP))
startnr++;
prevStopObj = obj;
}
return startnr.ToString();
}
private string calculatetDistance(CalcStops calcStop)
{
Double AllDistance = 0;
Boolean first = true;
Double distance = 0;
Int32 timeStart = 0, timeStop = 0, oldStop = 0;
if (calcStop.StopsList.Count > 1)
{
Stops prevObj = null;
foreach (Stops obj in calcStop.StopsList)
{
if (prevObj != null)
{
if (!((prevObj.type == car_state_e.CAR_STOP) && (obj.type == car_state_e.CAR_START)))
{
AllDistance += distance;
if (first)
{
timeStart = prevObj.stop;
first = false;
}
else
timeStart = oldStop;
timeStop = obj.start;
oldStop = obj.stop;
AllDistance += calcStop.calcDistance(timeStart, timeStop);
//DriveList.Add(distance);
}
else { first = true; }
}
prevObj = obj;
}
}
if (StaticMembers.isInMile)
return (Math.Round(((AllDistance * 0.621371192) * 1000)) / 1000).ToString();
return AllDistance.ToString();
2024-02-22 16:43:59 +00:00
}
}
[DataObject]
public class ChartDataObj
{
public List<ChartData> GetSpeedingChart()
{
ChartData cd = new ChartData();
List<ChartData> ChartDataList = new List<ChartData>();
//process data to generate
if (StaticMembers.SpeedingList != null)
{
if (StaticMembers.ReportType == rep_type.SPEED)
{
int[,] array2D = new int[,] { { 20, 50 }, { 50, 70 }, { 70, 90 }, { 90, 400 } };
var list = StaticMembers.SpeedingList.Select(x => x.Speed);
2024-02-22 16:43:59 +00:00
for (int i = 0; i < array2D.GetLength(0); i++)
{
var result = list.Select(int.Parse).Count(r => r >= array2D[i, 0] && r < array2D[i, 1]);
if (result > 0)
{
cd = new ChartData(string.Format("Between {0} and {1} {2}", array2D[i, 0], array2D[i, 1], (StaticMembers.isInMile) ? "mph" : "km/h"), result);
ChartDataList.Add(cd);
}
}
}
2024-02-22 16:43:59 +00:00
if (StaticMembers.ReportType == rep_type.JOB_TICKETING)
{
var query = StaticMembers.SpeedingList.GroupBy(n => n.Time,
(key, values) => new { Name = key, Count = values.Count() });
if (query.Count() > 1)
{
foreach (var result in query)
{
cd = new ChartData(result.Name.ToString(), result.Count);
ChartDataList.Add(cd);
}
}
}
}
2024-02-22 16:43:59 +00:00
return ChartDataList;
}
public List<ChartData> GetAllAlarmChart()
{
ChartData cd = new ChartData();
List<ChartData> ChartDataList = new List<ChartData>();
//process data to generate
if (StaticMembers.HistDataReport != null)
{
if (StaticMembers.ReportType == rep_type.ALLALARM)
{
var query = StaticMembers.HistDataReport.GroupBy(n => n.Time,
(key, values) => new { Name = key, Count = values.Count() });
foreach (var result in query)
{
cd = new ChartData(result.Name.ToString(), result.Count);
ChartDataList.Add(cd);
}
}
2024-02-22 16:43:59 +00:00
if (StaticMembers.ReportType == rep_type.HIST)
{
int[,] array2D = new int[,] { { 0, 40 }, { 40, 70 }, { 70, 90 }, { 90, 400 } };
var list = StaticMembers.HistDataReport.Select(x => x.Duration);
2024-02-22 16:43:59 +00:00
for (int i = 0; i < array2D.GetLength(0); i++)
{
var result = list.Select(int.Parse).Count(r => r >= array2D[i, 0] && r < array2D[i, 1]);
if (result > 0)
{
cd = new ChartData(string.Format("Between {0} and {1} {2}", array2D[i, 0], array2D[i, 1], (StaticMembers.isInMile) ? "mph" : "km/h"), result);
ChartDataList.Add(cd);
}
}
}
}
return ChartDataList;
}
}
}