diff options
| author | Jake Mannens <jake@asger.xyz> | 2024-09-20 16:13:30 +1000 |
|---|---|---|
| committer | Jake Mannens <jake@asger.xyz> | 2024-10-28 14:53:04 +1100 |
| commit | 7c6009abd22d8461039be15d0fd069a460340585 (patch) | |
| tree | 580890eacc1ae2d86ee3f95927e3aed47bb12c96 /PagerFetchService.cs | |
v0.1v0.1
Diffstat (limited to 'PagerFetchService.cs')
| -rw-r--r-- | PagerFetchService.cs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/PagerFetchService.cs b/PagerFetchService.cs new file mode 100644 index 0000000..97a03d5 --- /dev/null +++ b/PagerFetchService.cs @@ -0,0 +1,62 @@ +using PagerParser.Sites; + +namespace PagerParser; + +// Self-explanatory... Hosted background services that periodically fetches +// pager messages, tries to parse them, then adds both the raw pager message +// as well as the parsed message contents to the DB, ignoring already fetched +// messages in the process. +public class PagerFetchService : IHostedService { + private readonly TimeSpan FetchInterval = TimeSpan.FromMinutes(15); + + private IServiceProvider serviceProvider; + private IPagerMessageParserService parser; + + private Timer fetchTimer; + + private PagerMon pagerMon = new(); + + public PagerFetchService(IServiceProvider serviceProvider, IPagerMessageParserService parser) { + this.serviceProvider = serviceProvider; + this.parser = parser; + fetchTimer = new Timer(Fetch); + } + + public Task StartAsync(CancellationToken cancellationToken) { + fetchTimer.Change(TimeSpan.Zero, FetchInterval); + return Task.CompletedTask; + } + + public Task StopAsync(CancellationToken cancellationToken) { + fetchTimer.Change(Timeout.Infinite, Timeout.Infinite); + return Task.CompletedTask; + } + + private void Fetch(object? state) { + using var scope = serviceProvider.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService<PagerContext>(); + + // Fetch pager messages, ignoring message we've already got + var messages = pagerMon + .Fetch() + .Where(m => + !db.PagerMessages + .Select(m => m.Message) + .Contains(m.Message)); + + // Attempt to parse each newly-fetched message. + // Failure to parse isn't critical, as we still keep the raw message. + foreach(var message in messages) { + message.ParsedMessage = parser.TryParse(message.Message); + if(message.ParsedMessage is not null) + message.ParsedMessage.GpsPosition = + PositionCalculator.GetGpsPosition(message.ParsedMessage); + } + + db.AddRange(messages); + db.SaveChanges(); + } + + public void Dispose() => + fetchTimer.Dispose(); +} |
