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 _logger; public Worker(ILogger logger) { _logger = logger; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { using var sensor = new Bme680Sensor(_logger); while (!stoppingToken.IsCancellationRequested) { if (sensor.GetState() == Bme680SensorState.Initialized) { await sensor.TakeMeasurement(); } else { /* Attempt to reinitialize the sensor. */ sensor.InitializeSensor(); } await Task.Delay(1000, stoppingToken); } } } }