Пример #1
0
        private static async Task DesiredPropertyUpdateCallback(TwinCollection desiredProperties, object userContext)
        {
            try
            {
                var deviceClient = (DeviceClient)userContext;

                ConsoleWriteLine();
                ConsoleWriteLine($"Received twin update, including {desiredProperties.Count} desired properties:", ConsoleColor.Green);
                ConsoleWriteLine(desiredProperties.ToJson(Formatting.Indented), ConsoleColor.Green);

                var reportedProperties = new TwinCollection();
                var propertyNames      = new List <string>();

                // enumerate through the desired properties
                var enumerator = desiredProperties.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    var pair         = (KeyValuePair <string, object>)enumerator.Current;
                    var propertyName = pair.Key;

                    // copy desired property to reported properties
                    reportedProperties[propertyName] = pair.Value;

                    propertyNames.Add(propertyName);
                }

                reportedProperties["lastUpdateInfo"] =
                    $"Last desired property update @ {DateTime.UtcNow} involved these properties: {string.Join(", ", propertyNames)}";

                ConsoleWriteLine();
                ConsoleWriteLine($"Will change reported properties accordingly...", ConsoleColor.Green);
                await deviceClient.UpdateReportedPropertiesAsync(reportedProperties);

                ConsoleWriteLine($"Successfully changed reported properties", ConsoleColor.Green);

                await GetCurrentTwin(deviceClient);
            }
            catch (Exception ex)
            {
                ConsoleWriteLine($"* ERROR * {ex.Message}", ConsoleColor.Red);
            }
        }