diff --git a/NucuCar.Domain/Protos/NucuCarSensors.proto b/NucuCar.Domain/Protos/NucuCarSensors.proto index 19c5778..55ec222 100644 --- a/NucuCar.Domain/Protos/NucuCarSensors.proto +++ b/NucuCar.Domain/Protos/NucuCarSensors.proto @@ -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; } \ No newline at end of file diff --git a/NucuCar.Sensors/Environment/Bme680Config.cs b/NucuCar.Sensors/Environment/Bme680Config.cs index 0105193..4c3b34b 100644 --- a/NucuCar.Sensors/Environment/Bme680Config.cs +++ b/NucuCar.Sensors/Environment/Bme680Config.cs @@ -6,5 +6,6 @@ namespace NucuCar.Sensors.Environment { public bool Enabled { get; set; } public bool Telemetry { get; set; } + public bool Grpc { get; set; } } } \ No newline at end of file diff --git a/NucuCar.Sensors/Environment/Bme680GrpcService.cs b/NucuCar.Sensors/Environment/Bme680GrpcService.cs index f01b792..d802a49 100644 --- a/NucuCar.Sensors/Environment/Bme680GrpcService.cs +++ b/NucuCar.Sensors/Environment/Bme680GrpcService.cs @@ -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 _logger; + private readonly IOptions _options; private readonly ISensor _bme680Sensor; - public Bme680GrpcService(ILogger logger, ISensor bme680Sensor) + public Bme680GrpcService(ILogger logger, ISensor bme680Sensor, IOptions options) { _bme680Sensor = bme680Sensor; _logger = logger; - } - - public override Task GetState(Empty request, ServerCallContext context) - { - _logger?.LogDebug($"Calling {nameof(GetState)}."); - return Task.FromResult(new NucuCarSensorState() - { - State = _bme680Sensor.Object.GetState() - }); + _options = options; } public override Task GetMeasurement(Empty request, ServerCallContext context) { _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); } } } \ No newline at end of file diff --git a/NucuCar.Sensors/GrpcStartup.cs b/NucuCar.Sensors/Grpc/GrpcStartup.cs similarity index 98% rename from NucuCar.Sensors/GrpcStartup.cs rename to NucuCar.Sensors/Grpc/GrpcStartup.cs index cabe907..afef179 100644 --- a/NucuCar.Sensors/GrpcStartup.cs +++ b/NucuCar.Sensors/Grpc/GrpcStartup.cs @@ -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 { diff --git a/NucuCar.Sensors/Grpc/Responses.cs b/NucuCar.Sensors/Grpc/Responses.cs new file mode 100644 index 0000000..a85d5f7 --- /dev/null +++ b/NucuCar.Sensors/Grpc/Responses.cs @@ -0,0 +1,13 @@ +using NucuCarSensorsProto; + +namespace NucuCar.Sensors.Grpc +{ + public static class Responses + { + public static NucuCarSensorResponse GrpcIsDisabledResponse = new NucuCarSensorResponse() + { + State = SensorStateEnum.GrpcDisabled, + JsonData = "{}" + }; + } +} \ No newline at end of file diff --git a/NucuCar.Sensors/Health/CpuTempConfig.cs b/NucuCar.Sensors/Health/CpuTempConfig.cs index 6684a21..554cf7e 100644 --- a/NucuCar.Sensors/Health/CpuTempConfig.cs +++ b/NucuCar.Sensors/Health/CpuTempConfig.cs @@ -5,5 +5,6 @@ namespace NucuCar.Sensors.Health { public bool Enabled { get; set; } public bool Telemetry { get; set; } + public bool Grpc { get; set; } } } \ No newline at end of file diff --git a/NucuCar.Sensors/Health/CpuTempGrpcService.cs b/NucuCar.Sensors/Health/CpuTempGrpcService.cs index b300474..21a0b32 100644 --- a/NucuCar.Sensors/Health/CpuTempGrpcService.cs +++ b/NucuCar.Sensors/Health/CpuTempGrpcService.cs @@ -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 _logger; private readonly ISensor _sensor; + private readonly IOptions _options; - - public CpuTempGrpcService(ILogger logger, ISensor sensor) + public CpuTempGrpcService(ILogger logger, ISensor sensor, + IOptions options) { _logger = logger; _sensor = sensor; + _options = options; } - + public override Task GetCpuTemperature(Empty request, ServerCallContext context) { _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); } } } \ No newline at end of file diff --git a/NucuCar.Sensors/Program.cs b/NucuCar.Sensors/Program.cs index f8ea359..4be4a44 100644 --- a/NucuCar.Sensors/Program.cs +++ b/NucuCar.Sensors/Program.cs @@ -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; diff --git a/NucuCar.Sensors/appsettings.json b/NucuCar.Sensors/appsettings.json index e5ba70c..aa114ce 100644 --- a/NucuCar.Sensors/appsettings.json +++ b/NucuCar.Sensors/appsettings.json @@ -7,11 +7,13 @@ }, "EnvironmentSensor": { "Enabled": true, - "Telemetry": true + "Telemetry": true, + "Grpc": true }, "HealthSensor": { "Enabled": true, - "Telemetry": true + "Telemetry": true, + "Grpc": true }, "Logging": { "LogLevel": { diff --git a/NucuCar.TestClient/Sensors/SensorsCmd.cs b/NucuCar.TestClient/Sensors/SensorsCmd.cs index 6c7a77a..ae35520 100644 --- a/NucuCar.TestClient/Sensors/SensorsCmd.cs +++ b/NucuCar.TestClient/Sensors/SensorsCmd.cs @@ -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); } } @@ -89,14 +90,9 @@ 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); } } diff --git a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680GrpcServiceTest.cs b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680GrpcServiceTest.cs index c12557d..57d23e0 100644 --- a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680GrpcServiceTest.cs +++ b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680GrpcServiceTest.cs @@ -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> _mockLogger; private readonly Mock> _mockSensor; + private readonly Mock> _mockOptions; private readonly Mock _mockTestSensor; public Bme680GrpcServiceTest() { _mockLogger = new Mock>(); _mockSensor = new Mock>(); + _mockOptions = new Mock>(); _mockTestSensor = new Mock(); + + _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>(); + 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); } } } \ No newline at end of file