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.",
|
2020-08-09 09:49:36 +00:00
|
|
|
Default = "http://localhost:8000")]
|
2019-11-23 14:09:44 +00:00
|
|
|
public string GrpcServiceAddress { get; set; }
|
2020-02-01 14:59:07 +00:00
|
|
|
|
|
|
|
[Option('s', "sensor", Required = false, HelpText = "The sensor name you'd like to test.",
|
|
|
|
Default = "environment")]
|
|
|
|
public string SensorName { get; set; }
|
2019-11-23 14:09:44 +00:00
|
|
|
}
|
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;
|
|
|
|
|
2020-02-01 14:59:07 +00:00
|
|
|
switch (options.SensorName)
|
|
|
|
{
|
|
|
|
case "environment":
|
|
|
|
{
|
|
|
|
await sensorsCommandLine.EnvironmentSensorGrpcServiceTest();
|
|
|
|
break;
|
|
|
|
}
|
2020-02-01 15:42:39 +00:00
|
|
|
case "health":
|
|
|
|
{
|
|
|
|
await sensorsCommandLine.HealthSensorGrpcServiceTest();
|
|
|
|
break;
|
|
|
|
}
|
2020-02-01 14:59:07 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
throw new ArgumentException($"Invalid sensor name: ${options.SensorName}");
|
|
|
|
}
|
|
|
|
}
|
2019-11-17 14:33:24 +00:00
|
|
|
}
|
|
|
|
|
2020-02-01 15:42:39 +00:00
|
|
|
private async Task HealthSensorGrpcServiceTest()
|
|
|
|
{
|
|
|
|
var cts = SetupCancellation();
|
|
|
|
var channel = SetupGrpc();
|
|
|
|
var client = new HealthSensorGrpcService.HealthSensorGrpcServiceClient(channel);
|
|
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (cts.Token.IsCancellationRequested)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
await Task.Delay(1000, cts.Token);
|
|
|
|
|
|
|
|
var measurementJson = await client.GetCpuTemperatureAsync(new Empty());
|
2020-02-02 14:19:53 +00:00
|
|
|
_logger.LogInformation("State: " + measurementJson.State);
|
2020-02-01 15:42:39 +00:00
|
|
|
_logger.LogInformation("CpuTemperature: " + measurementJson.JsonData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-17 14:33:24 +00:00
|
|
|
public async Task EnvironmentSensorGrpcServiceTest()
|
|
|
|
{
|
2020-02-01 14:59:07 +00:00
|
|
|
var cts = SetupCancellation();
|
2020-02-01 15:42:39 +00:00
|
|
|
var channel = SetupGrpc();
|
|
|
|
var client = new EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceClient(channel);
|
2019-11-30 13:58:19 +00:00
|
|
|
|
|
|
|
while (true)
|
2019-11-17 14:33:24 +00:00
|
|
|
{
|
2019-11-30 13:58:19 +00:00
|
|
|
if (cts.Token.IsCancellationRequested)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2020-02-01 14:59:07 +00:00
|
|
|
|
2020-02-01 15:42:39 +00:00
|
|
|
await Task.Delay(1000, cts.Token);
|
2020-02-02 14:19:53 +00:00
|
|
|
|
2019-12-06 22:31:28 +00:00
|
|
|
var measurementJson = await client.GetMeasurementAsync(new Empty());
|
2020-02-02 14:19:53 +00:00
|
|
|
_logger.LogInformation("State " + measurementJson.State);
|
2019-12-06 22:31:28 +00:00
|
|
|
_logger.LogInformation(measurementJson.JsonData);
|
2019-11-17 14:33:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-01 14:59:07 +00:00
|
|
|
|
|
|
|
private static CancellationTokenSource SetupCancellation()
|
|
|
|
{
|
|
|
|
var cts = new CancellationTokenSource();
|
|
|
|
|
|
|
|
Console.CancelKeyPress += (s, e) =>
|
|
|
|
{
|
|
|
|
e.Cancel = true;
|
|
|
|
cts.Cancel();
|
|
|
|
Console.WriteLine("Shutting down...");
|
|
|
|
};
|
|
|
|
return cts;
|
|
|
|
}
|
|
|
|
|
2020-02-01 15:42:39 +00:00
|
|
|
private GrpcChannel SetupGrpc()
|
2020-02-01 14:59:07 +00:00
|
|
|
{
|
|
|
|
// Used to allow gRPC calls over unsecured HTTP.
|
|
|
|
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
|
|
|
|
|
|
|
|
// Allow untrusted certificates.
|
|
|
|
var httpClientHandler = new HttpClientHandler
|
|
|
|
{
|
|
|
|
ServerCertificateCustomValidationCallback =
|
|
|
|
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
|
|
|
|
};
|
|
|
|
var httpClient = new HttpClient(httpClientHandler);
|
|
|
|
|
|
|
|
|
|
|
|
var channel = GrpcChannel.ForAddress(GrpcServiceAddress,
|
|
|
|
new GrpcChannelOptions {HttpClient = httpClient});
|
2020-02-01 15:42:39 +00:00
|
|
|
return channel;
|
2020-02-01 14:59:07 +00:00
|
|
|
}
|
2019-11-17 14:33:24 +00:00
|
|
|
}
|
|
|
|
}
|