public async void Run(IBackgroundTaskInstance taskInstance) { _deferral = taskInstance.GetDeferral(); var device = new TpmDevice(0); try { string deviceConnectionString = await device.GetConnectionStringAsync(); // 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 DeviceManagementRequestHandler(); // Create the DeviceManagementClient, the main entry point into device management _dmClient = await DeviceManagementClient.CreateAsync(deviceTwinProxy, appRequestHandler); // 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); } catch { LogError(); } }
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 DeviceManagementRequestHandler(); // Create the DeviceManagementClient, the main entry point into device management this._dmClient = await DeviceManagementClient.CreateAsync(deviceTwin, appRequestHandler); // 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._dmClient.ApplyDesiredStateAsync(); Logger.Log("ResetConnectionAsync end", LoggingLevel.Verbose); }