NucuCar: Add options for dissabling gRPC in NucuCar.Sensors
This commit is contained in:
parent
d9823f2af8
commit
1650b87e5b
11 changed files with 81 additions and 49 deletions
|
@ -9,23 +9,21 @@ enum SensorStateEnum {
|
|||
Uninitialized = 1;
|
||||
Initialized = 2;
|
||||
Disabled = 3;
|
||||
GrpcDisabled = 4;
|
||||
}
|
||||
|
||||
// Environment Sensor
|
||||
service EnvironmentSensorGrpcService {
|
||||
rpc GetState(google.protobuf.Empty) returns (NucuCarSensorState) {}
|
||||
rpc GetMeasurement(google.protobuf.Empty) returns (NucuCarSensorResponse) {}
|
||||
}
|
||||
|
||||
// Health Sensor
|
||||
service HealthSensorGrpcService {
|
||||
rpc GetCpuTemperature(google.protobuf.Empty) returns (NucuCarSensorResponse) {}
|
||||
}
|
||||
|
||||
// Responses
|
||||
message NucuCarSensorResponse {
|
||||
SensorStateEnum State = 1;
|
||||
string JsonData = 2;
|
||||
}
|
||||
|
||||
message NucuCarSensorState {
|
||||
SensorStateEnum state = 1;
|
||||
}
|
|
@ -6,5 +6,6 @@ namespace NucuCar.Sensors.Environment
|
|||
{
|
||||
public bool Enabled { get; set; }
|
||||
public bool Telemetry { get; set; }
|
||||
public bool Grpc { get; set; }
|
||||
}
|
||||
}
|
|
@ -2,6 +2,8 @@ using System.Threading.Tasks;
|
|||
using Google.Protobuf.WellKnownTypes;
|
||||
using Grpc.Core;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NucuCar.Sensors.Grpc;
|
||||
using NucuCarSensorsProto;
|
||||
|
||||
namespace NucuCar.Sensors.Environment
|
||||
|
@ -13,28 +15,26 @@ namespace NucuCar.Sensors.Environment
|
|||
public class Bme680GrpcService : EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceBase
|
||||
{
|
||||
private readonly ILogger<Bme680GrpcService> _logger;
|
||||
private readonly IOptions<Bme680Config> _options;
|
||||
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;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public override Task<NucuCarSensorState> GetState(Empty request, ServerCallContext context)
|
||||
{
|
||||
_logger?.LogDebug($"Calling {nameof(GetState)}.");
|
||||
return Task.FromResult(new NucuCarSensorState()
|
||||
{
|
||||
State = _bme680Sensor.Object.GetState()
|
||||
});
|
||||
_options = options;
|
||||
}
|
||||
|
||||
public override Task<NucuCarSensorResponse> GetMeasurement(Empty request,
|
||||
ServerCallContext context)
|
||||
{
|
||||
_logger?.LogDebug($"Calling {nameof(GetMeasurement)}.");
|
||||
if (_options.Value.Grpc)
|
||||
{
|
||||
return Task.FromResult(_bme680Sensor.Object.GetMeasurement());
|
||||
}
|
||||
|
||||
return Task.FromResult(Responses.GrpcIsDisabledResponse);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,7 +6,7 @@ using Microsoft.Extensions.Hosting;
|
|||
using NucuCar.Sensors.Environment;
|
||||
using NucuCar.Sensors.Health;
|
||||
|
||||
namespace NucuCar.Sensors
|
||||
namespace NucuCar.Sensors.Grpc
|
||||
{
|
||||
public class GrpcStartup
|
||||
{
|
13
NucuCar.Sensors/Grpc/Responses.cs
Normal file
13
NucuCar.Sensors/Grpc/Responses.cs
Normal 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 = "{}"
|
||||
};
|
||||
}
|
||||
}
|
|
@ -5,5 +5,6 @@ namespace NucuCar.Sensors.Health
|
|||
{
|
||||
public bool Enabled { get; set; }
|
||||
public bool Telemetry { get; set; }
|
||||
public bool Grpc { get; set; }
|
||||
}
|
||||
}
|
|
@ -2,6 +2,8 @@ using System.Threading.Tasks;
|
|||
using Google.Protobuf.WellKnownTypes;
|
||||
using Grpc.Core;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NucuCar.Sensors.Grpc;
|
||||
using NucuCarSensorsProto;
|
||||
|
||||
namespace NucuCar.Sensors.Health
|
||||
|
@ -10,18 +12,25 @@ namespace NucuCar.Sensors.Health
|
|||
{
|
||||
private readonly ILogger<CpuTempGrpcService> _logger;
|
||||
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;
|
||||
_sensor = sensor;
|
||||
_options = options;
|
||||
}
|
||||
|
||||
public override Task<NucuCarSensorResponse> GetCpuTemperature(Empty request, ServerCallContext context)
|
||||
{
|
||||
_logger?.LogDebug($"Calling {nameof(GetCpuTemperature)}.");
|
||||
if (_options.Value.Grpc)
|
||||
{
|
||||
return Task.FromResult(_sensor.Object.GetMeasurement());
|
||||
}
|
||||
|
||||
return Task.FromResult(Responses.GrpcIsDisabledResponse);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Hosting;
|
|||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using NucuCar.Sensors.Environment;
|
||||
using NucuCar.Sensors.Grpc;
|
||||
using NucuCar.Sensors.Health;
|
||||
using NucuCar.Sensors.Telemetry;
|
||||
|
||||
|
|
|
@ -7,11 +7,13 @@
|
|||
},
|
||||
"EnvironmentSensor": {
|
||||
"Enabled": true,
|
||||
"Telemetry": true
|
||||
"Telemetry": true,
|
||||
"Grpc": true
|
||||
},
|
||||
"HealthSensor": {
|
||||
"Enabled": true,
|
||||
"Telemetry": true
|
||||
"Telemetry": true,
|
||||
"Grpc": true
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
|
|
|
@ -71,6 +71,7 @@ namespace NucuCar.TestClient.Sensors
|
|||
await Task.Delay(1000, cts.Token);
|
||||
|
||||
var measurementJson = await client.GetCpuTemperatureAsync(new Empty());
|
||||
_logger.LogInformation("State: " + measurementJson.State);
|
||||
_logger.LogInformation("CpuTemperature: " + measurementJson.JsonData);
|
||||
}
|
||||
}
|
||||
|
@ -90,13 +91,8 @@ namespace NucuCar.TestClient.Sensors
|
|||
|
||||
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());
|
||||
_logger.LogInformation("State " + measurementJson.State);
|
||||
_logger.LogInformation(measurementJson.JsonData);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using NucuCar.Domain.Sensors;
|
||||
using NucuCar.Sensors;
|
||||
using NucuCar.Sensors.Environment;
|
||||
using NucuCarSensorsProto;
|
||||
|
@ -12,44 +12,55 @@ namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests
|
|||
{
|
||||
private readonly Mock<ILogger<Bme680GrpcService>> _mockLogger;
|
||||
private readonly Mock<ISensor<Bme680Sensor>> _mockSensor;
|
||||
private readonly Mock<IOptions<Bme680Config>> _mockOptions;
|
||||
private readonly Mock<TestBme680Sensor> _mockTestSensor;
|
||||
|
||||
public Bme680GrpcServiceTest()
|
||||
{
|
||||
_mockLogger = new Mock<ILogger<Bme680GrpcService>>();
|
||||
_mockSensor = new Mock<ISensor<Bme680Sensor>>();
|
||||
_mockOptions = new Mock<IOptions<Bme680Config>>();
|
||||
_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);
|
||||
}
|
||||
|
||||
|
||||
[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]
|
||||
public void Test_GetSensorMeasurement()
|
||||
{
|
||||
_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);
|
||||
|
||||
// Verify that the sensor get measurement method is called.
|
||||
_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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue