2019-11-23 14:09:44 +00:00
|
|
|
// ReSharper disable UnusedAutoPropertyAccessor.Global
|
2019-11-30 13:58:19 +00:00
|
|
|
|
2019-11-17 14:33:24 +00:00
|
|
|
using System;
|
|
|
|
using System.Net.Http;
|
2019-11-30 13:58:19 +00:00
|
|
|
using System.Threading;
|
2019-11-17 14:33:24 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using CommandLine;
|
|
|
|
using Google.Protobuf.WellKnownTypes;
|
|
|
|
using Grpc.Net.Client;
|
2019-11-23 14:09:44 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2019-11-17 14:33:24 +00:00
|
|
|
using NucuCarSensorsProto;
|
|
|
|
|
2019-11-23 16:54:25 +00:00
|
|
|
namespace NucuCar.TestClient.Sensors
|
2019-11-17 14:33:24 +00:00
|
|
|
{
|
2019-11-23 14:09:44 +00:00
|
|
|
public class SensorsCmd
|
2019-11-17 14:33:24 +00:00
|
|
|
{
|
2019-11-23 14:09:44 +00:00
|
|
|
[Verb("sensors", HelpText = "Test the gRPC sensors services.")]
|
|
|
|
public class SensorsCmdOptions
|
|
|
|
{
|
|
|
|
[Option('u', "url", Required = false, HelpText = "The url and port of the gRPC server.",
|
|
|
|
Default = "https://localhost:8000")]
|
|
|
|
public string GrpcServiceAddress { get; set; }
|
|
|
|
}
|
2019-11-30 13:58:19 +00:00
|
|
|
|
2019-11-17 14:33:24 +00:00
|
|
|
public string GrpcServiceAddress { get; set; }
|
2019-11-23 14:09:44 +00:00
|
|
|
private static ILogger _logger;
|
2019-11-30 13:58:19 +00:00
|
|
|
|
2019-11-23 14:09:44 +00:00
|
|
|
public static async Task RunSensorsTestCommand(SensorsCmdOptions options)
|
2019-11-17 14:33:24 +00:00
|
|
|
{
|
2019-11-23 14:09:44 +00:00
|
|
|
_logger = LoggerFactory.Create(builder => { builder.AddConsole(); }).CreateLogger<SensorsCmd>();
|
|
|
|
var sensorsCommandLine = new SensorsCmd();
|
2019-11-17 14:33:24 +00:00
|
|
|
sensorsCommandLine.GrpcServiceAddress = options.GrpcServiceAddress;
|
|
|
|
|
|
|
|
await sensorsCommandLine.EnvironmentSensorGrpcServiceTest();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task EnvironmentSensorGrpcServiceTest()
|
|
|
|
{
|
|
|
|
// Used to allow gRPC calls over unsecured HTTP.
|
|
|
|
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
|
|
|
|
|
|
|
|
// Allow untrusted certificates.
|
2019-11-23 14:09:44 +00:00
|
|
|
var httpClientHandler = new HttpClientHandler
|
|
|
|
{
|
|
|
|
ServerCertificateCustomValidationCallback =
|
|
|
|
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
|
|
|
|
};
|
2019-11-17 14:33:24 +00:00
|
|
|
var httpClient = new HttpClient(httpClientHandler);
|
|
|
|
|
|
|
|
|
|
|
|
var channel = GrpcChannel.ForAddress(GrpcServiceAddress,
|
|
|
|
new GrpcChannelOptions {HttpClient = httpClient});
|
|
|
|
var client = new EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceClient(channel);
|
2019-11-30 13:58:19 +00:00
|
|
|
|
|
|
|
var cts = new CancellationTokenSource();
|
|
|
|
|
|
|
|
Console.CancelKeyPress += (s, e) =>
|
|
|
|
{
|
|
|
|
e.Cancel = true;
|
|
|
|
cts.Cancel();
|
|
|
|
Console.WriteLine("Shutting down...");
|
|
|
|
};
|
|
|
|
|
|
|
|
while (true)
|
2019-11-17 14:33:24 +00:00
|
|
|
{
|
2019-11-30 13:58:19 +00:00
|
|
|
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;
|
|
|
|
|
2019-11-17 14:33:24 +00:00
|
|
|
var measurement = await client.GetSensorMeasurementAsync(new Empty());
|
2019-11-23 14:09:44 +00:00
|
|
|
_logger.LogInformation(
|
2019-11-30 13:58:19 +00:00
|
|
|
$"ENVIRONMENT_SENSOR=temperature:{measurement.Temperature}|humidity:{measurement.Humidity}|" +
|
|
|
|
$"pressure:{measurement.Pressure}|voc:{measurement.VolatileOrganicCompound}|ts:{DateTime.Now}");
|
2019-11-17 14:33:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|