NucuCar.TestClient: add command option sensor
This commit is contained in:
parent
ec60ba588b
commit
2f6a97cf86
1 changed files with 55 additions and 28 deletions
|
@ -8,7 +8,6 @@ using CommandLine;
|
|||
using Google.Protobuf.WellKnownTypes;
|
||||
using Grpc.Net.Client;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using NucuCarSensorsProto;
|
||||
|
||||
namespace NucuCar.TestClient.Sensors
|
||||
|
@ -21,6 +20,10 @@ namespace NucuCar.TestClient.Sensors
|
|||
[Option('u', "url", Required = false, HelpText = "The url and port of the gRPC server.",
|
||||
Default = "https://localhost:8000")]
|
||||
public string GrpcServiceAddress { get; set; }
|
||||
|
||||
[Option('s', "sensor", Required = false, HelpText = "The sensor name you'd like to test.",
|
||||
Default = "environment")]
|
||||
public string SensorName { get; set; }
|
||||
}
|
||||
|
||||
public string GrpcServiceAddress { get; set; }
|
||||
|
@ -32,10 +35,59 @@ namespace NucuCar.TestClient.Sensors
|
|||
var sensorsCommandLine = new SensorsCmd();
|
||||
sensorsCommandLine.GrpcServiceAddress = options.GrpcServiceAddress;
|
||||
|
||||
switch (options.SensorName)
|
||||
{
|
||||
case "environment":
|
||||
{
|
||||
await sensorsCommandLine.EnvironmentSensorGrpcServiceTest();
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new ArgumentException($"Invalid sensor name: ${options.SensorName}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task EnvironmentSensorGrpcServiceTest()
|
||||
{
|
||||
var cts = SetupCancellation();
|
||||
var client = SetupGrpc();
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (cts.Token.IsCancellationRequested)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
await Task.Delay(1000);
|
||||
|
||||
var reply = await client.GetStateAsync(new Empty());
|
||||
var state = reply.State;
|
||||
|
||||
_logger.LogInformation("EnvironmentSensorState: " + state);
|
||||
if (state != SensorStateEnum.Initialized) continue;
|
||||
|
||||
var measurementJson = await client.GetMeasurementAsync(new Empty());
|
||||
_logger.LogInformation(measurementJson.JsonData);
|
||||
}
|
||||
}
|
||||
|
||||
private static CancellationTokenSource SetupCancellation()
|
||||
{
|
||||
var cts = new CancellationTokenSource();
|
||||
|
||||
Console.CancelKeyPress += (s, e) =>
|
||||
{
|
||||
e.Cancel = true;
|
||||
cts.Cancel();
|
||||
Console.WriteLine("Shutting down...");
|
||||
};
|
||||
return cts;
|
||||
}
|
||||
|
||||
private EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceClient SetupGrpc()
|
||||
{
|
||||
// Used to allow gRPC calls over unsecured HTTP.
|
||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
|
||||
|
@ -53,32 +105,7 @@ namespace NucuCar.TestClient.Sensors
|
|||
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.GetStateAsync(new Empty());
|
||||
var state = reply.State;
|
||||
|
||||
_logger.LogInformation("EnvironmentSensorState: " + state);
|
||||
if (state != SensorStateEnum.Initialized) continue;
|
||||
|
||||
var measurementJson = await client.GetMeasurementAsync(new Empty());
|
||||
_logger.LogInformation(measurementJson.JsonData);
|
||||
}
|
||||
return client;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue