add upload times

This commit is contained in:
2026-08-06 13:17:29 -05:00
parent 3b54437269
commit c5da73d0ec
2 changed files with 35 additions and 24 deletions
+16 -24
View File
@@ -1,4 +1,3 @@
using Microsoft.AspNetCore.Http.HttpResults;
using SafelyYou;
using SafelyYou.Api;
using SafelyYou.Config;
@@ -22,6 +21,7 @@ builder.Services.AddSingleton<HeartBeatHistoryConfig>(_ =>
});
builder.Services.AddSingleton<HeartBeatHistories>();
builder.Services.AddSingleton<UploadTimes>();
var app = builder.Build();
@@ -36,7 +36,6 @@ app.UseHttpsRedirection();
// heartbeat history
// upload stats
app.MapPost("/devices/{deviceId}/heartbeat", (string deviceId, HeartbeatRequest request,
KnownDevices knownDevices, HeartBeatHistories histories) =>
{
@@ -50,36 +49,29 @@ app.MapPost("/devices/{deviceId}/heartbeat", (string deviceId, HeartbeatRequest
return addResult switch
{
DeviceHeartBeatHistory.AddResult.Added => Results.Ok(),
DeviceHeartBeatHistory.AddResult.Added => Results.NoContent(),
DeviceHeartBeatHistory.AddResult.OutOfSequence => Results.InternalServerError(
new ErrorResponse("Attempted to add a timestamp that was out of sequence or invalid.")),
_ => throw new ArgumentOutOfRangeException()
};
});
var summaries = new[]
app.MapPost("/devices/{deviceId}/stats", (string deviceId, UploadStatsRequest request, KnownDevices knownDevices, UploadTimes uploadTimes) =>
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
var device = new DeviceId(deviceId);
if (!knownDevices.Contains(device))
{
return Results.NotFound(new NotFoundResponse($"Device ID {deviceId} not found"));
}
uploadTimes.Add(device, request.UploadTime);
return Results.NoContent();
// not sure where the 500 would go here beyond doing a try/catch
});
app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast");
app.Run();
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}