private async Task ResetConnectionAsync() { Logger.Log("ResetConnectionAsync start", LoggingLevel.Verbose); // Attempt to close any existing connections before // creating a new one if (_deviceClient != null) { await _deviceClient.CloseAsync().ContinueWith((t) => { var e = t.Exception; if (e != null) { var msg = "existingClient.CloseAsync exception: " + e.Message + "\n" + e.StackTrace; System.Diagnostics.Debug.WriteLine(msg); Logger.Log(msg, LoggingLevel.Verbose); } }); } // Get new SAS Token var deviceConnectionString = await GetConnectionStringAsync(); // Create DeviceClient. Application uses DeviceClient for telemetry messages, device twin // as well as device management _deviceClient = DeviceClient.CreateFromConnectionString(deviceConnectionString, TransportType.Mqtt); // For testing connection failure, we can use a short time-out. // _deviceClient.OperationTimeoutInMilliseconds = 5000; // IDeviceTwin abstracts away communication with the back-end. // AzureIoTHubDeviceTwinProxy is an implementation of Azure IoT Hub IDeviceTwin deviceTwin = new AzureIoTHubDeviceTwinProxy(_deviceClient, _iotHubOfflineEvent, Logger.Log); // IDeviceManagementRequestHandler handles device management-specific requests to the app, // such as whether it is OK to perform a reboot at any givem moment, according the app business logic // ToasterDeviceManagementRequestHandler is the Toaster app implementation of the interface IDeviceManagementRequestHandler appRequestHandler = new ToasterDeviceManagementRequestHandler(this); // Create the DeviceManagementClient, the main entry point into device management this.deviceManagementClient = await DeviceManagementClient.CreateAsync(deviceTwin, appRequestHandler); await EnableDeviceManagementUiAsync(true); // Set the callback for desired properties update. The callback will be invoked // for all desired properties -- including those specific to device management await _deviceClient.SetDesiredPropertyUpdateCallbackAsync(OnDesiredPropertyUpdated, null); // Tell the deviceManagementClient to sync the device with the current desired state. await this.deviceManagementClient.ApplyDesiredStateAsync(); Logger.Log("ResetConnectionAsync end", LoggingLevel.Verbose); }
private async Task InitializeDeviceClientAsync() { // Create DeviceClient. Application uses DeviceClient for telemetry messages, device twin // as well as device management DeviceClient deviceClient = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Mqtt); // IDeviceTwin abstracts away communication with the back-end. // AzureIoTHubDeviceTwinProxy is an implementation of Azure IoT Hub IDeviceTwin deviceTwinProxy = new AzureIoTHubDeviceTwinProxy(deviceClient); // IDeviceManagementRequestHandler handles device management-specific requests to the app, // such as whether it is OK to perform a reboot at any givem moment, according the app business logic // ToasterDeviceManagementRequestHandler is the Toaster app implementation of the interface IDeviceManagementRequestHandler appRequestHandler = new ToasterDeviceManagementRequestHandler(this); // Create the DeviceManagementClient, the main entry point into device management this.deviceManagementClient = await DeviceManagementClient.CreateAsync(deviceTwinProxy, appRequestHandler); EnableDeviceManagementUI(true); // Set the callback for desired properties update. The callback will be invoked // for all desired properties -- including those specific to device management await deviceClient.SetDesiredPropertyUpdateCallback(OnDesiredPropertyUpdate, null); }