From caf67bec4cfdd900c149975ce74ac595275942fc Mon Sep 17 00:00:00 2001 From: Denis-Cosmin Nutiu Date: Sat, 14 Dec 2019 16:31:32 +0200 Subject: [PATCH] NucuCar.Sensors: Move sensor configuration from worker in sensor --- .../EnvironmentSensor/Bme680Config.cs | 5 +- .../EnvironmentSensor/Bme680Sensor.cs | 11 +-- .../EnvironmentSensor/Bme680Worker.cs | 23 ++--- NucuCar.Sensors/appsettings.json | 5 +- .../Bme680WorkerTest.cs | 84 ------------------- 5 files changed, 16 insertions(+), 112 deletions(-) delete mode 100644 NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680WorkerTest.cs diff --git a/NucuCar.Sensors/EnvironmentSensor/Bme680Config.cs b/NucuCar.Sensors/EnvironmentSensor/Bme680Config.cs index 26cf5cc..30745ab 100644 --- a/NucuCar.Sensors/EnvironmentSensor/Bme680Config.cs +++ b/NucuCar.Sensors/EnvironmentSensor/Bme680Config.cs @@ -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; } } } \ No newline at end of file diff --git a/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs b/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs index cbafdb4..6476101 100644 --- a/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs +++ b/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs @@ -18,6 +18,7 @@ namespace NucuCar.Sensors.EnvironmentSensor /// public class Bme680Sensor : IDisposable, ITelemeter, ISensor { + 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 GetTelemetryData() { Dictionary returnValue = null; - if (_lastMeasurement != null) + if (_lastMeasurement != null && _telemetryEnabled) { returnValue = new Dictionary { diff --git a/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs b/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs index bf9d530..65675ab 100644 --- a/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs +++ b/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs @@ -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 /// public class Bme680Worker : BackgroundService { - private readonly bool _telemetryEnabled; - private readonly bool _serviceEnabled; private readonly int _measurementInterval; private readonly ILogger _logger; private readonly TelemetryPublisher _telemetryPublisher; private readonly ISensor _bme680Sensor; - public Bme680Worker(ILogger logger, IOptions options, - SensorTelemetry sensorTelemetry, ISensor sensor) + public Bme680Worker(ILogger logger, SensorTelemetry sensorTelemetry, ISensor 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) - { - _telemetryPublisher?.RegisterTelemeter(_bme680Sensor.Object); - } + _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(); diff --git a/NucuCar.Sensors/appsettings.json b/NucuCar.Sensors/appsettings.json index e7d450e..8aa155f 100644 --- a/NucuCar.Sensors/appsettings.json +++ b/NucuCar.Sensors/appsettings.json @@ -5,9 +5,8 @@ "ConnectionString": "YOUR_CONNECTION_STRING" }, "EnvironmentSensor": { - "ServiceEnabled": true, - "TelemetryEnabled": true, - "MeasurementInterval": 1000 + "Enabled": true, + "Telemetry": true }, "Logging": { "LogLevel": { diff --git a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680WorkerTest.cs b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680WorkerTest.cs deleted file mode 100644 index e1ea20b..0000000 --- a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680WorkerTest.cs +++ /dev/null @@ -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> _mockLogger; - private readonly Mock> _mockOptions; - private readonly Mock _mockSensorTelemetry; - private readonly Mock _mockTestBme680Sensor; - private readonly Mock> _mockBme680ISensor; - private readonly CancellationTokenSource _cts; - - public Bme680WorkerTest() - { - _cts = new CancellationTokenSource(); - _mockLogger = new Mock>(); - _mockOptions = new Mock>(); - _mockSensorTelemetry = new Mock(); - _mockTestBme680Sensor = new Mock(); - _mockBme680ISensor = new Mock>(); - - _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); - } - } -} \ No newline at end of file