示例#1
0
        private async Task <IotHubDevice> AcquireDeviceId()
        {
            IotHubDevice device = null;

            try
            {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(ServiceUri);
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    // HTTP GET
                    HttpResponseMessage response = await client.GetAsync(RequestUri + "/ 1");

                    if (response.IsSuccessStatusCode)
                    {
                        device = await response.Content.ReadAsAsync <IotHubDevice>();

                        Debug.WriteLine("AcquireDeviceId: " + device.DeviceId + ", " + device.DeviceKey + ",\r\n    " + device.ConnectionString);

                        device.IsUsed = true;
                        response      = await client.PutAsJsonAsync(RequestUri + "/" + device.DevId.ToString(), device);

                        Debug.WriteLine(response.StatusCode.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return(device);
        }
示例#2
0
        public async Task <string> GetDeviceId()
        {
            if (App.curScenario.EmuAzure != 0)
            {
                return("");
            }

            var    localSettings = ApplicationData.Current.LocalSettings;
            string deviceId      = localSettings.Values[DEVICEID_TAG] as string;

            if (deviceId == null)
            {
                IotHubDevice device = await AcquireDeviceId();

                localSettings.Values[IOTDEVICESERVICEURL_TAG] = ServiceUri;
                localSettings.Values[DEVICEID_TAG]            = device.DeviceId;
                localSettings.Values[IOTHUBRECVCONN_TAG]      = device.ConnectionString;
                localSettings.Values[IOTHUBKEY_TAG]           = device.DeviceKey;

                deviceId = device.DeviceId;
            }
            return(deviceId);
        }