diff --git a/Environment/Service/Controllers/ReadingsController.cs b/Environment/Service/Controllers/ReadingsController.cs new file mode 100644 index 0000000..6f00a2e --- /dev/null +++ b/Environment/Service/Controllers/ReadingsController.cs @@ -0,0 +1,35 @@ +using ChrisKaczor.HomeMonitor.Environment.Service.Data; +using ChrisKaczor.HomeMonitor.Environment.Service.Models; +using Microsoft.AspNetCore.Mvc; + +namespace ChrisKaczor.HomeMonitor.Environment.Service.Controllers; + +[Route("[controller]")] +[ApiController] +public class ReadingsController(Database database) : ControllerBase +{ + [HttpGet("recent")] + public async Task>> GetRecent([FromQuery] string? tz) + { + var recentReadings = await database.GetRecentReadings(); + + if (string.IsNullOrWhiteSpace(tz)) + return Ok(recentReadings); + + try + { + var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(tz); + + foreach (var recentReading in recentReadings) + { + recentReading.Time = recentReading.Time.ToOffset(timeZoneInfo.GetUtcOffset(recentReading.Time)); + } + } + catch (Exception e) + { + return BadRequest(e.Message); + } + + return Ok(recentReadings); + } +} \ No newline at end of file diff --git a/Environment/Service/Data/Database.cs b/Environment/Service/Data/Database.cs index e46e28f..d08e882 100644 --- a/Environment/Service/Data/Database.cs +++ b/Environment/Service/Data/Database.cs @@ -1,7 +1,9 @@ -using Dapper; +using ChrisKaczor.HomeMonitor.Environment.Service.Models; +using Dapper; using DbUp; using Npgsql; using System.Reflection; +using ChrisKaczor.HomeMonitor.Environment.Service.Models.Indoor; namespace ChrisKaczor.HomeMonitor.Environment.Service.Data; @@ -41,7 +43,7 @@ public class Database(IConfiguration configuration) return connection; } - public async Task StoreMessageAsync(Message message) + public async Task StoreMessageAsync(DeviceMessage message) { await using var connection = CreateConnection(); @@ -49,4 +51,13 @@ public class Database(IConfiguration configuration) await connection.QueryAsync(query, message); } + + public async Task> GetRecentReadings() + { + await using var connection = CreateConnection(); + + var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Environment.Service.Data.Queries.GetRecentReadings.sql"); + + return await connection.QueryAsync(query).ConfigureAwait(false); + } } \ No newline at end of file diff --git a/Environment/Service/Data/Queries/GetRecentReadings.sql b/Environment/Service/Data/Queries/GetRecentReadings.sql new file mode 100644 index 0000000..b4443ee --- /dev/null +++ b/Environment/Service/Data/Queries/GetRecentReadings.sql @@ -0,0 +1,16 @@ +SELECT DISTINCT ON (name) + time, + name, + model, + temperature, + pressure, + humidity, + luminance, + gas_resistance AS gasResistance, + color_temperature AS colorTemperature, + air_quality_index AS airQualityIndex +FROM + reading +ORDER BY + name, + time DESC diff --git a/Environment/Service/Environment.http b/Environment/Service/Environment.http index f257474..6c23683 100644 --- a/Environment/Service/Environment.http +++ b/Environment/Service/Environment.http @@ -1,6 +1,9 @@ -@Environment_HostAddress = http://localhost:5234 +@Environment_HostAddress = http://localhost:5000 -GET {{Environment_HostAddress}}/weatherforecast/ +GET {{Environment_HostAddress}}/readings/recent Accept: application/json ### + +GET {{Environment_HostAddress}}/readings/recent?tz=America/New_York +Accept: application/json diff --git a/Environment/Service/MessageHandler.cs b/Environment/Service/MessageHandler.cs index 0f6c3b7..2a22964 100644 --- a/Environment/Service/MessageHandler.cs +++ b/Environment/Service/MessageHandler.cs @@ -1,5 +1,6 @@ using System.Text.Json; using ChrisKaczor.HomeMonitor.Environment.Service.Data; +using ChrisKaczor.HomeMonitor.Environment.Service.Models.Indoor; using Microsoft.AspNetCore.SignalR.Client; using MQTTnet; using MQTTnet.Client; @@ -66,7 +67,7 @@ public class MessageHandler : IHostedService WriteLog($"Topic: {topic} = {payload}"); - var message = JsonSerializer.Deserialize(payload); + var message = JsonSerializer.Deserialize(payload); if (message == null) return; @@ -76,7 +77,7 @@ public class MessageHandler : IHostedService await SendMessage(message); } - private async Task SendMessage(Message message) + private async Task SendMessage(DeviceMessage message) { try { diff --git a/Environment/Service/Message.cs b/Environment/Service/Models/Indoor/DeviceMessage.cs similarity index 82% rename from Environment/Service/Message.cs rename to Environment/Service/Models/Indoor/DeviceMessage.cs index d1cfb12..d455c04 100644 --- a/Environment/Service/Message.cs +++ b/Environment/Service/Models/Indoor/DeviceMessage.cs @@ -1,8 +1,8 @@ using System.Text.Json.Serialization; -namespace ChrisKaczor.HomeMonitor.Environment.Service; +namespace ChrisKaczor.HomeMonitor.Environment.Service.Models.Indoor; -public class Message +public class DeviceMessage { [JsonPropertyName("model")] public required string Model { get; set; } @@ -11,7 +11,7 @@ public class Message public required string Name { get; set; } [JsonPropertyName("readings")] - public required Readings Readings { get; set; } + public required DeviceReadings Readings { get; set; } [JsonPropertyName("timestamp")] public required DateTimeOffset Timestamp { get; set; } diff --git a/Environment/Service/Readings.cs b/Environment/Service/Models/Indoor/DeviceReadings.cs similarity index 87% rename from Environment/Service/Readings.cs rename to Environment/Service/Models/Indoor/DeviceReadings.cs index c1f4919..77c0c1d 100644 --- a/Environment/Service/Readings.cs +++ b/Environment/Service/Models/Indoor/DeviceReadings.cs @@ -1,10 +1,10 @@ using System.Text.Json.Serialization; using JetBrains.Annotations; -namespace ChrisKaczor.HomeMonitor.Environment.Service; +namespace ChrisKaczor.HomeMonitor.Environment.Service.Models.Indoor; [UsedImplicitly] -public class Readings +public class DeviceReadings { [JsonPropertyName("aqi")] public decimal AirQualityIndex { get; set; } diff --git a/Environment/Service/Models/Readings.cs b/Environment/Service/Models/Readings.cs new file mode 100644 index 0000000..8c583ab --- /dev/null +++ b/Environment/Service/Models/Readings.cs @@ -0,0 +1,24 @@ +namespace ChrisKaczor.HomeMonitor.Environment.Service.Models; + +public class Readings +{ + public DateTimeOffset Time { get; set; } + + public string? Name { get; set; } + + public string? Model { get; set; } + + public decimal AirQualityIndex { get; set; } + + public decimal ColorTemperature { get; set; } + + public decimal GasResistance { get; set; } + + public decimal Humidity { get; set; } + + public decimal Luminance { get; set; } + + public decimal Pressure { get; set; } + + public decimal Temperature { get; set; } +} \ No newline at end of file diff --git a/Environment/Service/Service.csproj b/Environment/Service/Service.csproj index b729ca1..a337ca3 100644 --- a/Environment/Service/Service.csproj +++ b/Environment/Service/Service.csproj @@ -11,10 +11,12 @@ + + @@ -29,7 +31,6 @@ -