2026-08-06 12:58:54 -05:00
|
|
|
using SafelyYou;
|
|
|
|
|
using SafelyYou.Api;
|
|
|
|
|
using SafelyYou.Config;
|
|
|
|
|
|
2026-08-06 11:33:04 -05:00
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
|
|
|
|
// Add services to the container.
|
|
|
|
|
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
|
|
|
|
builder.Services.AddOpenApi();
|
|
|
|
|
|
2026-08-06 12:58:54 -05:00
|
|
|
builder.Services.AddSingleton<KnownDevices>(_ =>
|
|
|
|
|
{
|
|
|
|
|
var deviceIds = File.ReadAllLines("/devices.csv").Skip(1).Select(id => new DeviceId(id)).ToHashSet();
|
|
|
|
|
return new KnownDevices(deviceIds);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
builder.Services.AddSingleton<HeartBeatHistoryConfig>(_ =>
|
|
|
|
|
{
|
|
|
|
|
var millis = builder.Configuration.GetSection("HeartBeatHistory").GetValue<int>("WindowMillis");
|
|
|
|
|
return new HeartBeatHistoryConfig() { Window = TimeSpan.FromMilliseconds(millis) };
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
builder.Services.AddSingleton<HeartBeatHistories>();
|
2026-08-06 13:17:29 -05:00
|
|
|
builder.Services.AddSingleton<UploadTimes>();
|
2026-08-06 12:58:54 -05:00
|
|
|
|
2026-08-06 11:33:04 -05:00
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
|
|
// Configure the HTTP request pipeline.
|
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.MapOpenApi();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
|
|
2026-08-06 12:58:54 -05:00
|
|
|
// heartbeat history
|
|
|
|
|
// upload stats
|
|
|
|
|
|
|
|
|
|
app.MapPost("/devices/{deviceId}/heartbeat", (string deviceId, HeartbeatRequest request,
|
|
|
|
|
KnownDevices knownDevices, HeartBeatHistories histories) =>
|
|
|
|
|
{
|
|
|
|
|
var device = new DeviceId(deviceId);
|
|
|
|
|
if (!knownDevices.Contains(device))
|
|
|
|
|
{
|
|
|
|
|
return Results.NotFound(new NotFoundResponse($"Device ID {deviceId} not found"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var addResult = histories.Add(device, request.SentAt);
|
|
|
|
|
|
|
|
|
|
return addResult switch
|
|
|
|
|
{
|
2026-08-06 13:17:29 -05:00
|
|
|
DeviceHeartBeatHistory.AddResult.Added => Results.NoContent(),
|
2026-08-06 12:58:54 -05:00
|
|
|
DeviceHeartBeatHistory.AddResult.OutOfSequence => Results.InternalServerError(
|
|
|
|
|
new ErrorResponse("Attempted to add a timestamp that was out of sequence or invalid.")),
|
|
|
|
|
_ => throw new ArgumentOutOfRangeException()
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2026-08-06 13:17:29 -05:00
|
|
|
app.MapPost("/devices/{deviceId}/stats", (string deviceId, UploadStatsRequest request, KnownDevices knownDevices, UploadTimes uploadTimes) =>
|
2026-08-06 11:33:04 -05:00
|
|
|
{
|
2026-08-06 13:17:29 -05:00
|
|
|
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
|
|
|
|
|
});
|
|
|
|
|
|
2026-08-06 11:33:04 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
app.Run();
|
|
|
|
|
|