Record last time an environmental device reported

This commit is contained in:
2024-05-28 13:53:02 -04:00
parent d69d69bf68
commit b1a9230f91
12 changed files with 181 additions and 5 deletions

View File

@@ -1,4 +1,5 @@
using ChrisKaczor.HomeMonitor.Environment.Service.Models;
using ChrisKaczor.HomeMonitor.Environment.Service.Models.Device;
using ChrisKaczor.HomeMonitor.Environment.Service.Models.Indoor;
using Dapper;
using DbUp;
@@ -80,4 +81,31 @@ public class Database(IConfiguration configuration)
return await connection.QueryAsync<ReadingsAggregate>(query, new { Start = start, End = end }).ConfigureAwait(false);
}
public async Task<IEnumerable<Device>> GetDevicesAsync()
{
await using var connection = CreateConnection();
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Environment.Service.Data.Queries.GetDevices.psql");
return await connection.QueryAsync<Device>(query).ConfigureAwait(false);
}
public async Task<Device?> GetDeviceAsync(string deviceName)
{
await using var connection = CreateConnection();
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Environment.Service.Data.Queries.GetDevice.psql");
return await connection.QueryFirstOrDefaultAsync<Device>(query, new { Name = deviceName }).ConfigureAwait(false);
}
public async Task SetDeviceLastUpdatedAsync(string deviceName, DateTimeOffset? lastUpdated)
{
await using var connection = CreateConnection();
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Environment.Service.Data.Queries.SetDeviceLastUpdated.psql");
await connection.ExecuteAsync(query, new { Name = deviceName, LastUpdated = lastUpdated }).ConfigureAwait(false);
}
}

View File

@@ -0,0 +1,7 @@
SELECT
name AS Name,
last_updated AS LastUpdated
FROM
device
WHERE
name = @Name

View File

@@ -0,0 +1,5 @@
SELECT
name AS Name,
last_updated AS LastUpdated
FROM
device

View File

@@ -0,0 +1,11 @@
INSERT INTO device(
name,
last_updated
)
VALUES (
@Name,
@LastUpdated
)
ON CONFLICT (name)
DO UPDATE
SET last_updated = EXCLUDED.last_updated

View File

@@ -0,0 +1,5 @@
CREATE TABLE device(
name text NOT NULL,
last_updated timestamptz NULL,
CONSTRAINT device_pk PRIMARY KEY (name)
);