示例#1
0
        private async Task GetInitialConfiguration()
        {
            // First - Read settings from a file
            SumpPumpSettings fileSettings = null;
            SumpPumpSettings twinSettings = null;

            if (File.Exists(ConfigFile))
            {
                fileSettings = JsonConvert.DeserializeObject <SumpPumpSettings>(File.ReadAllText(ConfigFile));
            }

            // Now grab the device twin settings
            var twin = await iotClient.GetTwinAsync();

            if (twin != null)
            {
                twinSettings = JsonConvert.DeserializeObject <SumpPumpSettings>(twin.Properties.Desired.ToString());
            }

            // Take the best version of the settings
            if (twinSettings != null)
            {
                settings = twinSettings;
            }
            else if (fileSettings != null)
            {
                settings = fileSettings;
            }

            // Report back to the device twin that we took the new settings
            if (twinSettings != null)
            {
                await SaveSettings(fileSettings, twinSettings);
            }
        }
示例#2
0
        private async Task SaveSettings(SumpPumpSettings currentSettings, SumpPumpSettings newSettings)
        {
            if (currentSettings == null || currentSettings.DeviceName != newSettings.DeviceName || currentSettings.MaxWaterLevel != newSettings.MaxWaterLevel)
            {
                // Save settings to a file
                string newSettingsJson = JsonConvert.SerializeObject(newSettings);
                using (var writer = File.CreateText(ConfigFile))
                {
                    writer.WriteLine(newSettingsJson);
                    writer.Flush();
                    writer.Close();
                }

                // Tell Azure we took their settings
                var serializerSettings = new JsonSerializerSettings()
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                };
                await iotClient.UpdateReportedPropertiesAsync(JsonConvert.DeserializeObject <TwinCollection>(newSettingsJson, serializerSettings));
            }
        }