From 73bae4046f972454e10fdf3e7ac14484f2323e59 Mon Sep 17 00:00:00 2001 From: Denis-Cosmin Nutiu Date: Sat, 30 Nov 2019 15:58:19 +0200 Subject: [PATCH] Modify NucuCar.TestClient SensorsCmd to read continously --- NucuCar.TestClient/Sensors/SensorsCmd.cs | 38 ++++++++++++++++++------ 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/NucuCar.TestClient/Sensors/SensorsCmd.cs b/NucuCar.TestClient/Sensors/SensorsCmd.cs index 205fc18..b352495 100644 --- a/NucuCar.TestClient/Sensors/SensorsCmd.cs +++ b/NucuCar.TestClient/Sensors/SensorsCmd.cs @@ -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; @@ -19,10 +21,10 @@ namespace NucuCar.TestClient.Sensors Default = "https://localhost:8000")] public string GrpcServiceAddress { get; set; } } - + public string GrpcServiceAddress { get; set; } private static ILogger _logger; - + public static async Task RunSensorsTestCommand(SensorsCmdOptions options) { _logger = LoggerFactory.Create(builder => { builder.AddConsole(); }).CreateLogger(); @@ -49,17 +51,35 @@ namespace NucuCar.TestClient.Sensors var channel = GrpcChannel.ForAddress(GrpcServiceAddress, new GrpcChannelOptions {HttpClient = httpClient}); var client = new EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceClient(channel); - var reply = await client.GetSensorStateAsync(new Empty()); - var state = reply.State; - _logger.LogInformation("EnvironmentSensorState: " + state); - if (state == SensorStateEnum.Initialized) + + 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) 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"); } } } \ No newline at end of file