diff --git a/ora2mqtt/RunCommand.cs b/ora2mqtt/RunCommand.cs index 71af92a..1c9671e 100644 --- a/ora2mqtt/RunCommand.cs +++ b/ora2mqtt/RunCommand.cs @@ -31,15 +31,16 @@ public async Task Run(CancellationToken cancellationToken) using var mqtt = await ConnectMqttAsync(config.Mqtt, cancellationToken); - var api = GetGwmApiClient(config, cancellationToken); + var api = GetGwmApiClient(config); try { + using var timer = new PeriodicTimer(TimeSpan.FromSeconds(Intervall)); while (!cancellationToken.IsCancellationRequested) { await RefreshTokenAsync(api, config, cancellationToken); await PublishStatusAsync(mqtt, api, cancellationToken); - await Task.Delay(Intervall * 1000, cancellationToken); + await timer.WaitForNextTickAsync(cancellationToken); } } catch (TaskCanceledException) @@ -72,7 +73,7 @@ private async Task ConnectMqttAsync(Ora2MqttMqttOptions options,Can return client; } - private GwmApiClient GetGwmApiClient(Ora2MqttOptions options, CancellationToken cancellationToken) + private GwmApiClient GetGwmApiClient(Ora2MqttOptions options) { var client = ConfigureApiClient(options); client.SetAccessToken(options.Account.AccessToken);