Turn NucuCar.TestClient into a command line client
This commit is contained in:
parent
6f1f69bd8f
commit
451c3ee664
4 changed files with 70 additions and 33 deletions
|
@ -6,6 +6,7 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="CommandLineParser" Version="2.6.0" />
|
||||||
<PackageReference Include="Grpc.Net.Client" Version="2.25.0" />
|
<PackageReference Include="Grpc.Net.Client" Version="2.25.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
59
NucuCar.TestClient/NucuCarSensorsCommandLine.cs
Normal file
59
NucuCar.TestClient/NucuCarSensorsCommandLine.cs
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
using System;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CommandLine;
|
||||||
|
using Google.Protobuf.WellKnownTypes;
|
||||||
|
using Grpc.Net.Client;
|
||||||
|
using NucuCarSensorsProto;
|
||||||
|
|
||||||
|
namespace NucuCar.TestClient
|
||||||
|
{
|
||||||
|
[Verb("sensors", HelpText = "Test the gRPC sensors services.")]
|
||||||
|
public class NucuCarSensorsCommandLineOptions
|
||||||
|
{
|
||||||
|
[Option('u', "url", Required = false, HelpText = "The url and port of the gRPC server.",
|
||||||
|
Default = "https://localhost:8000")]
|
||||||
|
public string GrpcServiceAddress { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class NucuCarSensorsCommandLine
|
||||||
|
{
|
||||||
|
public string GrpcServiceAddress { get; set; }
|
||||||
|
|
||||||
|
public static async Task RunSensorsTestCommand(NucuCarSensorsCommandLineOptions options)
|
||||||
|
{
|
||||||
|
var sensorsCommandLine = new NucuCarSensorsCommandLine();
|
||||||
|
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.
|
||||||
|
var httpClientHandler = new HttpClientHandler();
|
||||||
|
httpClientHandler.ServerCertificateCustomValidationCallback =
|
||||||
|
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
|
||||||
|
var httpClient = new HttpClient(httpClientHandler);
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
Console.WriteLine("EnvironmentSensorState: " + state);
|
||||||
|
if (state == SensorStateEnum.Initialized)
|
||||||
|
{
|
||||||
|
var measurement = await client.GetSensorMeasurementAsync(new Empty());
|
||||||
|
Console.WriteLine(
|
||||||
|
$"t: {measurement.Temperature} | h: {measurement.Humidity} | p: {measurement.Pressure}");
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine("Done");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,48 +1,25 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using CommandLine;
|
||||||
using Google.Protobuf.WellKnownTypes;
|
using static NucuCar.TestClient.NucuCarSensorsCommandLine;
|
||||||
using Grpc.Net.Client;
|
|
||||||
using NucuCarSensorsProto;
|
|
||||||
|
|
||||||
namespace NucuCar.TestClient
|
namespace NucuCar.TestClient
|
||||||
{
|
{
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
/* Warning! Before issuing a gRPC call the dev certificate must be trusted or you'll get:
|
|
||||||
* Detail="Error starting gRPC call: The SSL connection could not be established, see inner exception."
|
|
||||||
* See: https://docs.microsoft.com/en-us/aspnet/core/security/enforcing-ssl?view=aspnetcore-3.0&tabs=visual-studio#trust-the-aspnet-core-https-development-certificate-on-windows-and-macos
|
|
||||||
*/
|
|
||||||
private static HttpClient _httpClient;
|
|
||||||
|
|
||||||
// ReSharper disable once ArrangeTypeMemberModifiers
|
// ReSharper disable once ArrangeTypeMemberModifiers
|
||||||
static async Task Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
// Used to allow gRPC calls over unsecured HTTP.
|
Parser.Default.ParseArguments<NucuCarSensorsCommandLineOptions>(args)
|
||||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
|
.WithParsed(opts => { RunSensorsTestCommand(opts).GetAwaiter().GetResult(); })
|
||||||
|
.WithNotParsed(HandleParseError);
|
||||||
// Allow untrusted certificates.
|
|
||||||
var httpClientHandler = new HttpClientHandler();
|
|
||||||
httpClientHandler.ServerCertificateCustomValidationCallback =
|
|
||||||
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
|
|
||||||
_httpClient = new HttpClient(httpClientHandler);
|
|
||||||
|
|
||||||
await EnvironmentSensorGrpcServiceTest();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task EnvironmentSensorGrpcServiceTest()
|
private static void HandleParseError(IEnumerable<Error> errs)
|
||||||
{
|
{
|
||||||
var channel = GrpcChannel.ForAddress("https://localhost:8000",
|
foreach (var e in errs)
|
||||||
new GrpcChannelOptions {HttpClient = _httpClient});
|
|
||||||
var client = new EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceClient(channel);
|
|
||||||
var reply = await client.GetSensorStateAsync(new Empty());
|
|
||||||
var state = reply.State;
|
|
||||||
Console.WriteLine("EnviromentSensorState: " + state);
|
|
||||||
if (state == SensorStateEnum.Initialized)
|
|
||||||
{
|
{
|
||||||
var measurement = await client.GetSensorMeasurementAsync(new Empty());
|
Console.WriteLine("Argument parse error:", e);
|
||||||
Console.WriteLine(
|
|
||||||
$"t: {measurement.Temperature} | h: {measurement.Humidity} | p: {measurement.Pressure}");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue