/// <summary> /// Initializes trait schemas. /// </summary> /// <param name="cancellationToken">Cancellation token.</param> /// <returns>A dictionary of trait schemas.</returns> private static async Task <IList <TraitSchema> > InitTraitSchemas(CancellationToken cancellationToken = default) { var traitTypesNames = Enum.GetNames(typeof(TraitType)); var traitTypes = traitTypesNames .Where(x => x.ToLower() != "unknown"); var result = new List <TraitSchema>(); foreach (var traitType in traitTypes) { var traitSchema = await TraitSchema.ForTraitType(Enum.Parse <TraitType>(traitType), cancellationToken); if (traitSchema != null) { result.Add(traitSchema); } } return(result); }
/// <summary> /// Gets trait state as a Google device state object in a flattened state. /// </summary> /// <param name="stateCache">Current state cache.</param> /// <param name="traitSchema">Trait schema.</param> /// <returns>A Google device state object in a flattened state.</returns> public IDictionary <string, object> GetGoogleStateFlattened(IDictionary <string, string> stateCache, TraitSchema traitSchema) { var result = new Dictionary <string, object>(); if (State != null) { foreach (var state in State) { if (state.Value.Topic != null && stateCache.ContainsKey(state.Value.Topic)) { var googleType = traitSchema.GetGoogleTypeForFlattenedPath(state.Key); result.Add(state.Key, state.Value.MapValueToGoogle(stateCache[state.Value.Topic], googleType)); } else if (state.Value.Topic == null && state.Value.ValueMap != null && state.Value.ValueMap.Any(x => x is StaticMap)) { var googleType = traitSchema.GetGoogleTypeForFlattenedPath(state.Key); result.Add(state.Key, state.Value.MapValueToGoogle(null, googleType)); } } } return(result); }
/// <summary> /// Gets device state as a Google device state object in a flattened state with dummy data for initial validation. /// </summary> /// <param name="stateConfigs">Current state cache.</param> /// <param name="traitSchema">Trait schema.</param> /// <returns>A Google device state object in a flattened state.</returns> private static IDictionary <string, object> GetFakeGoogleState(IDictionary <string, DeviceState> stateConfigs, TraitSchema traitSchema) { var stateValues = new Dictionary <string, object>(); foreach (var state in stateConfigs) { var googleType = traitSchema.GetGoogleTypeForFlattenedPath(state.Key); var enumValues = traitSchema.GetEnumValuesForFlattenedPath(state.Key); var enumValue = enumValues?.FirstOrDefault()?.ToString(); switch (googleType) { case GoogleType.Bool: stateValues.Add(state.Key, state.Value.MapValueToGoogle(enumValue ?? "true", googleType)); break; case GoogleType.Numeric: stateValues.Add(state.Key, state.Value.MapValueToGoogle(enumValue ?? "1", googleType)); break; case GoogleType.String: default: stateValues.Add(state.Key, state.Value.MapValueToGoogle(enumValue ?? "default", googleType)); break; } } return(stateValues.ToNestedDictionary()); }