diff --git a/Weather/Service/Controllers/ReadingsController.cs b/Weather/Service/Controllers/ReadingsController.cs new file mode 100644 index 0000000..d1d240c --- /dev/null +++ b/Weather/Service/Controllers/ReadingsController.cs @@ -0,0 +1,25 @@ +using ChrisKaczor.HomeMonitor.Weather.Models; +using ChrisKaczor.HomeMonitor.Weather.Service.Data; +using Microsoft.AspNetCore.Mvc; +using System.Threading.Tasks; + +namespace ChrisKaczor.HomeMonitor.Weather.Service.Controllers +{ + [Route("[controller]")] + [ApiController] + public class ReadingsController : ControllerBase + { + private readonly Database _database; + + public ReadingsController(Database database) + { + _database = database; + } + + [HttpGet("recent")] + public async Task> GetRecent() + { + return await _database.GetRecentReading(); + } + } +} \ No newline at end of file diff --git a/Weather/Service/Controllers/ValuesController.cs b/Weather/Service/Controllers/ValuesController.cs deleted file mode 100644 index a45cc90..0000000 --- a/Weather/Service/Controllers/ValuesController.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using System.Collections.Generic; - -namespace ChrisKaczor.HomeMonitor.Weather.Service.Controllers -{ - [Route("[controller]")] - [ApiController] - public class ValuesController : ControllerBase - { - [HttpGet] - public ActionResult> Get() - { - return new[] { "value1", "value2" }; - } - } -} \ No newline at end of file diff --git a/Weather/Service/Data/Database.cs b/Weather/Service/Data/Database.cs index 39deee5..0c26ac7 100644 --- a/Weather/Service/Data/Database.cs +++ b/Weather/Service/Data/Database.cs @@ -2,6 +2,7 @@ using Dapper; using Microsoft.Extensions.Configuration; using System.Data.SqlClient; +using System.Threading.Tasks; namespace ChrisKaczor.HomeMonitor.Weather.Service.Data { @@ -24,32 +25,31 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Data InitialCatalog = "master" }; - using (var connection = new SqlConnection(connectionStringBuilder.ConnectionString)) + using var connection = new SqlConnection(connectionStringBuilder.ConnectionString); + + var command = new SqlCommand { Connection = connection }; + + connection.Open(); + + // Check to see if the database exists + command.CommandText = $"SELECT CAST(1 as bit) from sys.databases WHERE name='{_configuration["Weather:Database:Name"]}'"; + var databaseExists = (bool?)command.ExecuteScalar(); + + // Create database if needed + if (!databaseExists.GetValueOrDefault(false)) { - var command = new SqlCommand { Connection = connection }; - - connection.Open(); - - // Check to see if the database exists - command.CommandText = $"SELECT CAST(1 as bit) from sys.databases WHERE name='{_configuration["Weather:Database:Name"]}'"; - var databaseExists = (bool?)command.ExecuteScalar(); - - // Create database if needed - if (!databaseExists.GetValueOrDefault(false)) - { - command.CommandText = $"CREATE DATABASE {_configuration["Weather:Database:Name"]}"; - command.ExecuteNonQuery(); - } - - // Switch to the database now that we're sure it exists - connection.ChangeDatabase(_configuration["Weather:Database:Name"]); - - var schema = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.Schema.sql"); - - // Make sure the database is up to date - command.CommandText = schema; + command.CommandText = $"CREATE DATABASE {_configuration["Weather:Database:Name"]}"; command.ExecuteNonQuery(); } + + // Switch to the database now that we're sure it exists + connection.ChangeDatabase(_configuration["Weather:Database:Name"]); + + var schema = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.Schema.sql"); + + // Make sure the database is up to date + command.CommandText = schema; + command.ExecuteNonQuery(); } private SqlConnection CreateConnection() @@ -70,12 +70,20 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Data public void StoreWeatherData(WeatherMessage weatherMessage) { - using (var connection = CreateConnection()) - { - var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.CreateReading.sql"); + using var connection = CreateConnection(); - connection.Query(query, weatherMessage); - } + var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.CreateReading.sql"); + + connection.Query(query, weatherMessage); + } + + public async Task GetRecentReading() + { + await using var connection = CreateConnection(); + + var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.GetRecentReading.sql"); + + return await connection.QueryFirstOrDefaultAsync(query); } } } \ No newline at end of file diff --git a/Weather/Service/Data/Resources/GetRecentReading.sql b/Weather/Service/Data/Resources/GetRecentReading.sql new file mode 100644 index 0000000..cec182f --- /dev/null +++ b/Weather/Service/Data/Resources/GetRecentReading.sql @@ -0,0 +1,17 @@ +SELECT TOP 1 Timestamp, + WindDirection, + WindSpeed, + Humidity, + HumidityTemperature, + Rain, + Pressure, + PressureTemperature, + BatteryLevel, + LightLevel, + Latitude, + Longitude, + Altitude, + SatelliteCount, + GpsTimestamp +FROM Reading +ORDER BY Timestamp DESC \ No newline at end of file diff --git a/Weather/Service/MessageHandler.cs b/Weather/Service/MessageHandler.cs index 1904ed8..826564f 100644 --- a/Weather/Service/MessageHandler.cs +++ b/Weather/Service/MessageHandler.cs @@ -33,11 +33,16 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service public Task StartAsync(CancellationToken cancellationToken) { + var host = _configuration["Weather:Queue:Host"]; + + if (string.IsNullOrEmpty(host)) + return Task.CompletedTask; + WriteLog("MessageHandler: Start"); var factory = new ConnectionFactory { - HostName = _configuration["Weather:Queue:Host"], + HostName = host, UserName = _configuration["Weather:Queue:User"], Password = _configuration["Weather:Queue:Password"] }; @@ -64,8 +69,8 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service _hubConnection?.StopAsync(cancellationToken).Wait(cancellationToken); - _queueModel.Close(); - _queueConnection.Close(); + _queueModel?.Close(); + _queueConnection?.Close(); return Task.CompletedTask; } diff --git a/Weather/Service/Service.csproj b/Weather/Service/Service.csproj index 73fcdcc..17c807f 100644 --- a/Weather/Service/Service.csproj +++ b/Weather/Service/Service.csproj @@ -10,16 +10,18 @@ + + - +