Remove EnvironmentSensorMeasurement type from Bme680Sensor
This commit is contained in:
parent
a5600e40cd
commit
5be270210a
7 changed files with 52 additions and 27 deletions
|
@ -25,7 +25,7 @@ namespace NucuCar.Domain.Telemetry
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger?.LogInformation("Started the AzureTelemetryPublisher!");
|
Logger?.LogDebug("Started the AzureTelemetryPublisher!");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Google.Protobuf.WellKnownTypes;
|
using Google.Protobuf.WellKnownTypes;
|
||||||
using Grpc.Core;
|
using Grpc.Core;
|
||||||
|
@ -35,7 +36,13 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
{
|
{
|
||||||
_logger?.LogDebug($"Calling {nameof(GetSensorMeasurement)}.");
|
_logger?.LogDebug($"Calling {nameof(GetSensorMeasurement)}.");
|
||||||
var sensorMeasurement = _bme680Sensor.Object.GetMeasurement();
|
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),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -21,12 +21,11 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
private I2cConnectionSettings _i2CSettings;
|
private I2cConnectionSettings _i2CSettings;
|
||||||
private I2cDevice _i2CDevice;
|
private I2cDevice _i2CDevice;
|
||||||
private Bme680 _bme680;
|
private Bme680 _bme680;
|
||||||
private EnvironmentSensorMeasurement _lastMeasurement;
|
private Dictionary<string, double> _lastMeasurement;
|
||||||
private SensorStateEnum _sensorStateEnum;
|
private SensorStateEnum _sensorStateEnum;
|
||||||
|
|
||||||
public Bme680Sensor()
|
public Bme680Sensor()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bme680Sensor(ILogger<Bme680Sensor> logger, IOptions<Bme680Config> options)
|
public Bme680Sensor(ILogger<Bme680Sensor> logger, IOptions<Bme680Config> options)
|
||||||
|
@ -42,8 +41,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
Object = this;
|
Object = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Make more generic
|
public virtual Dictionary<string, double> GetMeasurement()
|
||||||
public virtual EnvironmentSensorMeasurement GetMeasurement()
|
|
||||||
{
|
{
|
||||||
return _lastMeasurement;
|
return _lastMeasurement;
|
||||||
}
|
}
|
||||||
|
@ -65,6 +63,8 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_lastMeasurement = new Dictionary<string, double>();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
/* Connect to default i2c address 0x76 */
|
/* Connect to default i2c address 0x76 */
|
||||||
|
@ -73,7 +73,6 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
_bme680 = new Bme680(_i2CDevice);
|
_bme680 = new Bme680(_i2CDevice);
|
||||||
|
|
||||||
/* Initialize measurement */
|
/* Initialize measurement */
|
||||||
_lastMeasurement = new EnvironmentSensorMeasurement();
|
|
||||||
_bme680.Reset();
|
_bme680.Reset();
|
||||||
_bme680.SetHumiditySampling(Sampling.UltraLowPower);
|
_bme680.SetHumiditySampling(Sampling.UltraLowPower);
|
||||||
_bme680.SetTemperatureSampling(Sampling.UltraHighResolution);
|
_bme680.SetTemperatureSampling(Sampling.UltraHighResolution);
|
||||||
|
@ -102,13 +101,17 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
/* Force the sensor to take a measurement. */
|
/* Force the sensor to take a measurement. */
|
||||||
_bme680.SetPowerMode(Bme680PowerMode.Forced);
|
_bme680.SetPowerMode(Bme680PowerMode.Forced);
|
||||||
|
|
||||||
_lastMeasurement.Temperature = (await _bme680.ReadTemperatureAsync()).Celsius;
|
_lastMeasurement["temperature"] = (await _bme680.ReadTemperatureAsync()).Celsius;
|
||||||
_lastMeasurement.Pressure = await _bme680.ReadPressureAsync();
|
_lastMeasurement["pressure"] = await _bme680.ReadPressureAsync();
|
||||||
_lastMeasurement.Humidity = await _bme680.ReadHumidityAsync();
|
_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(
|
_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()
|
public string GetIdentifier()
|
||||||
|
@ -123,10 +126,10 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
{
|
{
|
||||||
returnValue = new Dictionary<string, object>
|
returnValue = new Dictionary<string, object>
|
||||||
{
|
{
|
||||||
["temperature"] = _lastMeasurement.Temperature,
|
["temperature"] = _lastMeasurement.GetValueOrDefault("temperature"),
|
||||||
["humidity"] = _lastMeasurement.Humidity,
|
["humidity"] = _lastMeasurement.GetValueOrDefault("humidity"),
|
||||||
["pressure"] = _lastMeasurement.Pressure,
|
["pressure"] = _lastMeasurement.GetValueOrDefault("pressure"),
|
||||||
["voc"] = _lastMeasurement.VolatileOrganicCompound
|
["voc"] = _lastMeasurement.GetValueOrDefault("voc")
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,6 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
/* If sensor is ok attempt to read. */
|
/* If sensor is ok attempt to read. */
|
||||||
if (_bme680Sensor.Object.GetState() == SensorStateEnum.Initialized)
|
if (_bme680Sensor.Object.GetState() == SensorStateEnum.Initialized)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Taking measurement!");
|
|
||||||
await _bme680Sensor.Object.TakeMeasurement();
|
await _bme680Sensor.Object.TakeMeasurement();
|
||||||
}
|
}
|
||||||
/* Else attempt to re-initialize. */
|
/* Else attempt to re-initialize. */
|
||||||
|
|
|
@ -13,6 +13,7 @@ namespace NucuCar.Sensors.Telemetry
|
||||||
public class TelemetryWorker : BackgroundService
|
public class TelemetryWorker : BackgroundService
|
||||||
{
|
{
|
||||||
private readonly int _interval;
|
private readonly int _interval;
|
||||||
|
private readonly bool _serviceEnabled;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly TelemetryPublisher _telemetryPublisher;
|
private readonly TelemetryPublisher _telemetryPublisher;
|
||||||
|
|
||||||
|
@ -21,20 +22,31 @@ namespace NucuCar.Sensors.Telemetry
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_interval = options.Value.PublishInterval;
|
_interval = options.Value.PublishInterval;
|
||||||
|
_serviceEnabled = options.Value.ServiceEnabled;
|
||||||
_telemetryPublisher = sensorTelemetry.Publisher;
|
_telemetryPublisher = sensorTelemetry.Publisher;
|
||||||
if (_telemetryPublisher == null)
|
if (_telemetryPublisher == null)
|
||||||
{
|
{
|
||||||
logger.LogCritical("Invalid state! TelemetryPublisher is null!");
|
logger?.LogCritical("Invalid state! TelemetryPublisher is null!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
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);
|
await Task.Delay(_interval, stoppingToken);
|
||||||
while (!stoppingToken.IsCancellationRequested)
|
while (!stoppingToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Is publishing telemetry data!");
|
|
||||||
await _telemetryPublisher.PublishAsync(stoppingToken);
|
await _telemetryPublisher.PublishAsync(stoppingToken);
|
||||||
|
_logger?.LogDebug("Telemetry data published!");
|
||||||
await Task.Delay(_interval, stoppingToken);
|
await Task.Delay(_interval, stoppingToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Moq;
|
using Moq;
|
||||||
using NucuCar.Sensors;
|
using NucuCar.Sensors;
|
||||||
|
@ -8,18 +9,20 @@ using Xunit.Abstractions;
|
||||||
|
|
||||||
namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests
|
namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests
|
||||||
{
|
{
|
||||||
public partial class Bme680GrpcServiceTest
|
public class Bme680GrpcServiceTest
|
||||||
{
|
{
|
||||||
private readonly ITestOutputHelper _testOutputHelper;
|
private readonly ITestOutputHelper _testOutputHelper;
|
||||||
private readonly Mock<ILogger<Bme680GrpcService>> _mockLogger;
|
private readonly Mock<ILogger<Bme680GrpcService>> _mockLogger;
|
||||||
private readonly Mock<ISensor<Bme680Sensor>> _mockSensor;
|
private readonly Mock<ISensor<Bme680Sensor>> _mockSensor;
|
||||||
|
private readonly Mock<TestBme680Sensor> _mockTestSensor;
|
||||||
|
|
||||||
public Bme680GrpcServiceTest(ITestOutputHelper testOutputHelper)
|
public Bme680GrpcServiceTest(ITestOutputHelper testOutputHelper)
|
||||||
{
|
{
|
||||||
_testOutputHelper = testOutputHelper;
|
_testOutputHelper = testOutputHelper;
|
||||||
_mockLogger = new Mock<ILogger<Bme680GrpcService>>();
|
_mockLogger = new Mock<ILogger<Bme680GrpcService>>();
|
||||||
_mockSensor = new Mock<ISensor<Bme680Sensor>>();
|
_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,6 +46,7 @@ namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Test_GetSensorMeasurement()
|
public void Test_GetSensorMeasurement()
|
||||||
{
|
{
|
||||||
|
_mockTestSensor.Setup(s => s.GetMeasurement()).Returns(new Dictionary<string, double>());
|
||||||
var service = new Bme680GrpcService(_mockLogger.Object, _mockSensor.Object);
|
var service = new Bme680GrpcService(_mockLogger.Object, _mockSensor.Object);
|
||||||
service.GetSensorMeasurement(null, null);
|
service.GetSensorMeasurement(null, null);
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using NucuCar.Sensors.EnvironmentSensor;
|
using NucuCar.Sensors.EnvironmentSensor;
|
||||||
using NucuCarSensorsProto;
|
using NucuCarSensorsProto;
|
||||||
|
@ -16,10 +17,9 @@ namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Make more generic
|
public override Dictionary<string, double> GetMeasurement()
|
||||||
public override EnvironmentSensorMeasurement GetMeasurement()
|
|
||||||
{
|
{
|
||||||
return new EnvironmentSensorMeasurement();
|
return new Dictionary<string, double>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override SensorStateEnum GetState()
|
public override SensorStateEnum GetState()
|
||||||
|
|
Loading…
Reference in a new issue