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)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + "Make sure you are using the latest Microsoft.Azure.Devices package.", "Device Twin Properties");
            }
            return(result);
        }
        private async void ReadDTReported()
        {
            DeviceTwinData deviceTwinData = await _deviceTwin.GetDeviceTwinData();

            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;
            }
            JObject windowsObject = (JObject)windowsToken;

            foreach (JProperty jsonProp in windowsObject.Children())
            {
                if (jsonProp.Name == "timeInfo" && jsonProp.Value.Type == JTokenType.Object)
                {
                    Debug.WriteLine(jsonProp.Value.ToString());
                    TimeReportedState.FromJson((JObject)jsonProp.Value);
                }
                else if (jsonProp.Name == TimeSvcReportedState.SectionName && jsonProp.Value.Type == JTokenType.Object)
                {
                    Debug.WriteLine(jsonProp.Value.ToString());
                    TimeSvcReportedState.FromJson((JObject)jsonProp.Value);
                }
                else if (jsonProp.Name == CertificatesDataContract.SectionName && jsonProp.Value.Type == JTokenType.Object)
                {
                    Debug.WriteLine(jsonProp.Value.ToString());
                    var reportedProperties = CertificatesDataContract.ReportedProperties.FromJsonObject((JObject)jsonProp.Value);
                    CertificatesInfoToUI(reportedProperties);
                }
                else if (jsonProp.Name == DeviceInfoDataContract.SectionName)
                {
                    Debug.WriteLine(jsonProp.Value.ToString());
                    if (jsonProp.Value is JObject)
                    {
                        DeviceInfoReportedState.FromJsonObject((JObject)jsonProp.Value);
                    }
                    else
                    {
                        MessageBox.Show("Expected json object as a value for " + DeviceInfoReportedState.SectionName);
                    }
                }
                else if (jsonProp.Name == ExternalStorageDataContract.SectionName)
                {
                    Debug.WriteLine(jsonProp.Value.ToString());
                    if (jsonProp.Value is JObject)
                    {
                        var reportedProperties = ExternalStorageDataContract.ReportedProperties.FromJsonObject((JObject)jsonProp.Value);
                        AzureStorageReportedConnectionString.Text = reportedProperties.connectionString;
                    }
                    else
                    {
                        MessageBox.Show("Expected json object as a value for " + DeviceInfoReportedState.SectionName);
                    }
                }
                else if (jsonProp.Name == DmAppStoreUpdateDataContract.SectionName)
                {
                    Debug.WriteLine(jsonProp.Value.ToString());
                    if (jsonProp.Value is JObject)
                    {
                        DmAppStoreUpdateReportedState.FromJsonObject((JObject)jsonProp.Value);
                    }
                    else
                    {
                        MessageBox.Show("Expected json object as a value for " + DmAppStoreUpdateDataContract.SectionName);
                    }
                }
                else if (jsonProp.Name == RebootInfoDataContract.SectionName)
                {
                    Debug.WriteLine(jsonProp.Value.ToString());
                    RebootInfoReportedState.FromJsonObject(jsonProp.Value);
                }
                else if (jsonProp.Name == RebootCmdDataContract.SectionName)
                {
                    Debug.WriteLine(jsonProp.Value.ToString());
                    RebootCmdReportedState.FromJson(jsonProp.Value);
                }
                else if (jsonProp.Name == WindowsUpdatePolicyDataContract.SectionName)
                {
                    Debug.WriteLine(jsonProp.Value.ToString());
                    WindowsUpdatePolicyReportedState.FromJsonObject(jsonProp.Value);
                }
                else if (jsonProp.Name == WindowsUpdatesDataContract.SectionName)
                {
                    Debug.WriteLine(jsonProp.Value.ToString());
                    if (jsonProp.Value is JObject)
                    {
                        WindowsUpdatesConfigurationToUI((JObject)jsonProp.Value);
                    }
                    else
                    {
                        MessageBox.Show("Expected json object as a value for " + WindowsUpdatesDataContract.SectionName);
                    }
                }
                else if (jsonProp.Name == WindowsTelemetryDataContract.SectionName)
                {
                    Debug.WriteLine(jsonProp.Value.ToString());
                    if (jsonProp.Value is JObject)
                    {
                        WindowsTelemetryReportedState.FromJsonObject((JObject)jsonProp.Value);
                    }
                    else
                    {
                        MessageBox.Show("Expected json object as a value for " + WindowsTelemetryDataContract.SectionName);
                    }
                }
                else if (jsonProp.Name == "apps")
                {
                    Debug.WriteLine(jsonProp.Value.ToString());
                    TheAppsStatus.AppsStatusJsonToUI(jsonProp.Value);
                }
                else if (jsonProp.Name == "deviceHealthAttestation")
                {
                    Debug.WriteLine(jsonProp.Value.ToString());
                    var jobj = JObject.Parse(jsonProp.Value.ToString());
                    DeviceHealthAttestationReportedState.FromJson(jobj);
                }
                else if (jsonProp.Name == "wifi")
                {
                    Debug.WriteLine(jsonProp.Value.ToString());
                    this.WifiReportedState.FromJson(jsonProp.Value);
                }
                else if (jsonProp.Name == "eventTracingCollectors")
                {
                    Debug.WriteLine(jsonProp.Value.ToString());
                    this.ReportedDiagnosticLogs.FromJson((JObject)jsonProp.Value);
                }
            }
        }
Esempio n. 3
0
        private async void ReadDTReported()
        {
            DeviceTwinData deviceTwinData = await _deviceTwin.GetDeviceTwinData();

            Debug.WriteLine("json = " + deviceTwinData.reportedPropertiesJson);

            JObject jsonObject = (JObject)JsonConvert.DeserializeObject(deviceTwinData.reportedPropertiesJson);

            JToken microsoftNode;

            if (!jsonObject.TryGetValue("microsoft", out microsoftNode) || microsoftNode.Type != JTokenType.Object)
            {
                return;
            }
            JObject microsoftObject = (JObject)microsoftNode;

            JToken managementNode;

            if (!microsoftObject.TryGetValue("management", out managementNode) || managementNode.Type != JTokenType.Object)
            {
                return;
            }
            JObject managementObject = (JObject)managementNode;

            foreach (JProperty jsonProp in managementObject.Children())
            {
                if (jsonProp.Name == "timeInfo")
                {
                    Microsoft.Devices.Management.TimeInfo.GetResponse timeInfo = JsonConvert.DeserializeObject <Microsoft.Devices.Management.TimeInfo.GetResponse>(jsonProp.Value.ToString());
                    TimeInfoModelToUI(timeInfo);
                }
                if (jsonProp.Name == "certificates")
                {
                    Microsoft.Devices.Management.Certificates certificatesInfo = JsonConvert.DeserializeObject <Microsoft.Devices.Management.Certificates>(jsonProp.Value.ToString());
                    CertificatesInfoToUI(certificatesInfo);
                }
                else if (jsonProp.Name == "deviceInfo")
                {
                    Microsoft.Devices.Management.DeviceInfo deviceInfo = JsonConvert.DeserializeObject <Microsoft.Devices.Management.DeviceInfo>(jsonProp.Value.ToString());
                    DeviceStatusModelToUI(deviceInfo);
                }
                else if (jsonProp.Name == "rebootInfo")
                {
                    Debug.WriteLine(jsonProp.Value.ToString());
                    var rebootInfo = JsonConvert.DeserializeObject <Microsoft.Devices.Management.RebootInfo.GetResponse>(jsonProp.Value.ToString());
                    RebootInfoModelToUI(rebootInfo);
                }
                else if (jsonProp.Name == "windowsUpdatePolicy")
                {
                    Debug.WriteLine(jsonProp.Value.ToString());
                    var info = JsonConvert.DeserializeObject <Microsoft.Devices.Management.WindowsUpdatePolicyConfiguration>(jsonProp.Value.ToString());
                    WindowsUpdatePolicyConfigurationToUI(info);
                }
                else if (jsonProp.Name == "windowsUpdates")
                {
                    Debug.WriteLine(jsonProp.Value.ToString());
                    var info = JsonConvert.DeserializeObject <Microsoft.Devices.Management.WindowsUpdates.GetResponse>(jsonProp.Value.ToString());
                    WindowsUpdatesConfigurationToUI(info);
                }
            }
        }