Add GrpcService for CpuTemperatureSensor
This commit is contained in:
parent
1130464544
commit
0bd282f5db
4 changed files with 65 additions and 7 deletions
|
@ -17,6 +17,10 @@ service EnvironmentSensorGrpcService {
|
||||||
rpc GetMeasurement(google.protobuf.Empty) returns (NucuCarSensorResponse) {}
|
rpc GetMeasurement(google.protobuf.Empty) returns (NucuCarSensorResponse) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
service HealthSensorGrpcService {
|
||||||
|
rpc GetCpuTemperature(google.protobuf.Empty) returns (NucuCarSensorResponse) {}
|
||||||
|
}
|
||||||
|
|
||||||
message NucuCarSensorResponse {
|
message NucuCarSensorResponse {
|
||||||
SensorStateEnum State = 1;
|
SensorStateEnum State = 1;
|
||||||
string JsonData = 2;
|
string JsonData = 2;
|
||||||
|
|
|
@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Http;
|
||||||
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.Health;
|
||||||
|
|
||||||
namespace NucuCar.Sensors
|
namespace NucuCar.Sensors
|
||||||
{
|
{
|
||||||
|
@ -31,6 +32,7 @@ namespace NucuCar.Sensors
|
||||||
{
|
{
|
||||||
// Add the gRPC services here.
|
// Add the gRPC services here.
|
||||||
endpoints.MapGrpcService<Bme680GrpcService>();
|
endpoints.MapGrpcService<Bme680GrpcService>();
|
||||||
|
endpoints.MapGrpcService<CpuTempGrpcService>();
|
||||||
|
|
||||||
endpoints.MapGet("/",
|
endpoints.MapGet("/",
|
||||||
async context =>
|
async context =>
|
||||||
|
|
27
NucuCar.Sensors/Health/CpuTempGrpcService.cs
Normal file
27
NucuCar.Sensors/Health/CpuTempGrpcService.cs
Normal file
|
@ -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<CpuTempGrpcService> _logger;
|
||||||
|
private readonly ISensor<CpuTempSensor> _sensor;
|
||||||
|
|
||||||
|
|
||||||
|
public CpuTempGrpcService(ILogger<CpuTempGrpcService> logger, ISensor<CpuTempSensor> sensor)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_sensor = sensor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override Task<NucuCarSensorResponse> GetCpuTemperature(Empty request, ServerCallContext context)
|
||||||
|
{
|
||||||
|
_logger?.LogDebug($"Calling {nameof(GetCpuTemperature)}.");
|
||||||
|
return Task.FromResult(_sensor.Object.GetMeasurement());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -42,6 +42,11 @@ namespace NucuCar.TestClient.Sensors
|
||||||
await sensorsCommandLine.EnvironmentSensorGrpcServiceTest();
|
await sensorsCommandLine.EnvironmentSensorGrpcServiceTest();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case "health":
|
||||||
|
{
|
||||||
|
await sensorsCommandLine.HealthSensorGrpcServiceTest();
|
||||||
|
break;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
throw new ArgumentException($"Invalid sensor name: ${options.SensorName}");
|
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 cts = SetupCancellation();
|
||||||
var client = SetupGrpc();
|
var channel = SetupGrpc();
|
||||||
|
var client = new HealthSensorGrpcService.HealthSensorGrpcServiceClient(channel);
|
||||||
|
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
@ -61,7 +68,27 @@ namespace NucuCar.TestClient.Sensors
|
||||||
break;
|
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 reply = await client.GetStateAsync(new Empty());
|
||||||
var state = reply.State;
|
var state = reply.State;
|
||||||
|
@ -87,7 +114,7 @@ namespace NucuCar.TestClient.Sensors
|
||||||
return cts;
|
return cts;
|
||||||
}
|
}
|
||||||
|
|
||||||
private EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceClient SetupGrpc()
|
private GrpcChannel SetupGrpc()
|
||||||
{
|
{
|
||||||
// Used to allow gRPC calls over unsecured HTTP.
|
// Used to allow gRPC calls over unsecured HTTP.
|
||||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
|
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
|
||||||
|
@ -103,9 +130,7 @@ namespace NucuCar.TestClient.Sensors
|
||||||
|
|
||||||
var channel = GrpcChannel.ForAddress(GrpcServiceAddress,
|
var channel = GrpcChannel.ForAddress(GrpcServiceAddress,
|
||||||
new GrpcChannelOptions {HttpClient = httpClient});
|
new GrpcChannelOptions {HttpClient = httpClient});
|
||||||
var client = new EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceClient(channel);
|
return channel;
|
||||||
|
|
||||||
return client;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue