private static void ApplyPropertyValueModels(
            DeviceProperties deviceProperties,
            IEnumerable <DevicePropertyValueModel> devicePropertyValueModels)
        {
            object[]      args;
            TypeConverter converter;
            Type          devicePropertiesType;
            Dictionary <string, DevicePropertyMetadata> devicePropertyIndex;
            Dictionary <string, PropertyInfo>           propIndex;
            PropertyInfo           propInfo;
            DevicePropertyMetadata propMetadata;
            MethodInfo             setter;

            devicePropertyIndex = GetDevicePropertyConfiguration().ToDictionary(t => t.Name);

            devicePropertiesType = deviceProperties.GetType();
            propIndex            = devicePropertiesType.GetProperties().ToDictionary(t => t.Name);

            args = new object[1];
            foreach (DevicePropertyValueModel propVal in devicePropertyValueModels)
            {
                if ((propVal == null) ||
                    string.IsNullOrEmpty(propVal.Name))
                {
                    continue;
                }

                // Pass through properties that don't have a specified
                // configuration.
                if (devicePropertyIndex.TryGetValue(propVal.Name, out propMetadata) && !propMetadata.IsEditable)
                {
                    continue;
                }

                if (!propIndex.TryGetValue(propVal.Name, out propInfo) ||
                    ((setter = propInfo.GetSetMethod()) == null) ||
                    ((converter = TypeDescriptor.GetConverter(propInfo.PropertyType)) == null))
                {
                    continue;
                }

                try
                {
                    args[0] = converter.ConvertFromString(propVal.Value);
                }
                catch (NotSupportedException ex)
                {
                    throw new InvalidOperationException(
                              string.Format(
                                  CultureInfo.InvariantCulture,
                                  "Unable to assign value, \"{0},\" to Device property, {1}.",
                                  propVal.Value,
                                  propInfo.Name),
                              ex);
                }

                setter.Invoke(deviceProperties, args);
            }
        }
Пример #2
0
        /// <summary>
        /// Cross synchonize DeviceProperties and ReportedProperties
        /// </summary>
        /// <returns></returns>
        protected void CrossSyncProperties(TwinCollection patch, TwinCollection reported, bool regenerate)
        {
            var devicePropertiesType = DeviceProperties.GetType();
            var reportedPairs        = reported.AsEnumerableFlatten().ToDictionary(pair => pair.Key, pair => pair.Value);

            if (!regenerate)
            {
                // Overwrite regenerated DeviceProperties by current ReportedProperties
                foreach (var pair in reportedPairs)
                {
                    string devicePropertyName = _propertyMapping.SingleOrDefault(p => p.Value == pair.Key).Key;
                    if (string.IsNullOrWhiteSpace(devicePropertyName))
                    {
                        continue;
                    }

                    try
                    {
                        DeviceProperties.SetProperty(devicePropertyName, pair.Value.Value);
                    }
                    catch
                    {
                        // Ignore any failure while overwriting the DeviceProperties
                    }
                }
            }

            // Add missing DeviceProperties to ReportedProperties
            foreach (var property in devicePropertiesType.GetProperties())
            {
                string reportedName;
                if (!_propertyMapping.TryGetValue(property.Name, out reportedName))
                {
                    continue;
                }

                var value = property.GetValue(DeviceProperties);
                if (regenerate || value != null && !reportedPairs.ContainsKey(reportedName))
                {
                    patch.Set(reportedName, value);
                }
            }
        }
Пример #3
0
        private UpdateDevicePayload AppendExistingDeviceProperties(UpdateDevicePayload updateDevice, DeviceProperties existingDeviceProp)
        {
            foreach (PropertyInfo propertyInfo in updateDevice.GetType().GetProperties().Where(p => p.CanRead))
            {
                if (propertyInfo.Name != "OwningCustomerUID")
                {
                    if (propertyInfo.GetValue(updateDevice) == null)
                    {
                        object value = existingDeviceProp.GetType().GetProperties().Single(p => p.Name.ToString() == propertyInfo.Name.ToString()).GetValue(existingDeviceProp);
                        propertyInfo.SetValue(updateDevice, value);
                    }
                    else if (propertyInfo.GetValue(updateDevice)?.ToString() == string.Empty)
                    {
                        propertyInfo.SetValue(updateDevice, null);
                    }
                }
            }

            updateDevice.DeviceUID = Guid.Parse(existingDeviceProp.DeviceUID);
            return(updateDevice);
        }
        private static IEnumerable <DevicePropertyValueModel> ExtractPropertyValueModels(
            DeviceProperties deviceProperties)
        {
            DevicePropertyValueModel currentData;
            object currentValue;
            Dictionary <string, DevicePropertyMetadata> devicePropertyIndex;
            Type                   devicePropertiesType;
            bool                   isDisplayedRegistered;
            bool                   isDisplayedUnregistered;
            bool                   isEditable;
            int                    editableOrdering;
            MethodInfo             getMethod;
            int                    nonediableOrdering;
            DevicePropertyMetadata propertyMetadata;
            PropertyType           propertyType;

            if (deviceProperties == null)
            {
                throw new ArgumentNullException("deviceProperties is a null reference.");
            }

            devicePropertyIndex = GetDevicePropertyConfiguration().ToDictionary(t => t.Name);

            // For now, display r/o properties first.
            editableOrdering   = 1;
            nonediableOrdering = int.MinValue;

            devicePropertiesType = deviceProperties.GetType();
            foreach (PropertyInfo prop in devicePropertiesType.GetProperties())
            {
                if (devicePropertyIndex.TryGetValue(
                        prop.Name,
                        out propertyMetadata))
                {
                    isDisplayedRegistered   = propertyMetadata.IsDisplayedForRegisteredDevices;
                    isDisplayedUnregistered = propertyMetadata.IsDisplayedForUnregisteredDevices;
                    isEditable   = propertyMetadata.IsEditable;
                    propertyType = propertyMetadata.PropertyType;
                }
                else
                {
                    isDisplayedRegistered   = isEditable = true;
                    isDisplayedUnregistered = false;
                    propertyType            = PropertyType.String;
                }

                if (!isDisplayedRegistered && !isDisplayedUnregistered)
                {
                    continue;
                }

                if ((getMethod = prop.GetGetMethod()) == null)
                {
                    continue;
                }

                // Mark R/O properties as not-ediable.
                if (!prop.CanWrite)
                {
                    isEditable = false;
                }

                currentData = new DevicePropertyValueModel()
                {
                    Name         = prop.Name,
                    PropertyType = propertyType
                };

                if (isEditable)
                {
                    currentData.IsEditable   = true;
                    currentData.DisplayOrder = editableOrdering++;
                }
                else
                {
                    currentData.IsEditable   = false;
                    currentData.DisplayOrder = nonediableOrdering++;
                }

                currentData.IsIncludedWithUnregisteredDevices = isDisplayedUnregistered;

                currentValue = getMethod.Invoke(deviceProperties, ReflectionHelper.EmptyArray);
                if (currentValue == null)
                {
                    currentData.Value = string.Empty;
                }
                else
                {
                    currentData.Value = string.Format(CultureInfo.InvariantCulture, "{0}", currentValue);
                }

                yield return(currentData);
            }
        }