mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-01-14 01:25:38 -05:00
Start adding API endpoints
This commit is contained in:
35
Environment/Service/Controllers/ReadingsController.cs
Normal file
35
Environment/Service/Controllers/ReadingsController.cs
Normal file
@@ -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<ActionResult<IEnumerable<Readings>>> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
using Dapper;
|
using ChrisKaczor.HomeMonitor.Environment.Service.Models;
|
||||||
|
using Dapper;
|
||||||
using DbUp;
|
using DbUp;
|
||||||
using Npgsql;
|
using Npgsql;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using ChrisKaczor.HomeMonitor.Environment.Service.Models.Indoor;
|
||||||
|
|
||||||
namespace ChrisKaczor.HomeMonitor.Environment.Service.Data;
|
namespace ChrisKaczor.HomeMonitor.Environment.Service.Data;
|
||||||
|
|
||||||
@@ -41,7 +43,7 @@ public class Database(IConfiguration configuration)
|
|||||||
return connection;
|
return connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task StoreMessageAsync(Message message)
|
public async Task StoreMessageAsync(DeviceMessage message)
|
||||||
{
|
{
|
||||||
await using var connection = CreateConnection();
|
await using var connection = CreateConnection();
|
||||||
|
|
||||||
@@ -49,4 +51,13 @@ public class Database(IConfiguration configuration)
|
|||||||
|
|
||||||
await connection.QueryAsync(query, message);
|
await connection.QueryAsync(query, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<Readings>> GetRecentReadings()
|
||||||
|
{
|
||||||
|
await using var connection = CreateConnection();
|
||||||
|
|
||||||
|
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Environment.Service.Data.Queries.GetRecentReadings.sql");
|
||||||
|
|
||||||
|
return await connection.QueryAsync<Readings>(query).ConfigureAwait(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
16
Environment/Service/Data/Queries/GetRecentReadings.sql
Normal file
16
Environment/Service/Data/Queries/GetRecentReadings.sql
Normal file
@@ -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
|
||||||
@@ -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
|
Accept: application/json
|
||||||
|
|
||||||
###
|
###
|
||||||
|
|
||||||
|
GET {{Environment_HostAddress}}/readings/recent?tz=America/New_York
|
||||||
|
Accept: application/json
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using ChrisKaczor.HomeMonitor.Environment.Service.Data;
|
using ChrisKaczor.HomeMonitor.Environment.Service.Data;
|
||||||
|
using ChrisKaczor.HomeMonitor.Environment.Service.Models.Indoor;
|
||||||
using Microsoft.AspNetCore.SignalR.Client;
|
using Microsoft.AspNetCore.SignalR.Client;
|
||||||
using MQTTnet;
|
using MQTTnet;
|
||||||
using MQTTnet.Client;
|
using MQTTnet.Client;
|
||||||
@@ -66,7 +67,7 @@ public class MessageHandler : IHostedService
|
|||||||
|
|
||||||
WriteLog($"Topic: {topic} = {payload}");
|
WriteLog($"Topic: {topic} = {payload}");
|
||||||
|
|
||||||
var message = JsonSerializer.Deserialize<Message>(payload);
|
var message = JsonSerializer.Deserialize<DeviceMessage>(payload);
|
||||||
|
|
||||||
if (message == null)
|
if (message == null)
|
||||||
return;
|
return;
|
||||||
@@ -76,7 +77,7 @@ public class MessageHandler : IHostedService
|
|||||||
await SendMessage(message);
|
await SendMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task SendMessage(Message message)
|
private async Task SendMessage(DeviceMessage message)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
using System.Text.Json.Serialization;
|
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")]
|
[JsonPropertyName("model")]
|
||||||
public required string Model { get; set; }
|
public required string Model { get; set; }
|
||||||
@@ -11,7 +11,7 @@ public class Message
|
|||||||
public required string Name { get; set; }
|
public required string Name { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("readings")]
|
[JsonPropertyName("readings")]
|
||||||
public required Readings Readings { get; set; }
|
public required DeviceReadings Readings { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("timestamp")]
|
[JsonPropertyName("timestamp")]
|
||||||
public required DateTimeOffset Timestamp { get; set; }
|
public required DateTimeOffset Timestamp { get; set; }
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
|
|
||||||
namespace ChrisKaczor.HomeMonitor.Environment.Service;
|
namespace ChrisKaczor.HomeMonitor.Environment.Service.Models.Indoor;
|
||||||
|
|
||||||
[UsedImplicitly]
|
[UsedImplicitly]
|
||||||
public class Readings
|
public class DeviceReadings
|
||||||
{
|
{
|
||||||
[JsonPropertyName("aqi")]
|
[JsonPropertyName("aqi")]
|
||||||
public decimal AirQualityIndex { get; set; }
|
public decimal AirQualityIndex { get; set; }
|
||||||
24
Environment/Service/Models/Readings.cs
Normal file
24
Environment/Service/Models/Readings.cs
Normal file
@@ -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; }
|
||||||
|
}
|
||||||
@@ -11,10 +11,12 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Remove="Data\Queries\CreateReading.sql" />
|
<None Remove="Data\Queries\CreateReading.sql" />
|
||||||
|
<None Remove="Data\Queries\GetRecentReadings.sql" />
|
||||||
<None Remove="Data\Schema\1-Initial Schema.sql" />
|
<None Remove="Data\Schema\1-Initial Schema.sql" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<EmbeddedResource Include="Data\Queries\GetRecentReadings.sql" />
|
||||||
<EmbeddedResource Include="Data\Queries\CreateReading.sql" />
|
<EmbeddedResource Include="Data\Queries\CreateReading.sql" />
|
||||||
<EmbeddedResource Include="Data\Schema\1-Initial Schema.sql" />
|
<EmbeddedResource Include="Data\Schema\1-Initial Schema.sql" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
@@ -29,7 +31,6 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Controllers\" />
|
|
||||||
<Folder Include="Properties\" />
|
<Folder Include="Properties\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user