Introduce GenericSensor class
This commit is contained in:
parent
caf67bec4c
commit
e7e4997020
4 changed files with 42 additions and 27 deletions
18
NucuCar.Domain/Sensors/GenericSensor.cs
Normal file
18
NucuCar.Domain/Sensors/GenericSensor.cs
Normal file
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -16,15 +16,12 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
|||
/// Abstraction for the BME680 sensor.
|
||||
/// See: https://www.bosch-sensortec.com/bst/products/all_products/bme680
|
||||
/// </summary>
|
||||
public class Bme680Sensor : IDisposable, ITelemeter, ISensor<Bme680Sensor>
|
||||
public class Bme680Sensor : GenericSensor, IDisposable, ITelemeter, ISensor<Bme680Sensor>
|
||||
{
|
||||
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<Bme680Sensor> logger, IOptions<Bme680Config> 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<string, object> GetTelemetryData()
|
||||
{
|
||||
Dictionary<string, object> returnValue = null;
|
||||
if (_lastMeasurement != null && _telemetryEnabled)
|
||||
if (_lastMeasurement != null && TelemetryEnabled)
|
||||
{
|
||||
returnValue = new Dictionary<string, object>
|
||||
{
|
||||
["sensor_state"] = _sensorStateEnum,
|
||||
["sensor_state"] = SensorStateEnum,
|
||||
["temperature"] = _lastMeasurement.Temperature,
|
||||
["humidity"] = _lastMeasurement.Humidity,
|
||||
["pressure"] = _lastMeasurement.Pressure,
|
||||
|
|
|
@ -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 ||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue