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 Google.Protobuf.WellKnownTypes;
|
||||||
using Grpc.Net.Client;
|
using Grpc.Net.Client;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Newtonsoft.Json;
|
|
||||||
using NucuCarSensorsProto;
|
using NucuCarSensorsProto;
|
||||||
|
|
||||||
namespace NucuCar.TestClient.Sensors
|
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.",
|
[Option('u', "url", Required = false, HelpText = "The url and port of the gRPC server.",
|
||||||
Default = "https://localhost:8000")]
|
Default = "https://localhost:8000")]
|
||||||
public string GrpcServiceAddress { get; set; }
|
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; }
|
public string GrpcServiceAddress { get; set; }
|
||||||
|
@ -32,10 +35,59 @@ namespace NucuCar.TestClient.Sensors
|
||||||
var sensorsCommandLine = new SensorsCmd();
|
var sensorsCommandLine = new SensorsCmd();
|
||||||
sensorsCommandLine.GrpcServiceAddress = options.GrpcServiceAddress;
|
sensorsCommandLine.GrpcServiceAddress = options.GrpcServiceAddress;
|
||||||
|
|
||||||
await sensorsCommandLine.EnvironmentSensorGrpcServiceTest();
|
switch (options.SensorName)
|
||||||
|
{
|
||||||
|
case "environment":
|
||||||
|
{
|
||||||
|
await sensorsCommandLine.EnvironmentSensorGrpcServiceTest();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Invalid sensor name: ${options.SensorName}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task EnvironmentSensorGrpcServiceTest()
|
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.
|
// Used to allow gRPC calls over unsecured HTTP.
|
||||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
|
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
|
||||||
|
@ -52,33 +104,8 @@ namespace NucuCar.TestClient.Sensors
|
||||||
var channel = GrpcChannel.ForAddress(GrpcServiceAddress,
|
var channel = GrpcChannel.ForAddress(GrpcServiceAddress,
|
||||||
new GrpcChannelOptions {HttpClient = httpClient});
|
new GrpcChannelOptions {HttpClient = httpClient});
|
||||||
var client = new EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceClient(channel);
|
var client = new EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceClient(channel);
|
||||||
|
|
||||||
var cts = new CancellationTokenSource();
|
|
||||||
|
|
||||||
Console.CancelKeyPress += (s, e) =>
|
return client;
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue