Merge pull request 'refactor_AppServer_LocationThread' (#3) from refactor_AppServer_LocationThread into master

Reviewed-on: #3
This commit is contained in:
Laurențiu Constantin 2024-06-06 10:43:15 +00:00
commit e973feb124

View File

@ -123,7 +123,7 @@ namespace AppServer
while (line != default(string))
{
BeforeWriteDB = true;
string[] tempArr = line.Split('#');
string[] tempArr = line.Split("#".ToCharArray());
//imei
try
{
@ -200,7 +200,7 @@ namespace AppServer
if (SpeedProccess != "")
{
string[] temp = SpeedProccess.Split('_');
string[] temp = SpeedProccess.Split("_".ToCharArray());
int speedT = (int)Convert.ToDouble(temp[0]);
string speedU = temp[1];
processSpeedAlarm(radioID2, dec2.cell, speedT, speedU, alarm.Email, !prevGps);
@ -288,37 +288,40 @@ namespace AppServer
//Calculate status
if (MainForm.VehicleHashStat.ContainsKey(radioID2.ToString()) && !prevGps)
{
VehicleStatus vehicleStatus = (VehicleStatus)MainForm.VehicleHashStat[radioID2];
//MADE ON status when is the case
if ((((VehicleStatus)MainForm.VehicleHashStat[radioID2]).curentStatus == Status_for_tab.MADEOFF)
|| (((VehicleStatus)MainForm.VehicleHashStat[radioID2]).curentStatus == Status_for_tab.OFF))
if ((vehicleStatus.curentStatus == Status_for_tab.MADEOFF)
|| (vehicleStatus.curentStatus == Status_for_tab.OFF))
{
((VehicleStatus)MainForm.VehicleHashStat[radioID2]).curentStatus = Status_for_tab.MADEON;
vehicleStatus.curentStatus = Status_for_tab.MADEON;
PushStatus(radioID2, Status_for_tab.MADEON);
}
// update last status location and position time
((VehicleStatus)MainForm.VehicleHashStat[radioID2]).lastPositionTime = DateTime.Now;
((VehicleStatus)MainForm.VehicleHashStat[radioID2]).lat = dec2.cell.d_lat;
((VehicleStatus)MainForm.VehicleHashStat[radioID2]).lng = dec2.cell.d_lng;
vehicleStatus.lastPositionTime = DateTime.Now;
vehicleStatus.lat = dec2.cell.d_lat;
vehicleStatus.lng = dec2.cell.d_lng;
// do not push any status update if the current status is emergency
if (((VehicleStatus)MainForm.VehicleHashStat[radioID2]).curentStatus != Status_for_tab.EMERG)
if (vehicleStatus.curentStatus != Status_for_tab.EMERG)
{
if (dec2.cell.lat != "0" && dec2.cell.lat != "0.00000")
{
if (((VehicleStatus)MainForm.VehicleHashStat[radioID2]).curentStatus != Status_for_tab.GPS_ON)
if (vehicleStatus.curentStatus != Status_for_tab.GPS_ON)
{
((VehicleStatus)MainForm.VehicleHashStat[radioID2]).curentStatus = Status_for_tab.GPS_ON;
vehicleStatus.curentStatus = Status_for_tab.GPS_ON;
//SM.Debug("Push Status GPS ON for radio:"+radioID2);
PushStatus(radioID2, Status_for_tab.GPS_ON);
}
}
else
{
if (((VehicleStatus)MainForm.VehicleHashStat[radioID2]).curentStatus != Status_for_tab.NOGPSFIX)
if (vehicleStatus.curentStatus != Status_for_tab.NOGPSFIX)
{
((VehicleStatus)MainForm.VehicleHashStat[radioID2]).curentStatus = Status_for_tab.NOGPSFIX;
vehicleStatus.curentStatus = Status_for_tab.NOGPSFIX;
PushStatus(radioID2, Status_for_tab.NOGPSFIX);
}
}
@ -407,7 +410,7 @@ namespace AppServer
try
{
MailAddress from = new MailAddress(Program.cfg.emailAddress);
String[] address = mailAdr.Split(';');
String[] address = mailAdr.Split(";".ToCharArray());
MailAddress to = new MailAddress(address[0]);
MailMessage message = new MailMessage(from, to);
if (address.Count() > 1)
@ -415,8 +418,10 @@ namespace AppServer
Boolean skipeFirst = true;
foreach (String obj in address)
{
if (skipeFirst) { skipeFirst = false; }
else message.To.Add(new MailAddress(obj));
if (skipeFirst)
{ skipeFirst = false; }
else
message.To.Add(new MailAddress(obj));
}
}
message.Subject = subj;
@ -497,42 +502,29 @@ namespace AppServer
else if ((obj2.AlarmType == 1) && (position == 2)) validAlarm = true;
else if ((obj2.AlarmType == 2) && (position == 1)) validAlarm = true;
else if (obj2.AlarmType == 4) validAlarm = false;
if (validAlarm)
{
Utils.WriteLine("Add new zone alarm");
DBalarm.Insert_Zone_Alarm(((Vehicle_Data)MainForm.VehList[radioID.ToString()]).sc_id, time, obj2.zone_id, obj2.action, true);
Utils.WriteLine("Done Add new zone alarm");
if (obj2.speedUnit.Equals("k"))
string speed_km_or_mph = obj2.speedUnit.Equals("k") ? $"{speed} km/h" : $"{speedMiles} mph";
string message = "Zone alarm for " + ((Vehicle_Data)MainForm.VehList[radioID.ToString()]).Name + " " + tmpresp
+ $". Unit speed {speed_km_or_mph} at time:" + Utils.UnixTimeStampToDateTime(time).ToLocalTime() + " [" + cell.lat + " , " + cell.lng + "]";
string title = "Zone alarm for " + ((Vehicle_Data)MainForm.VehList[radioID.ToString()]).Name;
if (mailAdr != "")
{
string message = "Zone alarm for " + ((Vehicle_Data)MainForm.VehList[radioID.ToString()]).Name + " " + tmpresp
+ ". Unit speed " + speed + " km/h at time:" + Utils.UnixTimeStampToDateTime(time).ToLocalTime() + " [" + cell.lat + " , " + cell.lng + "]";
string title = "Zone alarm for " + ((Vehicle_Data)MainForm.VehList[radioID.ToString()]).Name;
if (mailAdr != "")
Task.Factory.StartNew(() =>
{
Task.Factory.StartNew(() =>
{
sendAlarmMail(title, message, mailAdr);
});
sendAlarmMail(title, message, mailAdr);
});
}
}
else
{
string message = "Zone alarm for " + ((Vehicle_Data)MainForm.VehList[radioID.ToString()]).Name + " " + tmpresp
+ ". Unit speed " + speedMiles + " mph at time:" + Utils.UnixTimeStampToDateTime(time).ToLocalTime() + " [" + cell.lat + " , " + cell.lng + "]";
string title = "Zone alarm for " + ((Vehicle_Data)MainForm.VehList[radioID.ToString()]).Name;
if (mailAdr != "")
{
Task.Factory.StartNew(() =>
{
sendAlarmMail(title, message, mailAdr);
});
}
}
String test = "";
if (sendOnMsgBus)
@ -548,14 +540,15 @@ namespace AppServer
{
String listOfUnits = obj2.unitids;
String listOfImeis = obj2.imeilist;
String[] tmpunits = listOfUnits.Split(',');
String[] tmpimeis = listOfImeis.Split(',');
String[] tmpunits = listOfUnits.Split(",".ToCharArray());
String[] tmpimeis = listOfImeis.Split(",".ToCharArray());
Hashtable tmpHashName = new Hashtable();
if (tmpunits.Count() > 1)
{
for (Int32 i = 0; i < tmpunits.Count() - 1; i++)
tmpHashName.Add((String)tmpunits[i], (String)tmpimeis[i]);
}
foreach (String keyobj in tmpHashName.Keys)
{
int obj = -1;
@ -572,6 +565,7 @@ namespace AppServer
}
}
}
if (obj2.sentmsg2 && sendOnMsgBus)
{
tmpX = dbvehs.getSystemPositionIMEI(radioID);
@ -580,6 +574,7 @@ namespace AppServer
Utils.WriteLine($"Zone alert sms request [{obj2.msgbody2}] for unit {radioID} on gw [{(tmpX.Gw_id + "." + tmpX.R_gw_id)}]");
}
//send Callout
if(obj2.callout && tmpresp.ToString().Contains("IN "))
{
@ -590,6 +585,7 @@ namespace AppServer
callOutQueue.PostItem(new CallOut() { RadioID = radioID, Severity = obj2.calloutSeverity, Time = DateTime.Now});
Utils.WriteLine($"Zone CallOut request with Severity [{obj2.calloutSeverity}] for unit {radioID} on gw [{(tmpX.Gw_id + "." + tmpX.R_gw_id)}]");
}
///send email
if (obj2.sentemail)
{
@ -613,32 +609,21 @@ namespace AppServer
{
Utils.WriteLine("Insert Landmarks Alarm " + radioID.ToString() + " " + ((Vehicle_Data)MainForm.VehList[radioID.ToString()]).sc_id);
DBalarm.Insert_Zone_Alarm(((Vehicle_Data)MainForm.VehList[radioID.ToString()]).sc_id, time, obj2.land_id, obj2.action, false);
if (obj2.speedUnit.Equals("k"))
string speed_km_or_mph = obj2.speedUnit.Equals("k") ? $"{speed} km/h" : $"{speedMiles} mph";
string message = "Landmark alarm for " + ((Vehicle_Data)MainForm.VehList[radioID.ToString()]).Name + " " + tmpresp
+ $". Unit speed {speed_km_or_mph} at time:" + Utils.UnixTimeStampToDateTime(time).ToLocalTime() + " [" + cell.lat + " , " + cell.lng + "]";
string title = "Landmark alarm for " + ((Vehicle_Data)MainForm.VehList[radioID.ToString()]).Name;
if (mailAdr != "")
{
string message = "Landmark alarm for " + ((Vehicle_Data)MainForm.VehList[radioID.ToString()]).Name + " " + tmpresp
+ ". Unit speed " + speed + " km/h at time:" + Utils.UnixTimeStampToDateTime(time).ToLocalTime() + " [" + cell.lat + " , " + cell.lng + "]";
string title = "Landmark alarm for " + ((Vehicle_Data)MainForm.VehList[radioID.ToString()]).Name;
if (mailAdr != "")
Task.Factory.StartNew(() =>
{
Task.Factory.StartNew(() =>
{
sendAlarmMail(title, message, mailAdr);
});
}
}
else
{
string message = "Landmark alarm for " + ((Vehicle_Data)MainForm.VehList[radioID.ToString()]).Name + " " + tmpresp
+ ". Unit speed " + speedMiles + " mph at time:" + Utils.UnixTimeStampToDateTime(time).ToLocalTime() + " [" + cell.lat + " , " + cell.lng + "]";
string title = "Landmark alarm for " + ((Vehicle_Data)MainForm.VehList[radioID.ToString()]).Name;
if (mailAdr != "")
{
Task.Factory.StartNew(() =>
{
sendAlarmMail(title, message, mailAdr);
});
}
sendAlarmMail(title, message, mailAdr);
});
}
if (sendOnMsgBus)
{
@ -683,10 +668,17 @@ namespace AppServer
DBalarmManager DBalarm = new DBalarmManager(Program.cfg.DB_IP, Program.cfg.DB_schema, Program.cfg.DB_user, Program.cfg.DB_passwd, Program.cfg.DB_port);
int speed = (int)Convert.ToDouble(cell.spd);
int treshold = (int)Convert.ToDouble(speedTreshold);
int speedMiles = (int)Math.Round(speed * 0.621371192);
uint time = DateTo70Format(cell.location_time);
string speed4send = speed.ToString() + "_" + speedUnits;
int treshold = (int)Convert.ToDouble(speedTreshold);
if (speedUnits == "m")
treshold = (int)(treshold * 1.609);
if (speedUnits != "m")
{
if (speed >= treshold)
@ -694,8 +686,11 @@ namespace AppServer
Utils.WriteLine("Speed alarm detected!");
DBalarm.Insert_Speed_Alarm(radioID, time, speed,cell.lat,cell.lng);
Utils.WriteLine("Done inserting speed alarm!");
string speed_km_or_mph = (speedUnits != "m") ? $"{speed} km/h" : $"{speedMiles} mph";
string message = "Speed alarm for " + ((Vehicle_Data)MainForm.VehList[radioID.ToString()]).Name
+ ". Unit speed " + speed + " km/h at time:" + Utils.UnixTimeStampToDateTime(time).ToLocalTime() + " [" + cell.lat + " , " + cell.lng + "]";
+ $". Unit speed {speed_km_or_mph} at time:" + Utils.UnixTimeStampToDateTime(time).ToLocalTime() + " [" + cell.lat + " , " + cell.lng + "]";
string title = "Speed alarm for unit " + ((Vehicle_Data)MainForm.VehList[radioID.ToString()]).Name;
if (mailAdr != "")
{
@ -713,32 +708,6 @@ namespace AppServer
}
}
}
else
{
if (speed >= (treshold * 1.609))
{
DBalarm.Insert_Speed_Alarm(radioID, time, speed, cell.lat, cell.lng);
String speedMiles = Convert.ToString((int)Math.Round(speed * 0.621371192));
SM.Debug("Speed alarm generated!");
string message = "Speed alarm for " + ((Vehicle_Data)MainForm.VehList[radioID.ToString()]).Name
+ ". Unit speed " + speedMiles + " mph at time:" + Utils.UnixTimeStampToDateTime(time).ToLocalTime() + " [" + cell.lat + " , " + cell.lng + "]";
string title = "Speed alarm for unit " + ((Vehicle_Data)MainForm.VehList[radioID.ToString()]).Name;
if (mailAdr != "Speed alarm")
{
Task.Factory.StartNew(() =>
{
sendAlarmMail(title, message, mailAdr);
});
}
if (sendOnMsgBus)
{
//send alarm on message buss
string test = "#135#" + radioID.ToString() + "#" + speed4send + "#";
MainForm.udp.Send(SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test), SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test).Length);
}
}
}
}
#endregion