diff options
Diffstat (limited to 'PollutometerWebApi')
-rw-r--r-- | PollutometerWebApi/AqiCalculator.cs | 102 | ||||
-rw-r--r-- | PollutometerWebApi/Controllers/ReadingsController.cs | 7 | ||||
-rw-r--r-- | PollutometerWebApi/EmailSender.cs | 19 | ||||
-rw-r--r-- | PollutometerWebApi/EmailService.cs | 119 | ||||
-rw-r--r-- | PollutometerWebApi/Models/Aqi.cs | 8 | ||||
-rw-r--r-- | PollutometerWebApi/PollutometerWebApi.csproj | 3 |
6 files changed, 121 insertions, 137 deletions
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" + "<img src=\"https://i.imgflip.com/20b4q2.jpg\"/>\n" + "\n" + - $"<p>The warning was triggered by {gasName}.</p>\n" + - $"<p>Air quality index: {max}</p>"; + $"<p>The warning was triggered by {aqi.GasName}.</p>\n" + + $"<p>Air quality index: {aqi.Value}</p>"; 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<string, double> results = new Dictionary<string, double>(); - 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 @@ <Compile Include="Models\Reading.cs" />
<Compile Include="SqlOperator.cs" />
<Compile Include="EmailSender.cs" />
- <Compile Include="EmailService.cs" />
+ <Compile Include="AqiCalculator.cs" />
+ <Compile Include="Models\Aqi.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\Web.config" />
|