Esempio n. 1
0
// Sets the value of the characteristic
// The implementation makes sure that the type of the value stays the same
// E.g. Type of characteristic value int, calling setValue("10.5") sets the value to int(10)
//
// When permissions are write only, this methods does not set the Value field.
func (c *Characteristic) setValue(value interface{}, conn net.Conn) {
	if c.Value != nil {
		converted, err := to.Convert(value, reflect.TypeOf(c.Value).Kind())
		if err == nil {
			value = converted
		}
	}

	// Ignore when new value is same
	if c.Value == value {
		return
	}

	// Ignore new values from remote when permissions don't allow write
	if c.hasWritePermissions() == false && conn != nil {
		return
	}

	old := c.Value
	if c.isWriteOnly() == false {
		c.Value = value
	} else {
		c.Value = nil
	}

	if conn != nil {
		c.onConnChange(c.connChangeFuncs, conn, value, old)
	} else {
		c.onChange(c.localChangeFuncs, value, old)
	}
}
Esempio n. 2
0
// Sets the value of the characteristic
// The implementation makes sure that the type of the value stays the same
// E.g. Type of characteristic value int, calling updateValue("10.5") sets the value to int(10)
//
// When permissions are write only, this methods does not set the Value field.
func (c *Characteristic) updateValue(value interface{}, conn net.Conn) {
	if c.Value != nil {
		if converted, err := to.Convert(value, reflect.TypeOf(c.Value).Kind()); err == nil {
			value = converted
		}
	}

	// Value must be within min and max
	switch c.Format {
	case FormatFloat:
		value = c.boundFloat64Value(value.(float64))
	case FormatUInt8, FormatUInt16, FormatUInt32, FormatUInt64, FormatInt32:
		value = c.boundIntValue(value.(int))
	}

	// Ignore when new value is same
	if c.Value == value {
		return
	}

	// Ignore new values from remote when permissions don't allow write
	if c.hasWritePerms() == false && conn != nil {
		return
	}

	old := c.Value
	if c.isWriteOnly() == false {
		c.Value = value
	} else {
		c.Value = nil
	}

	if conn != nil {
		c.onValueUpdateFromConn(c.connValueUpdateFuncs, conn, value, old)
	} else {
		c.onValueUpdate(c.valueChangeFuncs, value, old)
	}
}