OpenDataVisualizer/Schaad.OpenData.Watercraft/WatercraftRefreshService.cs
Claudio Schaad a082eafdad Add Thurgau watercraft map
New Schaad.OpenData.Watercraft library that discovers the yearly
"immatrikulierte Wasserfahrzeuge" datasets (2021–2025) on data.tg.ch,
caches them and aggregates boats per harbour location by type,
propulsion, fuel, emission group and use, with length/power bins.

API under /api/watercraft, new page /schifffahrt/thurgau with donut
markers per location, year playback, toggleable categories and a
location/canton panel. Swisstopo base layers moved to map/baseLayers.ts
for both maps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 21:49:32 +01:00

42 lines
1.3 KiB
C#

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Schaad.OpenData.Watercraft;
public sealed class WatercraftRefreshService(
WatercraftRefresher refresher,
IOptions<WatercraftOptions> options,
ILogger<WatercraftRefreshService> logger) : BackgroundService
{
private static readonly TimeSpan CheckInterval = TimeSpan.FromMinutes(30);
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
await refresher.LoadCacheAsync(stoppingToken);
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
logger.LogError(exception, "Could not load the watercraft cache");
}
while (!stoppingToken.IsCancellationRequested)
{
try
{
if (refresher.IsRefreshDue(options.Value.RefreshInterval))
{
await refresher.RefreshAsync(stoppingToken);
}
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
logger.LogError(exception, "Watercraft refresh failed");
}
await Task.Delay(CheckInterval, stoppingToken);
}
}
}