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 bool ServiceEnabled { get; set; }
|
||||
public bool TelemetryEnabled { get; set; }
|
||||
public int MeasurementInterval { get; set; }
|
||||
public bool Enabled { get; set; }
|
||||
public bool Telemetry { get; set; }
|
||||
}
|
||||
}
|
|
@ -18,6 +18,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
|||
/// </summary>
|
||||
public class Bme680Sensor : IDisposable, ITelemeter, ISensor<Bme680Sensor>
|
||||
{
|
||||
private readonly bool _telemetryEnabled;
|
||||
private readonly ILogger _logger;
|
||||
private I2cConnectionSettings _i2CSettings;
|
||||
private I2cDevice _i2CDevice;
|
||||
|
@ -33,12 +34,14 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
|||
{
|
||||
_sensorStateEnum = SensorStateEnum.Uninitialized;
|
||||
_logger = logger;
|
||||
if (!options.Value.ServiceEnabled)
|
||||
if (!options.Value.Enabled)
|
||||
{
|
||||
_logger?.LogInformation("BME680 Sensor is disabled!");
|
||||
_sensorStateEnum = SensorStateEnum.Disabled;
|
||||
}
|
||||
|
||||
_telemetryEnabled = options.Value.Telemetry;
|
||||
|
||||
Object = this;
|
||||
}
|
||||
|
||||
|
@ -94,9 +97,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
|||
{
|
||||
if (_sensorStateEnum != SensorStateEnum.Initialized)
|
||||
{
|
||||
_logger?.LogWarning(
|
||||
$"{DateTimeOffset.Now}:BME680: Attempting to take measurement while sensor is not initialized!");
|
||||
return;
|
||||
throw new InvalidOperationException("Can't take measurement on uninitialized sensor!");
|
||||
}
|
||||
|
||||
/* Force the sensor to take a measurement. */
|
||||
|
@ -123,7 +124,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
|||
public Dictionary<string, object> GetTelemetryData()
|
||||
{
|
||||
Dictionary<string, object> returnValue = null;
|
||||
if (_lastMeasurement != null)
|
||||
if (_lastMeasurement != null && _telemetryEnabled)
|
||||
{
|
||||
returnValue = new Dictionary<string, object>
|
||||
{
|
||||
|
|
|
@ -2,7 +2,6 @@ using System.Threading;
|
|||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NucuCar.Domain.Telemetry;
|
||||
using NucuCar.Sensors.Telemetry;
|
||||
using NucuCarSensorsProto;
|
||||
|
@ -15,35 +14,24 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
|||
/// </summary>
|
||||
public class Bme680Worker : BackgroundService
|
||||
{
|
||||
private readonly bool _telemetryEnabled;
|
||||
private readonly bool _serviceEnabled;
|
||||
private readonly int _measurementInterval;
|
||||
private readonly ILogger<Bme680Worker> _logger;
|
||||
private readonly TelemetryPublisher _telemetryPublisher;
|
||||
private readonly ISensor<Bme680Sensor> _bme680Sensor;
|
||||
|
||||
|
||||
public Bme680Worker(ILogger<Bme680Worker> logger, IOptions<Bme680Config> options,
|
||||
SensorTelemetry sensorTelemetry, ISensor<Bme680Sensor> sensor)
|
||||
public Bme680Worker(ILogger<Bme680Worker> logger, SensorTelemetry sensorTelemetry, ISensor<Bme680Sensor> sensor)
|
||||
{
|
||||
_logger = logger;
|
||||
_telemetryEnabled = options.Value.TelemetryEnabled;
|
||||
_serviceEnabled = options.Value.ServiceEnabled;
|
||||
_measurementInterval = options.Value.MeasurementInterval;
|
||||
_measurementInterval = 3000;
|
||||
_telemetryPublisher = sensorTelemetry.Publisher;
|
||||
_bme680Sensor = sensor;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
if (!_serviceEnabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (_telemetryEnabled)
|
||||
{
|
||||
_logger.LogInformation("Starting sensor worker");
|
||||
_telemetryPublisher?.RegisterTelemeter(_bme680Sensor.Object);
|
||||
}
|
||||
|
||||
_bme680Sensor.Object.InitializeSensor();
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
|
@ -54,7 +42,8 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
|||
await _bme680Sensor.Object.TakeMeasurement();
|
||||
}
|
||||
/* Else attempt to re-initialize. */
|
||||
else
|
||||
else if (_bme680Sensor.Object.GetState() == SensorStateEnum.Uninitialized ||
|
||||
_bme680Sensor.Object.GetState() == SensorStateEnum.Error)
|
||||
{
|
||||
await Task.Delay(10000, stoppingToken);
|
||||
_bme680Sensor.Object.InitializeSensor();
|
||||
|
|
|
@ -5,9 +5,8 @@
|
|||
"ConnectionString": "YOUR_CONNECTION_STRING"
|
||||
},
|
||||
"EnvironmentSensor": {
|
||||
"ServiceEnabled": true,
|
||||
"TelemetryEnabled": true,
|
||||
"MeasurementInterval": 1000
|
||||
"Enabled": true,
|
||||
"Telemetry": true
|
||||
},
|
||||
"Logging": {
|
||||
"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