mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-02-16 10:58:32 -05:00
Add history endpoint and do some cleanup
This commit is contained in:
@@ -32,4 +32,10 @@ public class ReadingsController(Database database) : ControllerBase
|
|||||||
|
|
||||||
return Ok(recentReadings);
|
return Ok(recentReadings);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet("history-grouped")]
|
||||||
|
public async Task<ActionResult<List<ReadingsGrouped>>> GetHistoryGrouped(DateTimeOffset start, DateTimeOffset end, int bucketMinutes = 5)
|
||||||
|
{
|
||||||
|
return (await database.GetReadingsHistoryGrouped(start, end, bucketMinutes)).ToList();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -47,7 +47,7 @@ public class Database(IConfiguration configuration)
|
|||||||
{
|
{
|
||||||
await using var connection = CreateConnection();
|
await using var connection = CreateConnection();
|
||||||
|
|
||||||
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Environment.Service.Data.Queries.CreateReading.sql");
|
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Environment.Service.Data.Queries.CreateReading.psql");
|
||||||
|
|
||||||
await connection.QueryAsync(query, message);
|
await connection.QueryAsync(query, message);
|
||||||
}
|
}
|
||||||
@@ -56,8 +56,17 @@ public class Database(IConfiguration configuration)
|
|||||||
{
|
{
|
||||||
await using var connection = CreateConnection();
|
await using var connection = CreateConnection();
|
||||||
|
|
||||||
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Environment.Service.Data.Queries.GetRecentReadings.sql");
|
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Environment.Service.Data.Queries.GetRecentReadings.psql");
|
||||||
|
|
||||||
return await connection.QueryAsync<Readings>(query).ConfigureAwait(false);
|
return await connection.QueryAsync<Readings>(query).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<ReadingsGrouped>> GetReadingsHistoryGrouped(DateTimeOffset start, DateTimeOffset end, int bucketMinutes)
|
||||||
|
{
|
||||||
|
await using var connection = CreateConnection();
|
||||||
|
|
||||||
|
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Environment.Service.Data.Queries.GetReadingsHistoryGrouped.psql");
|
||||||
|
|
||||||
|
return await connection.QueryAsync<ReadingsGrouped>(query, new { Start = start, End = end, BucketMinutes = bucketMinutes }).ConfigureAwait(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
SELECT
|
||||||
|
time_bucket('15 minute', time) AS bucket,
|
||||||
|
name,
|
||||||
|
ROUND(avg(temperature), 2) AS averageTemperature,
|
||||||
|
ROUND(avg(pressure), 2) AS averagePressure,
|
||||||
|
ROUND(avg(humidity), 2) AS averageHumidity,
|
||||||
|
ROUND(avg(luminance), 2) AS averageLuminance,
|
||||||
|
ROUND(avg(gas_resistance), 2) AS averageGasResistance,
|
||||||
|
ROUND(avg(color_temperature), 2) AS averageColorTemperature,
|
||||||
|
ROUND(avg(air_quality_index), 2) AS averageAirQualityIndex
|
||||||
|
FROM
|
||||||
|
reading
|
||||||
|
WHERE
|
||||||
|
time BETWEEN @Start AND @End
|
||||||
|
GROUP BY
|
||||||
|
bucket,
|
||||||
|
name
|
||||||
|
ORDER BY
|
||||||
|
bucket ASC,
|
||||||
|
name;
|
||||||
@@ -7,3 +7,8 @@ Accept: application/json
|
|||||||
|
|
||||||
GET {{Environment_HostAddress}}/readings/recent?tz=America/New_York
|
GET {{Environment_HostAddress}}/readings/recent?tz=America/New_York
|
||||||
Accept: application/json
|
Accept: application/json
|
||||||
|
|
||||||
|
###
|
||||||
|
|
||||||
|
GET {{Environment_HostAddress}}/readings/history-grouped?start=2024-02-02T06:00&end=2024-02-02T08:00&bucketMinutes=15
|
||||||
|
Accept: application/json
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
using System.Text.Json;
|
using ChrisKaczor.HomeMonitor.Environment.Service.Data;
|
||||||
using ChrisKaczor.HomeMonitor.Environment.Service.Data;
|
|
||||||
using ChrisKaczor.HomeMonitor.Environment.Service.Models.Indoor;
|
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;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace ChrisKaczor.HomeMonitor.Environment.Service;
|
namespace ChrisKaczor.HomeMonitor.Environment.Service;
|
||||||
|
|
||||||
@@ -22,8 +22,6 @@ public class MessageHandler : IHostedService
|
|||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
_database = database;
|
_database = database;
|
||||||
|
|
||||||
_database.EnsureDatabase();
|
|
||||||
|
|
||||||
_topic = _configuration["Mqtt:Topic"] ?? string.Empty;
|
_topic = _configuration["Mqtt:Topic"] ?? string.Empty;
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(_topic))
|
if (string.IsNullOrEmpty(_topic))
|
||||||
|
|||||||
25
Environment/Service/Models/ReadingsGrouped.cs
Normal file
25
Environment/Service/Models/ReadingsGrouped.cs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
using JetBrains.Annotations;
|
||||||
|
|
||||||
|
namespace ChrisKaczor.HomeMonitor.Environment.Service.Models;
|
||||||
|
|
||||||
|
[PublicAPI]
|
||||||
|
public class ReadingsGrouped
|
||||||
|
{
|
||||||
|
public DateTimeOffset Bucket { get; set; }
|
||||||
|
|
||||||
|
public string? Name { get; set; }
|
||||||
|
|
||||||
|
public decimal AverageTemperature { get; set; }
|
||||||
|
|
||||||
|
public decimal AveragePressure { get; set; }
|
||||||
|
|
||||||
|
public decimal AverageHumidity { get; set; }
|
||||||
|
|
||||||
|
public decimal AverageLuminance { get; set; }
|
||||||
|
|
||||||
|
public decimal AverageGasResistance { get; set; }
|
||||||
|
|
||||||
|
public decimal AverageColorTemperature { get; set; }
|
||||||
|
|
||||||
|
public decimal AverageAirQualityIndex { get; set; }
|
||||||
|
}
|
||||||
@@ -25,6 +25,9 @@ public static class Program
|
|||||||
|
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
|
|
||||||
|
var database = app.Services.GetRequiredService<Database>();
|
||||||
|
database.EnsureDatabase();
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -10,15 +10,17 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Remove="Data\Queries\CreateReading.sql" />
|
<None Remove="Data\Queries\CreateReading.psql" />
|
||||||
<None Remove="Data\Queries\GetRecentReadings.sql" />
|
<None Remove="Data\Queries\GetReadingsHistoryGrouped.psql" />
|
||||||
<None Remove="Data\Schema\1-Initial Schema.sql" />
|
<None Remove="Data\Queries\GetRecentReadings.psql" />
|
||||||
|
<None Remove="Data\Schema\1-Initial Schema.psql" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<EmbeddedResource Include="Data\Queries\GetRecentReadings.sql" />
|
<EmbeddedResource Include="Data\Queries\GetReadingsHistoryGrouped.psql" />
|
||||||
<EmbeddedResource Include="Data\Queries\CreateReading.sql" />
|
<EmbeddedResource Include="Data\Queries\GetRecentReadings.psql" />
|
||||||
<EmbeddedResource Include="Data\Schema\1-Initial Schema.sql" />
|
<EmbeddedResource Include="Data\Queries\CreateReading.psql" />
|
||||||
|
<EmbeddedResource Include="Data\Schema\1-Initial Schema.psql" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user