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
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using CommandLine;
using Google.Protobuf.WellKnownTypes;
@ -49,17 +51,35 @@ namespace NucuCar.TestClient.Sensors
var channel = GrpcChannel.ForAddress(GrpcServiceAddress,
new GrpcChannelOptions {HttpClient = httpClient});
var client = new EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceClient(channel);
var cts = new CancellationTokenSource();
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)
{
if (state != SensorStateEnum.Initialized) continue;
var measurement = await client.GetSensorMeasurementAsync(new Empty());
_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");
}
}
}