NucuCar/NucuCar.BME680Sensor/Worker.cs

37 lines
1,013 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.BME680Sensor
{
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-09 14:54:06 +00:00
using var sensor = new Bme680Sensor(_logger);
2019-11-09 13:34:49 +00:00
while (!stoppingToken.IsCancellationRequested)
{
2019-11-09 14:54:06 +00:00
if (sensor.GetState() == Bme680SensorState.Initialized)
{
await sensor.TakeMeasurement();
}
else
{
/* Attempt to reinitialize the sensor. */
sensor.InitializeSensor();
}
2019-11-09 13:34:49 +00:00
await Task.Delay(1000, stoppingToken);
}
}
}
}