Пример #1
0
        /// <summary>
        /// Write to the twin for the given flatten
        /// </summary>
        /// <param name="twin">The destination twin</param>
        /// <param name="flatName">The flat name, e.g. tags.city or properties.desired.TTL</param>
        /// <param name="value">The tag/property value</param>
        static public void Set(this Twin twin, string flatName, dynamic value)
        {
            // Remove the common prefix "twin."
            flatName = flatName.TryTrimPrefix(PREFIX_TWIN);

            if (flatName == DEVICEID)
            {
                twin.DeviceId = value.ToString();
            }

            // Pick the selector according to prefix of the flat name, then set the value
            string name;

            foreach (var selector in _selectors)
            {
                if (flatName.TryTrimPrefix(selector.Key, out name))
                {
                    TwinCollectionExtension.Set(selector.Value(twin), name, value);
                    return;
                }
            }

            // Invalid flat name should not cause any exception
            // Write to reported properties is allowed here
        }
Пример #2
0
        /// <summary>
        /// Read from the twin for the given flatten name
        /// </summary>
        /// <param name="twin">The source twin</param>
        /// <param name="flatName">The flat name, e.g. tags.city or properties.desired.TTL</param>
        /// <returns>The tag/property value</returns>
        static public dynamic Get(this Twin twin, string flatName)
        {
            // Remove the common prefix "twin."
            flatName = flatName.TryTrimPrefix(PREFIX_TWIN);

            // "deviceId" is a built-in property of the twin
            if (flatName == DEVICEID)
            {
                return(twin.DeviceId);
            }

            // Pick the selector according to prefix of the flat name, then get the value
            string name;

            foreach (var selector in _selectors)
            {
                if (flatName.TryTrimPrefix(selector.Key, out name))
                {
                    var collection = selector.Value(twin);
                    return(TwinCollectionExtension.Get(collection, name));
                }
            }

            return(null);
        }