Skip to content

Commit

Permalink
Merge pull request #7 from wirenboard/feature/37632-update-value-with…
Browse files Browse the repository at this point in the history
…out-notify

Update control values without local notify
  • Loading branch information
webconn authored Aug 26, 2021
2 parents 871bfcf + f253da6 commit fda7d23
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 16 deletions.
10 changes: 4 additions & 6 deletions control.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,9 @@ type Control interface {
// It also means that storage will be not used to store or restore values at all
SetLazyInit(bool) FuncError

// universal interface for UpdateValue and SetOnValue
SetValue(val interface{}) FuncError

// Updates control value for local device
UpdateValue(val interface{}) FuncError
// and notifies subscribers if flag is set
UpdateValue(val interface{}, notifySubs bool) FuncError

// Sets '/on' value for external devices
SetOnValue(val interface{}) FuncError
Expand Down Expand Up @@ -120,10 +118,10 @@ type Control interface {
// Methodes to accept values from MQTT, called generally by driver
AcceptValue(rawValue string) error
AcceptOnValue(rawValue string) error
AcceptMeta(event NewExternalDeviceControlMetaEvent) error
AcceptMeta(metaType, value string) error
}

// ControlValueHandler is a function that handles new values on /devices/+/controls/+
// XXX: TBD: reaction on error?
// Handlers are running sync with DriverFrontend, so try not to push heavy stuff here
type ControlValueHandler func(control Control, value interface{}, tx DriverTx) error
type ControlValueHandler func(control Control, value interface{}, prevValue interface{}, tx DriverTx) error
92 changes: 92 additions & 0 deletions conventions.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package wbgong

import (
"log"
)

const (
//
// Format strings for fmt.Printf to form topic names
Expand Down Expand Up @@ -107,3 +111,91 @@ const (
CONV_DATATYPE_FLOAT
CONV_DATATYPE_BUTTON
)

var (
funcToTypedValue func(string, string) (interface{}, error)
funcRawValueToDataTyped func(string, ControlDataType) (interface{}, error)
funcToRawValue func(interface{}, string) (string, error)
funcDataTypedToRawValue func(interface{}, ControlDataType) (string, error)
funcGetDefaultValue func(string) (string, error)
)

func ToTypedValue(rawValue, typestr string) (interface{}, error) {
if funcToTypedValue != nil {
return funcToTypedValue(rawValue, typestr)
}
funcSym, errSym := plug.Lookup("ToTypedValue")
if errSym != nil {
log.Fatalf("Error in lookup symbol: %s", errSym)
}
var okResolve bool
funcToTypedValue, okResolve = funcSym.(func(string, string) (interface{}, error))
if !okResolve {
log.Fatal("Wrong sign on resolving func")
}
return funcToTypedValue(rawValue, typestr)
}

func RawValueToDataTyped(rawValue string, dataType ControlDataType) (interface{}, error) {
if funcRawValueToDataTyped != nil {
return funcRawValueToDataTyped(rawValue, dataType)
}
funcSym, errSym := plug.Lookup("RawValueToDataTyped")
if errSym != nil {
log.Fatalf("Error in lookup symbol: %s", errSym)
}
var okResolve bool
funcRawValueToDataTyped, okResolve = funcSym.(func(string, ControlDataType) (interface{}, error))
if !okResolve {
log.Fatal("Wrong sign on resolving func")
}
return funcRawValueToDataTyped(rawValue, dataType)
}

func ToRawValue(value interface{}, typestr string) (raw string, err error) {
if funcToRawValue != nil {
return funcToRawValue(value, typestr)
}
funcSym, errSym := plug.Lookup("ToRawValue")
if errSym != nil {
log.Fatalf("Error in lookup symbol: %s", errSym)
}
var okResolve bool
funcToRawValue, okResolve = funcSym.(func(interface{}, string) (string, error))
if !okResolve {
log.Fatal("Wrong sign on resolving func")
}
return funcToRawValue(value, typestr)
}

func DataTypedToRawValue(value interface{}, dataType ControlDataType) (raw string, err error) {
if funcDataTypedToRawValue != nil {
return funcDataTypedToRawValue(value, dataType)
}
funcSym, errSym := plug.Lookup("DataTypedToRawValue")
if errSym != nil {
log.Fatalf("Error in lookup symbol: %s", errSym)
}
var okResolve bool
funcDataTypedToRawValue, okResolve = funcSym.(func(interface{}, ControlDataType) (string, error))
if !okResolve {
log.Fatal("Wrong sign on resolving func")
}
return funcDataTypedToRawValue(value, dataType)
}

func GetDefaultValue(typestr string) (raw string, err error) {
if funcGetDefaultValue != nil {
return funcGetDefaultValue(typestr)
}
funcSym, errSym := plug.Lookup("GetDefaultValue")
if errSym != nil {
log.Fatalf("Error in lookup symbol: %s", errSym)
}
var okResolve bool
funcGetDefaultValue, okResolve = funcSym.(func(string) (string, error))
if !okResolve {
log.Fatal("Wrong sign on resolving func")
}
return funcGetDefaultValue(typestr)
}
16 changes: 9 additions & 7 deletions events.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ func (e NewExternalDeviceMetaEvent) String() string {

// NewExternalDeviceControlMetaEvent a new external device control metadata received
type NewExternalDeviceControlMetaEvent struct {
Control Control
Type string
Value string
Control Control
Type string
Value string
PrevValue string
}

func (e NewExternalDeviceControlMetaEvent) String() string {
Expand All @@ -76,13 +77,14 @@ func (e NewExternalDeviceControlMetaEvent) String() string {
// Device may be either local or external. For local controls
// this event ignored by driver (sent for user handlers only)
type ControlValueEvent struct {
Control Control
RawValue string
Control Control
RawValue string
PrevRawValue string
}

func (e ControlValueEvent) String() string {
return fmt.Sprintf("ControlValueEvent{Device:%s,Control:%s,Value:%s}", e.Control.GetDevice().GetId(),
e.Control.GetId(), e.RawValue)
return fmt.Sprintf("ControlValueEvent{Device:%s,Control:%s,Value:%s->%s}", e.Control.GetDevice().GetId(),
e.Control.GetId(), e.PrevRawValue, e.RawValue)
}

// ControlOnValueEvent control received 'on' value
Expand Down
6 changes: 3 additions & 3 deletions tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ type DeviceDriverTx interface {
CreateControl(args ControlArgs) func() (Control, error)

// UpdateControlValue updates value for given control
// (sends message to /d/+/c/+)
// UpdateControlValues is a future request
UpdateControlValue(control Control, rawValue string) func() error
// (sends message to /d/+/c/+) and notifies local subscribers
// about this change if notification flag is set
UpdateControlValue(control Control, rawValue string, prevRawValue string, notifySubs bool) func() error

UpdateControlMeta(control Control, meta, value string) func() error
UpdateDeviceMeta(dev LocalDevice, meta, value string) func() error
Expand Down

0 comments on commit fda7d23

Please sign in to comment.