diff options
| author | Jake Mannens <jake@asger.xyz> | 2024-10-24 13:40:40 +1100 |
|---|---|---|
| committer | Jake Mannens <jake@asger.xyz> | 2024-11-01 16:23:35 +1100 |
| commit | 1ce51d389f190432ea62d28aff6e1423063d21b3 (patch) | |
| tree | dad011a8f4bf800ee80de276b58954efe044be36 /BartService.cs | |
| parent | 4e6257c9a68922199c4709076f156f1a3fef51eb (diff) | |
Added fetch timer
Diffstat (limited to 'BartService.cs')
| -rw-r--r-- | BartService.cs | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/BartService.cs b/BartService.cs index 3238e09..77a8e5a 100644 --- a/BartService.cs +++ b/BartService.cs @@ -5,6 +5,7 @@ using System.ComponentModel.DataAnnotations.Schema; using System.Security.Cryptography; using System.Text.Json; using System.Text.Json.Serialization; +using System.Timers; namespace PagerParser { public partial class PagerContext { @@ -79,6 +80,9 @@ namespace PagerParser.Bart { private const string AvailabilityEndpointUrl = @"https://bartapp.net/webapp/webservice/WebsiteService.svc/GetUserAvailabilityList"; + private readonly TimeSpan FetchInterval = + TimeSpan.FromMinutes(5); + private IConfiguration config; private ILogger logger; private IServiceProvider serviceProvider; @@ -87,6 +91,8 @@ namespace PagerParser.Bart { private HttpClient httpClient; + private System.Timers.Timer fetchTimer; + public BartService( IConfiguration config, ILogger<BartService> logger, @@ -95,6 +101,11 @@ namespace PagerParser.Bart { this.config = config; this.logger = logger; this.serviceProvider = serviceProvider; + + fetchTimer = new() { + Interval = FetchInterval.TotalMilliseconds + }; + fetchTimer.Elapsed += OnFetchTimer; } public async Task FetchDayAsync(DateOnly day) { @@ -216,15 +227,20 @@ namespace PagerParser.Bart { } } + private async void OnFetchTimer(object? sender, ElapsedEventArgs e) { + await FetchDayAsync(DateOnly.FromDateTime(DateTime.Now)); + } + public async Task StartAsync(CancellationToken cancellationToken) { logger.LogInformation("BART service starting..."); loginToken = config.GetValue<string>("PagerParser:Bart:Token"); httpClient = new(); - await FetchDayAsync(DateOnly.FromDateTime(DateTime.Now)); + fetchTimer.Start(); } public Task StopAsync(CancellationToken cancellationToken) { logger.LogInformation("BART service stopping..."); + fetchTimer.Stop(); loginToken = null; httpClient.Dispose(); return Task.CompletedTask; |
