2019-11-09 13:34:49 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2019-11-26 18:24:27 +00:00
|
|
|
using Microsoft.Extensions.Options;
|
2019-11-24 15:47:17 +00:00
|
|
|
using NucuCar.Domain.Telemetry;
|
2019-11-14 14:45:32 +00:00
|
|
|
using NucuCar.Sensors.Telemetry;
|
2019-11-11 11:28:52 +00:00
|
|
|
using NucuCarSensorsProto;
|
2019-11-09 13:34:49 +00:00
|
|
|
|
2019-11-09 17:23:07 +00:00
|
|
|
namespace NucuCar.Sensors.EnvironmentSensor
|
2019-11-09 13:34:49 +00:00
|
|
|
{
|
2019-11-24 13:12:12 +00:00
|
|
|
/// <summary>
|
|
|
|
/// EnvironmentSensor's background service worker.
|
|
|
|
/// It does periodic reads from the sensors and publishes telemetry data if the option is enabled.
|
|
|
|
/// </summary>
|
2019-11-24 16:03:46 +00:00
|
|
|
public class Bme680Worker : BackgroundService
|
2019-11-09 13:34:49 +00:00
|
|
|
{
|
2019-11-11 15:58:46 +00:00
|
|
|
private readonly bool _telemetryEnabled;
|
2019-11-24 16:40:42 +00:00
|
|
|
private readonly int _measurementInterval;
|
2019-11-24 16:03:46 +00:00
|
|
|
private readonly ILogger<Bme680Worker> _logger;
|
2019-11-24 15:47:17 +00:00
|
|
|
private readonly TelemetryPublisher _telemetryPublisher;
|
2019-11-24 16:03:46 +00:00
|
|
|
private readonly Bme680Sensor _bme680Sensor;
|
2019-11-09 13:34:49 +00:00
|
|
|
|
2019-11-09 14:54:06 +00:00
|
|
|
|
2019-11-26 18:24:27 +00:00
|
|
|
public Bme680Worker(ILogger<Bme680Worker> logger, IOptions<Bme680Config> options,
|
2019-11-24 16:03:46 +00:00
|
|
|
SensorTelemetry sensorTelemetry, Bme680Sensor bme680Sensor)
|
2019-11-09 13:34:49 +00:00
|
|
|
{
|
|
|
|
_logger = logger;
|
2019-11-26 18:24:27 +00:00
|
|
|
_telemetryEnabled = options.Value.TelemetryEnabled;
|
|
|
|
_measurementInterval = options.Value.MeasurementInterval;
|
2019-11-24 15:47:17 +00:00
|
|
|
_telemetryPublisher = sensorTelemetry.Publisher;
|
2019-11-24 16:03:46 +00:00
|
|
|
_bme680Sensor = bme680Sensor;
|
2019-11-09 13:34:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
|
|
{
|
2019-11-14 14:45:32 +00:00
|
|
|
if (_telemetryEnabled)
|
|
|
|
{
|
2019-11-24 16:03:46 +00:00
|
|
|
_telemetryPublisher?.RegisterTelemeter(_bme680Sensor);
|
2019-11-14 14:45:32 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 13:34:49 +00:00
|
|
|
while (!stoppingToken.IsCancellationRequested)
|
|
|
|
{
|
2019-11-24 15:47:17 +00:00
|
|
|
/* If sensor is ok attempt to read. */
|
2019-11-24 16:03:46 +00:00
|
|
|
if (_bme680Sensor.GetState() == SensorStateEnum.Initialized)
|
2019-11-09 14:54:06 +00:00
|
|
|
{
|
2019-11-24 16:06:52 +00:00
|
|
|
_logger.LogInformation("Taking measurement!");
|
2019-11-24 16:03:46 +00:00
|
|
|
await _bme680Sensor.TakeMeasurement();
|
2019-11-09 14:54:06 +00:00
|
|
|
}
|
2019-11-24 15:47:17 +00:00
|
|
|
/* Else attempt to re-initialize. */
|
2019-11-09 14:54:06 +00:00
|
|
|
else
|
|
|
|
{
|
2019-11-09 17:38:49 +00:00
|
|
|
await Task.Delay(10000, stoppingToken);
|
2019-11-24 16:03:46 +00:00
|
|
|
_bme680Sensor.InitializeSensor();
|
2019-11-09 14:54:06 +00:00
|
|
|
}
|
2019-11-09 17:37:28 +00:00
|
|
|
|
2019-11-24 16:40:42 +00:00
|
|
|
await Task.Delay(_measurementInterval, stoppingToken);
|
2019-11-09 13:34:49 +00:00
|
|
|
}
|
2019-11-24 13:12:12 +00:00
|
|
|
|
2019-11-24 16:03:46 +00:00
|
|
|
_telemetryPublisher?.UnRegisterTelemeter(_bme680Sensor);
|
2019-11-09 13:34:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|