From a3ad4472de9096ec0a12d15097048578b3514d4b Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Mon, 4 Dec 2017 10:14:32 +0100 Subject: Improved calculating of AQI and sending e-mail. --- PollutometerWebApi.Tests/Test.cs | 2 +- .../bin/Debug/PollutometerWebApi.Tests.dll | Bin 4096 -> 4096 bytes .../bin/Debug/PollutometerWebApi.Tests.pdb | Bin 484 -> 532 bytes .../bin/Debug/PollutometerWebApi.dll | Bin 16384 -> 16384 bytes .../bin/Debug/PollutometerWebApi.pdb | Bin 4064 -> 3984 bytes ...bApi.Tests.csprojResolveAssemblyReference.cache | Bin 109342 -> 109342 bytes .../obj/Debug/PollutometerWebApi.Tests.dll | Bin 4096 -> 4096 bytes .../obj/Debug/PollutometerWebApi.Tests.pdb | Bin 484 -> 532 bytes PollutometerWebApi.userprefs | 21 ++-- PollutometerWebApi/AqiCalculator.cs | 102 ++++++++++++++++++ .../Controllers/ReadingsController.cs | 7 +- PollutometerWebApi/EmailSender.cs | 19 +--- PollutometerWebApi/EmailService.cs | 119 --------------------- PollutometerWebApi/Models/Aqi.cs | 8 ++ PollutometerWebApi/PollutometerWebApi.csproj | 3 +- 15 files changed, 128 insertions(+), 153 deletions(-) create mode 100644 PollutometerWebApi/AqiCalculator.cs delete mode 100644 PollutometerWebApi/EmailService.cs create mode 100644 PollutometerWebApi/Models/Aqi.cs diff --git a/PollutometerWebApi.Tests/Test.cs b/PollutometerWebApi.Tests/Test.cs index 254f0bc..cc17234 100644 --- a/PollutometerWebApi.Tests/Test.cs +++ b/PollutometerWebApi.Tests/Test.cs @@ -8,7 +8,7 @@ namespace PollutometerWebApi.Tests [Test()] public void TestCase() { - EmailService.Start(); + var result = AqiCalculator.CalculateAqi(new Models.Reading() {Co = 30.4, No = 1.0, So = 0.4}); } } } diff --git a/PollutometerWebApi.Tests/bin/Debug/PollutometerWebApi.Tests.dll b/PollutometerWebApi.Tests/bin/Debug/PollutometerWebApi.Tests.dll index a1a6f09..4fec8a3 100644 Binary files a/PollutometerWebApi.Tests/bin/Debug/PollutometerWebApi.Tests.dll and b/PollutometerWebApi.Tests/bin/Debug/PollutometerWebApi.Tests.dll differ diff --git a/PollutometerWebApi.Tests/bin/Debug/PollutometerWebApi.Tests.pdb b/PollutometerWebApi.Tests/bin/Debug/PollutometerWebApi.Tests.pdb index ee082ed..4b521ed 100644 Binary files a/PollutometerWebApi.Tests/bin/Debug/PollutometerWebApi.Tests.pdb and b/PollutometerWebApi.Tests/bin/Debug/PollutometerWebApi.Tests.pdb differ diff --git a/PollutometerWebApi.Tests/bin/Debug/PollutometerWebApi.dll b/PollutometerWebApi.Tests/bin/Debug/PollutometerWebApi.dll index b8d1eba..205cb6b 100644 Binary files a/PollutometerWebApi.Tests/bin/Debug/PollutometerWebApi.dll and b/PollutometerWebApi.Tests/bin/Debug/PollutometerWebApi.dll differ diff --git a/PollutometerWebApi.Tests/bin/Debug/PollutometerWebApi.pdb b/PollutometerWebApi.Tests/bin/Debug/PollutometerWebApi.pdb index 1cf8ce4..176b41e 100644 Binary files a/PollutometerWebApi.Tests/bin/Debug/PollutometerWebApi.pdb and b/PollutometerWebApi.Tests/bin/Debug/PollutometerWebApi.pdb differ diff --git a/PollutometerWebApi.Tests/obj/Debug/PollutometerWebApi.Tests.csprojResolveAssemblyReference.cache b/PollutometerWebApi.Tests/obj/Debug/PollutometerWebApi.Tests.csprojResolveAssemblyReference.cache index 11b2422..e810d97 100644 Binary files a/PollutometerWebApi.Tests/obj/Debug/PollutometerWebApi.Tests.csprojResolveAssemblyReference.cache and b/PollutometerWebApi.Tests/obj/Debug/PollutometerWebApi.Tests.csprojResolveAssemblyReference.cache differ diff --git a/PollutometerWebApi.Tests/obj/Debug/PollutometerWebApi.Tests.dll b/PollutometerWebApi.Tests/obj/Debug/PollutometerWebApi.Tests.dll index a1a6f09..4fec8a3 100644 Binary files a/PollutometerWebApi.Tests/obj/Debug/PollutometerWebApi.Tests.dll and b/PollutometerWebApi.Tests/obj/Debug/PollutometerWebApi.Tests.dll differ diff --git a/PollutometerWebApi.Tests/obj/Debug/PollutometerWebApi.Tests.pdb b/PollutometerWebApi.Tests/obj/Debug/PollutometerWebApi.Tests.pdb index ee082ed..4b521ed 100644 Binary files a/PollutometerWebApi.Tests/obj/Debug/PollutometerWebApi.Tests.pdb and b/PollutometerWebApi.Tests/obj/Debug/PollutometerWebApi.Tests.pdb differ diff --git a/PollutometerWebApi.userprefs b/PollutometerWebApi.userprefs index 6eb030b..34c119a 100644 --- a/PollutometerWebApi.userprefs +++ b/PollutometerWebApi.userprefs @@ -1,19 +1,10 @@ - - + + - - - - - - - - - - - - - + + + + diff --git a/PollutometerWebApi/AqiCalculator.cs b/PollutometerWebApi/AqiCalculator.cs new file mode 100644 index 0000000..e00d792 --- /dev/null +++ b/PollutometerWebApi/AqiCalculator.cs @@ -0,0 +1,102 @@ +using PollutometerWebApi.Models; + +namespace PollutometerWebApi +{ + public static class AqiCalculator + { + public static Aqi CalculateAqi(Reading reading) + { + 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, c = 0, cLow = 0, cHigh = 0, iLow = 0, iHigh = 0; + Aqi aqi = new Aqi(); + + for (int x = 0; x < 4; x++) + { + switch (x) + { + case 0: + c = reading.Co; + break; + case 1: + c = reading.So; + break; + case 2: + c = reading.No; + break; + } + + for (int y = 0; y < 7; y++) + { + if (c >= breakpoints[x, y, 0] && c <= breakpoints[x, y, 1]) + { + cLow = breakpoints[x, y, 0]; + cHigh = breakpoints[x, y, 1]; + iLow = breakpoints[3, y, 0]; + iHigh = breakpoints[3, y, 1]; + break; + } + } + + i = (iHigh - iLow) / (cHigh - cLow) * (c - cLow) + iLow; + + if (i > aqi.Value) + { + aqi.Value = i; + switch (x) + { + case 0: + aqi.GasName = "CO"; + break; + case 1: + aqi.GasName = "SO"; + break; + case 2: + aqi.GasName = "NO"; + break; + } + } + } + + return aqi; + } + } +} diff --git a/PollutometerWebApi/Controllers/ReadingsController.cs b/PollutometerWebApi/Controllers/ReadingsController.cs index 9767645..f915e17 100644 --- a/PollutometerWebApi/Controllers/ReadingsController.cs +++ b/PollutometerWebApi/Controllers/ReadingsController.cs @@ -12,8 +12,6 @@ namespace PollutometerWebApi.Controllers public IHttpActionResult GetAllReadings() { - Task.Run(() => EmailService.Start()); - var command = "SELECT * FROM Readings"; var readings = SqlOperator.GetReadings(command); @@ -67,7 +65,10 @@ namespace PollutometerWebApi.Controllers { if (reading != null) { - SqlOperator.PostReading(reading); + SqlOperator.PostReading(reading); + var aqi = AqiCalculator.CalculateAqi(reading); + if (aqi.Value > 151) + EmailSender.SendEmail(aqi); return Ok(); } else return BadRequest(); diff --git a/PollutometerWebApi/EmailSender.cs b/PollutometerWebApi/EmailSender.cs index 6d8702e..287551e 100644 --- a/PollutometerWebApi/EmailSender.cs +++ b/PollutometerWebApi/EmailSender.cs @@ -1,17 +1,13 @@ using System; -using System.Collections.Generic; using System.Net.Mail; +using PollutometerWebApi.Models; namespace PollutometerWebApi { - public class EmailSender + public static class EmailSender { - public EmailSender() {} - - public static void SendEmail(string gasName, double max) + public static void SendEmail(Aqi aqi) { - try - { MailMessage mail = new MailMessage("***REMOVED***", "***REMOVED***@edu.easj.dk"); SmtpClient client = new SmtpClient() { @@ -29,14 +25,9 @@ namespace PollutometerWebApi "\n" + "\n" + "\n" + - $"

The warning was triggered by {gasName}.

\n" + - $"

Air quality index: {max}

"; + $"

The warning was triggered by {aqi.GasName}.

\n" + + $"

Air quality index: {aqi.Value}

"; 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 deleted file mode 100644 index 3681dd2..0000000 --- a/PollutometerWebApi/EmailService.cs +++ /dev/null @@ -1,119 +0,0 @@ -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/Models/Aqi.cs b/PollutometerWebApi/Models/Aqi.cs new file mode 100644 index 0000000..490df85 --- /dev/null +++ b/PollutometerWebApi/Models/Aqi.cs @@ -0,0 +1,8 @@ +namespace PollutometerWebApi.Models +{ + public class Aqi + { + public string GasName { get; set; } + public double Value { get; set; } + } +} diff --git a/PollutometerWebApi/PollutometerWebApi.csproj b/PollutometerWebApi/PollutometerWebApi.csproj index d2dcd57..f2ef659 100644 --- a/PollutometerWebApi/PollutometerWebApi.csproj +++ b/PollutometerWebApi/PollutometerWebApi.csproj @@ -90,7 +90,8 @@ - + + -- cgit v1.2.3