|
1 | 1 | // Copyright (c) Microsoft. All rights reserved. |
2 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
3 | 3 |
|
| 4 | +using Newtonsoft.Json; |
4 | 5 | using System; |
5 | 6 | using System.Collections.Generic; |
6 | | -using System.Text; |
7 | 7 | using System.Threading; |
8 | 8 | using System.Threading.Tasks; |
9 | | -using Microsoft.Azure.Devices; |
10 | 9 |
|
11 | 10 | namespace Microsoft.Azure.Devices.Samples |
12 | 11 | { |
| 12 | + /// <summary> |
| 13 | + /// This sample demonstrates automatic device management using device configurations. |
| 14 | + /// For module configurations, refer to: https://github.com/Azure-Samples/azure-iot-samples-csharp/tree/master/iot-hub/Samples/service/EdgeDeploymentSample |
| 15 | + /// </summary> |
13 | 16 | public class AutomaticDeviceManagementSample |
14 | 17 | { |
15 | 18 | private readonly RegistryManager _registryManager; |
@@ -47,117 +50,54 @@ public async Task RunSampleAsync() |
47 | 50 | await GetConfigurations(5).ConfigureAwait(false); |
48 | 51 | } |
49 | 52 |
|
50 | | - public async Task AddDeviceConfiguration(string configurationId) |
| 53 | + private async Task AddDeviceConfiguration(string configurationId) |
51 | 54 | { |
52 | 55 | Configuration configuration = new Configuration(configurationId); |
53 | 56 |
|
54 | 57 | CreateDeviceContent(configuration, configurationId); |
55 | | - CreateModulesContent(configuration, configurationId); |
56 | 58 | CreateMetricsAndTargetCondition(configuration, configurationId); |
57 | 59 |
|
58 | 60 | await _registryManager.AddConfigurationAsync(configuration).ConfigureAwait(false); |
59 | 61 |
|
60 | | - Console.WriteLine("Configuration added, id: " + configurationId); |
| 62 | + Console.WriteLine($"Configuration added, id: {configurationId}"); |
61 | 63 | } |
62 | 64 |
|
63 | | - public void CreateDeviceContent(Configuration configuration, string configurationId) |
| 65 | + private void CreateDeviceContent(Configuration configuration, string configurationId) |
64 | 66 | { |
65 | | - configuration.Content = new ConfigurationContent(); |
66 | | - configuration.Content.DeviceContent = new Dictionary<string, object>(); |
| 67 | + configuration.Content = new ConfigurationContent |
| 68 | + { |
| 69 | + DeviceContent = new Dictionary<string, object>() |
| 70 | + }; |
67 | 71 | configuration.Content.DeviceContent["properties.desired.deviceContent_key"] = "deviceContent_value-" + configurationId; |
68 | 72 | } |
69 | 73 |
|
70 | | - public void CreateModulesContent(Configuration configuration, string configurationId) |
71 | | - { |
72 | | - configuration.Content.ModulesContent = new Dictionary<string, IDictionary<string, object>>(); |
73 | | - IDictionary<string, object> modules_value = new Dictionary<string, object>(); |
74 | | - modules_value["properties.desired.modulesContent_key"] = "modulesContent_value-" + configurationId; |
75 | | - configuration.Content.ModulesContent["properties.desired.modules_key"] = modules_value; |
76 | | - } |
77 | | - |
78 | | - public void CreateMetricsAndTargetCondition(Configuration configuration, string configurationId) |
| 74 | + private void CreateMetricsAndTargetCondition(Configuration configuration, string configurationId) |
79 | 75 | { |
80 | 76 | configuration.Metrics.Queries.Add("waterSettingsPending", "SELECT deviceId FROM devices WHERE properties.reported.chillerWaterSettings.status=\'pending\'"); |
81 | 77 | configuration.TargetCondition = "properties.reported.chillerProperties.model=\'4000x\'"; |
82 | 78 | configuration.Priority = 20; |
83 | 79 | } |
84 | 80 |
|
85 | | - public async Task DeleteConfiguration(string configurationId) |
| 81 | + private async Task DeleteConfiguration(string configurationId) |
86 | 82 | { |
87 | 83 | await _registryManager.RemoveConfigurationAsync(configurationId).ConfigureAwait(false); |
88 | 84 |
|
89 | | - Console.WriteLine("Configuration deleted, id: " + configurationId); |
| 85 | + Console.WriteLine($"Configuration deleted, id: {configurationId}"); |
90 | 86 | } |
91 | 87 |
|
92 | | - public async Task GetConfigurations(int count) |
| 88 | + private async Task GetConfigurations(int count) |
93 | 89 | { |
94 | 90 | IEnumerable<Configuration> configurations = await _registryManager.GetConfigurationsAsync(count).ConfigureAwait(false); |
95 | 91 |
|
96 | 92 | // Check configuration's metrics for expected conditions |
97 | 93 | foreach (var configuration in configurations) |
98 | 94 | { |
99 | | - PrintConfiguration(configuration); |
| 95 | + string configurationString = JsonConvert.SerializeObject(configuration, Formatting.Indented); |
| 96 | + Console.WriteLine(configurationString); |
100 | 97 | Thread.Sleep(1000); |
101 | 98 | } |
102 | 99 |
|
103 | 100 | Console.WriteLine("Configurations received"); |
104 | 101 | } |
105 | | - |
106 | | - public void PrintConfiguration(Configuration configuration) |
107 | | - { |
108 | | - Console.WriteLine("Configuration Id: " + configuration.Id); |
109 | | - Console.WriteLine("Configuration SchemaVersion: " + configuration.SchemaVersion); |
110 | | - |
111 | | - Console.WriteLine("Configuration Labels: " + configuration.Labels); |
112 | | - |
113 | | - PrintContent(configuration.ContentType, configuration.Content); |
114 | | - |
115 | | - Console.WriteLine("Configuration TargetCondition: " + configuration.TargetCondition); |
116 | | - Console.WriteLine("Configuration CreatedTimeUtc: " + configuration.CreatedTimeUtc); |
117 | | - Console.WriteLine("Configuration LastUpdatedTimeUtc: " + configuration.LastUpdatedTimeUtc); |
118 | | - |
119 | | - Console.WriteLine("Configuration Priority: " + configuration.Priority); |
120 | | - |
121 | | - PrintConfigurationMetrics(configuration.SystemMetrics, "SystemMetrics"); |
122 | | - PrintConfigurationMetrics(configuration.Metrics, "Metrics"); |
123 | | - |
124 | | - Console.WriteLine("Configuration ETag: " + configuration.ETag); |
125 | | - Console.WriteLine("------------------------------------------------------------"); |
126 | | - } |
127 | | - |
128 | | - private void PrintContent(string contentType, ConfigurationContent configurationContent) |
129 | | - { |
130 | | - Console.WriteLine($"Configuration Content [type = {contentType}]"); |
131 | | - |
132 | | - Console.WriteLine("ModuleContent:"); |
133 | | - foreach (string modulesContentKey in configurationContent.ModulesContent.Keys) |
134 | | - { |
135 | | - foreach (string key in configurationContent.ModulesContent[modulesContentKey].Keys) |
136 | | - { |
137 | | - Console.WriteLine($"\t\t{key} = {configurationContent.ModulesContent[modulesContentKey][key]}"); |
138 | | - } |
139 | | - } |
140 | | - |
141 | | - Console.WriteLine("DeviceContent:"); |
142 | | - foreach (string key in configurationContent.DeviceContent.Keys) |
143 | | - { |
144 | | - Console.WriteLine($"\t{key} = {configurationContent.DeviceContent[key]}"); |
145 | | - } |
146 | | - } |
147 | | - |
148 | | - private void PrintConfigurationMetrics(ConfigurationMetrics metrics, string title) |
149 | | - { |
150 | | - Console.WriteLine($"{title} Results: ({metrics.Results.Count})"); |
151 | | - foreach (string key in metrics.Results.Keys) |
152 | | - { |
153 | | - Console.WriteLine($"\t{key} = {metrics.Results[key]}"); |
154 | | - } |
155 | | - |
156 | | - Console.WriteLine($"{title} Queries: ({metrics.Queries.Count})"); |
157 | | - foreach (string key in metrics.Queries.Keys) |
158 | | - { |
159 | | - Console.WriteLine($"\t{key} = {metrics.Queries[key]}"); |
160 | | - } |
161 | | - } |
162 | 102 | } |
163 | 103 | } |
0 commit comments