Пример #1
0
        private async Task OnDesiredPropertyUpdated(TwinCollection twinProperties, object userContext)
        {
            Dictionary <string, object> desiredProperties = AzureIoTHubDeviceTwinProxy.DictionaryFromTwinCollection(twinProperties);

            // Let the device management client process properties specific to device management
            await _dmClient.ApplyDesiredStateAsync(desiredProperties);
        }
        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 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);
        }
Пример #4
0
        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);
            deviceClient = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Mqtt);
            //deviceClient = DeviceClient.CreateFromConnectionString(DeviceConnectionString);

            // IDeviceTwin abstracts away communication with the back-end.
            // AzureIoTHubDeviceTwinProxy is an implementation of Azure IoT Hub
            IDeviceTwin deviceTwinProxy = new AzureIoTHubDeviceTwinProxy(deviceClient);

            try
            {
                // IDeviceManagementRequestHandler handles device management-specific requests to the app,
                // such as whether it is OK to perform a reboot at any givem moment, according to the app
                // business logic.
                // DMRequestHandler is the Toaster app implementation of the interface
                IDeviceManagementRequestHandler appRequestHandler = new DMRequestHandler(this);

                // Create the DeviceManagementClient, the main entry point into device management
                this.deviceManagementClient = 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);

                await this.deviceClient.OpenAsync();

                connectionstatus.Text = "Connected";

                // Set up callbacks:
                await deviceClient.SetMethodHandlerAsync("Getsensordata", Getsensordata, null);

                //this.UpdateDeviceStatus();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Error in sample InitializeDeviceClientAsync: {0}", ex.Message);
            }
        }
Пример #5
0
        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);
        }
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;



            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);


                string str     = "Device Restarted";
                var    message = new Message(Encoding.ASCII.GetBytes(str));
                await deviceClient.SendEventAsync(message);
            }
            catch
            {
            }


            await InitRfcommServer();

            ConnectCtrl c = new ConnectCtrl(this);

            main.Children.Add(c);
            ResultCollection = new ObservableCollection <RfcommChatDeviceDisplay>();

            c.SetResultList(ResultCollection);

            // TODO:
            // check if you are running in the factory (e.g. contacting a specific server or connecting to a predefined network)

            // initialize the TPM device
            //try
            //{
            //    tpm = new TpmDevice(0);
            //}
            //catch
            //{
            //    Debug.WriteLine("TPM not present Error");
            //    return;
            //}

            //// reset TPM to clean previous
            //try
            //{
            //    Debug.WriteLine("Reset TPM...");
            //    tpm.Destroy();
            //}
            //catch (Exception ex)
            //{
            //    Debug.WriteLine("TPM was not initialized!");
            //}

            //Debug.WriteLine("TPM initialized");
            //string id = tpm.GetDeviceId();

            ////HWID is unique for this device.
            //string hwid = tpm.GetHardwareDeviceId();
            //Debug.WriteLine("TPM Hardware ID:" + hwid);

            //string hmackey = CryptoKeyGenerator.GenerateKey(32);
            //Debug.WriteLine("TPM hmackey:" + hmackey);

            ////provision the device.
            //tpm.Provision(hmackey, "gunterlhub.azure-devices.net", hwid);

            // TODO:
            // send hmacky and hwid to production server and create the devices on the iot hub

            // connect to production server via bluetooth if availiable
            //
        }