From 79bf391df757c50374a5acde3008860d48295926 Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Fri, 1 Dec 2017 17:56:38 +0100 Subject: Added periodical checking of pollution and sending e-mail. --- .../Controllers/ReadingsController.cs | 10 +- PollutometerWebApi/EmailSender.cs | 40 ++++--- PollutometerWebApi/EmailService.cs | 119 +++++++++++++++++++++ PollutometerWebApi/PollutometerWebApi.csproj | 1 + 4 files changed, 153 insertions(+), 17 deletions(-) create mode 100644 PollutometerWebApi/EmailService.cs (limited to 'PollutometerWebApi') diff --git a/PollutometerWebApi/Controllers/ReadingsController.cs b/PollutometerWebApi/Controllers/ReadingsController.cs index cb91c71..9767645 100644 --- a/PollutometerWebApi/Controllers/ReadingsController.cs +++ b/PollutometerWebApi/Controllers/ReadingsController.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using System.Web.Http; using PollutometerWebApi.Models; using PollutometerWebApi.Singletons; @@ -7,13 +8,12 @@ namespace PollutometerWebApi.Controllers { public class ReadingsController : ApiController { - public ReadingsController() - { - EmailSender.SendEmail(); - } + public ReadingsController() {} public IHttpActionResult GetAllReadings() - { + { + Task.Run(() => EmailService.Start()); + var command = "SELECT * FROM Readings"; var readings = SqlOperator.GetReadings(command); diff --git a/PollutometerWebApi/EmailSender.cs b/PollutometerWebApi/EmailSender.cs index 0e328c9..9effa47 100644 --- a/PollutometerWebApi/EmailSender.cs +++ b/PollutometerWebApi/EmailSender.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Net.Mail; namespace PollutometerWebApi @@ -7,20 +8,35 @@ namespace PollutometerWebApi { public EmailSender() {} - public static void SendEmail() + public static void SendEmail(string gasName, double max) { - MailMessage mail = new MailMessage("***REMOVED***", "***REMOVED***@edu.easj.dk"); - SmtpClient client = new SmtpClient() + try { - Host = "mail.cock.li", - Port = 465, - DeliveryMethod = SmtpDeliveryMethod.Network, - UseDefaultCredentials = false, - Credentials = new System.Net.NetworkCredential("***REMOVED***", "***REMOVED***") - }; - mail.Subject = "this is a test email."; - mail.Body = "this is my test email body"; - client.Send(mail); + MailMessage mail = new MailMessage("***REMOVED***", "***REMOVED***@edu.easj.dk"); + SmtpClient client = new SmtpClient() + { + Host = "mail.cock.li", + Port = 587, + EnableSsl = true, + Timeout = 100, + DeliveryMethod = SmtpDeliveryMethod.Network, + UseDefaultCredentials = false, + Credentials = new System.Net.NetworkCredential("***REMOVED***", "***REMOVED***") + }; + mail.Subject = $"Pollutometer warning - {DateTime.Now}"; + mail.IsBodyHtml = true; + mail.Body = "

WARNING!

\n" + + "\n" + + "\n" + + "\n" + + $"The warning was triggered by {gasName}. " + + $"Air quality index: {max}"; + client.Send(mail); + } + catch(Exception ex) + { + Console.Write(ex.Message); + } } } } \ No newline at end of file diff --git a/PollutometerWebApi/EmailService.cs b/PollutometerWebApi/EmailService.cs new file mode 100644 index 0000000..06586b1 --- /dev/null +++ b/PollutometerWebApi/EmailService.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using PollutometerWebApi.Singletons; + +namespace PollutometerWebApi +{ + public class EmailService + { + public EmailService() + { + } + + public static void Start() + { + var startTimeSpan = TimeSpan.Zero; + var periodTimeSpan = TimeSpan.FromMinutes(60); + + var timer = new System.Threading.Timer((e) => + { + var command = "SELECT * FROM Readings " + + "WHERE TimeStamp IN(SELECT MAX(TimeStamp) FROM Readings)"; + var reading = SqlOperator.GetReadings(command)[0]; + + Dictionary results = new Dictionary(); + results["CO"] = CalculateAqi(reading.Co, "CO"); + results["NO"] = CalculateAqi(reading.No, "NO"); + results["SO"] = CalculateAqi(reading.So, "SO"); + + double max = 0; + string gasName = ""; + foreach (var result in results) + { + if (result.Value > max) + { + max = result.Value; + gasName = result.Key; + } + } + + if(max >= 151) EmailSender.SendEmail(gasName, max); + }, null, startTimeSpan, periodTimeSpan); + } + + static double CalculateAqi(double c, string t) + { + double[,,] breakpoints = + { + { + {0, 4.4}, + {4.5, 9.4}, + {9.5, 12.4}, + {12.5, 15.4}, + {15.5, 30.4}, + {30.5, 40.4}, + {40.5, 50.4} + }, + { + {0.000, 0.034}, + {0.035, 0.144}, + {0.145, 0.224}, + {0.225, 0.304}, + {0.305, 0.604}, + {0.605, 0.804}, + {0.805, 1.004} + }, + { + {0, 0.05}, + {0.08, 0.10}, + {0.15, 0.20}, + {0.25, 0.31}, + {0.65, 1.24}, + {1.25, 1.64}, + {1.65, 2.04} + }, + { + {0, 50}, + {51, 100}, + {101, 150}, + {151, 200}, + {201, 300}, + {301, 400}, + {401, 500} + } + }; + + double i, cLow = 0, cHigh = 0, iLow = 0, iHigh = 0; + int g = 0; + + switch (t) + { + case "CO": + g = 0; + break; + case "SO": + g = 1; + break; + case "NO": + g = 2; + break; + } + + for (int j = 0; j < 7; j++) + { + if (c >= breakpoints[g, j, 0] && c <= breakpoints[g, j, 1]) + { + cLow = breakpoints[g, j, 0]; + cHigh = breakpoints[g, j, 1]; + iLow = breakpoints[3, j, 0]; + iHigh = breakpoints[3, j, 1]; + break; + } + } + + i = (iHigh - iLow) / (cHigh - cLow) * (c - cLow) + iLow; + + return i; + } + } +} diff --git a/PollutometerWebApi/PollutometerWebApi.csproj b/PollutometerWebApi/PollutometerWebApi.csproj index 8bde6ba..d2dcd57 100644 --- a/PollutometerWebApi/PollutometerWebApi.csproj +++ b/PollutometerWebApi/PollutometerWebApi.csproj @@ -90,6 +90,7 @@ + -- cgit v1.2.3