From 5678ee6bed91eb5fe8bf5a1ce3bc5b2e764faac0 Mon Sep 17 00:00:00 2001 From: Nikolay Korotkiy Date: Mon, 25 Mar 2024 20:09:32 +0400 Subject: [PATCH] Allow specifying enum titles in virtual device definition (#104) --- debian/changelog | 14 ++++++++++---- wbrules/engine.go | 27 +++++++++++++++++++++++++++ wbrules/rule_meta_test.go | 1 + wbrules/strings.go | 1 + wbrules/testrules_meta.js | 12 ++++++++---- 5 files changed, 47 insertions(+), 8 deletions(-) diff --git a/debian/changelog b/debian/changelog index 4f6a9ef..68c4058 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +wb-rules (2.20.1) stable; urgency=medium + + * Allow specifying enum titles in virtual device definition + + -- Nikolay Korotkiy Fri, 22 Mar 2024 17:15:00 +0400 + wb-rules (2.20.0) stable; urgency=medium * Add device title translation support @@ -166,7 +172,7 @@ wb-rules (2.11.1) stable; urgency=medium * Updated documentation -- Alexandr Degtyarev Wed, 29 Dec 2021 16:07:00 +0400 - + wb-rules (2.11.0) stable; urgency=medium * Add support for Wiren Board 7 (wbgo-related) @@ -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 Wed, 15 Mar 2017 19:00:07 +0300 @@ -454,11 +460,11 @@ wb-rules (1.6.8) stable; urgency=medium -- Evgeny Boger 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 Tue, 24 May 2016 20:27:21 +0300 diff --git a/wbrules/engine.go b/wbrules/engine.go index e2b9f44..defd5aa 100644 --- a/wbrules/engine.go +++ b/wbrules/engine.go @@ -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 { diff --git a/wbrules/rule_meta_test.go b/wbrules/rule_meta_test.go index ff52c53..d931a88 100644 --- a/wbrules/rule_meta_test.go +++ b/wbrules/rule_meta_test.go @@ -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)", diff --git a/wbrules/strings.go b/wbrules/strings.go index 35e9a7f..4e76a9c 100644 --- a/wbrules/strings.go +++ b/wbrules/strings.go @@ -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" diff --git a/wbrules/testrules_meta.js b/wbrules/testrules_meta.js index 6c41791..df08745 100644 --- a/wbrules/testrules_meta.js +++ b/wbrules/testrules_meta.js @@ -20,6 +20,10 @@ defineVirtualDevice('testDevice', { type: 'text', value: 'some text', readonly: false, + enum: { + txt0: { en: 'zero' }, + txt1: { en: 'one' }, + }, }, startControl: { type: 'switch', @@ -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) { @@ -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' } }); } }, });