Merge pull request #2 from dnutiu/interface-no-go

Interface no go
This commit is contained in:
Denis-Cosmin Nutiu 2019-11-30 19:56:57 +02:00 committed by GitHub
commit fb43090994
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 60 additions and 28 deletions

View file

@ -13,9 +13,9 @@ namespace NucuCar.Sensors.EnvironmentSensor
public class Bme680GrpcService : EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceBase
{
private readonly ILogger<Bme680GrpcService> _logger;
private readonly Bme680Sensor _bme680Sensor;
private readonly ISensor<Bme680Sensor> _bme680Sensor;
public Bme680GrpcService(ILogger<Bme680GrpcService> logger, Bme680Sensor bme680Sensor)
public Bme680GrpcService(ILogger<Bme680GrpcService> logger, ISensor<Bme680Sensor> bme680Sensor)
{
_bme680Sensor = bme680Sensor;
_logger = logger;
@ -26,7 +26,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
_logger?.LogDebug($"Calling {nameof(GetSensorState)}.");
return Task.FromResult(new NucuCarSensorState()
{
State = _bme680Sensor.GetState()
State = _bme680Sensor.Object.GetState()
});
}
@ -34,7 +34,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
ServerCallContext context)
{
_logger?.LogDebug($"Calling {nameof(GetSensorMeasurement)}.");
var sensorMeasurement = _bme680Sensor.GetMeasurement();
var sensorMeasurement = _bme680Sensor.Object.GetMeasurement();
return Task.FromResult(sensorMeasurement);
}
}

View file

@ -15,7 +15,7 @@ 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
public class Bme680Sensor : IDisposable, ITelemeter, ISensor<Bme680Sensor>
{
private readonly ILogger _logger;
private I2cConnectionSettings _i2CSettings;
@ -42,15 +42,16 @@ namespace NucuCar.Sensors.EnvironmentSensor
_logger?.LogInformation("BME680 Sensor is disabled!");
_sensorStateEnum = SensorStateEnum.Disabled;
}
Object = this;
}
// TODO Make more generic, Add interface and remove virtual
// TODO Make more generic
public virtual EnvironmentSensorMeasurement GetMeasurement()
{
return _lastMeasurement;
}
// TODO: Add interface and remove virtual
public virtual SensorStateEnum GetState()
{
return _sensorStateEnum;
@ -135,5 +136,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
return returnValue;
}
public Bme680Sensor Object { get; }
}
}

View file

@ -20,50 +20,50 @@ namespace NucuCar.Sensors.EnvironmentSensor
private readonly int _measurementInterval;
private readonly ILogger<Bme680Worker> _logger;
private readonly TelemetryPublisher _telemetryPublisher;
private readonly Bme680Sensor _bme680Sensor;
private readonly ISensor<Bme680Sensor> _bme680Sensor;
public Bme680Worker(ILogger<Bme680Worker> logger, IOptions<Bme680Config> options,
SensorTelemetry sensorTelemetry, Bme680Sensor bme680Sensor)
SensorTelemetry sensorTelemetry, ISensor<Bme680Sensor> sensor)
{
_logger = logger;
_telemetryEnabled = options.Value.TelemetryEnabled;
_serviceEnabled = options.Value.ServiceEnabled;
_measurementInterval = options.Value.MeasurementInterval;
_telemetryPublisher = sensorTelemetry.Publisher;
_bme680Sensor = bme680Sensor;
_bme680Sensor = sensor;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
if (_serviceEnabled)
if (!_serviceEnabled)
{
return;
}
if (_telemetryEnabled)
{
_telemetryPublisher?.RegisterTelemeter(_bme680Sensor);
_telemetryPublisher?.RegisterTelemeter(_bme680Sensor.Object);
}
while (!stoppingToken.IsCancellationRequested)
{
/* If sensor is ok attempt to read. */
if (_bme680Sensor.GetState() == SensorStateEnum.Initialized)
if (_bme680Sensor.Object.GetState() == SensorStateEnum.Initialized)
{
_logger.LogInformation("Taking measurement!");
await _bme680Sensor.TakeMeasurement();
await _bme680Sensor.Object.TakeMeasurement();
}
/* Else attempt to re-initialize. */
else
{
await Task.Delay(10000, stoppingToken);
_bme680Sensor.InitializeSensor();
_bme680Sensor.Object.InitializeSensor();
}
await Task.Delay(_measurementInterval, stoppingToken);
}
_telemetryPublisher?.UnRegisterTelemeter(_bme680Sensor);
_telemetryPublisher?.UnRegisterTelemeter(_bme680Sensor.Object);
}
}
}

View file

@ -0,0 +1,9 @@
using NucuCar.Domain.Telemetry;
namespace NucuCar.Sensors
{
public interface ISensor<out TSensor> where TSensor : class, ITelemeter
{
TSensor Object { get; }
}
}

View file

@ -1,5 +1,4 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NucuCar.Sensors.EnvironmentSensor;
@ -23,7 +22,7 @@ namespace NucuCar.Sensors
// Singletons
services.AddSingleton<SensorTelemetry>();
services.AddSingleton<Bme680Sensor>();
services.AddSingleton<ISensor<Bme680Sensor>, Bme680Sensor>();
// Workers
services.AddHostedService<TelemetryWorker>();

View file

@ -1,7 +1,6 @@
using System;
using Grpc.Core;
using Microsoft.Extensions.Logging;
using Moq;
using NucuCar.Sensors;
using NucuCar.Sensors.EnvironmentSensor;
using NucuCarSensorsProto;
using Xunit;
@ -9,18 +8,18 @@ using Xunit.Abstractions;
namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests
{
public class Bme680GrpcServiceTest
public partial class Bme680GrpcServiceTest
{
private readonly ITestOutputHelper _testOutputHelper;
private readonly Mock<ILogger<Bme680GrpcService>> _mockLogger;
private readonly Mock<Bme680Sensor> _mockSensor;
private readonly Mock<ISensor<Bme680Sensor>> _mockSensor;
public Bme680GrpcServiceTest(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
_mockLogger = new Mock<ILogger<Bme680GrpcService>>();
_mockSensor = new Mock<Bme680Sensor>();
_mockSensor = new Mock<ISensor<Bme680Sensor>>();
_mockSensor.Setup(ms => ms.Object).Returns(new Mock<TestBme680Sensor>().Object);
}
@ -34,9 +33,9 @@ namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests
Assert.Equal(SensorStateEnum.Error, result.State);
// Verify that the sensor get state method is called.
_mockSensor.Verify(s => s.GetState(), Times.AtLeastOnce());
_mockSensor.Verify(s => s.Object.GetState(), Times.AtLeastOnce());
_mockSensor.Setup(s => s.GetState()).Returns(SensorStateEnum.Initialized);
_mockSensor.Setup(s => s.Object.GetState()).Returns(SensorStateEnum.Initialized);
result = service.GetSensorState(null, null).Result;
Assert.Equal(SensorStateEnum.Initialized, result.State);
}
@ -48,7 +47,7 @@ namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests
service.GetSensorMeasurement(null, null);
// Verify that the sensor get measurement method is called.
_mockSensor.Verify(s => s.GetMeasurement(), Times.AtLeastOnce());
_mockSensor.Verify(s => s.Object.GetMeasurement(), Times.AtLeastOnce());
}
}

View file

@ -0,0 +1,22 @@
using NucuCar.Sensors.EnvironmentSensor;
using NucuCarSensorsProto;
namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests
{
public partial class Bme680GrpcServiceTest
{
public class TestBme680Sensor : Bme680Sensor
{
// TODO Make more generic
public override EnvironmentSensorMeasurement GetMeasurement()
{
return new EnvironmentSensorMeasurement();
}
public override SensorStateEnum GetState()
{
return SensorStateEnum.Error;
}
}
}
}