NucuCar: Add options for dissabling gRPC in NucuCar.Sensors

This commit is contained in:
Denis-Cosmin Nutiu 2020-02-02 16:19:53 +02:00
parent d9823f2af8
commit 1650b87e5b
11 changed files with 81 additions and 49 deletions

View file

@ -9,23 +9,21 @@ enum SensorStateEnum {
Uninitialized = 1; Uninitialized = 1;
Initialized = 2; Initialized = 2;
Disabled = 3; Disabled = 3;
GrpcDisabled = 4;
} }
// Environment Sensor // Environment Sensor
service EnvironmentSensorGrpcService { service EnvironmentSensorGrpcService {
rpc GetState(google.protobuf.Empty) returns (NucuCarSensorState) {}
rpc GetMeasurement(google.protobuf.Empty) returns (NucuCarSensorResponse) {} rpc GetMeasurement(google.protobuf.Empty) returns (NucuCarSensorResponse) {}
} }
// Health Sensor
service HealthSensorGrpcService { service HealthSensorGrpcService {
rpc GetCpuTemperature(google.protobuf.Empty) returns (NucuCarSensorResponse) {} rpc GetCpuTemperature(google.protobuf.Empty) returns (NucuCarSensorResponse) {}
} }
// Responses
message NucuCarSensorResponse { message NucuCarSensorResponse {
SensorStateEnum State = 1; SensorStateEnum State = 1;
string JsonData = 2; string JsonData = 2;
}
message NucuCarSensorState {
SensorStateEnum state = 1;
} }

View file

@ -6,5 +6,6 @@ namespace NucuCar.Sensors.Environment
{ {
public bool Enabled { get; set; } public bool Enabled { get; set; }
public bool Telemetry { get; set; } public bool Telemetry { get; set; }
public bool Grpc { get; set; }
} }
} }

View file

@ -2,6 +2,8 @@ using System.Threading.Tasks;
using Google.Protobuf.WellKnownTypes; using Google.Protobuf.WellKnownTypes;
using Grpc.Core; using Grpc.Core;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NucuCar.Sensors.Grpc;
using NucuCarSensorsProto; using NucuCarSensorsProto;
namespace NucuCar.Sensors.Environment namespace NucuCar.Sensors.Environment
@ -13,28 +15,26 @@ namespace NucuCar.Sensors.Environment
public class Bme680GrpcService : EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceBase public class Bme680GrpcService : EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceBase
{ {
private readonly ILogger<Bme680GrpcService> _logger; private readonly ILogger<Bme680GrpcService> _logger;
private readonly IOptions<Bme680Config> _options;
private readonly ISensor<Bme680Sensor> _bme680Sensor; private readonly ISensor<Bme680Sensor> _bme680Sensor;
public Bme680GrpcService(ILogger<Bme680GrpcService> logger, ISensor<Bme680Sensor> bme680Sensor) public Bme680GrpcService(ILogger<Bme680GrpcService> logger, ISensor<Bme680Sensor> bme680Sensor, IOptions<Bme680Config> options)
{ {
_bme680Sensor = bme680Sensor; _bme680Sensor = bme680Sensor;
_logger = logger; _logger = logger;
} _options = options;
public override Task<NucuCarSensorState> GetState(Empty request, ServerCallContext context)
{
_logger?.LogDebug($"Calling {nameof(GetState)}.");
return Task.FromResult(new NucuCarSensorState()
{
State = _bme680Sensor.Object.GetState()
});
} }
public override Task<NucuCarSensorResponse> GetMeasurement(Empty request, public override Task<NucuCarSensorResponse> GetMeasurement(Empty request,
ServerCallContext context) ServerCallContext context)
{ {
_logger?.LogDebug($"Calling {nameof(GetMeasurement)}."); _logger?.LogDebug($"Calling {nameof(GetMeasurement)}.");
return Task.FromResult(_bme680Sensor.Object.GetMeasurement()); if (_options.Value.Grpc)
{
return Task.FromResult(_bme680Sensor.Object.GetMeasurement());
}
return Task.FromResult(Responses.GrpcIsDisabledResponse);
} }
} }
} }

View file

@ -6,7 +6,7 @@ using Microsoft.Extensions.Hosting;
using NucuCar.Sensors.Environment; using NucuCar.Sensors.Environment;
using NucuCar.Sensors.Health; using NucuCar.Sensors.Health;
namespace NucuCar.Sensors namespace NucuCar.Sensors.Grpc
{ {
public class GrpcStartup public class GrpcStartup
{ {

View file

@ -0,0 +1,13 @@
using NucuCarSensorsProto;
namespace NucuCar.Sensors.Grpc
{
public static class Responses
{
public static NucuCarSensorResponse GrpcIsDisabledResponse = new NucuCarSensorResponse()
{
State = SensorStateEnum.GrpcDisabled,
JsonData = "{}"
};
}
}

View file

@ -5,5 +5,6 @@ namespace NucuCar.Sensors.Health
{ {
public bool Enabled { get; set; } public bool Enabled { get; set; }
public bool Telemetry { get; set; } public bool Telemetry { get; set; }
public bool Grpc { get; set; }
} }
} }

View file

@ -2,6 +2,8 @@ using System.Threading.Tasks;
using Google.Protobuf.WellKnownTypes; using Google.Protobuf.WellKnownTypes;
using Grpc.Core; using Grpc.Core;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NucuCar.Sensors.Grpc;
using NucuCarSensorsProto; using NucuCarSensorsProto;
namespace NucuCar.Sensors.Health namespace NucuCar.Sensors.Health
@ -10,18 +12,25 @@ namespace NucuCar.Sensors.Health
{ {
private readonly ILogger<CpuTempGrpcService> _logger; private readonly ILogger<CpuTempGrpcService> _logger;
private readonly ISensor<CpuTempSensor> _sensor; private readonly ISensor<CpuTempSensor> _sensor;
private readonly IOptions<CpuTempConfig> _options;
public CpuTempGrpcService(ILogger<CpuTempGrpcService> logger, ISensor<CpuTempSensor> sensor,
public CpuTempGrpcService(ILogger<CpuTempGrpcService> logger, ISensor<CpuTempSensor> sensor) IOptions<CpuTempConfig> options)
{ {
_logger = logger; _logger = logger;
_sensor = sensor; _sensor = sensor;
_options = options;
} }
public override Task<NucuCarSensorResponse> GetCpuTemperature(Empty request, ServerCallContext context) public override Task<NucuCarSensorResponse> GetCpuTemperature(Empty request, ServerCallContext context)
{ {
_logger?.LogDebug($"Calling {nameof(GetCpuTemperature)}."); _logger?.LogDebug($"Calling {nameof(GetCpuTemperature)}.");
return Task.FromResult(_sensor.Object.GetMeasurement()); if (_options.Value.Grpc)
{
return Task.FromResult(_sensor.Object.GetMeasurement());
}
return Task.FromResult(Responses.GrpcIsDisabledResponse);
} }
} }
} }

View file

@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using NucuCar.Sensors.Environment; using NucuCar.Sensors.Environment;
using NucuCar.Sensors.Grpc;
using NucuCar.Sensors.Health; using NucuCar.Sensors.Health;
using NucuCar.Sensors.Telemetry; using NucuCar.Sensors.Telemetry;

View file

@ -7,11 +7,13 @@
}, },
"EnvironmentSensor": { "EnvironmentSensor": {
"Enabled": true, "Enabled": true,
"Telemetry": true "Telemetry": true,
"Grpc": true
}, },
"HealthSensor": { "HealthSensor": {
"Enabled": true, "Enabled": true,
"Telemetry": true "Telemetry": true,
"Grpc": true
}, },
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {

View file

@ -71,6 +71,7 @@ namespace NucuCar.TestClient.Sensors
await Task.Delay(1000, cts.Token); await Task.Delay(1000, cts.Token);
var measurementJson = await client.GetCpuTemperatureAsync(new Empty()); var measurementJson = await client.GetCpuTemperatureAsync(new Empty());
_logger.LogInformation("State: " + measurementJson.State);
_logger.LogInformation("CpuTemperature: " + measurementJson.JsonData); _logger.LogInformation("CpuTemperature: " + measurementJson.JsonData);
} }
} }
@ -89,14 +90,9 @@ namespace NucuCar.TestClient.Sensors
} }
await Task.Delay(1000, cts.Token); await Task.Delay(1000, cts.Token);
var reply = await client.GetStateAsync(new Empty());
var state = reply.State;
_logger.LogInformation("EnvironmentSensorState: " + state);
if (state != SensorStateEnum.Initialized) continue;
var measurementJson = await client.GetMeasurementAsync(new Empty()); var measurementJson = await client.GetMeasurementAsync(new Empty());
_logger.LogInformation("State " + measurementJson.State);
_logger.LogInformation(measurementJson.JsonData); _logger.LogInformation(measurementJson.JsonData);
} }
} }

View file

@ -1,6 +1,6 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq; using Moq;
using NucuCar.Domain.Sensors;
using NucuCar.Sensors; using NucuCar.Sensors;
using NucuCar.Sensors.Environment; using NucuCar.Sensors.Environment;
using NucuCarSensorsProto; using NucuCarSensorsProto;
@ -12,44 +12,55 @@ namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests
{ {
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<IOptions<Bme680Config>> _mockOptions;
private readonly Mock<TestBme680Sensor> _mockTestSensor; private readonly Mock<TestBme680Sensor> _mockTestSensor;
public Bme680GrpcServiceTest() public Bme680GrpcServiceTest()
{ {
_mockLogger = new Mock<ILogger<Bme680GrpcService>>(); _mockLogger = new Mock<ILogger<Bme680GrpcService>>();
_mockSensor = new Mock<ISensor<Bme680Sensor>>(); _mockSensor = new Mock<ISensor<Bme680Sensor>>();
_mockOptions = new Mock<IOptions<Bme680Config>>();
_mockTestSensor = new Mock<TestBme680Sensor>(); _mockTestSensor = new Mock<TestBme680Sensor>();
_mockOptions.Setup(mo => mo.Value).Returns(new Bme680Config()
{
Grpc = true,
Telemetry = true,
Enabled = true
});
_mockSensor.Setup(ms => ms.Object).Returns(_mockTestSensor.Object); _mockSensor.Setup(ms => ms.Object).Returns(_mockTestSensor.Object);
} }
[Fact]
public void Test_GetSensorState()
{
var service = new Bme680GrpcService(_mockLogger.Object, _mockSensor.Object);
var result = service.GetState(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.GetState(null, null).Result;
Assert.Equal(SensorStateEnum.Initialized, result.State);
}
[Fact] [Fact]
public void Test_GetSensorMeasurement() public void Test_GetSensorMeasurement()
{ {
_mockTestSensor.Setup(s => s.GetMeasurement()).Returns(new NucuCarSensorResponse()); _mockTestSensor.Setup(s => s.GetMeasurement()).Returns(new NucuCarSensorResponse());
var service = new Bme680GrpcService(_mockLogger.Object, _mockSensor.Object); var service = new Bme680GrpcService(_mockLogger.Object, _mockSensor.Object, _mockOptions.Object);
service.GetMeasurement(null, null); service.GetMeasurement(null, null);
// Verify that the sensor get measurement method is called. // Verify that the sensor get measurement method is called.
_mockSensor.Verify(s => s.Object.GetMeasurement(), Times.AtLeastOnce()); _mockSensor.Verify(s => s.Object.GetMeasurement(), Times.AtLeastOnce());
}
[Fact]
public void Test_GetSensorMeasurement_Disabled()
{
_mockTestSensor.Setup(s => s.GetMeasurement()).Returns(new NucuCarSensorResponse());
var options = new Mock<IOptions<Bme680Config>>();
options.Setup(o => o.Value).Returns(new Bme680Config
{
Enabled = true,
Telemetry = true,
Grpc = false
});
var service = new Bme680GrpcService(_mockLogger.Object, _mockSensor.Object, options.Object);
var result = service.GetMeasurement(null, null);
// Verify that the sensor get measurement method is not called.
_mockSensor.Verify(s => s.Object.GetMeasurement(), Times.Never());
Assert.Equal(SensorStateEnum.GrpcDisabled, result.Result.State);
} }
} }
} }