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-09 17:23:07 +00:00
|
|
|
namespace NucuCar.Sensors.EnvironmentSensor
|
2019-11-09 13:34:49 +00:00
|
|
|
{
|
|
|
|
public class Worker : BackgroundService
|
|
|
|
{
|
|
|
|
private readonly ILogger<Worker> _logger;
|
|
|
|
|
2019-11-09 14:54:06 +00:00
|
|
|
|
2019-11-09 13:34:49 +00:00
|
|
|
public Worker(ILogger<Worker> logger)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
|
|
{
|
2019-11-10 12:38:40 +00:00
|
|
|
using var sensor = Sensor.Instance;
|
|
|
|
sensor.SetLogger(_logger);
|
|
|
|
sensor.InitializeSensor();
|
|
|
|
|
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-09 13:34:49 +00:00
|
|
|
await Task.Delay(1000, stoppingToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|