Beispiel #1
0
        /// <summary>
        /// Retrieve Device Twin from given feature parameter.
        /// </summary>
        /// <param name="windowsObj">The json section name.</param>
        /// <returns>Return the JToken object for given parameter.</returns>
        public async Task <JToken> GetTwinData(string windowsObj)
        {
            if (_deviceTwin == null)
            {
                ShowDialogAsync("Device Twin Error", "No device twin found. Please reconnect to IoT Hub.");
                return(null);
            }

            JsonConvert.DefaultSettings = () => new JsonSerializerSettings
            {
                CheckAdditionalContent = false
            };
            DeviceTwinData deviceTwinData = await _deviceTwin.GetDeviceTwinData();

            if (deviceTwinData.reportedPropertiesJson == null)
            {
                ShowDialogAsync("Read Twin Error", windowsObj + ": Reported Twin in not valid");
                return(null);
            }

            Debug.WriteLine("json = " + deviceTwinData.reportedPropertiesJson);
            JObject desiredObject = (JObject)JsonConvert.DeserializeObject(deviceTwinData.reportedPropertiesJson);

            JToken windowsToken;

            if (!desiredObject.TryGetValue(DMJSonConstants.DTWindowsIoTNameSpace, out windowsToken) || windowsToken.Type != JTokenType.Object)
            {
                return(null);
            }
            JObject windowsObject = (JObject)windowsToken;
            var     appsJson      = windowsObject[windowsObj];

            if (appsJson != null)
            {
                return(appsJson);
            }
            else
            {
                ShowDialogAsync("Read Twin Error", windowsObj + ": Reported Twin in not valid");
                return(null);
            }
        }
        public async Task <DeviceTwinData> GetDeviceTwinData()
        {
            DeviceTwinData result = new DeviceTwinData();

            dynamic registryManager = RegistryManager.CreateFromConnectionString(connString);

            try
            {
                var deviceTwin = await registryManager.GetTwinAsync(deviceName);

                if (deviceTwin != null)
                {
                    result.deviceJson             = deviceTwin.ToJson();
                    result.tagsJson               = deviceTwin.Tags.ToJson();
                    result.reportedPropertiesJson = deviceTwin.Properties.Reported.ToJson();
                    result.desiredPropertiesJson  = deviceTwin.Properties.Desired.ToJson();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message + Environment.NewLine + "Make sure you are using the latest Microsoft.Azure.Devices package.", "Device Twin Properties");
            }
            return(result);
        }