2019-11-09 13:34:49 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2019-11-11 10:59:07 +00:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2019-11-09 13:34:49 +00:00
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
using Microsoft.Extensions.Logging;
|
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-11 10:59:07 +00:00
|
|
|
public class BackgroundWorker : BackgroundService
|
2019-11-09 13:34:49 +00:00
|
|
|
{
|
2019-11-11 15:58:46 +00:00
|
|
|
private readonly bool _serviceEnabled;
|
|
|
|
private readonly bool _telemetryEnabled;
|
|
|
|
private readonly int _measurementDelay;
|
2019-11-11 10:59:07 +00:00
|
|
|
private readonly ILogger<BackgroundWorker> _logger;
|
2019-11-09 13:34:49 +00:00
|
|
|
|
2019-11-09 14:54:06 +00:00
|
|
|
|
2019-11-11 10:59:07 +00:00
|
|
|
public BackgroundWorker(ILogger<BackgroundWorker> logger, IConfiguration config)
|
2019-11-09 13:34:49 +00:00
|
|
|
{
|
|
|
|
_logger = logger;
|
2019-11-23 18:53:04 +00:00
|
|
|
_serviceEnabled = config.GetValue<bool>("EnvironmentSensor:Enabled");
|
|
|
|
_telemetryEnabled = config.GetValue<bool>("EnvironmentSensor:Telemetry");
|
|
|
|
_measurementDelay = config.GetValue<int>("EnvironmentSensor:MeasurementInterval");
|
2019-11-09 13:34:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
|
|
{
|
2019-11-11 15:58:46 +00:00
|
|
|
if (!_serviceEnabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-23 21:06:01 +00:00
|
|
|
using var sensor = Bme680Sensor.Instance;
|
|
|
|
sensor.Logger = _logger;
|
2019-11-10 12:38:40 +00:00
|
|
|
sensor.InitializeSensor();
|
2019-11-14 14:45:32 +00:00
|
|
|
if (_telemetryEnabled)
|
|
|
|
{
|
2019-11-23 18:53:04 +00:00
|
|
|
SensorTelemetryPublisher.Instance.RegisterTelemeter(sensor);
|
2019-11-14 14:45:32 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 13:34:49 +00:00
|
|
|
while (!stoppingToken.IsCancellationRequested)
|
|
|
|
{
|
2019-11-10 14:02:50 +00:00
|
|
|
if (sensor.GetState() == SensorStateEnum.Initialized)
|
2019-11-09 14:54:06 +00:00
|
|
|
{
|
|
|
|
await sensor.TakeMeasurement();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-09 17:38:49 +00:00
|
|
|
await Task.Delay(10000, stoppingToken);
|
2019-11-09 14:54:06 +00:00
|
|
|
/* Attempt to reinitialize the sensor. */
|
|
|
|
sensor.InitializeSensor();
|
|
|
|
}
|
2019-11-09 17:37:28 +00:00
|
|
|
|
2019-11-11 15:58:46 +00:00
|
|
|
await Task.Delay(_measurementDelay, stoppingToken);
|
2019-11-09 13:34:49 +00:00
|
|
|
}
|
2019-11-24 13:12:12 +00:00
|
|
|
|
2019-11-23 18:53:04 +00:00
|
|
|
SensorTelemetryPublisher.Instance.UnRegisterTelemeter(sensor);
|
2019-11-09 13:34:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|