From c8f0195ccc06575349c0e675c23276ac089bf9c7 Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Fri, 1 Dec 2017 14:48:19 +0100 Subject: Changed name of the project. --- PollutometerWebApi/.gitignore | 288 +++++++++++++++++++++ PollutometerWebApi/App_Start/RouteConfig.cs | 19 ++ PollutometerWebApi/App_Start/WebApiConfig.cs | 24 ++ PollutometerWebApi/Controllers/HomeController.cs | 19 ++ .../Controllers/ReadingsController.cs | 88 +++++++ PollutometerWebApi/Global.asax | 1 + PollutometerWebApi/Global.asax.cs | 17 ++ PollutometerWebApi/Models/Reading.cs | 11 + PollutometerWebApi/PollutometerWebApi.csproj | 113 ++++++++ PollutometerWebApi/SqlOperator.cs | 84 ++++++ PollutometerWebApi/Views/Home/Index.cshtml | 1 + PollutometerWebApi/Views/Shared/Error.cshtml | 5 + PollutometerWebApi/Views/Shared/_Layout.cshtml | 9 + PollutometerWebApi/Views/Web.config | 30 +++ PollutometerWebApi/Views/_ViewStart.cshtml | 3 + PollutometerWebApi/Web.config | 25 ++ PollutometerWebApi/packages.config | 12 + 17 files changed, 749 insertions(+) create mode 100644 PollutometerWebApi/.gitignore create mode 100644 PollutometerWebApi/App_Start/RouteConfig.cs create mode 100644 PollutometerWebApi/App_Start/WebApiConfig.cs create mode 100644 PollutometerWebApi/Controllers/HomeController.cs create mode 100644 PollutometerWebApi/Controllers/ReadingsController.cs create mode 100644 PollutometerWebApi/Global.asax create mode 100644 PollutometerWebApi/Global.asax.cs create mode 100644 PollutometerWebApi/Models/Reading.cs create mode 100644 PollutometerWebApi/PollutometerWebApi.csproj create mode 100644 PollutometerWebApi/SqlOperator.cs create mode 100644 PollutometerWebApi/Views/Home/Index.cshtml create mode 100644 PollutometerWebApi/Views/Shared/Error.cshtml create mode 100644 PollutometerWebApi/Views/Shared/_Layout.cshtml create mode 100644 PollutometerWebApi/Views/Web.config create mode 100644 PollutometerWebApi/Views/_ViewStart.cshtml create mode 100644 PollutometerWebApi/Web.config create mode 100644 PollutometerWebApi/packages.config (limited to 'PollutometerWebApi') diff --git a/PollutometerWebApi/.gitignore b/PollutometerWebApi/.gitignore new file mode 100644 index 0000000..940794e --- /dev/null +++ b/PollutometerWebApi/.gitignore @@ -0,0 +1,288 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ +**/Properties/launchSettings.json + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Typescript v1 declaration files +typings/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs diff --git a/PollutometerWebApi/App_Start/RouteConfig.cs b/PollutometerWebApi/App_Start/RouteConfig.cs new file mode 100644 index 0000000..7455a55 --- /dev/null +++ b/PollutometerWebApi/App_Start/RouteConfig.cs @@ -0,0 +1,19 @@ +using System.Web.Mvc; +using System.Web.Routing; + +namespace PollutometerWebApi +{ + public class RouteConfig + { + public static void RegisterRoutes(RouteCollection routes) + { + routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); + + routes.MapRoute( + name: "Default", + url: "{controller}/{action}/{id}", + defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } + ); + } + } +} diff --git a/PollutometerWebApi/App_Start/WebApiConfig.cs b/PollutometerWebApi/App_Start/WebApiConfig.cs new file mode 100644 index 0000000..8c2725a --- /dev/null +++ b/PollutometerWebApi/App_Start/WebApiConfig.cs @@ -0,0 +1,24 @@ +using System.Net.Http.Formatting; +using System.Web.Http; + +namespace PollutometerWebApi +{ + public static class WebApiConfig + { + public static void Register(HttpConfiguration config) + { + // Web API configuration and services + config.Formatters.Clear(); + config.Formatters.Add(new JsonMediaTypeFormatter()); + + // Web API routes + config.MapHttpAttributeRoutes(); + + config.Routes.MapHttpRoute( + name: "DefaultApi", + routeTemplate: "api/{controller}/{id}", + defaults: new { id = RouteParameter.Optional } + ); + } + } +} diff --git a/PollutometerWebApi/Controllers/HomeController.cs b/PollutometerWebApi/Controllers/HomeController.cs new file mode 100644 index 0000000..69e4846 --- /dev/null +++ b/PollutometerWebApi/Controllers/HomeController.cs @@ -0,0 +1,19 @@ +using System; +using System.Web.Mvc; + +namespace PollutometerWebApi.Controllers +{ + public class HomeController : Controller + { + public ActionResult Index() + { + var mvcName = typeof(Controller).Assembly.GetName(); + var isMono = Type.GetType("Mono.Runtime") != null; + + ViewData["Version"] = mvcName.Version.Major + "." + mvcName.Version.Minor; + ViewData["Runtime"] = isMono ? "Mono" : ".NET"; + + return View(); + } + } +} diff --git a/PollutometerWebApi/Controllers/ReadingsController.cs b/PollutometerWebApi/Controllers/ReadingsController.cs new file mode 100644 index 0000000..b692a9c --- /dev/null +++ b/PollutometerWebApi/Controllers/ReadingsController.cs @@ -0,0 +1,88 @@ +using System; +using System.Web.Http; +using PollutometerWebApi.Models; +using PollutometerWebApi.Singletons; + +namespace PollutometerWebApi.Controllers +{ + public class ReadingsController : ApiController + { + public ReadingsController() { } + + public IHttpActionResult GetAllReadings() + { + var command = "SELECT * FROM Readings"; + var readings = SqlOperator.GetReadings(command); + + if (readings.Count > 0) return Ok(readings); + else return NotFound(); + } + + public IHttpActionResult GetReading(int id) + { + var command = $"SELECT * FROM Readings WHERE Id={id}"; + var reading = SqlOperator.GetReadings(command)[0]; + + if (reading != null) return Ok(reading); + else return NotFound(); + } + + [Route("api/Readings/latest")] + public IHttpActionResult GetLatestReading() + { + var command = "SELECT * FROM Readings " + + "WHERE TimeStamp IN(SELECT MAX(TimeStamp) FROM Readings)"; + var reading = SqlOperator.GetReadings(command)[0]; + + if (reading != null) return Ok(reading); + else return NotFound(); + } + + [Route("api/Readings/lastweek")] + public IHttpActionResult GetReadingsFromLastWeek() + { + var timeNow = DateTimeOffset.Now.ToUnixTimeSeconds(); + var command = "SELECT * FROM Readings " + + $"WHERE TimeStamp BETWEEN {timeNow-7*24*3600} AND {timeNow}"; + var readings = SqlOperator.GetReadings(command); + + if (readings.Count > 0) return Ok(readings); + else return NotFound(); + } + + public IHttpActionResult PutReading(int id, Reading reading) + { + if (reading != null) + { + SqlOperator.PutReading(id, reading); + return Ok(); + } + else return BadRequest(); + } + + public IHttpActionResult PostReading(Reading reading) + { + if (reading != null) + { + SqlOperator.PostReading(reading); + return Ok(); + } + else return BadRequest(); + } + + public IHttpActionResult DeleteReading(int id) + { + var command = $"SELECT * FROM Readings WHERE Id={id}"; + + Reading reading = SqlOperator.GetReadings(command)[0]; + if (reading == null) + { + return NotFound(); + } + + SqlOperator.DeleteReading(id); + + return Ok(reading); + } + } +} \ No newline at end of file diff --git a/PollutometerWebApi/Global.asax b/PollutometerWebApi/Global.asax new file mode 100644 index 0000000..ee57536 --- /dev/null +++ b/PollutometerWebApi/Global.asax @@ -0,0 +1 @@ +<%@ Application Inherits="PollutometerWebApi.Global" %> diff --git a/PollutometerWebApi/Global.asax.cs b/PollutometerWebApi/Global.asax.cs new file mode 100644 index 0000000..3a03cf6 --- /dev/null +++ b/PollutometerWebApi/Global.asax.cs @@ -0,0 +1,17 @@ +using System.Web; +using System.Web.Mvc; +using System.Web.Routing; +using System.Web.Http; + +namespace PollutometerWebApi +{ + public class Global : HttpApplication + { + protected void Application_Start() + { + AreaRegistration.RegisterAllAreas(); + GlobalConfiguration.Configure(WebApiConfig.Register); + RouteConfig.RegisterRoutes(RouteTable.Routes); + } + } +} diff --git a/PollutometerWebApi/Models/Reading.cs b/PollutometerWebApi/Models/Reading.cs new file mode 100644 index 0000000..18b2326 --- /dev/null +++ b/PollutometerWebApi/Models/Reading.cs @@ -0,0 +1,11 @@ +namespace PollutometerWebApi.Models +{ + public class Reading + { + public int Id { get; set; } + public int TimeStamp { get; set; } + public double Co { get; set; } + public double No { get; set; } + public double So { get; set; } + } +} diff --git a/PollutometerWebApi/PollutometerWebApi.csproj b/PollutometerWebApi/PollutometerWebApi.csproj new file mode 100644 index 0000000..a997321 --- /dev/null +++ b/PollutometerWebApi/PollutometerWebApi.csproj @@ -0,0 +1,113 @@ + + + + Debug + AnyCPU + {22C32F4C-0DEB-40EA-9D56-48942CBB2A07} + {349C5851-65DF-11DA-9384-00065B846F21};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + Library + PollutometerWebApi + PollutometerWebApi + v4.6.2 + + + true + full + false + bin + DEBUG; + prompt + 4 + + + true + bin + prompt + 4 + + + + + + + + + + + + + + + + ..\packages\Microsoft.AspNet.Razor.3.2.3\lib\net45\System.Web.Razor.dll + + + ..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll + + + ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.Helpers.dll + + + ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Deployment.dll + + + ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.dll + + + ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Razor.dll + + + ..\packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45\System.Web.Mvc.dll + + + + + ..\packages\Microsoft.AspNet.WebApi.Client.5.2.0\lib\net45\System.Net.Http.Formatting.dll + + + ..\packages\Microsoft.AspNet.WebApi.Core.5.2.0\lib\net45\System.Web.Http.dll + + + ..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.0\lib\net45\System.Web.Http.WebHost.dll + + + ..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll + + + + + + + + + + + Global.asax + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/PollutometerWebApi/SqlOperator.cs b/PollutometerWebApi/SqlOperator.cs new file mode 100644 index 0000000..d85aa3d --- /dev/null +++ b/PollutometerWebApi/SqlOperator.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Data.SqlClient; +using PollutometerWebApi.Models; + +namespace PollutometerWebApi.Singletons +{ + public static class SqlOperator + { + const string ConnectionString = + "Server=tcp:forschool.database.windows.net,1433;" + + "Initial Catalog=schooldb;" + + "Persist Security Info=False;" + + "User ID=***REMOVED***;" + + "Password=***REMOVED***;" + + "MultipleActiveResultSets=False;" + + "Encrypt=True;" + + "TrustServerCertificate=False;" + + "Connection Timeout=30;"; + + public static List GetReadings(string command) + { + var readings = new List(); + + using (SqlConnection databaseConnection = new SqlConnection(ConnectionString)) + { + databaseConnection.Open(); + SqlCommand selectCommand = new SqlCommand(command, databaseConnection); + var reader = selectCommand.ExecuteReader(); + while (reader.Read()) + { + readings.Add(new Reading + { + Id = reader.GetInt32(0), + TimeStamp = reader.GetInt32(1), + Co = reader.GetDouble(2), + No = reader.GetDouble(3), + So = reader.GetDouble(4) + }); + } + } + + return readings; + } + + public static void PutReading(int id, Reading reading) + { + using (SqlConnection dbCon = new SqlConnection(ConnectionString)) + { + dbCon.Open(); + string query = $"UPDATE Readings SET TimeStamp='{reading.TimeStamp}'," + + $"Co='{reading.Co}', No='{reading.No}', So='{reading.So}' WHERE Id={id};"; + var cmd = new SqlCommand(query, dbCon); + cmd.ExecuteNonQuery(); + dbCon.Close(); + } + } + + public static void PostReading(Reading reading) + { + using (SqlConnection dbCon = new SqlConnection(ConnectionString)) + { + dbCon.Open(); + string query = $"INSERT INTO Readings (TimeStamp,Co,No,So)" + + $"VALUES('{reading.TimeStamp}',{reading.Co},{reading.No},{reading.So});"; + var cmd = new SqlCommand(query, dbCon); + cmd.ExecuteNonQuery(); + dbCon.Close(); + } + } + + public static void DeleteReading(int id) + { + using (SqlConnection dbCon = new SqlConnection(ConnectionString)) + { + dbCon.Open(); + string query = $"DELETE FROM Readings WHERE Id={id};"; + var cmd = new SqlCommand(query, dbCon); + cmd.ExecuteNonQuery(); + dbCon.Close(); + } + } + } +} \ No newline at end of file diff --git a/PollutometerWebApi/Views/Home/Index.cshtml b/PollutometerWebApi/Views/Home/Index.cshtml new file mode 100644 index 0000000..2501eee --- /dev/null +++ b/PollutometerWebApi/Views/Home/Index.cshtml @@ -0,0 +1 @@ +

Welcome to ASP.NET MVC @ViewData["Version"] on @ViewData["Runtime"]!

diff --git a/PollutometerWebApi/Views/Shared/Error.cshtml b/PollutometerWebApi/Views/Shared/Error.cshtml new file mode 100644 index 0000000..20ec06a --- /dev/null +++ b/PollutometerWebApi/Views/Shared/Error.cshtml @@ -0,0 +1,5 @@ +@model System.Web.Mvc.HandleErrorInfo + +
+

An error occurred while processing your request.

+
diff --git a/PollutometerWebApi/Views/Shared/_Layout.cshtml b/PollutometerWebApi/Views/Shared/_Layout.cshtml new file mode 100644 index 0000000..54f6729 --- /dev/null +++ b/PollutometerWebApi/Views/Shared/_Layout.cshtml @@ -0,0 +1,9 @@ + + + + @ViewBag.Title + + + @RenderBody() + + diff --git a/PollutometerWebApi/Views/Web.config b/PollutometerWebApi/Views/Web.config new file mode 100644 index 0000000..c23d873 --- /dev/null +++ b/PollutometerWebApi/Views/Web.config @@ -0,0 +1,30 @@ + + + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/PollutometerWebApi/Views/_ViewStart.cshtml b/PollutometerWebApi/Views/_ViewStart.cshtml new file mode 100644 index 0000000..2de6241 --- /dev/null +++ b/PollutometerWebApi/Views/_ViewStart.cshtml @@ -0,0 +1,3 @@ +@{ + Layout = "~/Views/Shared/_Layout.cshtml"; +} diff --git a/PollutometerWebApi/Web.config b/PollutometerWebApi/Web.config new file mode 100644 index 0000000..77dd8b3 --- /dev/null +++ b/PollutometerWebApi/Web.config @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/PollutometerWebApi/packages.config b/PollutometerWebApi/packages.config new file mode 100644 index 0000000..8631ca7 --- /dev/null +++ b/PollutometerWebApi/packages.config @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file -- cgit v1.2.3 From b7757c89b6576cfc33579f1dec118086e159e03e Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Fri, 1 Dec 2017 14:56:49 +0100 Subject: Added method for sending e-mail. --- .../Controllers/ReadingsController.cs | 5 ++++- PollutometerWebApi/EmailSender.cs | 26 ++++++++++++++++++++++ PollutometerWebApi/PollutometerWebApi.csproj | 1 + 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 PollutometerWebApi/EmailSender.cs (limited to 'PollutometerWebApi') diff --git a/PollutometerWebApi/Controllers/ReadingsController.cs b/PollutometerWebApi/Controllers/ReadingsController.cs index b692a9c..cb91c71 100644 --- a/PollutometerWebApi/Controllers/ReadingsController.cs +++ b/PollutometerWebApi/Controllers/ReadingsController.cs @@ -7,7 +7,10 @@ namespace PollutometerWebApi.Controllers { public class ReadingsController : ApiController { - public ReadingsController() { } + public ReadingsController() + { + EmailSender.SendEmail(); + } public IHttpActionResult GetAllReadings() { diff --git a/PollutometerWebApi/EmailSender.cs b/PollutometerWebApi/EmailSender.cs new file mode 100644 index 0000000..0e328c9 --- /dev/null +++ b/PollutometerWebApi/EmailSender.cs @@ -0,0 +1,26 @@ +using System; +using System.Net.Mail; + +namespace PollutometerWebApi +{ + public class EmailSender + { + public EmailSender() {} + + public static void SendEmail() + { + MailMessage mail = new MailMessage("***REMOVED***", "***REMOVED***@edu.easj.dk"); + SmtpClient client = new SmtpClient() + { + 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); + } + } +} \ No newline at end of file diff --git a/PollutometerWebApi/PollutometerWebApi.csproj b/PollutometerWebApi/PollutometerWebApi.csproj index a997321..8bde6ba 100644 --- a/PollutometerWebApi/PollutometerWebApi.csproj +++ b/PollutometerWebApi/PollutometerWebApi.csproj @@ -89,6 +89,7 @@ + -- cgit v1.2.3 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 From 644d63b7073f59290029bf084fb57db5a501309d Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Fri, 1 Dec 2017 18:07:59 +0100 Subject: Fixed e-mail layout. --- PollutometerWebApi/EmailSender.cs | 4 ++-- PollutometerWebApi/EmailService.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'PollutometerWebApi') diff --git a/PollutometerWebApi/EmailSender.cs b/PollutometerWebApi/EmailSender.cs index 9effa47..6d8702e 100644 --- a/PollutometerWebApi/EmailSender.cs +++ b/PollutometerWebApi/EmailSender.cs @@ -29,8 +29,8 @@ namespace PollutometerWebApi "\n" + "\n" + "\n" + - $"The warning was triggered by {gasName}. " + - $"Air quality index: {max}"; + $"

The warning was triggered by {gasName}.

\n" + + $"

Air quality index: {max}

"; client.Send(mail); } catch(Exception ex) diff --git a/PollutometerWebApi/EmailService.cs b/PollutometerWebApi/EmailService.cs index 06586b1..3681dd2 100644 --- a/PollutometerWebApi/EmailService.cs +++ b/PollutometerWebApi/EmailService.cs @@ -32,7 +32,7 @@ namespace PollutometerWebApi { if (result.Value > max) { - max = result.Value; + max = result.Value; gasName = result.Key; } } -- cgit v1.2.3 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/AqiCalculator.cs | 102 ++++++++++++++++++ .../Controllers/ReadingsController.cs | 7 +- PollutometerWebApi/EmailSender.cs | 19 +--- PollutometerWebApi/EmailService.cs | 119 --------------------- PollutometerWebApi/Models/Aqi.cs | 8 ++ PollutometerWebApi/PollutometerWebApi.csproj | 3 +- 6 files changed, 121 insertions(+), 137 deletions(-) create mode 100644 PollutometerWebApi/AqiCalculator.cs delete mode 100644 PollutometerWebApi/EmailService.cs create mode 100644 PollutometerWebApi/Models/Aqi.cs (limited to 'PollutometerWebApi') 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 From 37985c9923e712dc517b03f3280ce44b28090548 Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Mon, 4 Dec 2017 10:24:05 +0100 Subject: Sending e-mail if value is bigger or equal 151. --- PollutometerWebApi/Controllers/ReadingsController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'PollutometerWebApi') diff --git a/PollutometerWebApi/Controllers/ReadingsController.cs b/PollutometerWebApi/Controllers/ReadingsController.cs index f915e17..c80599f 100644 --- a/PollutometerWebApi/Controllers/ReadingsController.cs +++ b/PollutometerWebApi/Controllers/ReadingsController.cs @@ -67,7 +67,7 @@ namespace PollutometerWebApi.Controllers { SqlOperator.PostReading(reading); var aqi = AqiCalculator.CalculateAqi(reading); - if (aqi.Value > 151) + if (aqi.Value >= 151) EmailSender.SendEmail(aqi); return Ok(); } -- cgit v1.2.3 From e41a7bee10dbb968913429c5d0e4a1fcbbb5b5e3 Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Mon, 4 Dec 2017 12:38:28 +0100 Subject: Changed e-mail address. --- PollutometerWebApi/EmailSender.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'PollutometerWebApi') diff --git a/PollutometerWebApi/EmailSender.cs b/PollutometerWebApi/EmailSender.cs index 287551e..f62a9ca 100644 --- a/PollutometerWebApi/EmailSender.cs +++ b/PollutometerWebApi/EmailSender.cs @@ -8,7 +8,7 @@ namespace PollutometerWebApi { public static void SendEmail(Aqi aqi) { - MailMessage mail = new MailMessage("***REMOVED***", "***REMOVED***@edu.easj.dk"); + MailMessage mail = new MailMessage("***REMOVED***", "alek2026@edu.easj.dk"); SmtpClient client = new SmtpClient() { Host = "mail.cock.li", -- cgit v1.2.3 From 54d1019413958e0ba24b85007c1f5f97ca19a0b3 Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Mon, 4 Dec 2017 13:01:04 +0100 Subject: Added levels of AQI. --- PollutometerWebApi/EmailSender.cs | 39 ++++++++++++++++++++------------------- PollutometerWebApi/Models/Aqi.cs | 28 +++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 20 deletions(-) (limited to 'PollutometerWebApi') diff --git a/PollutometerWebApi/EmailSender.cs b/PollutometerWebApi/EmailSender.cs index f62a9ca..6cc60fc 100644 --- a/PollutometerWebApi/EmailSender.cs +++ b/PollutometerWebApi/EmailSender.cs @@ -8,26 +8,27 @@ namespace PollutometerWebApi { public static void SendEmail(Aqi aqi) { - MailMessage mail = new MailMessage("***REMOVED***", "alek2026@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" + + MailMessage mail = new MailMessage("***REMOVED***", "alek2026@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 {aqi.GasName}.

\n" + - $"

Air quality index: {aqi.Value}

"; - client.Send(mail); + $"

Air quality index: {aqi.Value}

" + + $"

The AQI level is {aqi.Level}!"; + client.Send(mail); } } } \ No newline at end of file diff --git a/PollutometerWebApi/Models/Aqi.cs b/PollutometerWebApi/Models/Aqi.cs index 490df85..b13ddb0 100644 --- a/PollutometerWebApi/Models/Aqi.cs +++ b/PollutometerWebApi/Models/Aqi.cs @@ -2,7 +2,33 @@ { public class Aqi { + double _value; + public string GasName { get; set; } - public double Value { get; set; } + public double Value + { + get + { + return _value; + } + set + { + _value = value; + + if (_value >= 0 && _value <= 50) + Level = "Good"; + else if (_value >= 51 && _value <= 100) + Level = "Moderate"; + else if (_value >= 101 && _value <= 150) + Level = "Unhealthy for Sensitive Groups"; + else if (_value >= 151 && _value <= 200) + Level = "Unhealthy"; + else if (_value >= 201 && _value <= 300) + Level = "Very Unhealthy"; + else if (_value >= 301 && _value <= 500) + Level = "Hazardous"; + } + } + public string Level { get; set; } } } -- cgit v1.2.3 From e4e7dbf553bfa17a39830e67d14d3715946beab3 Mon Sep 17 00:00:00 2001 From: Marcin Zelent Date: Tue, 12 Dec 2017 14:24:47 +0100 Subject: Added unit tests and fixed some bugs. --- PollutometerWebApi/App_Start/WebApiConfig.cs | 46 ++--- PollutometerWebApi/AqiCalculator.cs | 188 ++++++++++----------- .../Controllers/ReadingsController.cs | 127 +++++++------- PollutometerWebApi/PollutometerWebApi.csproj | 47 +++++- PollutometerWebApi/SqlOperator.cs | 5 +- PollutometerWebApi/Views/Web.config | 2 +- 6 files changed, 221 insertions(+), 194 deletions(-) (limited to 'PollutometerWebApi') diff --git a/PollutometerWebApi/App_Start/WebApiConfig.cs b/PollutometerWebApi/App_Start/WebApiConfig.cs index 8c2725a..d3b2dca 100644 --- a/PollutometerWebApi/App_Start/WebApiConfig.cs +++ b/PollutometerWebApi/App_Start/WebApiConfig.cs @@ -1,24 +1,24 @@ using System.Net.Http.Formatting; -using System.Web.Http; - -namespace PollutometerWebApi -{ - public static class WebApiConfig - { - public static void Register(HttpConfiguration config) - { - // Web API configuration and services - config.Formatters.Clear(); - config.Formatters.Add(new JsonMediaTypeFormatter()); - - // Web API routes - config.MapHttpAttributeRoutes(); - - config.Routes.MapHttpRoute( - name: "DefaultApi", - routeTemplate: "api/{controller}/{id}", - defaults: new { id = RouteParameter.Optional } - ); - } - } -} +using System.Web.Http; + +namespace PollutometerWebApi +{ + public static class WebApiConfig + { + public static void Register(HttpConfiguration config) + { + // Web API configuration and services + config.Formatters.Clear(); + config.Formatters.Add(new JsonMediaTypeFormatter()); + + // Web API routes + config.MapHttpAttributeRoutes(); + + config.Routes.MapHttpRoute( + name: "DefaultApi", + routeTemplate: "api/{controller}/{id}", + defaults: new { id = RouteParameter.Optional } + ); + } + } +} diff --git a/PollutometerWebApi/AqiCalculator.cs b/PollutometerWebApi/AqiCalculator.cs index e00d792..58f6194 100644 --- a/PollutometerWebApi/AqiCalculator.cs +++ b/PollutometerWebApi/AqiCalculator.cs @@ -1,102 +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} - } - }; - +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(); - + 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; - } + { + 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) + 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; - } + 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; - } - } -} + } + + return aqi; + } + } +} diff --git a/PollutometerWebApi/Controllers/ReadingsController.cs b/PollutometerWebApi/Controllers/ReadingsController.cs index c80599f..adf4d5d 100644 --- a/PollutometerWebApi/Controllers/ReadingsController.cs +++ b/PollutometerWebApi/Controllers/ReadingsController.cs @@ -1,92 +1,85 @@ using System; -using System.Threading.Tasks; using System.Web.Http; -using PollutometerWebApi.Models; -using PollutometerWebApi.Singletons; - +using PollutometerWebApi.Models; + namespace PollutometerWebApi.Controllers { public class ReadingsController : ApiController - { - public ReadingsController() {} - - public IHttpActionResult GetAllReadings() - { + { + public ReadingsController() {} + + public IHttpActionResult GetAllReadings() + { var command = "SELECT * FROM Readings"; - var readings = SqlOperator.GetReadings(command); - + var readings = SqlOperator.GetReadings(command); + if (readings.Count > 0) return Ok(readings); - else return NotFound(); - } - - public IHttpActionResult GetReading(int id) + else return NotFound(); + } + + public IHttpActionResult GetReading(int id) { - var command = $"SELECT * FROM Readings WHERE Id={id}"; - var reading = SqlOperator.GetReadings(command)[0]; - - if (reading != null) return Ok(reading); - else return NotFound(); + var command = $"SELECT * FROM Readings WHERE Id={id}"; + var readings = SqlOperator.GetReadings(command); + + if (readings.Count == 1) return Ok(readings[0]); + else return NotFound(); } [Route("api/Readings/latest")] public IHttpActionResult GetLatestReading() - { + { var command = "SELECT * FROM Readings " + - "WHERE TimeStamp IN(SELECT MAX(TimeStamp) FROM Readings)"; - var reading = SqlOperator.GetReadings(command)[0]; - - if (reading != null) return Ok(reading); + "WHERE TimeStamp IN(SELECT MAX(TimeStamp) FROM Readings)"; + var reading = SqlOperator.GetReadings(command)[0]; + + if (reading != null) return Ok(reading); else return NotFound(); - } + } - [Route("api/Readings/lastweek")] - public IHttpActionResult GetReadingsFromLastWeek() + [Route("api/Readings/lastweek")] + public IHttpActionResult GetReadingsFromLastWeek() { - var timeNow = DateTimeOffset.Now.ToUnixTimeSeconds(); + var timeNow = DateTimeOffset.Now.ToUnixTimeSeconds(); var command = "SELECT * FROM Readings " + - $"WHERE TimeStamp BETWEEN {timeNow-7*24*3600} AND {timeNow}"; - var readings = SqlOperator.GetReadings(command); - - if (readings.Count > 0) return Ok(readings); - else return NotFound(); - } - - public IHttpActionResult PutReading(int id, Reading reading) - { - if (reading != null) - { - SqlOperator.PutReading(id, reading); - return Ok(); - } - else return BadRequest(); - } - - public IHttpActionResult PostReading(Reading reading) - { - if (reading != null) - { + $"WHERE TimeStamp BETWEEN {timeNow-7*24*3600} AND {timeNow}"; + var readings = SqlOperator.GetReadings(command); + + if (readings.Count > 0) return Ok(readings); + else return NotFound(); + } + + public IHttpActionResult PutReading(int id, Reading reading) + { + if (reading != null) + { + SqlOperator.PutReading(id, reading); + return Ok(); + } + else return BadRequest(); + } + + public IHttpActionResult PostReading(Reading reading) + { + if (reading != null) + { SqlOperator.PostReading(reading); var aqi = AqiCalculator.CalculateAqi(reading); if (aqi.Value >= 151) - EmailSender.SendEmail(aqi); - return Ok(); - } - else return BadRequest(); - } - - public IHttpActionResult DeleteReading(int id) + EmailSender.SendEmail(aqi); + return Ok(reading); + } + else return BadRequest(); + } + + public IHttpActionResult DeleteReading(int id) { var command = $"SELECT * FROM Readings WHERE Id={id}"; - - Reading reading = SqlOperator.GetReadings(command)[0]; - if (reading == null) - { - return NotFound(); - } - - SqlOperator.DeleteReading(id); - - return Ok(reading); + var readings = SqlOperator.GetReadings(command); + if (readings.Count == 0) return NotFound(); + SqlOperator.DeleteReading(id); + + return Ok(readings[0]); } } } \ No newline at end of file diff --git a/PollutometerWebApi/PollutometerWebApi.csproj b/PollutometerWebApi/PollutometerWebApi.csproj index f2ef659..6e5eb45 100644 --- a/PollutometerWebApi/PollutometerWebApi.csproj +++ b/PollutometerWebApi/PollutometerWebApi.csproj @@ -1,5 +1,6 @@ - + + Debug AnyCPU @@ -9,6 +10,18 @@ PollutometerWebApi PollutometerWebApi v4.6.2 + + + + + 4.0 + true + + + + + + true @@ -76,7 +89,6 @@ - @@ -94,7 +106,9 @@ - + + Designer + @@ -103,14 +117,35 @@ + + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + - - + + + + + + + True + True + 0 + / + http://localhost:50114/ + False + False + + + False + + + - + \ No newline at end of file diff --git a/PollutometerWebApi/SqlOperator.cs b/PollutometerWebApi/SqlOperator.cs index d85aa3d..1e296a1 100644 --- a/PollutometerWebApi/SqlOperator.cs +++ b/PollutometerWebApi/SqlOperator.cs @@ -1,9 +1,8 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Data.SqlClient; using PollutometerWebApi.Models; -namespace PollutometerWebApi.Singletons +namespace PollutometerWebApi { public static class SqlOperator { diff --git a/PollutometerWebApi/Views/Web.config b/PollutometerWebApi/Views/Web.config index c23d873..35da8d0 100644 --- a/PollutometerWebApi/Views/Web.config +++ b/PollutometerWebApi/Views/Web.config @@ -14,7 +14,7 @@ - + -- cgit v1.2.3