From f13a918af3b9eb84aa52f8a4495b79891f4b166a Mon Sep 17 00:00:00 2001 From: Denis-Cosmin Nutiu Date: Sat, 30 Nov 2019 17:44:54 +0200 Subject: [PATCH 1/3] Add ISensor interface --- .../EnvironmentSensor/Bme680GrpcService.cs | 8 +-- .../EnvironmentSensor/Bme680Worker.cs | 16 +++--- NucuCar.Sensors/ISensor.cs | 9 +++ NucuCar.Sensors/Program.cs | 1 + .../Bme680GrpcServiceTest.cs | 56 +++++++++---------- 5 files changed, 50 insertions(+), 40 deletions(-) create mode 100644 NucuCar.Sensors/ISensor.cs diff --git a/NucuCar.Sensors/EnvironmentSensor/Bme680GrpcService.cs b/NucuCar.Sensors/EnvironmentSensor/Bme680GrpcService.cs index 414b24f..49a7529 100644 --- a/NucuCar.Sensors/EnvironmentSensor/Bme680GrpcService.cs +++ b/NucuCar.Sensors/EnvironmentSensor/Bme680GrpcService.cs @@ -13,9 +13,9 @@ namespace NucuCar.Sensors.EnvironmentSensor public class Bme680GrpcService : EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceBase { private readonly ILogger _logger; - private readonly Bme680Sensor _bme680Sensor; + private readonly ISensor _bme680Sensor; - public Bme680GrpcService(ILogger logger, Bme680Sensor bme680Sensor) + public Bme680GrpcService(ILogger logger, ISensor 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); } } diff --git a/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs b/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs index 3a48871..bf65523 100644 --- a/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs +++ b/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs @@ -20,18 +20,18 @@ namespace NucuCar.Sensors.EnvironmentSensor private readonly int _measurementInterval; private readonly ILogger _logger; private readonly TelemetryPublisher _telemetryPublisher; - private readonly Bme680Sensor _bme680Sensor; + private readonly ISensor _bme680Sensor; public Bme680Worker(ILogger logger, IOptions options, - SensorTelemetry sensorTelemetry, Bme680Sensor bme680Sensor) + SensorTelemetry sensorTelemetry, ISensor 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) @@ -42,28 +42,28 @@ namespace NucuCar.Sensors.EnvironmentSensor } 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); } } } \ No newline at end of file diff --git a/NucuCar.Sensors/ISensor.cs b/NucuCar.Sensors/ISensor.cs new file mode 100644 index 0000000..207c79b --- /dev/null +++ b/NucuCar.Sensors/ISensor.cs @@ -0,0 +1,9 @@ +using NucuCar.Domain.Telemetry; + +namespace NucuCar.Sensors +{ + public interface ISensor where TSensor : class, new() + { + TSensor Object { get; } + } +} \ No newline at end of file diff --git a/NucuCar.Sensors/Program.cs b/NucuCar.Sensors/Program.cs index 0eb1245..5322e4f 100644 --- a/NucuCar.Sensors/Program.cs +++ b/NucuCar.Sensors/Program.cs @@ -24,6 +24,7 @@ namespace NucuCar.Sensors // Singletons services.AddSingleton(); services.AddSingleton(); + services.AddSingleton>(); // Workers services.AddHostedService(); diff --git a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680GrpcServiceTest.cs b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680GrpcServiceTest.cs index 8af2c42..5d0237e 100644 --- a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680GrpcServiceTest.cs +++ b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680GrpcServiceTest.cs @@ -23,33 +23,33 @@ namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests _mockSensor = new Mock(); } - - [Fact] - public void Test_GetSensorState() - { - var service = new Bme680GrpcService(_mockLogger.Object, _mockSensor.Object); - var result = service.GetSensorState(null, null).Result; - - // Default sensor state is error - Assert.Equal(SensorStateEnum.Error, result.State); - - // Verify that the sensor get state method is called. - _mockSensor.Verify(s => s.GetState(), Times.AtLeastOnce()); - - _mockSensor.Setup(s => s.GetState()).Returns(SensorStateEnum.Initialized); - result = service.GetSensorState(null, null).Result; - Assert.Equal(SensorStateEnum.Initialized, result.State); - } - - [Fact] - public void Test_GetSensorMeasurement() - { - 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.GetMeasurement(), Times.AtLeastOnce()); - - } +// +// [Fact] +// public void Test_GetSensorState() +// { +// var service = new Bme680GrpcService(_mockLogger.Object, _mockSensor.Object); +// var result = service.GetSensorState(null, null).Result; +// +// // Default sensor state is error +// Assert.Equal(SensorStateEnum.Error, result.State); +// +// // Verify that the sensor get state method is called. +// _mockSensor.Verify(s => s.GetState(), Times.AtLeastOnce()); +// +// _mockSensor.Setup(s => s.GetState()).Returns(SensorStateEnum.Initialized); +// result = service.GetSensorState(null, null).Result; +// Assert.Equal(SensorStateEnum.Initialized, result.State); +// } +// +// [Fact] +// public void Test_GetSensorMeasurement() +// { +// 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.GetMeasurement(), Times.AtLeastOnce()); +// +// } } } \ No newline at end of file From beeb6d22a2fac0c205d0e72961a82b0f908f6256 Mon Sep 17 00:00:00 2001 From: Denis-Cosmin Nutiu Date: Sat, 30 Nov 2019 18:55:55 +0200 Subject: [PATCH 2/3] Fix interface and restore gRPC tests --- .../EnvironmentSensor/Bme680Sensor.cs | 9 ++- .../EnvironmentSensor/Bme680Worker.cs | 2 +- NucuCar.Sensors/ISensor.cs | 2 +- NucuCar.Sensors/Program.cs | 6 +- NucuCar.Sensors/Sensor.cs | 9 +++ .../Bme680GrpcServiceTest.cs | 67 +++++++++---------- .../TestBme680Sensor.cs | 22 ++++++ 7 files changed, 74 insertions(+), 43 deletions(-) create mode 100644 NucuCar.Sensors/Sensor.cs create mode 100644 NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/TestBme680Sensor.cs diff --git a/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs b/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs index aedcba5..516f201 100644 --- a/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs +++ b/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs @@ -15,7 +15,7 @@ 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 + public class Bme680Sensor : IDisposable, ITelemeter, ISensor { 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; } } } \ No newline at end of file diff --git a/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs b/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs index bf65523..499ac71 100644 --- a/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs +++ b/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs @@ -36,7 +36,7 @@ namespace NucuCar.Sensors.EnvironmentSensor protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - if (_serviceEnabled) + if (!_serviceEnabled) { return; } diff --git a/NucuCar.Sensors/ISensor.cs b/NucuCar.Sensors/ISensor.cs index 207c79b..db1ac21 100644 --- a/NucuCar.Sensors/ISensor.cs +++ b/NucuCar.Sensors/ISensor.cs @@ -2,7 +2,7 @@ using NucuCar.Domain.Telemetry; namespace NucuCar.Sensors { - public interface ISensor where TSensor : class, new() + public interface ISensor where TSensor : class, ITelemeter { TSensor Object { get; } } diff --git a/NucuCar.Sensors/Program.cs b/NucuCar.Sensors/Program.cs index 5322e4f..a1b2916 100644 --- a/NucuCar.Sensors/Program.cs +++ b/NucuCar.Sensors/Program.cs @@ -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,9 +22,8 @@ namespace NucuCar.Sensors // Singletons services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton>(); - + services.AddSingleton, Bme680Sensor>(); + // Workers services.AddHostedService(); services.AddHostedService(); diff --git a/NucuCar.Sensors/Sensor.cs b/NucuCar.Sensors/Sensor.cs new file mode 100644 index 0000000..ab61aaf --- /dev/null +++ b/NucuCar.Sensors/Sensor.cs @@ -0,0 +1,9 @@ +using NucuCar.Domain.Telemetry; + +namespace NucuCar.Sensors +{ + public class Sensor : ISensor where T : class, ITelemeter + { + public T Object { get; } + } +} \ No newline at end of file diff --git a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680GrpcServiceTest.cs b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680GrpcServiceTest.cs index 5d0237e..c3b7f2f 100644 --- a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680GrpcServiceTest.cs +++ b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680GrpcServiceTest.cs @@ -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,47 +8,47 @@ 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> _mockLogger; - private readonly Mock _mockSensor; + private readonly Mock> _mockSensor; - public Bme680GrpcServiceTest(ITestOutputHelper testOutputHelper) { _testOutputHelper = testOutputHelper; _mockLogger = new Mock>(); - _mockSensor = new Mock(); + _mockSensor = new Mock>(); + _mockSensor.Setup(ms => ms.Object).Returns(new Mock().Object); } -// -// [Fact] -// public void Test_GetSensorState() -// { -// var service = new Bme680GrpcService(_mockLogger.Object, _mockSensor.Object); -// var result = service.GetSensorState(null, null).Result; -// -// // Default sensor state is error -// Assert.Equal(SensorStateEnum.Error, result.State); -// -// // Verify that the sensor get state method is called. -// _mockSensor.Verify(s => s.GetState(), Times.AtLeastOnce()); -// -// _mockSensor.Setup(s => s.GetState()).Returns(SensorStateEnum.Initialized); -// result = service.GetSensorState(null, null).Result; -// Assert.Equal(SensorStateEnum.Initialized, result.State); -// } -// -// [Fact] -// public void Test_GetSensorMeasurement() -// { -// 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.GetMeasurement(), Times.AtLeastOnce()); -// -// } + + [Fact] + public void Test_GetSensorState() + { + var service = new Bme680GrpcService(_mockLogger.Object, _mockSensor.Object); + var result = service.GetSensorState(null, null).Result; + + // Default sensor state is error + Assert.Equal(SensorStateEnum.Error, result.State); + + // Verify that the sensor get state method is called. + _mockSensor.Verify(s => s.Object.GetState(), Times.AtLeastOnce()); + + _mockSensor.Setup(s => s.Object.GetState()).Returns(SensorStateEnum.Initialized); + result = service.GetSensorState(null, null).Result; + Assert.Equal(SensorStateEnum.Initialized, result.State); + } + + [Fact] + public void Test_GetSensorMeasurement() + { + 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()); + + } } } \ No newline at end of file diff --git a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/TestBme680Sensor.cs b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/TestBme680Sensor.cs new file mode 100644 index 0000000..1c7c60a --- /dev/null +++ b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/TestBme680Sensor.cs @@ -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; + } + } + } +} \ No newline at end of file From 0a5319d723f0644c3c94cfd81f2fda1b725c00e1 Mon Sep 17 00:00:00 2001 From: Denis-Cosmin Nutiu Date: Sat, 30 Nov 2019 18:58:27 +0200 Subject: [PATCH 3/3] Remove Sensor.cs --- NucuCar.Sensors/Sensor.cs | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 NucuCar.Sensors/Sensor.cs diff --git a/NucuCar.Sensors/Sensor.cs b/NucuCar.Sensors/Sensor.cs deleted file mode 100644 index ab61aaf..0000000 --- a/NucuCar.Sensors/Sensor.cs +++ /dev/null @@ -1,9 +0,0 @@ -using NucuCar.Domain.Telemetry; - -namespace NucuCar.Sensors -{ - public class Sensor : ISensor where T : class, ITelemeter - { - public T Object { get; } - } -} \ No newline at end of file