diff --git a/NucuCar.Sensors/.editorconfig b/.editorconfig similarity index 100% rename from NucuCar.Sensors/.editorconfig rename to .editorconfig diff --git a/NucuCar.TestClient/NucuCar.TestClient.csproj b/NucuCar.TestClient/NucuCar.TestClient.csproj index 007150d..eeb97d5 100644 --- a/NucuCar.TestClient/NucuCar.TestClient.csproj +++ b/NucuCar.TestClient/NucuCar.TestClient.csproj @@ -6,6 +6,7 @@ + diff --git a/NucuCar.TestClient/NucuCarSensorsCommandLine.cs b/NucuCar.TestClient/NucuCarSensorsCommandLine.cs new file mode 100644 index 0000000..1e55b5c --- /dev/null +++ b/NucuCar.TestClient/NucuCarSensorsCommandLine.cs @@ -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"); + } + } +} \ No newline at end of file diff --git a/NucuCar.TestClient/Program.cs b/NucuCar.TestClient/Program.cs index cf2c63b..aa362e0 100644 --- a/NucuCar.TestClient/Program.cs +++ b/NucuCar.TestClient/Program.cs @@ -1,48 +1,25 @@ using System; -using System.Net.Http; -using System.Threading.Tasks; -using Google.Protobuf.WellKnownTypes; -using Grpc.Net.Client; -using NucuCarSensorsProto; +using System.Collections.Generic; +using CommandLine; +using static NucuCar.TestClient.NucuCarSensorsCommandLine; namespace NucuCar.TestClient { 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 - static async Task Main(string[] args) + static void Main(string[] args) { - // 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; - _httpClient = new HttpClient(httpClientHandler); - - await EnvironmentSensorGrpcServiceTest(); + Parser.Default.ParseArguments(args) + .WithParsed(opts => { RunSensorsTestCommand(opts).GetAwaiter().GetResult(); }) + .WithNotParsed(HandleParseError); } - private static async Task EnvironmentSensorGrpcServiceTest() + private static void HandleParseError(IEnumerable errs) { - var channel = GrpcChannel.ForAddress("https://localhost:8000", - 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) + foreach (var e in errs) { - var measurement = await client.GetSensorMeasurementAsync(new Empty()); - Console.WriteLine( - $"t: {measurement.Temperature} | h: {measurement.Humidity} | p: {measurement.Pressure}"); + Console.WriteLine("Argument parse error:", e); } } }