private void OnSelectedReportedDeploymentChanged(string deploymentId)
        {
            if (DeploymentsList.SelectedIndex != -1)
            {
                if (((DeploymentSummary)DeploymentsList.SelectedItem).Name == deploymentId)
                {
                    return;
                }
            }

            int index = -1;
            DeploymentSummary selectedDeployment = null;

            foreach (DeploymentSummary ds in DeploymentsList.Items)
            {
                ++index;

                if (ds.Name == deploymentId)
                {
                    selectedDeployment = ds;
                    break;
                }
            }

            if (index == -1)
            {
                MessageBox.Show("Deployment is not found.");
                return;
            }

            DeploymentsList.SelectedIndex = index;

            DeploymentPanel.Show(selectedDeployment);
        }
        private async Task LoadDeploymentsAsync()
        {
            if (String.IsNullOrEmpty(_connectionString) || _connectionString == "<connection string>")
            {
                return;
            }

            int             maxDeploymentCount = 20;
            RegistryManager registryManager;

            try
            {
                registryManager = RegistryManager.CreateFromConnectionString(_connectionString);
            }
            catch (Exception e)
            {
                MessageBox.Show("Failed to connect to IoTHub.\n\nMake sure you are using the iothubowner connection string (not a device connection string).\n\nIoT Hub Error: " + e.Message, "Connection Error", MessageBoxButton.OK);
                return;
            }

            try
            {
                _deploymentSummaries = new List <DeploymentSummary>();

                DevicesQuery allDevicesQuery = new DevicesQuery("");
                await allDevicesQuery.Refresh(_connectionString);

                DeploymentSummary cs = new DeploymentSummary();
                cs.Name = DMConstants.AllDevicesConfigName;
                cs.AzureConfiguration          = new Configuration(DMConstants.AllDevicesConfigName);
                cs.AzureConfiguration.Priority = 1;
                cs.AppliedCount  = "-";
                cs.FailedCount   = allDevicesQuery.FailedDeviceCount.ToString();
                cs.SuccessCount  = allDevicesQuery.SuccessDeviceCount.ToString();
                cs.TargetedCount = allDevicesQuery.DeviceCount.ToString();
                cs.PendingCount  = allDevicesQuery.PendingDeviceCount.ToString();

                _deploymentSummaries.Add(cs);

                IEnumerable <Configuration> configurations = await registryManager.GetConfigurationsAsync(maxDeploymentCount);

                foreach (var configuration in configurations)
                {
                    _deploymentSummaries.Add(SummarizeConfiguration(configuration));
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Failed to enumerate existing deployments.", "Enumeration Error", MessageBoxButton.OK);
                return;
            }
        }
        private DeploymentSummary SummarizeConfiguration(Configuration configuration)
        {
            DeploymentSummary cs = new DeploymentSummary();

            cs.AzureConfiguration = configuration;

            cs.Name        = configuration.Id;
            cs.FailedCount = "-";
            if (configuration.Metrics.Results.ContainsKey("failureQuery"))  // Sometimes, the query hasn't been run yet.
            {
                cs.FailedCount = configuration.Metrics.Results["failureQuery"].ToString();
            }

            cs.SuccessCount = "-";
            if (configuration.Metrics.Results.ContainsKey("successQuery"))
            {
                cs.SuccessCount = configuration.Metrics.Results["successQuery"].ToString();
            }

            cs.PendingCount = "-";
            if (configuration.Metrics.Results.ContainsKey("pendingQuery"))
            {
                cs.PendingCount = configuration.Metrics.Results["pendingQuery"].ToString();
            }

            cs.AppliedCount = "-";
            if (configuration.SystemMetrics.Results.ContainsKey("appliedCount"))
            {
                cs.AppliedCount = configuration.SystemMetrics.Results["appliedCount"].ToString();
            }

            cs.TargetedCount = "-";
            if (configuration.SystemMetrics.Results.ContainsKey("targetedCount"))
            {
                cs.TargetedCount = configuration.SystemMetrics.Results["targetedCount"].ToString();
            }

            return(cs);
        }
        public void Show(DeploymentSummary deploymentSummary)
        {
            TargetConditionValueBox.Text = deploymentSummary.AzureConfiguration.TargetCondition;

            MetricsList.Items.Clear();

            foreach (var p in deploymentSummary.AzureConfiguration.Metrics.Queries)
            {
                MetricSummary ms = new MetricSummary();
                ms.Name  = p.Key;
                ms.Query = p.Value;

                if (deploymentSummary.AzureConfiguration.Metrics.Results.Keys.Contains(p.Key))
                {
                    ms.Count = deploymentSummary.AzureConfiguration.Metrics.Results[p.Key].ToString();
                }
                else
                {
                    ms.Count = "<unknown>";
                }

                MetricsList.Items.Add(ms);
            }

            SystemMetricsList.Items.Clear();

            if (deploymentSummary.AzureConfiguration.SystemMetrics != null)
            {
                foreach (var p in deploymentSummary.AzureConfiguration.SystemMetrics.Queries)
                {
                    MetricSummary ms = new MetricSummary();
                    ms.Name  = p.Key;
                    ms.Query = p.Value;

                    string resultCount = "<unknown>";
                    if (deploymentSummary.AzureConfiguration.SystemMetrics.Results.Keys.Contains(p.Key))
                    {
                        long count = deploymentSummary.AzureConfiguration.SystemMetrics.Results[p.Key];
                        resultCount = count.ToString();
                    }
                    ms.Count = resultCount;

                    SystemMetricsList.Items.Add(ms);
                }
            }

            MetricConditionValueBox.Text = "";

            PriorityValueBox.Text = deploymentSummary.AzureConfiguration.Priority.ToString();

            DeploymentNameValueBox.Text = deploymentSummary.AzureConfiguration.Id;

            StringBuilder sb1 = new StringBuilder();

            if (deploymentSummary.AzureConfiguration.Content?.DeviceContent != null)
            {
                IDictionary <string, object> d = deploymentSummary.AzureConfiguration.Content.DeviceContent;

                JObject desiredPropertiesObject = new JObject();
                foreach (var p in d)
                {
                    string   path  = p.Key;
                    string[] parts = path.Split('.');
                    if (parts.Length == 3 && parts[0] == "properties" && parts[1] == "desired")
                    {
                        desiredPropertiesObject[parts[2]] = (JToken)p.Value;
                    }
                }

                DesiredPropertiesValueBox.Text = desiredPropertiesObject.ToString();
            }

            /* ToDo: When module support is added, we need to figure out what to doe with ModuleContent.
             *
             * if (deploymentSummary.AzureConfiguration.Content?.ModulesContent != null)
             * {
             *  foreach (var p in deploymentSummary.AzureConfiguration.Content.ModulesContent)
             *  {
             *      if (sb1.Length > 0)
             *          sb1.Append("\n");
             *
             *      sb1.Append("[M]\n");
             *      sb1.Append("  [K] " + p.Key + "\n");
             *      if (p.Value is IDictionary<string, object>)
             *      {
             *          foreach (var pi in p.Value)
             *          {
             *              if (sb1.Length > 0)
             *                  sb1.Append("\n");
             *
             *              sb1.Append("  [V]\n");
             *              sb1.Append("  " + pi.Key);
             *              sb1.Append(" : ");
             *              sb1.Append(pi.Value);
             *          }
             *      }
             *      else
             *      {
             *          sb1.Append("  [V] " + p.Value + "\n");
             *      }
             *  }
             * }
             */

            _deploymentSummary = deploymentSummary;
        }