From 5be270210ab98ec9070741b4a79109747dac29f4 Mon Sep 17 00:00:00 2001 From: Denis-Cosmin Nutiu Date: Sun, 1 Dec 2019 18:04:46 +0200 Subject: [PATCH] Remove EnvironmentSensorMeasurement type from Bme680Sensor --- .../Telemetry/TelemetryPublisherAzure.cs | 2 +- .../EnvironmentSensor/Bme680GrpcService.cs | 9 ++++- .../EnvironmentSensor/Bme680Sensor.cs | 35 ++++++++++--------- .../EnvironmentSensor/Bme680Worker.cs | 1 - NucuCar.Sensors/Telemetry/TelemetryWorker.cs | 16 +++++++-- .../Bme680GrpcServiceTest.cs | 10 ++++-- .../TestBme680Sensor.cs | 6 ++-- 7 files changed, 52 insertions(+), 27 deletions(-) diff --git a/NucuCar.Domain/Telemetry/TelemetryPublisherAzure.cs b/NucuCar.Domain/Telemetry/TelemetryPublisherAzure.cs index 554f772..16ffbab 100644 --- a/NucuCar.Domain/Telemetry/TelemetryPublisherAzure.cs +++ b/NucuCar.Domain/Telemetry/TelemetryPublisherAzure.cs @@ -25,7 +25,7 @@ namespace NucuCar.Domain.Telemetry throw; } - Logger?.LogInformation("Started the AzureTelemetryPublisher!"); + Logger?.LogDebug("Started the AzureTelemetryPublisher!"); } /// diff --git a/NucuCar.Sensors/EnvironmentSensor/Bme680GrpcService.cs b/NucuCar.Sensors/EnvironmentSensor/Bme680GrpcService.cs index 49a7529..46b5e8c 100644 --- a/NucuCar.Sensors/EnvironmentSensor/Bme680GrpcService.cs +++ b/NucuCar.Sensors/EnvironmentSensor/Bme680GrpcService.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System.Threading.Tasks; using Google.Protobuf.WellKnownTypes; using Grpc.Core; @@ -35,7 +36,13 @@ namespace NucuCar.Sensors.EnvironmentSensor { _logger?.LogDebug($"Calling {nameof(GetSensorMeasurement)}."); var sensorMeasurement = _bme680Sensor.Object.GetMeasurement(); - return Task.FromResult(sensorMeasurement); + return Task.FromResult(new EnvironmentSensorMeasurement() + { + Temperature = sensorMeasurement.GetValueOrDefault("temperature", -1.0), + Humidity = sensorMeasurement.GetValueOrDefault("pressure", -1.0), + Pressure = sensorMeasurement.GetValueOrDefault("humidity",-1.0), + VolatileOrganicCompound = sensorMeasurement.GetValueOrDefault("voc", -1.0), + }); } } } \ No newline at end of file diff --git a/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs b/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs index e2ac2dd..dc2ec3d 100644 --- a/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs +++ b/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs @@ -21,14 +21,13 @@ namespace NucuCar.Sensors.EnvironmentSensor private I2cConnectionSettings _i2CSettings; private I2cDevice _i2CDevice; private Bme680 _bme680; - private EnvironmentSensorMeasurement _lastMeasurement; + private Dictionary _lastMeasurement; private SensorStateEnum _sensorStateEnum; public Bme680Sensor() { - } - + public Bme680Sensor(ILogger logger, IOptions options) { _sensorStateEnum = SensorStateEnum.Uninitialized; @@ -42,8 +41,7 @@ namespace NucuCar.Sensors.EnvironmentSensor Object = this; } - // TODO Make more generic - public virtual EnvironmentSensorMeasurement GetMeasurement() + public virtual Dictionary GetMeasurement() { return _lastMeasurement; } @@ -65,6 +63,8 @@ namespace NucuCar.Sensors.EnvironmentSensor return; } + _lastMeasurement = new Dictionary(); + try { /* Connect to default i2c address 0x76 */ @@ -73,7 +73,6 @@ namespace NucuCar.Sensors.EnvironmentSensor _bme680 = new Bme680(_i2CDevice); /* Initialize measurement */ - _lastMeasurement = new EnvironmentSensorMeasurement(); _bme680.Reset(); _bme680.SetHumiditySampling(Sampling.UltraLowPower); _bme680.SetTemperatureSampling(Sampling.UltraHighResolution); @@ -89,7 +88,7 @@ namespace NucuCar.Sensors.EnvironmentSensor _sensorStateEnum = SensorStateEnum.Error; } } - + public virtual async Task TakeMeasurement() { if (_sensorStateEnum != SensorStateEnum.Initialized) @@ -102,13 +101,17 @@ namespace NucuCar.Sensors.EnvironmentSensor /* Force the sensor to take a measurement. */ _bme680.SetPowerMode(Bme680PowerMode.Forced); - _lastMeasurement.Temperature = (await _bme680.ReadTemperatureAsync()).Celsius; - _lastMeasurement.Pressure = await _bme680.ReadPressureAsync(); - _lastMeasurement.Humidity = await _bme680.ReadHumidityAsync(); + _lastMeasurement["temperature"] = (await _bme680.ReadTemperatureAsync()).Celsius; + _lastMeasurement["pressure"] = await _bme680.ReadPressureAsync(); + _lastMeasurement["humidity"] = await _bme680.ReadHumidityAsync(); + _lastMeasurement["voc"] = 0.0; // Not implemented. - _logger?.LogInformation($"{DateTimeOffset.Now}:BME680: reading"); + _logger?.LogDebug($"{DateTimeOffset.Now}:BME680: reading"); _logger?.LogInformation( - $"{_lastMeasurement.Temperature:N2} \u00B0C | {_lastMeasurement.Pressure:N2} hPa | {_lastMeasurement.Humidity:N2} %rH"); + $"temperature:{_lastMeasurement.GetValueOrDefault("temperature"):N2} \u00B0C|" + + $"pressure:{_lastMeasurement.GetValueOrDefault("pressure"):N2} hPa|" + + $"humidity:{_lastMeasurement.GetValueOrDefault("humidity"):N2} %rH|" + + $"voc:{_lastMeasurement.GetValueOrDefault("voc")}"); } public string GetIdentifier() @@ -123,10 +126,10 @@ namespace NucuCar.Sensors.EnvironmentSensor { returnValue = new Dictionary { - ["temperature"] = _lastMeasurement.Temperature, - ["humidity"] = _lastMeasurement.Humidity, - ["pressure"] = _lastMeasurement.Pressure, - ["voc"] = _lastMeasurement.VolatileOrganicCompound + ["temperature"] = _lastMeasurement.GetValueOrDefault("temperature"), + ["humidity"] = _lastMeasurement.GetValueOrDefault("humidity"), + ["pressure"] = _lastMeasurement.GetValueOrDefault("pressure"), + ["voc"] = _lastMeasurement.GetValueOrDefault("voc") }; } diff --git a/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs b/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs index 524c088..bf9d530 100644 --- a/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs +++ b/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs @@ -51,7 +51,6 @@ namespace NucuCar.Sensors.EnvironmentSensor /* If sensor is ok attempt to read. */ if (_bme680Sensor.Object.GetState() == SensorStateEnum.Initialized) { - _logger.LogInformation("Taking measurement!"); await _bme680Sensor.Object.TakeMeasurement(); } /* Else attempt to re-initialize. */ diff --git a/NucuCar.Sensors/Telemetry/TelemetryWorker.cs b/NucuCar.Sensors/Telemetry/TelemetryWorker.cs index 790abe1..595cac8 100644 --- a/NucuCar.Sensors/Telemetry/TelemetryWorker.cs +++ b/NucuCar.Sensors/Telemetry/TelemetryWorker.cs @@ -13,6 +13,7 @@ namespace NucuCar.Sensors.Telemetry public class TelemetryWorker : BackgroundService { private readonly int _interval; + private readonly bool _serviceEnabled; private readonly ILogger _logger; private readonly TelemetryPublisher _telemetryPublisher; @@ -21,20 +22,31 @@ namespace NucuCar.Sensors.Telemetry { _logger = logger; _interval = options.Value.PublishInterval; + _serviceEnabled = options.Value.ServiceEnabled; _telemetryPublisher = sensorTelemetry.Publisher; if (_telemetryPublisher == null) { - logger.LogCritical("Invalid state! TelemetryPublisher is null!"); + logger?.LogCritical("Invalid state! TelemetryPublisher is null!"); } } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { + if (!_serviceEnabled) + { + return; + } + if (_telemetryPublisher == null) + { + _logger?.LogCritical("Invalid state! TelemetryPublisher is null!"); + return; + } + await Task.Delay(_interval, stoppingToken); while (!stoppingToken.IsCancellationRequested) { - _logger.LogInformation("Is publishing telemetry data!"); await _telemetryPublisher.PublishAsync(stoppingToken); + _logger?.LogDebug("Telemetry data published!"); await Task.Delay(_interval, stoppingToken); } } diff --git a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680GrpcServiceTest.cs b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680GrpcServiceTest.cs index c3b7f2f..00ebee5 100644 --- a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680GrpcServiceTest.cs +++ b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680GrpcServiceTest.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using Microsoft.Extensions.Logging; using Moq; using NucuCar.Sensors; @@ -8,18 +9,20 @@ using Xunit.Abstractions; namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests { - public partial class Bme680GrpcServiceTest + public class Bme680GrpcServiceTest { private readonly ITestOutputHelper _testOutputHelper; private readonly Mock> _mockLogger; private readonly Mock> _mockSensor; + private readonly Mock _mockTestSensor; public Bme680GrpcServiceTest(ITestOutputHelper testOutputHelper) { _testOutputHelper = testOutputHelper; _mockLogger = new Mock>(); _mockSensor = new Mock>(); - _mockSensor.Setup(ms => ms.Object).Returns(new Mock().Object); + _mockTestSensor = new Mock(); + _mockSensor.Setup(ms => ms.Object).Returns(_mockTestSensor.Object); } @@ -43,9 +46,10 @@ namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests [Fact] public void Test_GetSensorMeasurement() { + _mockTestSensor.Setup(s => s.GetMeasurement()).Returns(new Dictionary()); var service = new Bme680GrpcService(_mockLogger.Object, _mockSensor.Object); service.GetSensorMeasurement(null, null); - + // Verify that the sensor get measurement method is called. _mockSensor.Verify(s => s.Object.GetMeasurement(), Times.AtLeastOnce()); diff --git a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/TestBme680Sensor.cs b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/TestBme680Sensor.cs index fd506eb..4e37da0 100644 --- a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/TestBme680Sensor.cs +++ b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/TestBme680Sensor.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System.Threading.Tasks; using NucuCar.Sensors.EnvironmentSensor; using NucuCarSensorsProto; @@ -16,10 +17,9 @@ namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests } - // TODO Make more generic - public override EnvironmentSensorMeasurement GetMeasurement() + public override Dictionary GetMeasurement() { - return new EnvironmentSensorMeasurement(); + return new Dictionary(); } public override SensorStateEnum GetState()