Skip to content

Commit

Permalink
Feature/add control meta v2 (#10)
Browse files Browse the repository at this point in the history
* Added CONV_CONTROL_META_V2_FMT
* Changed MetaInfo type
* Added Title type
* Changed signature for UpdateMeta methods
* Changed String() method
* Added addition info to error log
* Added UpdateControlMetaJson(control Control) func() error method to DeviceDriverTx
* Added UpdateControlMetaJson() method
* Added MetaJson method to Control interface
* Added method UpdateControlMetaJson to fake_driver
  • Loading branch information
Roman Kochkin authored Aug 26, 2022
1 parent 64b09fd commit 4a71179
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 16 deletions.
11 changes: 7 additions & 4 deletions control.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type ControlError interface {
type ControlArgs interface {
SetDevice(Device) ControlArgs
SetId(string) ControlArgs
SetTitle(string) ControlArgs
SetTitle(title Title) ControlArgs
SetDescription(string) ControlArgs
SetType(string) ControlArgs
SetUnits(string) ControlArgs
Expand All @@ -28,7 +28,7 @@ type ControlArgs interface {

GetDevice() Device
GetID() *string
GetTitle() *string
GetTitle() *Title
GetDescription() *string
GetType() *string
GetUnits() *string
Expand Down Expand Up @@ -66,7 +66,7 @@ type Control interface {

// generic getters
GetId() string // Gets control id (/devices/+/controls/[id])
GetTitle() string // Gets control title (/meta/title)
GetTitle() Title // Gets control title (/meta/title)
GetDescription() string // Gets control description (/meta/description)
GetType() string // Gets control type string (/meta/type) (TODO: special type for this)
GetUnits() string // Gets control value units (/meta/units)
Expand All @@ -81,7 +81,7 @@ type Control interface {

// generic setters
SetDescription(desc string) FuncError
SetTitle(title string) FuncError
SetTitle(title Title) FuncError
SetType(t string) FuncError
SetUnits(units string) FuncError
SetReadonly(r bool) FuncError
Expand All @@ -104,6 +104,9 @@ type Control interface {
// Gets all metadata from control (for driver)
GetMeta() MetaInfo

// Gets all metadata from control for /meta (for driver)
GetMetaJson() MetaInfo

// Saves single meta value in control structure (for driver)
SetSingleMeta(meta string, value string) error

Expand Down
6 changes: 6 additions & 0 deletions conventions.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ const (
// 3. Meta subtopic name
CONV_CONTROL_META_FMT = "/devices/%s/controls/%s/meta/%s"

// Device control meta info v2 topic format string
// Parameters:
// 1. Device name
// 2. Control name
CONV_CONTROL_META_V2_FMT = "/devices/%s/controls/%s/meta"

// Device control all meta info topic format string
// Parameters:
// 1. Device name
Expand Down
4 changes: 2 additions & 2 deletions devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ type LocalDeviceArgs interface {
}

// MetaInfo is a type that represents /meta/+ topics for drivers and controls
type MetaInfo map[string]string
type MetaInfo map[string]interface{}

// Implementation of Stringer interface to print metadata correctly
func (m MetaInfo) String() (ret string) {
Expand All @@ -118,7 +118,7 @@ func (m MetaInfo) String() (ret string) {
sort.Strings(keys)

for _, key := range keys {
ret += fmt.Sprintf("%s: '%s' ", key, m[key])
ret += fmt.Sprintf("%s: '%v' ", key, m[key])
}

ret += "]"
Expand Down
5 changes: 3 additions & 2 deletions driver_frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ type DriverBackend interface {
UpdateControlValue(control Control, rawValue string) <-chan error
SetOnValue(control Control, rawValue string) <-chan error

UpdateControlMeta(control Control, meta, value string) <-chan error
UpdateDeviceMeta(dev LocalDevice, meta, value string) <-chan error
UpdateControlMeta(control Control, meta string, value interface{}) <-chan error
UpdateControlMetaJson(control Control) <-chan error
UpdateDeviceMeta(dev LocalDevice, meta string, value interface{}) <-chan error

// these are sent by suicide devices (when all device/control info is cleared)
RemoveExternalDevice(dev ExternalDevice)
Expand Down
4 changes: 2 additions & 2 deletions events.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ func (e NewExternalDeviceMetaEvent) String() string {
type NewExternalDeviceControlMetaEvent struct {
Control Control
Type string
Value string
PrevValue string
Value interface{}
PrevValue interface{}
}

func (e NewExternalDeviceControlMetaEvent) String() string {
Expand Down
9 changes: 7 additions & 2 deletions testutils/fake_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,21 @@ func (backend *FakeDriverBackend) SetOnValue(control wbgong.Control, rawValue st
return closedChan()
}

func (backend *FakeDriverBackend) UpdateDeviceMeta(dev wbgong.LocalDevice, meta, value string) <-chan error {
func (backend *FakeDriverBackend) UpdateDeviceMeta(dev wbgong.LocalDevice, meta string, value interface{}) <-chan error {
backend.Rec("[FakeDriverBackend] UpdateDeviceMeta(device %s, meta %s, value %s)", dev.GetId(), meta, value)
return closedChan()
}

func (backend *FakeDriverBackend) UpdateControlMeta(control wbgong.Control, meta, value string) <-chan error {
func (backend *FakeDriverBackend) UpdateControlMeta(control wbgong.Control, meta string, value interface{}) <-chan error {
backend.Rec("[FakeDriverBackend] UpdateControlMeta(control %s/%s, meta %s, value %s)", control.GetDevice().GetId(), control.GetId(), meta, value)
return closedChan()
}

func (backend *FakeDriverBackend) UpdateControlMetaJson(control wbgong.Control) <-chan error {
backend.Rec("[FakeDriverBackend] UpdateControlMetaJson(control %s/%s)", control.GetDevice().GetId(), control.GetId())
return closedChan()
}

func (backend *FakeDriverBackend) RemoveControl(ctrl wbgong.Control) <-chan error {
backend.Rec("[FakeDriverBackend] RemoveControl(control %s/%s)", ctrl.GetDevice().GetId(), ctrl.GetId())
return closedChan()
Expand Down
4 changes: 2 additions & 2 deletions testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"testing"
"time"

"github.com/wirenboard/wbgong"
"github.com/stretchr/objx"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/wirenboard/wbgong"
)

const (
Expand Down Expand Up @@ -182,7 +182,7 @@ func (rec *Recorder) verify(sortLogs bool, msg string, logs []interface{}) {
select {
case <-timer.C:
require.FailNow(rec.t, "timed out waiting for log item",
"%s", expectedItem)
"expectedItem: %s\nItems left: %d", expectedItem, len(logs)-n)
case logItem := <-rec.ch:
timer.Stop()
// If a regular expression is specified and it
Expand Down
5 changes: 5 additions & 0 deletions title.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package wbgong

// Name type whose key is the language code. For example: "en", "ru"
// The value is the text that appears in the title
type Title map[string]string
5 changes: 3 additions & 2 deletions tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ type DeviceDriverTx interface {
// 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
UpdateControlMeta(control Control, meta string, value interface{}) func() error
UpdateControlMetaJson(control Control) func() error
UpdateDeviceMeta(dev LocalDevice, meta string, value interface{}) func() error

// SetOnValue sends /on value for given control
// SetOnValue is a future request
Expand Down

0 comments on commit 4a71179

Please sign in to comment.