SafeDispatch/ReportsLibrary/Reports/ClassForReports.cs

708 lines
27 KiB
C#

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);
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);
string dateTimeFormat = StaticMembers.is24hours ? "MM/dd/yyyy HH:mm:ss" : "MM/dd/yyyy hh:mm:ss tt";
tmpData.StartTime = ((lastStopTimeStop).GetDTLocalFromSeconds()).ToString(dateTimeFormat);
timeforStartCalc = lastStopTimeStop;
tmpData.StartAddress = lastStopNameStop;
}
count3++;
if (tmpData != null)
{
totaltime += (obj3.start - timeforStartCalc);
tmpData.StopTime = ((obj3.start).GetDTLocalFromSeconds()).ToString();
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();
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);
}
lastStopIsStop = true;
lastStopTimeStop = obj3.stop;
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();
}
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();
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
{
if (!StaticMembers.IsIdle)
{
calcStop.calculate_zone();
calcStop.calc_zone_with_delta2();
calcStop.create_stop_list();
}
else
{
calcStop.create_stop_list_EON(StaticMembers.IDLETime);
}
}
catch (Exception ex)
{
SM.Debug("Database connection failed:" + ex.ToString());
}
if (StaticMembers.StartStop)
{
calcStop.StartStopCalc();
calcStop.StartStopCalcPosition();
}
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));
}
if (!StaticMembers.ShowCoordinates)
{
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 180 msec");
Thread.Sleep(180);
retrynew = true;
}
}
if (cntmax > 10)
retrynew = false;
}
}
}
}
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>();
foreach (Stops obj3 in calcStop.StopsList)
{
countStops++;
if (obj3.type == car_state_e.CAR_STOP)
{
// Populate StopData object
tmpData = new StopData();
try
{
RowNumber++;
tmpData.Data = ((obj3.start).GetDTLocalFromSeconds()).Date.ToShortDateString();
tmpData.Time = ((Int32)obj3.start).TimeOfDayHHMMLocal(!StaticMembers.is24hours);
// get location
if (!StaticMembers.LATLNGforDinamicReports)
{
if ((StaticMembers.MapType == MapTYPE.Google) || (StaticMembers.MapType == MapTYPE.ArcGis))
tmpData.Location = StaticMembers.IdReportHS[obj3.name].ToString();
}
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))
{
if (((Stops)(calcStop.StopsList[countStops + 1])).type == car_state_e.CAR_START)
stoptime = ((Stops)(calcStop.StopsList[countStops + 1])).stop;
}
tmpData.Duration = FormatDuration(starttime, stoptime);
}
catch (Exception ex)
{
SM.Debug("EX:" + ex.ToString());
}
// add to list
list.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 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");
}
}
[DataObject]
public class BatchHistoryDataObj
{
[DataObjectMethod(DataObjectMethodType.Select)]
public List<BatchHistoryData> GetALL()
{
return StaticMembers.BatchHistoryReport;
}
}
[DataObject]
public class FleetDataObj
{
[DataObjectMethod(DataObjectMethodType.Select)]
public List<FleetData> GetALL()
{
var fleetDataList = new List<FleetData>();
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
FleetData tmpData = new FleetData();
tmpData.Name = veh.Name;
tmpData.Distance = calculatetDistance(calcStop);
tmpData.Trips = "0";
if (calcStop.StopsList.Count > 1)
tmpData.Trips = calculateTrips(calcStop);
tmpData.Start = "";
tmpData.Stop = "";
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);
}
// add to list
fleetDataList.Add(tmpData);
}
}
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
});
}
}
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();
}
}
[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);
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);
}
}
}
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);
}
}
}
}
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);
}
}
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);
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;
}
}
}