/// <summary> /// Initialize the device properties for a new device. /// </summary> /// <param name="device"></param> /// <param name="deviceId"></param> /// <param name="isSimulated"></param> /// <returns></returns> private static void InitializeDeviceProperties(DeviceModel device, string deviceId, bool isSimulated) { DeviceProperties deviceProps = new DeviceProperties(); deviceProps.DeviceID = deviceId; deviceProps.HubEnabledState = null; deviceProps.CreatedTime = DateTime.UtcNow; deviceProps.DeviceState = "normal"; deviceProps.UpdatedTime = null; device.DeviceProperties = deviceProps; }
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; } }
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); } }