Skip to content

Commit

Permalink
Allow specifying enum titles in virtual device definition (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
sikmir committed Mar 25, 2024
1 parent 9a65aba commit 5678ee6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
14 changes: 10 additions & 4 deletions debian/changelog
@@ -1,3 +1,9 @@
wb-rules (2.20.1) stable; urgency=medium

* Allow specifying enum titles in virtual device definition

-- Nikolay Korotkiy <nikolay.korotkiy@wirenboard.com> Fri, 22 Mar 2024 17:15:00 +0400

wb-rules (2.20.0) stable; urgency=medium

* Add device title translation support
Expand Down Expand Up @@ -166,7 +172,7 @@ wb-rules (2.11.1) stable; urgency=medium
* Updated documentation

-- Alexandr Degtyarev <a.degtyarev@wirenboard.com> Wed, 29 Dec 2021 16:07:00 +0400

wb-rules (2.11.0) stable; urgency=medium

* Add support for Wiren Board 7 (wbgo-related)
Expand Down Expand Up @@ -419,7 +425,7 @@ wb-rules (2.1~alpha1) stable; urgency=medium

wb-rules (2.0~beta3) stable; urgency=medium

* Fix: hang on boot on high load
* Fix: hang on boot on high load

-- Nikita Maslov <n.maslov@contactless.ru> Wed, 15 Mar 2017 19:00:07 +0300

Expand Down Expand Up @@ -454,11 +460,11 @@ wb-rules (1.6.8) stable; urgency=medium
-- Evgeny Boger <boger@contactless.ru> Fri, 19 Aug 2016 17:54:47 +0300

wb-rules (1.6.7) stable; urgency=medium

* adds support for delays to alarm engine:
alarmDelayMs: if set, the alarm condition must be violated for
the specified time interval (in ms) for the alarm to be triggered
noAlarmDelayMs: if set, the alarm condition must be met for the
noAlarmDelayMs: if set, the alarm condition must be met for the
specified time interval (in ms) for the alarm to be cleared

-- Evgeny Boger <boger@contactless.ru> Tue, 24 May 2016 20:27:21 +0300
Expand Down
27 changes: 27 additions & 0 deletions wbrules/engine.go
Expand Up @@ -1502,6 +1502,33 @@ func fillControlArgs(devId, ctrlId string, ctrlDef objx.Map, args wbgong.Control
}
}

if ctrlType == wbgong.CONV_TYPE_VALUE || ctrlType == wbgong.CONV_TYPE_TEXT {
enum, ok := ctrlDef[VDEV_CONTROL_DESCR_PROP_ENUM]
if ok {
var enumTitlesMap map[string]wbgong.Title

switch t := enum.(type) {
case map[string]interface{}:
enumTitlesMap = make(map[string]wbgong.Title)
for key, value := range t {
if submap, ok := value.(map[string]interface{}); ok {
titleMap := make(wbgong.Title)
for lang, title := range submap {
if str, ok := title.(string); ok {
titleMap[lang] = str
}
}
enumTitlesMap[key] = titleMap
}
}
default:
return fmt.Errorf("%s/%s: non-map value type %T of enum property", devId, ctrlId, enum)
}

args.SetEnumTitles(enumTitlesMap)
}
}

// get properties for 'range' type
// FIXME: deprecated
if ctrlType == wbgong.CONV_TYPE_RANGE {
Expand Down
1 change: 1 addition & 0 deletions wbrules/rule_meta_test.go
Expand Up @@ -66,6 +66,7 @@ func (s *RuleMetaSuite) TestMeta() {
s.publish("/devices/somedev/controls/sw/meta/error", "another error", "somedev/sw", "testDevice/switchControl")
s.VerifyUnordered(
"driver -> /devices/testDevice/controls/switchControl: [1] (QoS 1, retained)",
"driver -> /devices/testDevice/controls/textControl/meta: [{\"description\":\"old description\",\"enum\":{\"str0\":{\"en\":\"Off\"},\"str1\":{\"en\":\"On\"}},\"error\":\"\",\"order\":5,\"readonly\":false,\"type\":\"text\",\"units\":\"chars\"}] (QoS 1, retained)",
"driver -> /devices/testDevice/controls/textControl/meta: [{\"description\":\"old description\",\"enum\":{\"txt0\":{\"en\":\"zero\"},\"txt1\":{\"en\":\"one\"}},\"error\":\"\",\"order\":5,\"readonly\":false,\"type\":\"text\",\"units\":\"chars\"}] (QoS 1, retained)",
"tst -> /devices/somedev/controls/sw/meta/error: [another error] (QoS 1, retained)",
"wbrules-log -> /wbrules/log/info: [got sw, changed: somedev/sw#error -> another error] (QoS 1)",
Expand Down
1 change: 1 addition & 0 deletions wbrules/strings.go
Expand Up @@ -18,6 +18,7 @@ const (
VDEV_CONTROL_DESCR_PROP_TITLE = "title"
VDEV_CONTROL_DESCR_PROP_ORDER = "order"
VDEV_CONTROL_DESCR_PROP_UNITS = "units"
VDEV_CONTROL_DESCR_PROP_ENUM = "enum"
// FIXME: deprecated
VDEV_CONTROL_DESCR_PROP_MAX = "max"
VDEV_CONTROL_DESCR_PROP_MIN = "min"
Expand Down
12 changes: 8 additions & 4 deletions wbrules/testrules_meta.js
Expand Up @@ -20,6 +20,10 @@ defineVirtualDevice('testDevice', {
type: 'text',
value: 'some text',
readonly: false,
enum: {
txt0: { en: 'zero' },
txt1: { en: 'one' },
},
},
startControl: {
type: 'switch',
Expand All @@ -44,10 +48,6 @@ defineVirtualDevice('testDevice', {
},
});

getDevice('testDevice')
.getControl('textControl')
.setEnumTitles({ txt0: { en: 'zero' }, txt1: { en: 'one' } });

defineRule('onChangeStartControl', {
whenChanged: 'testDevice/startControl',
then: function (newValue, devName, cellName) {
Expand All @@ -74,6 +74,10 @@ defineRule('onChangeStartControl', {
dev['testDevice/textControl#order'] = '5';
dev['testDevice/textControl#units'] = 'chars';
dev['testDevice/textControl#readonly'] = '0';

getDevice('testDevice')
.getControl('textControl')
.setEnumTitles({ str0: { en: 'Off' }, str1: { en: 'On' } });
}
},
});
Expand Down

0 comments on commit 5678ee6

Please sign in to comment.