Skip to content

Commit

Permalink
Add next_run attribute to operational sensor for each pool
Browse files Browse the repository at this point in the history
Fixes #26
  • Loading branch information
LordMike committed Nov 5, 2020
1 parent 74fb505 commit e9a6635
Showing 1 changed file with 24 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,8 @@ private async Task Run()
ISensorContainer operationalSensor = CreateSystemEntities();

// Update loop
DateTime? lastRun = null;

while (!_stoppingToken.Token.IsCancellationRequested)
{
// Calculate time to next update
TimeSpan toDelay = _delayCalculator.CalculateNextRun(lastRun);

// Wait on the force sync reset event, for the specified time.
// If either the reset event or the time runs out, we do an update
using (CancellationTokenSource cts = new CancellationTokenSource(toDelay))
using (CancellationTokenSource linkedToken = CancellationTokenSource.CreateLinkedTokenSource(cts.Token, _stoppingToken.Token))
{
try
{
await _forceSyncResetEvent.WaitAsync(linkedToken.Token);

// We were forced
_logger.LogDebug("Forcing a sync for pool {Pool} with BlueRiiot", _pool.SwimmingPoolId);
}
catch (OperationCanceledException)
{
}
}

_logger.LogDebug("Beginning update for pool {Pool}", _pool.SwimmingPoolId);

try
Expand All @@ -103,7 +81,7 @@ private async Task Run()

// Track API operational status
operationalSensor.SetValue(HassTopicKind.State, BlueRiiotMqttService.OkMessage);
operationalSensor.SetAttribute("last_ok", DateTime.UtcNow.ToString("O"));
operationalSensor.SetAttribute("last_ok", DateTime.UtcNow);
}
catch (OperationCanceledException) when (_stoppingToken.Token.IsCancellationRequested)
{
Expand All @@ -115,10 +93,16 @@ private async Task Run()

// Track API operational status
operationalSensor.SetValue(HassTopicKind.State, BlueRiiotMqttService.ProblemMessage);
operationalSensor.SetAttribute("last_bad", DateTime.UtcNow.ToString("O"));
operationalSensor.SetAttribute("last_bad", DateTime.UtcNow);
operationalSensor.SetAttribute("last_bad_status", e.Message);
}

// Calculate time to next update
TimeSpan runDelay = _delayCalculator.CalculateNextRun(DateTime.UtcNow);
DateTime runNext = DateTime.UtcNow + runDelay;

operationalSensor.SetAttribute("next_run", runNext);

try
{
await _hassMqttManager.FlushAll(_stoppingToken.Token);
Expand All @@ -132,7 +116,22 @@ private async Task Run()
_logger.LogError(e, "An error occurred while pushing updated data to MQTT for pool {Pool}", _pool.SwimmingPoolId);
}

lastRun = DateTime.UtcNow;
// Wait on the force sync reset event, for the specified time.
// If either the reset event or the time runs out, we do an update
using (CancellationTokenSource cts = new CancellationTokenSource(runDelay))
using (CancellationTokenSource linkedToken = CancellationTokenSource.CreateLinkedTokenSource(cts.Token, _stoppingToken.Token))
{
try
{
await _forceSyncResetEvent.WaitAsync(linkedToken.Token);

// We were forced
_logger.LogDebug("Forcing a sync for pool {Pool} with BlueRiiot", _pool.SwimmingPoolId);
}
catch (OperationCanceledException)
{
}
}
}
}

Expand Down

0 comments on commit e9a6635

Please sign in to comment.