mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-01-22 17:23:52 -05:00
Record last time an environmental device reported
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
7
Environment/Service/Data/Queries/GetDevice.psql
Normal file
7
Environment/Service/Data/Queries/GetDevice.psql
Normal file
@@ -0,0 +1,7 @@
|
||||
SELECT
|
||||
name AS Name,
|
||||
last_updated AS LastUpdated
|
||||
FROM
|
||||
device
|
||||
WHERE
|
||||
name = @Name
|
||||
5
Environment/Service/Data/Queries/GetDevices.psql
Normal file
5
Environment/Service/Data/Queries/GetDevices.psql
Normal file
@@ -0,0 +1,5 @@
|
||||
SELECT
|
||||
name AS Name,
|
||||
last_updated AS LastUpdated
|
||||
FROM
|
||||
device
|
||||
11
Environment/Service/Data/Queries/SetDeviceLastUpdated.psql
Normal file
11
Environment/Service/Data/Queries/SetDeviceLastUpdated.psql
Normal 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
|
||||
5
Environment/Service/Data/Schema/2-Device Table.psql
Normal file
5
Environment/Service/Data/Schema/2-Device Table.psql
Normal file
@@ -0,0 +1,5 @@
|
||||
CREATE TABLE device(
|
||||
name text NOT NULL,
|
||||
last_updated timestamptz NULL,
|
||||
CONSTRAINT device_pk PRIMARY KEY (name)
|
||||
);
|
||||
Reference in New Issue
Block a user