/// <summary> /// Constructor to create an <see cref="IDeviceField"/> from the external <see cref="field"/> object. /// </summary> /// <param name="field">The external <see cref="field"/> to encapsulate.</param> /// <param name="capability">The capabilties describing the field.</param> /// <returns>An <see cref="IDeviceField"/> object.</returns> public static IDeviceField Create(field field, fieldCapability capability = null) { if (field == null) { throw new ArgumentNullException("field", "The external field was not provided from the external service call."); } IDictionary <string, Func <field, fieldCapability, IDeviceField> > typeMap = new Dictionary <string, Func <field, fieldCapability, IDeviceField> >() { { DM_FIELD_STRING, (f, c) => new DeviceField <string>(f, c) }, { DM_FIELD_UNSIGNED_INT, (f, c) => new DeviceField <uint>(f, c) }, { DM_FIELD_ENUM, (f, c) => { //TODO: Parse the known enumeration types // "ON, OFF" return(new DeviceField <bool>(f, c, (name, type) => { if (String.Equals("ON", f.value, StringComparison.CurrentCultureIgnoreCase)) { return (true); } if (String.Equals("OFF", f.value, StringComparison.CurrentCultureIgnoreCase)) { return (false); } Trace.TraceWarning("Unknown {0} detected. Values: [{1}] {2}", DM_FIELD_ENUM, c.rangeType, String.Join(",", c.valueEnum)); return (DeviceField <bool> .DefaultValueConversion(name, type)); })); } } }.WithDefaultValue((f, c) => { Trace.TraceWarning("Unknown fieldType {0} detected. Name: {1}, Value: {2}", field.type, field.name, field.value); return(null); }); return(typeMap[field.type.ToUpper()].Invoke(field, capability)); }
/// <summary> /// Checks to see if the specified IRawDeviceCounter is a number. /// </summary> /// <param name="capability">The field to check.</param> /// <returns>True if the counter is a number; false if otherwise.</returns> public static bool IsNumber(this fieldCapability capability) { if (capability == null) { return(false); } return(String.Equals(capability.type, DeviceFieldFactory.DM_FIELD_UNSIGNED_INT, StringComparison.CurrentCultureIgnoreCase)); }
/// <summary> /// Retrieves the requested capabilities from the list. If not found, returnes the default value.. /// </summary> /// <param name="capabilities">The fields to search.</param> /// <param name="name">The name of the capability to retrieve.</param> /// <param name="defaultValue">The default value to return if the field is not found.</param> /// <returns>True if the counter is a number; false if otherwise.</returns> public static fieldCapability Get(this IEnumerable <fieldCapability> capabilities, string name, fieldCapability defaultValue = null) { if ((capabilities == null) || (!capabilities.Any())) { return(defaultValue); } fieldCapability capability = capabilities.FirstOrDefault(c => String.Equals(c.name, name, StringComparison.CurrentCultureIgnoreCase)); return(capability ?? defaultValue); }
/// <summary> /// Converts the specified <see cref="fieldCapability"/> into the default field representation. /// </summary> /// <param name="capability">The capability to convert to a default field representation.</param> /// <param name="value">The optional default value to assign the field.</param> /// <returns>The default field representation of the capability.</returns> public static field ToField(this fieldCapability capability, string value = null) { if (capability == null) { return(null); } return(new field() { name = capability.name, type = capability.type, value = value ?? capability.value }); }
/// <summary> /// Checks to see if the specified field capability is updatable. /// </summary> /// <param name="capability">The field to inspect.</param> /// <returns>True if updatable, false if otherwise.</returns> private bool IsUserCounterCapabilityUpdatable(fieldCapability capability) { if (capability == null) { return(false); } // The *Account,*Total files indicate they are writable, but older firmware will throw a MANAGEMENT_BAD_OBJECT_ID. // These fields are NOT writable even though the capability may indicate otherwise. return(capability.IsNumber() && !(capability.name ?? String.Empty).EndsWith("Account", StringComparison.CurrentCultureIgnoreCase) && !(capability.name ?? String.Empty).EndsWith("Total", StringComparison.CurrentCultureIgnoreCase)); }
/// <param name="field">The external field value to parse.</param> /// <param name="capabilities">All of the capabilities for the object to be searched for a match against.</param> /// <param name="conversion">The function to execute to convert the value to the appropriate type.</param> internal DeviceField(field field, IEnumerable <fieldCapability> capabilities, Func <string, Type, T> conversion = null) { if (field == null) { throw new ArgumentNullException("field", "The external field was not provided from the external service call."); } Name = field.name; _externalType = field.type; Value = conversion != null?conversion(field.value, Type) : DefaultValueConversion(field.value, Type); // Assign the appropriate capability to the device. fieldCapability capability = capabilities.Get(field.name); if (capability != null) { IsReadable = capability.readable; IsWritable = capability.writable; } }
/// <param name="field">The external field value to parse.</param> /// <param name="capability">The capabilities of the field.</param> /// <param name="conversion">The function to execute to convert the value to the appropriate type.</param> internal DeviceField(field field, fieldCapability capability, Func <string, Type, T> conversion = null) : this(field, new[] { capability }, conversion) { }
/// <summary> /// Occurs when an unknown field is detected. /// </summary> /// <param name="field">The unknown field.</param> /// <param name="capability">The capabilities and limits describing the field.</param> protected virtual void OnUnknownField(field field, fieldCapability capability) { Add(DeviceFieldFactory.Create(field, capability)); }