NucuCar.Sensors: Move sensor configuration from worker in sensor
This commit is contained in:
parent
d6204b06fe
commit
caf67bec4c
5 changed files with 16 additions and 112 deletions
|
@ -4,8 +4,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
{
|
{
|
||||||
public class Bme680Config
|
public class Bme680Config
|
||||||
{
|
{
|
||||||
public bool ServiceEnabled { get; set; }
|
public bool Enabled { get; set; }
|
||||||
public bool TelemetryEnabled { get; set; }
|
public bool Telemetry { get; set; }
|
||||||
public int MeasurementInterval { get; set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -18,6 +18,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Bme680Sensor : IDisposable, ITelemeter, ISensor<Bme680Sensor>
|
public class Bme680Sensor : IDisposable, ITelemeter, ISensor<Bme680Sensor>
|
||||||
{
|
{
|
||||||
|
private readonly bool _telemetryEnabled;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private I2cConnectionSettings _i2CSettings;
|
private I2cConnectionSettings _i2CSettings;
|
||||||
private I2cDevice _i2CDevice;
|
private I2cDevice _i2CDevice;
|
||||||
|
@ -33,12 +34,14 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
{
|
{
|
||||||
_sensorStateEnum = SensorStateEnum.Uninitialized;
|
_sensorStateEnum = SensorStateEnum.Uninitialized;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
if (!options.Value.ServiceEnabled)
|
if (!options.Value.Enabled)
|
||||||
{
|
{
|
||||||
_logger?.LogInformation("BME680 Sensor is disabled!");
|
_logger?.LogInformation("BME680 Sensor is disabled!");
|
||||||
_sensorStateEnum = SensorStateEnum.Disabled;
|
_sensorStateEnum = SensorStateEnum.Disabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_telemetryEnabled = options.Value.Telemetry;
|
||||||
|
|
||||||
Object = this;
|
Object = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,9 +97,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
{
|
{
|
||||||
if (_sensorStateEnum != SensorStateEnum.Initialized)
|
if (_sensorStateEnum != SensorStateEnum.Initialized)
|
||||||
{
|
{
|
||||||
_logger?.LogWarning(
|
throw new InvalidOperationException("Can't take measurement on uninitialized sensor!");
|
||||||
$"{DateTimeOffset.Now}:BME680: Attempting to take measurement while sensor is not initialized!");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Force the sensor to take a measurement. */
|
/* Force the sensor to take a measurement. */
|
||||||
|
@ -123,7 +124,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
public Dictionary<string, object> GetTelemetryData()
|
public Dictionary<string, object> GetTelemetryData()
|
||||||
{
|
{
|
||||||
Dictionary<string, object> returnValue = null;
|
Dictionary<string, object> returnValue = null;
|
||||||
if (_lastMeasurement != null)
|
if (_lastMeasurement != null && _telemetryEnabled)
|
||||||
{
|
{
|
||||||
returnValue = new Dictionary<string, object>
|
returnValue = new Dictionary<string, object>
|
||||||
{
|
{
|
||||||
|
|
|
@ -2,7 +2,6 @@ using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
using NucuCar.Domain.Telemetry;
|
using NucuCar.Domain.Telemetry;
|
||||||
using NucuCar.Sensors.Telemetry;
|
using NucuCar.Sensors.Telemetry;
|
||||||
using NucuCarSensorsProto;
|
using NucuCarSensorsProto;
|
||||||
|
@ -15,35 +14,24 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Bme680Worker : BackgroundService
|
public class Bme680Worker : BackgroundService
|
||||||
{
|
{
|
||||||
private readonly bool _telemetryEnabled;
|
|
||||||
private readonly bool _serviceEnabled;
|
|
||||||
private readonly int _measurementInterval;
|
private readonly int _measurementInterval;
|
||||||
private readonly ILogger<Bme680Worker> _logger;
|
private readonly ILogger<Bme680Worker> _logger;
|
||||||
private readonly TelemetryPublisher _telemetryPublisher;
|
private readonly TelemetryPublisher _telemetryPublisher;
|
||||||
private readonly ISensor<Bme680Sensor> _bme680Sensor;
|
private readonly ISensor<Bme680Sensor> _bme680Sensor;
|
||||||
|
|
||||||
|
|
||||||
public Bme680Worker(ILogger<Bme680Worker> logger, IOptions<Bme680Config> options,
|
public Bme680Worker(ILogger<Bme680Worker> logger, SensorTelemetry sensorTelemetry, ISensor<Bme680Sensor> sensor)
|
||||||
SensorTelemetry sensorTelemetry, ISensor<Bme680Sensor> sensor)
|
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_telemetryEnabled = options.Value.TelemetryEnabled;
|
_measurementInterval = 3000;
|
||||||
_serviceEnabled = options.Value.ServiceEnabled;
|
|
||||||
_measurementInterval = options.Value.MeasurementInterval;
|
|
||||||
_telemetryPublisher = sensorTelemetry.Publisher;
|
_telemetryPublisher = sensorTelemetry.Publisher;
|
||||||
_bme680Sensor = sensor;
|
_bme680Sensor = sensor;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
{
|
{
|
||||||
if (!_serviceEnabled)
|
_logger.LogInformation("Starting sensor worker");
|
||||||
{
|
_telemetryPublisher?.RegisterTelemeter(_bme680Sensor.Object);
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (_telemetryEnabled)
|
|
||||||
{
|
|
||||||
_telemetryPublisher?.RegisterTelemeter(_bme680Sensor.Object);
|
|
||||||
}
|
|
||||||
|
|
||||||
_bme680Sensor.Object.InitializeSensor();
|
_bme680Sensor.Object.InitializeSensor();
|
||||||
while (!stoppingToken.IsCancellationRequested)
|
while (!stoppingToken.IsCancellationRequested)
|
||||||
|
@ -54,7 +42,8 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
await _bme680Sensor.Object.TakeMeasurement();
|
await _bme680Sensor.Object.TakeMeasurement();
|
||||||
}
|
}
|
||||||
/* Else attempt to re-initialize. */
|
/* Else attempt to re-initialize. */
|
||||||
else
|
else if (_bme680Sensor.Object.GetState() == SensorStateEnum.Uninitialized ||
|
||||||
|
_bme680Sensor.Object.GetState() == SensorStateEnum.Error)
|
||||||
{
|
{
|
||||||
await Task.Delay(10000, stoppingToken);
|
await Task.Delay(10000, stoppingToken);
|
||||||
_bme680Sensor.Object.InitializeSensor();
|
_bme680Sensor.Object.InitializeSensor();
|
||||||
|
|
|
@ -5,9 +5,8 @@
|
||||||
"ConnectionString": "YOUR_CONNECTION_STRING"
|
"ConnectionString": "YOUR_CONNECTION_STRING"
|
||||||
},
|
},
|
||||||
"EnvironmentSensor": {
|
"EnvironmentSensor": {
|
||||||
"ServiceEnabled": true,
|
"Enabled": true,
|
||||||
"TelemetryEnabled": true,
|
"Telemetry": true
|
||||||
"MeasurementInterval": 1000
|
|
||||||
},
|
},
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
|
|
|
@ -1,84 +0,0 @@
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
using Moq;
|
|
||||||
using NucuCar.Sensors;
|
|
||||||
using NucuCar.Sensors.EnvironmentSensor;
|
|
||||||
using NucuCar.Sensors.Telemetry;
|
|
||||||
using NucuCarSensorsProto;
|
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests
|
|
||||||
{
|
|
||||||
public class Bme680WorkerTest
|
|
||||||
{
|
|
||||||
private readonly Mock<ILogger<Bme680Worker>> _mockLogger;
|
|
||||||
private readonly Mock<IOptions<Bme680Config>> _mockOptions;
|
|
||||||
private readonly Mock<SensorTelemetry> _mockSensorTelemetry;
|
|
||||||
private readonly Mock<TestBme680Sensor> _mockTestBme680Sensor;
|
|
||||||
private readonly Mock<ISensor<Bme680Sensor>> _mockBme680ISensor;
|
|
||||||
private readonly CancellationTokenSource _cts;
|
|
||||||
|
|
||||||
public Bme680WorkerTest()
|
|
||||||
{
|
|
||||||
_cts = new CancellationTokenSource();
|
|
||||||
_mockLogger = new Mock<ILogger<Bme680Worker>>();
|
|
||||||
_mockOptions = new Mock<IOptions<Bme680Config>>();
|
|
||||||
_mockSensorTelemetry = new Mock<SensorTelemetry>();
|
|
||||||
_mockTestBme680Sensor = new Mock<TestBme680Sensor>();
|
|
||||||
_mockBme680ISensor = new Mock<ISensor<Bme680Sensor>>();
|
|
||||||
|
|
||||||
_mockBme680ISensor.Setup(o => o.Object).Returns(_mockTestBme680Sensor.Object);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task Test_Bme680Worker_ServiceDisabled()
|
|
||||||
{
|
|
||||||
_mockOptions.Setup(o => o.Value).Returns(new Bme680Config()
|
|
||||||
{
|
|
||||||
ServiceEnabled = false
|
|
||||||
});
|
|
||||||
var service = new Bme680Worker(_mockLogger.Object, _mockOptions.Object, _mockSensorTelemetry.Object,
|
|
||||||
_mockBme680ISensor.Object);
|
|
||||||
|
|
||||||
await service.StartAsync(_cts.Token);
|
|
||||||
_mockTestBme680Sensor.Verify(s => s.InitializeSensor(), Times.Never);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task Test_Bme680Worker_SensorIsInitialized()
|
|
||||||
{
|
|
||||||
_mockOptions.Setup(o => o.Value).Returns(new Bme680Config()
|
|
||||||
{
|
|
||||||
ServiceEnabled = true,
|
|
||||||
MeasurementInterval = 1000
|
|
||||||
});
|
|
||||||
var service = new Bme680Worker(_mockLogger.Object, _mockOptions.Object, _mockSensorTelemetry.Object,
|
|
||||||
_mockBme680ISensor.Object);
|
|
||||||
|
|
||||||
await service.StartAsync(_cts.Token);
|
|
||||||
_mockTestBme680Sensor.Verify(s => s.InitializeSensor(), Times.AtLeastOnce);
|
|
||||||
await service.StopAsync(_cts.Token);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task Test_Bme680Worker_SensorIsBeingMeasured()
|
|
||||||
{
|
|
||||||
_mockOptions.Setup(o => o.Value).Returns(new Bme680Config()
|
|
||||||
{
|
|
||||||
ServiceEnabled = true,
|
|
||||||
MeasurementInterval = 100
|
|
||||||
});
|
|
||||||
_mockTestBme680Sensor.Setup(s => s.GetState()).Returns(SensorStateEnum.Initialized);
|
|
||||||
|
|
||||||
var service = new Bme680Worker(_mockLogger.Object, _mockOptions.Object, _mockSensorTelemetry.Object,
|
|
||||||
_mockBme680ISensor.Object);
|
|
||||||
|
|
||||||
await service.StartAsync(_cts.Token);
|
|
||||||
_mockTestBme680Sensor.Verify(s => s.InitializeSensor(), Times.AtLeastOnce);
|
|
||||||
_mockTestBme680Sensor.Verify(s => s.TakeMeasurement(), Times.AtLeastOnce);
|
|
||||||
await service.StopAsync(_cts.Token);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue