NucuCar/NucuCar.Sensors/EnvironmentSensor/Worker.cs

37 lines
1,014 B
C#
Raw Normal View History

2019-11-09 13:34:49 +00:00
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
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)
{
using var sensor = new Sensor(_logger);
2019-11-09 13:34:49 +00:00
while (!stoppingToken.IsCancellationRequested)
{
if (sensor.GetState() == SensorState.Initialized)
2019-11-09 14:54:06 +00:00
{
await sensor.TakeMeasurement();
}
else
{
/* Attempt to reinitialize the sensor. */
sensor.InitializeSensor();
}
2019-11-09 13:34:49 +00:00
await Task.Delay(1000, stoppingToken);
}
}
}
}