From 7aa6ca0484969940adcacd7b4ebc674f7cd7859f Mon Sep 17 00:00:00 2001 From: Sindhu Nagesh Date: Thu, 10 Dec 2020 11:21:07 -0800 Subject: [PATCH] (gh-pages): Fixing scrip to conditionally generate docs (#173) --- .../DeviceClientStreamingSample.csproj | 2 +- .../DeviceStreamSample.cs | 2 +- .../DeviceStreamSample.cs | 10 ++-- .../DeviceStreamingSample/Parameters.cs | 33 ++++++++++++ .../service/DeviceStreamingSample/Program.cs | 54 ++++++------------- .../ServiceClientStreamingSample.csproj | 3 +- 6 files changed, 59 insertions(+), 45 deletions(-) create mode 100644 iot-hub/Samples/service/DeviceStreamingSample/Parameters.cs diff --git a/iot-hub/Samples/device/DeviceStreamingSample/DeviceClientStreamingSample.csproj b/iot-hub/Samples/device/DeviceStreamingSample/DeviceClientStreamingSample.csproj index 9dee04a9..275db94c 100644 --- a/iot-hub/Samples/device/DeviceStreamingSample/DeviceClientStreamingSample.csproj +++ b/iot-hub/Samples/device/DeviceStreamingSample/DeviceClientStreamingSample.csproj @@ -16,6 +16,6 @@ - + diff --git a/iot-hub/Samples/device/DeviceStreamingSample/DeviceStreamSample.cs b/iot-hub/Samples/device/DeviceStreamingSample/DeviceStreamSample.cs index 655f9d30..52168055 100644 --- a/iot-hub/Samples/device/DeviceStreamingSample/DeviceStreamSample.cs +++ b/iot-hub/Samples/device/DeviceStreamingSample/DeviceStreamSample.cs @@ -38,7 +38,7 @@ public async Task RunSampleAsync(bool acceptDeviceStreamingRequest = true) await _deviceClient.AcceptDeviceStreamRequestAsync(streamRequest, cts.Token); using ClientWebSocket webSocket = await DeviceStreamingCommon.GetStreamingClientAsync( - streamRequest.Url, + streamRequest.Uri, streamRequest.AuthorizationToken, cts.Token); diff --git a/iot-hub/Samples/service/DeviceStreamingSample/DeviceStreamSample.cs b/iot-hub/Samples/service/DeviceStreamingSample/DeviceStreamSample.cs index 584fcf00..a088a1dd 100644 --- a/iot-hub/Samples/service/DeviceStreamingSample/DeviceStreamSample.cs +++ b/iot-hub/Samples/service/DeviceStreamingSample/DeviceStreamSample.cs @@ -27,23 +27,25 @@ public async Task RunSampleAsync() { var deviceStreamRequest = new DeviceStreamRequest("TestStream"); - DeviceStreamResponse result = await _serviceClient.CreateStreamAsync(_deviceId, deviceStreamRequest).ConfigureAwait(false); + DeviceStreamResponse result = await _serviceClient.CreateStreamAsync(_deviceId, deviceStreamRequest); Console.WriteLine($"Stream response received: Name={deviceStreamRequest.StreamName} IsAccepted={result.IsAccepted}"); if (result.IsAccepted) { using var cts = new CancellationTokenSource(TimeSpan.FromMinutes(1)); - using ClientWebSocket stream = await DeviceStreamingCommon.GetStreamingClientAsync(result.Url, result.AuthorizationToken, cts.Token).ConfigureAwait(false); + using ClientWebSocket stream = await DeviceStreamingCommon.GetStreamingClientAsync(result.Uri, result.AuthorizationToken, cts.Token); byte[] sendBuffer = Encoding.UTF8.GetBytes("Streaming data over a stream..."); byte[] receiveBuffer = new byte[1024]; - await stream.SendAsync(sendBuffer, WebSocketMessageType.Binary, true, cts.Token).ConfigureAwait(false); + await stream.SendAsync(sendBuffer, WebSocketMessageType.Binary, true, cts.Token); Console.WriteLine($"Sent stream data: {Encoding.UTF8.GetString(sendBuffer, 0, sendBuffer.Length)}"); - var receiveResult = await stream.ReceiveAsync(receiveBuffer, cts.Token).ConfigureAwait(false); + var receiveResult = await stream.ReceiveAsync(receiveBuffer, cts.Token); Console.WriteLine($"Received stream data: {Encoding.UTF8.GetString(receiveBuffer, 0, receiveResult.Count)}"); + + await stream.CloseAsync(WebSocketCloseStatus.NormalClosure, "Streaming completed", new CancellationToken()); } else { diff --git a/iot-hub/Samples/service/DeviceStreamingSample/Parameters.cs b/iot-hub/Samples/service/DeviceStreamingSample/Parameters.cs new file mode 100644 index 00000000..88389e25 --- /dev/null +++ b/iot-hub/Samples/service/DeviceStreamingSample/Parameters.cs @@ -0,0 +1,33 @@ +using CommandLine; +using Microsoft.Azure.Devices; + +namespace ServiceClientStreamingSample +{ + /// + /// Parameters for the application. + /// + internal class Parameters + { + [Option( + 'c', + "HubConnectionString", + Required = true, + HelpText = "The connection string of the IoT Hub instance to connect to.")] + public string HubConnectionString { get; set; } + + [Option( + 'd', + "DeviceId", + Required = true, + HelpText = "The Id of the device to connect to.")] + public string DeviceId { get; set; } + + [Option( + 't', + "TransportType", + Default = TransportType.Amqp, + Required = false, + HelpText = "The transport to use to communicate with the IoT Hub. Possible values include Amqp and Amqp_WebSocket_Only.")] + public TransportType TransportType { get; set; } + } +} diff --git a/iot-hub/Samples/service/DeviceStreamingSample/Program.cs b/iot-hub/Samples/service/DeviceStreamingSample/Program.cs index 5b8a79ee..a7b01419 100644 --- a/iot-hub/Samples/service/DeviceStreamingSample/Program.cs +++ b/iot-hub/Samples/service/DeviceStreamingSample/Program.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using CommandLine; +using ServiceClientStreamingSample; using System; using System.Threading.Tasks; @@ -8,46 +10,22 @@ namespace Microsoft.Azure.Devices.Samples { public static class Program { - // The IoT Hub connection string. This is available under the "Shared access policies" in the Azure portal. - - // For this sample either: - // - pass this value as a command-prompt argument - // - set the IOTHUB_CONN_STRING_CSHARP environment variable - // - create a launchSettings.json (see launchSettings.json.template) containing the variable - private static string s_connectionString = Environment.GetEnvironmentVariable("IOTHUB_CONN_STRING_CSHARP"); - - // ID of the device to interact with. - // - pass this value as a command-prompt argument - // - set the DEVICE_ID environment variable - // - create a launchSettings.json (see launchSettings.json.template) containing the variable - private static string s_deviceId = Environment.GetEnvironmentVariable("DEVICE_ID"); - - // Select one of the following transports used by ServiceClient to connect to IoT Hub. - private static TransportType s_transportType = TransportType.Amqp; - //private static TransportType s_transportType = TransportType.Amqp_WebSocket_Only; - public static async Task Main(string[] args) { - if (string.IsNullOrEmpty(s_connectionString) && args.Length > 0) - { - s_connectionString = args[0]; - } - - if (string.IsNullOrEmpty(s_deviceId) && args.Length > 1) - { - s_deviceId = args[1]; - } - - if (string.IsNullOrEmpty(s_connectionString) || - string.IsNullOrEmpty(s_deviceId)) - { - Console.WriteLine("Please provide a connection string and device ID"); - Console.WriteLine("Usage: ServiceClientC2DStreamingSample [iotHubConnString] [deviceId]"); - return 1; - } - - using ServiceClient serviceClient = ServiceClient.CreateFromConnectionString(s_connectionString, s_transportType); - var sample = new DeviceStreamSample(serviceClient, s_deviceId); + // Parse application parameters + Parameters parameters = null; + Parser.Default.ParseArguments(args) + .WithParsed(parsedParams => + { + parameters = parsedParams; + }) + .WithNotParsed(errors => + { + Environment.Exit(1); + }); + + using ServiceClient serviceClient = ServiceClient.CreateFromConnectionString(parameters.HubConnectionString, parameters.TransportType); + var sample = new DeviceStreamSample(serviceClient, parameters.DeviceId); await sample.RunSampleAsync().ConfigureAwait(false); Console.WriteLine("Done.\n"); diff --git a/iot-hub/Samples/service/DeviceStreamingSample/ServiceClientStreamingSample.csproj b/iot-hub/Samples/service/DeviceStreamingSample/ServiceClientStreamingSample.csproj index a2fe38b9..caad31a0 100644 --- a/iot-hub/Samples/service/DeviceStreamingSample/ServiceClientStreamingSample.csproj +++ b/iot-hub/Samples/service/DeviceStreamingSample/ServiceClientStreamingSample.csproj @@ -11,11 +11,12 @@ + - +