From e7e4997020b7d7a8d26f09ccb3d11fd78d116c0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Nu=C8=9Biu?= Date: Tue, 17 Dec 2019 21:40:20 +0200 Subject: [PATCH] Introduce GenericSensor class --- NucuCar.Domain/Sensors/GenericSensor.cs | 18 +++++++ .../EnvironmentSensor/Bme680Sensor.cs | 47 +++++++++---------- .../EnvironmentSensor/Bme680Worker.cs | 2 +- .../TestBme680Sensor.cs | 2 +- 4 files changed, 42 insertions(+), 27 deletions(-) create mode 100644 NucuCar.Domain/Sensors/GenericSensor.cs diff --git a/NucuCar.Domain/Sensors/GenericSensor.cs b/NucuCar.Domain/Sensors/GenericSensor.cs new file mode 100644 index 0000000..55d83e1 --- /dev/null +++ b/NucuCar.Domain/Sensors/GenericSensor.cs @@ -0,0 +1,18 @@ +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using NucuCarSensorsProto; + +namespace NucuCar.Domain.Sensors +{ + public abstract class GenericSensor + { + protected bool TelemetryEnabled; + protected ILogger Logger; + protected SensorStateEnum SensorStateEnum; + + public abstract void InitializeSensor(); + public abstract Task TakeMeasurementAsync(); + public abstract Bme680MeasurementData GetMeasurement(); + public abstract SensorStateEnum GetState(); + } +} \ No newline at end of file diff --git a/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs b/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs index 6476101..8fb69b3 100644 --- a/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs +++ b/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs @@ -16,15 +16,12 @@ namespace NucuCar.Sensors.EnvironmentSensor /// Abstraction for the BME680 sensor. /// See: https://www.bosch-sensortec.com/bst/products/all_products/bme680 /// - public class Bme680Sensor : IDisposable, ITelemeter, ISensor + public class Bme680Sensor : GenericSensor, IDisposable, ITelemeter, ISensor { - private readonly bool _telemetryEnabled; - private readonly ILogger _logger; private I2cConnectionSettings _i2CSettings; private I2cDevice _i2CDevice; private Bme680 _bme680; private Bme680MeasurementData _lastMeasurement; - private SensorStateEnum _sensorStateEnum; public Bme680Sensor() { @@ -32,27 +29,27 @@ namespace NucuCar.Sensors.EnvironmentSensor public Bme680Sensor(ILogger logger, IOptions options) { - _sensorStateEnum = SensorStateEnum.Uninitialized; - _logger = logger; + SensorStateEnum = SensorStateEnum.Uninitialized; + Logger = logger; if (!options.Value.Enabled) { - _logger?.LogInformation("BME680 Sensor is disabled!"); - _sensorStateEnum = SensorStateEnum.Disabled; + Logger?.LogInformation("BME680 Sensor is disabled!"); + SensorStateEnum = SensorStateEnum.Disabled; } - _telemetryEnabled = options.Value.Telemetry; + TelemetryEnabled = options.Value.Telemetry; Object = this; } - public virtual Bme680MeasurementData GetMeasurement() + public override Bme680MeasurementData GetMeasurement() { return _lastMeasurement; } - public virtual SensorStateEnum GetState() + public override SensorStateEnum GetState() { - return _sensorStateEnum; + return SensorStateEnum; } public void Dispose() @@ -60,9 +57,9 @@ namespace NucuCar.Sensors.EnvironmentSensor _bme680?.Dispose(); } - public virtual void InitializeSensor() + public override void InitializeSensor() { - if (_sensorStateEnum == SensorStateEnum.Initialized || _sensorStateEnum == SensorStateEnum.Disabled) + if (SensorStateEnum == SensorStateEnum.Initialized || SensorStateEnum == SensorStateEnum.Disabled) { return; } @@ -81,21 +78,21 @@ namespace NucuCar.Sensors.EnvironmentSensor _bme680.SetHumiditySampling(Sampling.UltraLowPower); _bme680.SetTemperatureSampling(Sampling.UltraHighResolution); _bme680.SetPressureSampling(Sampling.UltraLowPower); - _sensorStateEnum = SensorStateEnum.Initialized; + SensorStateEnum = SensorStateEnum.Initialized; - _logger?.LogInformation($"{DateTimeOffset.Now}:BME680 Sensor initialization OK."); + Logger?.LogInformation($"{DateTimeOffset.Now}:BME680 Sensor initialization OK."); } catch (System.IO.IOException e) { - _logger?.LogError($"{DateTimeOffset.Now}:BME680 Sensor initialization FAIL."); - _logger?.LogTrace(e.Message); - _sensorStateEnum = SensorStateEnum.Error; + Logger?.LogError($"{DateTimeOffset.Now}:BME680 Sensor initialization FAIL."); + Logger?.LogTrace(e.Message); + SensorStateEnum = SensorStateEnum.Error; } } - public virtual async Task TakeMeasurement() + public override async Task TakeMeasurementAsync() { - if (_sensorStateEnum != SensorStateEnum.Initialized) + if (SensorStateEnum != SensorStateEnum.Initialized) { throw new InvalidOperationException("Can't take measurement on uninitialized sensor!"); } @@ -108,8 +105,8 @@ namespace NucuCar.Sensors.EnvironmentSensor _lastMeasurement.Humidity = await _bme680.ReadHumidityAsync(); _lastMeasurement.VolatileOrganicCompounds = 0.0; // Not implemented. - _logger?.LogDebug($"{DateTimeOffset.Now}:BME680: reading"); - _logger?.LogInformation( + Logger?.LogDebug($"{DateTimeOffset.Now}:BME680: reading"); + Logger?.LogInformation( $"temperature:{_lastMeasurement.Temperature:N2} \u00B0C|" + $"pressure:{_lastMeasurement.Pressure:N2} hPa|" + $"humidity:{_lastMeasurement.Humidity:N2} %rH|" + @@ -124,11 +121,11 @@ namespace NucuCar.Sensors.EnvironmentSensor public Dictionary GetTelemetryData() { Dictionary returnValue = null; - if (_lastMeasurement != null && _telemetryEnabled) + if (_lastMeasurement != null && TelemetryEnabled) { returnValue = new Dictionary { - ["sensor_state"] = _sensorStateEnum, + ["sensor_state"] = SensorStateEnum, ["temperature"] = _lastMeasurement.Temperature, ["humidity"] = _lastMeasurement.Humidity, ["pressure"] = _lastMeasurement.Pressure, diff --git a/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs b/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs index 65675ab..c541473 100644 --- a/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs +++ b/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs @@ -39,7 +39,7 @@ namespace NucuCar.Sensors.EnvironmentSensor /* If sensor is ok attempt to read. */ if (_bme680Sensor.Object.GetState() == SensorStateEnum.Initialized) { - await _bme680Sensor.Object.TakeMeasurement(); + await _bme680Sensor.Object.TakeMeasurementAsync(); } /* Else attempt to re-initialize. */ else if (_bme680Sensor.Object.GetState() == SensorStateEnum.Uninitialized || diff --git a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/TestBme680Sensor.cs b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/TestBme680Sensor.cs index 8402521..1c1c6ab 100644 --- a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/TestBme680Sensor.cs +++ b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/TestBme680Sensor.cs @@ -8,7 +8,7 @@ namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests { public class TestBme680Sensor : Bme680Sensor { - public override Task TakeMeasurement() + public override Task TakeMeasurementAsync() { return Task.CompletedTask; }