From 0bd282f5db79f9867e09abef4f4278b04de87ca5 Mon Sep 17 00:00:00 2001 From: Denis-Cosmin Nutiu Date: Sat, 1 Feb 2020 17:42:39 +0200 Subject: [PATCH] Add GrpcService for CpuTemperatureSensor --- NucuCar.Domain/Protos/NucuCarSensors.proto | 4 ++ NucuCar.Sensors/GrpcStartup.cs | 2 + NucuCar.Sensors/Health/CpuTempGrpcService.cs | 27 ++++++++++++++ NucuCar.TestClient/Sensors/SensorsCmd.cs | 39 ++++++++++++++++---- 4 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 NucuCar.Sensors/Health/CpuTempGrpcService.cs diff --git a/NucuCar.Domain/Protos/NucuCarSensors.proto b/NucuCar.Domain/Protos/NucuCarSensors.proto index 161e182..19c5778 100644 --- a/NucuCar.Domain/Protos/NucuCarSensors.proto +++ b/NucuCar.Domain/Protos/NucuCarSensors.proto @@ -17,6 +17,10 @@ service EnvironmentSensorGrpcService { rpc GetMeasurement(google.protobuf.Empty) returns (NucuCarSensorResponse) {} } +service HealthSensorGrpcService { + rpc GetCpuTemperature(google.protobuf.Empty) returns (NucuCarSensorResponse) {} +} + message NucuCarSensorResponse { SensorStateEnum State = 1; string JsonData = 2; diff --git a/NucuCar.Sensors/GrpcStartup.cs b/NucuCar.Sensors/GrpcStartup.cs index be29db1..cabe907 100644 --- a/NucuCar.Sensors/GrpcStartup.cs +++ b/NucuCar.Sensors/GrpcStartup.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using NucuCar.Sensors.Environment; +using NucuCar.Sensors.Health; namespace NucuCar.Sensors { @@ -31,6 +32,7 @@ namespace NucuCar.Sensors { // Add the gRPC services here. endpoints.MapGrpcService(); + endpoints.MapGrpcService(); endpoints.MapGet("/", async context => diff --git a/NucuCar.Sensors/Health/CpuTempGrpcService.cs b/NucuCar.Sensors/Health/CpuTempGrpcService.cs new file mode 100644 index 0000000..b300474 --- /dev/null +++ b/NucuCar.Sensors/Health/CpuTempGrpcService.cs @@ -0,0 +1,27 @@ +using System.Threading.Tasks; +using Google.Protobuf.WellKnownTypes; +using Grpc.Core; +using Microsoft.Extensions.Logging; +using NucuCarSensorsProto; + +namespace NucuCar.Sensors.Health +{ + public class CpuTempGrpcService : HealthSensorGrpcService.HealthSensorGrpcServiceBase + { + private readonly ILogger _logger; + private readonly ISensor _sensor; + + + public CpuTempGrpcService(ILogger logger, ISensor sensor) + { + _logger = logger; + _sensor = sensor; + } + + public override Task GetCpuTemperature(Empty request, ServerCallContext context) + { + _logger?.LogDebug($"Calling {nameof(GetCpuTemperature)}."); + return Task.FromResult(_sensor.Object.GetMeasurement()); + } + } +} \ No newline at end of file diff --git a/NucuCar.TestClient/Sensors/SensorsCmd.cs b/NucuCar.TestClient/Sensors/SensorsCmd.cs index 90b51bd..6c7a77a 100644 --- a/NucuCar.TestClient/Sensors/SensorsCmd.cs +++ b/NucuCar.TestClient/Sensors/SensorsCmd.cs @@ -42,6 +42,11 @@ namespace NucuCar.TestClient.Sensors await sensorsCommandLine.EnvironmentSensorGrpcServiceTest(); break; } + case "health": + { + await sensorsCommandLine.HealthSensorGrpcServiceTest(); + break; + } default: { throw new ArgumentException($"Invalid sensor name: ${options.SensorName}"); @@ -49,10 +54,12 @@ namespace NucuCar.TestClient.Sensors } } - public async Task EnvironmentSensorGrpcServiceTest() + private async Task HealthSensorGrpcServiceTest() { var cts = SetupCancellation(); - var client = SetupGrpc(); + var channel = SetupGrpc(); + var client = new HealthSensorGrpcService.HealthSensorGrpcServiceClient(channel); + while (true) { @@ -61,7 +68,27 @@ namespace NucuCar.TestClient.Sensors break; } - await Task.Delay(1000); + await Task.Delay(1000, cts.Token); + + var measurementJson = await client.GetCpuTemperatureAsync(new Empty()); + _logger.LogInformation("CpuTemperature: " + measurementJson.JsonData); + } + } + + public async Task EnvironmentSensorGrpcServiceTest() + { + var cts = SetupCancellation(); + var channel = SetupGrpc(); + var client = new EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceClient(channel); + + while (true) + { + if (cts.Token.IsCancellationRequested) + { + break; + } + + await Task.Delay(1000, cts.Token); var reply = await client.GetStateAsync(new Empty()); var state = reply.State; @@ -87,7 +114,7 @@ namespace NucuCar.TestClient.Sensors return cts; } - private EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceClient SetupGrpc() + private GrpcChannel SetupGrpc() { // Used to allow gRPC calls over unsecured HTTP. AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); @@ -103,9 +130,7 @@ namespace NucuCar.TestClient.Sensors var channel = GrpcChannel.ForAddress(GrpcServiceAddress, new GrpcChannelOptions {HttpClient = httpClient}); - var client = new EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceClient(channel); - - return client; + return channel; } } } \ No newline at end of file