11// Copyright (c) Microsoft. All rights reserved.
22// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33
4+ using CommandLine ;
45using Microsoft . Extensions . Logging ;
56using System ;
6- using System . Collections . Generic ;
77using System . Diagnostics . Tracing ;
88using System . Threading . Tasks ;
99
1010namespace Microsoft . Azure . Devices . Client . Samples
1111{
1212 public class Program
1313 {
14- // String containing Hostname, Device Id & Device Key in one of the following formats:
15- // "HostName=<iothub_host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>"
16- // "HostName=<iothub_host_name>;CredentialType=SharedAccessSignature;DeviceId=<device_id>;SharedAccessSignature=SharedAccessSignature sr=<iot_host>/devices/<device_id>&sig=<token>&se=<expiry_time>";
17-
18- // For this sample either
19- // - pass this value as a command-prompt argument
20- // - set the IOTHUB_DEVICE_CONN_STRING environment variable
21- // - create a launchSettings.json (see launchSettings.json.template) containing the variable
22- private static string s_deviceConnectionStringPrimary = Environment . GetEnvironmentVariable ( "IOTHUB_DEVICE_CONN_STRING" ) ;
23- private static string s_deviceConnectionStringSecondary = Environment . GetEnvironmentVariable ( "IOTHUB_DEVICE_CONN_STRING_SECONDARY" ) ;
24-
25- // Specify one of the following transports used by DeviceClient to connect to IoT Hub.
26- // Mqtt
27- // Mqtt_WebSocket_Only
28- // Mqtt_Tcp_Only
29- // Amqp
30- // Amqp_WebSocket_Only
31- // Amqp_Tcp_only
32- // Http1
33- private static readonly string s_transportType = Environment . GetEnvironmentVariable ( "IOTHUB_DEVICE_TRANSPORT_TYPE" ) ;
34-
3514 public static async Task < int > Main ( string [ ] args )
3615 {
37- // Create a console logger, that logs all events that are categorized at Debug level or higher.
38- // For additional details, see https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.ilogger?view=dotnet-plat-ext-3.1.
39- ILoggerFactory loggerFactory = new LoggerFactory ( ) ;
16+ // Parse application parameters
17+ Parameters parameters = null ;
18+ ParserResult < Parameters > result = Parser . Default . ParseArguments < Parameters > ( args )
19+ . WithParsed ( parsedParams =>
20+ {
21+ parameters = parsedParams ;
22+ } )
23+ . WithNotParsed ( errors =>
24+ {
25+ Environment . Exit ( 1 ) ;
26+ } ) ;
4027
28+ // Set up logging
29+ ILoggerFactory loggerFactory = new LoggerFactory ( ) ;
4130 loggerFactory . AddColorConsoleLogger (
4231 new ColorConsoleLoggerConfiguration
4332 {
@@ -49,46 +38,12 @@ public static async Task<int> Main(string[] args)
4938 // Instantiating this seems to do all we need for outputing SDK events to our console log
5039 _ = new ConsoleEventListener ( SdkEventProviderPrefix , logger ) ;
5140
52- var connectionStrings = new List < string > ( 2 ) ;
53- if ( string . IsNullOrEmpty ( s_deviceConnectionStringPrimary ) && args . Length > 0 )
54- {
55- s_deviceConnectionStringPrimary = args [ 0 ] ;
56- }
57- connectionStrings . Add ( s_deviceConnectionStringPrimary ) ;
58-
59- if ( string . IsNullOrEmpty ( s_deviceConnectionStringSecondary ) && args . Length > 1 )
60- {
61- s_deviceConnectionStringSecondary = args [ 1 ] ;
62- }
63- if ( ! string . IsNullOrWhiteSpace ( s_deviceConnectionStringSecondary ) )
64- {
65- connectionStrings . Add ( s_deviceConnectionStringSecondary ) ;
66- }
67-
68- var sample = new DeviceReconnectionSample ( connectionStrings , GetTransportType ( args ) , logger ) ;
41+ // Run the sample
42+ var sample = new DeviceReconnectionSample ( parameters . GetConnectionStrings ( ) , parameters . TransportType , logger ) ;
6943 await sample . RunSampleAsync ( ) ;
7044
7145 logger . LogInformation ( "Done." ) ;
7246 return 0 ;
7347 }
74-
75- private static TransportType GetTransportType ( string [ ] args )
76- {
77- // Check environment variable for transport type
78- if ( Enum . TryParse ( s_transportType , true , out TransportType transportType ) )
79- {
80- return transportType ;
81- }
82-
83- // then check argument for transport type
84- if ( args . Length > 2
85- && Enum . TryParse ( args [ 2 ] , true , out transportType ) )
86- {
87- return transportType ;
88- }
89-
90- // otherwise default to MQTT
91- return TransportType . Mqtt ;
92- }
9348 }
9449}
0 commit comments