Remove EnvironmentSensorMeasurement type from Bme680Sensor

This commit is contained in:
Denis-Cosmin Nutiu 2019-12-01 18:04:46 +02:00
parent a5600e40cd
commit 5be270210a
7 changed files with 52 additions and 27 deletions

View file

@ -25,7 +25,7 @@ namespace NucuCar.Domain.Telemetry
throw;
}
Logger?.LogInformation("Started the AzureTelemetryPublisher!");
Logger?.LogDebug("Started the AzureTelemetryPublisher!");
}
/// <summary>

View file

@ -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),
});
}
}
}

View file

@ -21,14 +21,13 @@ namespace NucuCar.Sensors.EnvironmentSensor
private I2cConnectionSettings _i2CSettings;
private I2cDevice _i2CDevice;
private Bme680 _bme680;
private EnvironmentSensorMeasurement _lastMeasurement;
private Dictionary<string, double> _lastMeasurement;
private SensorStateEnum _sensorStateEnum;
public Bme680Sensor()
{
}
public Bme680Sensor(ILogger<Bme680Sensor> logger, IOptions<Bme680Config> 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<string, double> GetMeasurement()
{
return _lastMeasurement;
}
@ -65,6 +63,8 @@ namespace NucuCar.Sensors.EnvironmentSensor
return;
}
_lastMeasurement = new Dictionary<string, double>();
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<string, object>
{
["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")
};
}

View file

@ -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. */

View file

@ -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);
}
}

View file

@ -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<ILogger<Bme680GrpcService>> _mockLogger;
private readonly Mock<ISensor<Bme680Sensor>> _mockSensor;
private readonly Mock<TestBme680Sensor> _mockTestSensor;
public Bme680GrpcServiceTest(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
_mockLogger = new Mock<ILogger<Bme680GrpcService>>();
_mockSensor = new Mock<ISensor<Bme680Sensor>>();
_mockSensor.Setup(ms => ms.Object).Returns(new Mock<TestBme680Sensor>().Object);
_mockTestSensor = new Mock<TestBme680Sensor>();
_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<string, double>());
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());

View file

@ -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<string, double> GetMeasurement()
{
return new EnvironmentSensorMeasurement();
return new Dictionary<string, double>();
}
public override SensorStateEnum GetState()