private static async Task <ThermostatData> ExecuteGetThermostatDataAsync(Stream inputBlob, string deviceId, TraceWriter log, ExecutionContext context) { var requireNewReading = true; IConfigurationRoot configurationRoot = ReadConfiguration(context); // Using an In-Memory cache to avoid throttling the API //// TODO: Resolve possible racing conditions if (latestReading != null && latestReadingTimestamp != null) { var cacheTimeout = DefaultCacheTimeout; if (int.TryParse(configurationRoot["ThermostatDataCacheTimeoutMs"], out int cacheTimeoutOverride)) { cacheTimeout = cacheTimeoutOverride; } var newTimestamp = DateTime.UtcNow.AddMilliseconds(-cacheTimeout); if (newTimestamp < latestReadingTimestamp) { requireNewReading = false; } } if (requireNewReading) { log.Info("Obtaining a new Reading from Nest"); var result = await GetThermostatData(inputBlob, deviceId, log, configurationRoot); latestReadingTimestamp = DateTime.UtcNow; latestReading = result; } return(latestReading); }
/// <summary> /// Helper method to convert a <see cref="ThermostatData"/> instance /// to a <see cref="ThermostatInfo"/> instance. /// </summary> /// <param name="data">The data instance.</param> /// <returns>The converted data.</returns> public static ThermostatInfo ToThermostatInfo(this ThermostatData data) { return(new ThermostatInfo() { Uuid = data?.Uuid, Link = data?.Link, Name = data?.Name }); }
/// <summary> /// Helper method to copy data from a <see cref="ThermostatData"/> /// to a <see cref="ThermostatInfo"/> instance. /// </summary> /// <param name="info">The full data instance.</param> /// <param name="data">The data instance.</param> public static void CopyFrom(this ThermostatInfo info, ThermostatData data) { info.Link = data?.Link; info.Name = data?.Name; }