remove email addresses from blacklist after MAX_ATTEMPTS to give them one more chance #7

Merged
laurentiu.constantin merged 1 commits from handle_email_address_in_blacklist into master 2024-06-13 11:04:29 +00:00
3 changed files with 216 additions and 112 deletions

View File

@ -22,7 +22,7 @@ namespace AppServer
private volatile DBsubsOperationManager dbsubsoperManage;
private volatile DBvehiclesManager dbvehs;
private DBvehiclesManager DBvehInfo;
List<string> blackListAddress = new List<string>();
public static volatile Int32 LocationCheckQueuesCount = 0;
Int64 count = 0;
@ -185,12 +185,12 @@ namespace AppServer
{
Vehicle_Data v_data = (Vehicle_Data)MainForm.VehList[radioID2.ToString()];
Alarm alarm = v_data.alm;
bool zone = false;
bool landMark = false;
String SpeedProccess = "";
if (alarm.Zone != "") zone = true;
if (alarm.Landmark != "") landMark = true;
bool zone = (alarm.Zone != "") ;
bool landMark = (alarm.Landmark != "") ;
String SpeedProccess = "";
if (zone || landMark)
{
SpeedProccess = processZoneAlarm(radioID2, dec2.cell, zone, landMark, alarm.Email, !prevGps);
@ -351,7 +351,7 @@ namespace AppServer
string test2 = "#210#1#Can't write GPS data in DB#";
if (ex.Message == "AlarmError") test2 = "#210#5#Can't write Alarm data in DB#";
else if (ex.Message == "StolenError") test2 = "#210#6#Can't write Stolen data in DB#";
MainForm.udp.Send(SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test2), SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test2).Length);
MainForm.udp.Send(Utils.Convert_text_For_multicast("#0.0" + test2), Utils.Convert_text_For_multicast("#0.0" + test2).Length);
}
catch (Exception ex2)
{
@ -397,7 +397,7 @@ namespace AppServer
{
// send callout clear
string test = "#178#" + callout.RadioID.ToString() + "#" + callout.Severity + "#";
MainForm.udp.Send(SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test), SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test).Length);
MainForm.udp.Send(Utils.Convert_text_For_multicast("#0.0" + test), Utils.Convert_text_For_multicast("#0.0" + test).Length);
Utils.WriteLine($"Sent CallOut Stop request with Severity [{callout.Severity}] for unit {callout.RadioID.ToString()} ");
}
}
@ -409,11 +409,13 @@ namespace AppServer
private void sendMailGeo(string mes, String subj, string mailAdr)
{
if (Program.cfg.enableEmailService && !String.IsNullOrEmpty(Program.cfg.emailAddress) && !String.IsNullOrEmpty(mailAdr))
if (Program.cfg.enableEmailService)
{
if (blackListAddress.Contains(mailAdr))
if (!String.IsNullOrEmpty(Program.cfg.emailAddress) && !String.IsNullOrEmpty(mailAdr))
{
Utils.WriteLine($" sendMailGeo : The email was not sent. Email address '{mailAdr}' is in blacklist ");
if (MainForm.IsBlacklistedAddress(mailAdr))
{
Utils.WriteLine($"sendMailGeo : The email was not sent. Email address '{mailAdr}' is in blacklist ");
return;
}
@ -441,24 +443,32 @@ namespace AppServer
catch (Exception ex)
{
if (ex.ToString().Contains("timed out"))
blackListAddress.Add(mailAdr);
MainForm.HandleTimeoutError(mailAdr);
Utils.WriteLine($"Exception in sendMailGeo: {ex.ToString()}");
}
}
else
{
Utils.WriteLine($"Missing email address From = {Program.cfg.emailAddress} To = {mailAdr})", ConsoleColor.Cyan);
}
}
else
{
Utils.WriteLine("Email Server not Set", ConsoleColor.Cyan);
}
}
private void sendAlarmMail(string title, string mes, string mailAdr)
{
if (Program.cfg.enableEmailService && !String.IsNullOrEmpty(Program.cfg.emailAddress) && !String.IsNullOrEmpty(mailAdr))
if (Program.cfg.enableEmailService)
{
if (!String.IsNullOrEmpty(Program.cfg.emailAddress) && !String.IsNullOrEmpty(mailAdr))
{
if (blackListAddress.Contains(mailAdr))
if (MainForm.IsBlacklistedAddress(mailAdr))
{
Utils.WriteLine($" sendAlarmMail : The email was not sent. Email address '{mailAdr}' is in blacklist ");
return;
@ -476,12 +486,17 @@ namespace AppServer
catch (Exception ex)
{
if (ex.ToString().Contains("timed out"))
blackListAddress.Add(mailAdr);
MainForm.HandleTimeoutError(mailAdr);
Utils.WriteLine("Exception in sendMailAlarm2: {0}" + ex.ToString(), ConsoleColor.Red);
}
}
else
{
Utils.WriteLine($"Missing email address From = {Program.cfg.emailAddress} To = {mailAdr})", ConsoleColor.Cyan);
}
}
else
{
Utils.WriteLine("Email Server not Set", ConsoleColor.Cyan);
}
@ -561,7 +576,7 @@ namespace AppServer
{
//send alarm on message buss
test = "#136#" + radioID.ToString() + "#" + tmpresp + "#";
MainForm.udp.Send(SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test), SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test).Length);
MainForm.udp.Send(Utils.Convert_text_For_multicast("#0.0" + test), Utils.Convert_text_For_multicast("#0.0" + test).Length);
}
String date = DateTime.Now.ToUniversalTime().DateTo70Format().ToString();
@ -589,7 +604,7 @@ namespace AppServer
tmpX = dbvehs.getSystemPosition(Convert.ToInt32(keyobj));
test = "#142#" + tmpX.Gw_id + "." + tmpX.R_gw_id + "." + (String)tmpHashName[keyobj] + "#" + /*"Message from unit " + ((Vehicle_Data)MainForm.VehList[radioID.ToString()]).Name + " :" +*/ obj2.msgbody + "#" + date + "#";
MainForm.udp.Send(SafeMobileLib.Utils.Convert_text_For_multicast("#0." + date + test), SafeMobileLib.Utils.Convert_text_For_multicast("#0." + date + test).Length);
MainForm.udp.Send(Utils.Convert_text_For_multicast("#0." + date + test), Utils.Convert_text_For_multicast("#0." + date + test).Length);
Thread.Sleep(100);
Utils.WriteLine($"Zone alert sms request [{obj2.msgbody}] for unit {(String)tmpHashName[keyobj]} on gw [{(tmpX.Gw_id + "." + tmpX.R_gw_id)}]");
@ -601,7 +616,7 @@ namespace AppServer
{
tmpX = dbvehs.getSystemPositionIMEI(radioID);
test = "#142#" + tmpX.Gw_id + "." + tmpX.R_gw_id + "." + radioID + "#" + obj2.msgbody2 + "#" + DateTime.Now.ToUniversalTime().DateTo70Format().ToString() + "#";
MainForm.udp.Send(SafeMobileLib.Utils.Convert_text_For_multicast("#0." + date + test), SafeMobileLib.Utils.Convert_text_For_multicast("#0." + date + test).Length);
MainForm.udp.Send(Utils.Convert_text_For_multicast("#0." + date + test), Utils.Convert_text_For_multicast("#0." + date + test).Length);
Utils.WriteLine($"Zone alert sms request [{obj2.msgbody2}] for unit {radioID} on gw [{(tmpX.Gw_id + "." + tmpX.R_gw_id)}]");
}
@ -611,7 +626,7 @@ namespace AppServer
{
tmpX = dbvehs.getSystemPositionIMEI(radioID);
test = "#177#" + radioID.ToString() + "#" + obj2.calloutSeverity + "#" + obj2.name + "#";
MainForm.udp.Send(SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test), SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test).Length);
MainForm.udp.Send(Utils.Convert_text_For_multicast("#0.0" + test), Utils.Convert_text_For_multicast("#0.0" + test).Length);
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)}]");
@ -667,14 +682,14 @@ namespace AppServer
{
//send alarm on message buss
string test = "#137#" + radioID.ToString() + "#" + tmpresp + "#";
MainForm.udp.Send(SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test), SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test).Length);
MainForm.udp.Send(Utils.Convert_text_For_multicast("#0.0" + test), Utils.Convert_text_For_multicast("#0.0" + test).Length);
}
//send CallOut
if(obj2.callout && tmpresp.ToString().Contains("IN "))
{
string test = "#177#" + radioID + "#" + obj2.calloutSeverity + "#" + obj2.name + "#";
MainForm.udp.Send(SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test), SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test).Length);
MainForm.udp.Send(Utils.Convert_text_For_multicast("#0.0" + test), Utils.Convert_text_For_multicast("#0.0" + test).Length);
callOutQueue.PostItem(new CallOut() { RadioID = radioID, Severity = obj2.calloutSeverity, Time = DateTime.Now });
Utils.WriteLine($"Landmark CallOut request with Severity [{obj2.calloutSeverity}] for unit {radioID} ");
@ -743,7 +758,7 @@ namespace AppServer
//send alarm on message buss
string speed4send = $"{speed}_{speedUnits}";
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);
MainForm.udp.Send(Utils.Convert_text_For_multicast("#0.0" + test), Utils.Convert_text_For_multicast("#0.0" + test).Length);
}
}

View File

@ -96,6 +96,9 @@ namespace AppServer
public static String dbAccess;
private WebsocketThread websocket = null;
public static Dictionary<string, int> blackListAddress = new Dictionary<string, int>();
public const int MAX_ATTEMPTS = 100;
public delegate void UpdateLicensesUIInvokeCallBack();
public MainForm()
@ -338,6 +341,37 @@ namespace AppServer
}
public static void HandleTimeoutError(string mailAdr)
{
// add address to backlist
if (!blackListAddress.ContainsKey(mailAdr))
blackListAddress.Add(mailAdr, MAX_ATTEMPTS);
}
public static bool IsBlacklistedAddress(string mailAdr)
{
if (!blackListAddress.ContainsKey(mailAdr))
return false;
// avoid to send the email MAX_ATTEMPTS times => give another chance to try to send email when count = 0
int count = blackListAddress[mailAdr];
if (count == 0)
{
blackListAddress.Remove(mailAdr);
return false;
}
blackListAddress[mailAdr] = count - 1;
return true;
}
private void ConfigureWebSocket()
{
if (!checkWebsocketServerStatus.Enabled)

View File

@ -28,7 +28,6 @@ namespace AppServer
private DBvehiclesManager dbvehs;
internal DBsubsOperationManager dbsubsoperManage;
private DBcallPatchManager dbCallPatch;
private List<string> blackListAddress = new List<string>();
#region code for zone
private volatile static ArrayList ZoneList = new ArrayList();
@ -2151,9 +2150,13 @@ namespace AppServer
}
private void sendAlarmMail(string mes, string mailAdr)
{
if (Program.cfg.enableEmailService)
{
if (blackListAddress.Contains(mailAdr))
if (!String.IsNullOrEmpty(Program.cfg.emailAddress) && !String.IsNullOrEmpty(mailAdr))
{
if (MainForm.IsBlacklistedAddress(mailAdr))
{
Utils.WriteLine($" sendAlarmMail : The email was not sent. Email address '{mailAdr}' is in blacklist ");
return;
@ -2171,13 +2174,22 @@ namespace AppServer
catch (Exception ex)
{
if (ex.ToString().Contains("timed out"))
blackListAddress.Add(mailAdr);
MainForm.HandleTimeoutError(mailAdr);
Utils.WriteLine("Exception in sendMailAlarm: " + ex.ToString(), ConsoleColor.Red);
//Console.WriteLine(e.ToString());
}
}
else
{
Utils.WriteLine($"Missing email address From = {Program.cfg.emailAddress} To = {mailAdr})", ConsoleColor.Red);
}
}
else
{
SM.Debug("email disabled");
}
}
public void sendDailyReportMail(String PdfFile, string mailAdr)
@ -2362,6 +2374,16 @@ namespace AppServer
{
if (Program.cfg.enableEmailService)
{
if (!String.IsNullOrEmpty(Program.cfg.emailAddress) && !String.IsNullOrEmpty(mailAdr))
{
if (MainForm.IsBlacklistedAddress(mailAdr))
{
Utils.WriteLine($"sendMailGeo : The email was not sent. Email address '{mailAdr}' is in blacklist ");
return;
}
SM.Debug("Sending mail to " + mailAdr + " from radioId " + radioID.ToString());
try
{
@ -2379,10 +2401,18 @@ namespace AppServer
}
catch (Exception ex)
{
if (ex.ToString().Contains("timed out"))
MainForm.HandleTimeoutError(mailAdr);
Utils.WriteLine("Exception in sendMail: " + ex.ToString(), ConsoleColor.Red);
}
}
else
{
Utils.WriteLine($"Missing email address From = {Program.cfg.emailAddress} To = {mailAdr})", ConsoleColor.Red);
}
}
else
{
SM.Debug("email disabled");
}
@ -2392,7 +2422,19 @@ namespace AppServer
//sms-> email Sierra Wireless
private void sendMail_SW(string mailAdr, string mailBody)
{
if (Program.cfg.enableEmailService)
{
if (!String.IsNullOrEmpty(Program.cfg.emailAddress) && !String.IsNullOrEmpty(mailAdr))
{
if (MainForm.IsBlacklistedAddress(mailAdr))
{
Utils.WriteLine($"sendMailGeo : The email was not sent. Email address '{mailAdr}' is in blacklist ");
return;
}
SM.Debug("Sending mail to " + mailAdr);
try
{
MailMessage message = new MailMessage();
@ -2405,7 +2447,20 @@ namespace AppServer
}
catch (Exception ex)
{
Utils.WriteLine("Exception in sendMail Sierra Wireless Gateway: "+ ex.ToString() ,ConsoleColor.Red);
if (ex.ToString().Contains("timed out"))
MainForm.HandleTimeoutError(mailAdr);
Utils.WriteLine("Exception in sendMail Sierra Wireless Gateway: " + ex.ToString(), ConsoleColor.Red);
}
}
else
{
Utils.WriteLine($"Missing email address From = {Program.cfg.emailAddress} To = {mailAdr})", ConsoleColor.Red);
}
}
else
{
SM.Debug("email disabled");
}
}