Modify NucuCar.TestClient SensorsCmd to read continously

This commit is contained in:
Denis-Cosmin Nutiu 2019-11-30 15:58:19 +02:00
parent fb777b4769
commit 73bae4046f

View file

@ -1,6 +1,8 @@
// ReSharper disable UnusedAutoPropertyAccessor.Global // ReSharper disable UnusedAutoPropertyAccessor.Global
using System; using System;
using System.Net.Http; using System.Net.Http;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using CommandLine; using CommandLine;
using Google.Protobuf.WellKnownTypes; using Google.Protobuf.WellKnownTypes;
@ -19,10 +21,10 @@ namespace NucuCar.TestClient.Sensors
Default = "https://localhost:8000")] Default = "https://localhost:8000")]
public string GrpcServiceAddress { get; set; } public string GrpcServiceAddress { get; set; }
} }
public string GrpcServiceAddress { get; set; } public string GrpcServiceAddress { get; set; }
private static ILogger _logger; private static ILogger _logger;
public static async Task RunSensorsTestCommand(SensorsCmdOptions options) public static async Task RunSensorsTestCommand(SensorsCmdOptions options)
{ {
_logger = LoggerFactory.Create(builder => { builder.AddConsole(); }).CreateLogger<SensorsCmd>(); _logger = LoggerFactory.Create(builder => { builder.AddConsole(); }).CreateLogger<SensorsCmd>();
@ -49,17 +51,35 @@ 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); var client = new EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceClient(channel);
var reply = await client.GetSensorStateAsync(new Empty());
var state = reply.State; var cts = new CancellationTokenSource();
_logger.LogInformation("EnvironmentSensorState: " + state);
if (state == SensorStateEnum.Initialized) Console.CancelKeyPress += (s, e) =>
{ {
e.Cancel = true;
cts.Cancel();
Console.WriteLine("Shutting down...");
};
while (true)
{
if (cts.Token.IsCancellationRequested)
{
break;
}
await Task.Delay(1000);
var reply = await client.GetSensorStateAsync(new Empty());
var state = reply.State;
_logger.LogInformation("EnvironmentSensorState: " + state);
if (state != SensorStateEnum.Initialized) continue;
var measurement = await client.GetSensorMeasurementAsync(new Empty()); var measurement = await client.GetSensorMeasurementAsync(new Empty());
_logger.LogInformation( _logger.LogInformation(
$"t: {measurement.Temperature} | h: {measurement.Humidity} | p: {measurement.Pressure}"); $"ENVIRONMENT_SENSOR=temperature:{measurement.Temperature}|humidity:{measurement.Humidity}|" +
$"pressure:{measurement.Pressure}|voc:{measurement.VolatileOrganicCompound}|ts:{DateTime.Now}");
} }
_logger.LogInformation("Done");
} }
} }
} }