public static async Task Main(string[] args) { // Parse application parameters Parameters parameters = null; ParserResult <Parameters> result = Parser.Default.ParseArguments <Parameters>(args) .WithParsed(parsedParams => { parameters = parsedParams; }) .WithNotParsed(errors => { Environment.Exit(1); }); s_logger = InitializeConsoleDebugLogger(); if (!parameters.Validate(s_logger)) { throw new ArgumentException("Required parameters are not set. Please recheck required variables by using \"--help\""); } s_logger.LogInformation("Press Control+C to quit the sample."); using var cts = new CancellationTokenSource(); Console.CancelKeyPress += (sender, eventArgs) => { eventArgs.Cancel = true; cts.Cancel(); s_logger.LogInformation("Sample execution cancellation requested; will exit."); }; s_logger.LogDebug($"Set up the device client."); using DeviceClient deviceClient = await SetupDeviceClientAsync(parameters, cts.Token); var sample = new ThermostatSample(deviceClient, s_logger); await sample.PerformOperationsAsync(cts.Token); }
public static async Task Main(string[] args) { /* Made by DavidB for tinkering: * string strArgs = "les args: "; * for (int i = 0; i < args.Length; i++) * strArgs += args[i] + ", "; * Console.WriteLine(strArgs); * Console.ReadLine(); */ // Parse application parameters Parameters parameters = null; ParserResult <Parameters> result = Parser.Default.ParseArguments <Parameters>(args) .WithParsed(parsedParams => { parameters = parsedParams; }) .WithNotParsed(errors => { Environment.Exit(1); }); s_logger = InitializeConsoleDebugLogger(); if (!parameters.Validate(s_logger)) { throw new ArgumentException("Required parameters are not set. Please recheck required " + "variables by using \"--help\""); } /* Made by precious DavieB to test the logger: * s_logger.LogInformation("Like, uuummmm, this stuff is written by s_logger:"); * s_logger.LogWarning("Holy blizzard, my hair is on fire!!!"); * s_logger.LogCritical("Oh no, my computer is shooting sparks!!"); * s_logger.LogError("OMG, I forgot my name!"); */ /* Made by sweet DavieB to see what's in parameters:*/ s_logger.LogInformation($"DeviceId = {parameters.DeviceId}"); //sample-device-01 //foreach (var parm in parameters ) //not enumerable s_logger.LogWarning($"DpsIdScope = {parameters.DpsIdScope}"); //global.azure-devices-provisioning.net: s_logger.LogDebug($"DpsEndpoint = {parameters.DpsEndpoint}"); s_logger.LogWarning($"DeviceSecurityType={parameters.DeviceSecurityType}"); //DPS s_logger.LogCritical($"DeviceSymmetricKey = {parameters.DeviceSymmetricKey}"); //Console.WriteLine($"ApplicationRunningTime = {parameters.ApplicationRunningTime}"); //blank var runningTime = parameters.ApplicationRunningTime != null ? TimeSpan.FromSeconds((double)parameters.ApplicationRunningTime) : Timeout.InfiniteTimeSpan; s_logger.LogInformation("Press Control+C to quit the sample."); using var cts = new CancellationTokenSource(runningTime); Console.CancelKeyPress += (sender, eventArgs) => { eventArgs.Cancel = true; cts.Cancel(); s_logger.LogInformation("Sample execution cancellation requested; will exit."); }; //Console.ReadLine(); added by Darling DavieB s_logger.LogDebug($"Set up the device client."); using DeviceClient deviceClient = await SetupDeviceClientAsync(parameters, cts.Token); var sample = new ThermostatSample(deviceClient, s_logger); await sample.PerformOperationsAsync(cts.Token); }
public static async Task Main(string[] args) { // Parse application parameters Parameters parameters = null; ParserResult <Parameters> result = Parser.Default.ParseArguments <Parameters>(args) .WithParsed(parsedParams => { parameters = parsedParams; }) .WithNotParsed(errors => { Environment.Exit(1); }); s_logger = InitializeConsoleDebugLogger(); if (!parameters.Validate(s_logger)) { throw new ArgumentException("Required parameters are not set. Please recheck required variables by using \"--help\""); } var runningTime = parameters.ApplicationRunningTime != null ? TimeSpan.FromSeconds((double)parameters.ApplicationRunningTime) : Timeout.InfiniteTimeSpan; s_logger.LogInformation("Press Control+C to quit the sample."); using var cts = new CancellationTokenSource(runningTime); Console.CancelKeyPress += (sender, eventArgs) => { eventArgs.Cancel = true; cts.Cancel(); s_logger.LogInformation("Sample execution cancellation requested; will exit."); }; s_logger.LogDebug($"Set up the device client."); using DeviceClient deviceClient = await SetupDeviceClientAsync(parameters, cts.Token); var sample = new ThermostatSample(deviceClient, s_logger); await sample.PerformOperationsAsync(cts.Token); // PerformOperationsAsync is designed to run until cancellation has been explicitly requested, either through // cancellation token expiration or by Console.CancelKeyPress. // As a result, by the time the control reaches the call to close the device client, the cancellation token source would // have already had cancellation requested. // Hence, if you want to pass a cancellation token to any subsequent calls, a new token needs to be generated. // For device client APIs, you can also call them without a cancellation token, which will set a default // cancellation timeout of 4 minutes: https://github.com/Azure/azure-iot-sdk-csharp/blob/64f6e9f24371bc40ab3ec7a8b8accbfb537f0fe1/iothub/device/src/InternalClient.cs#L1922 await deviceClient.CloseAsync(); }