From feca5eb8a2efe920cc8925f0c8298a977b85a238 Mon Sep 17 00:00:00 2001 From: Max Hauser Date: Fri, 16 Apr 2021 17:23:40 +0200 Subject: [PATCH] respect common.step attribute of states (#1260) * respect common.step attribute of states - respected if strictObjectChecks active - rounds value to next step - closes #424 * also clarify common.step rounding in jsdoc of strictobjectcheck method --- lib/adapter.js | 9 ++++++++- test/lib/io-package.json | 14 +++++--------- test/lib/testStates.js | 27 +++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/lib/adapter.js b/lib/adapter.js index afc5f14b1f..b0b5f440e6 100644 --- a/lib/adapter.js +++ b/lib/adapter.js @@ -1717,6 +1717,7 @@ function Adapter(options) { /** * Performs the strict object check, which includes checking object existence, read-only logic, type and min/max + * additionally it rounds state values whose objects have a common.step attribute defined * * @param {string} id - id of the state * @param {object} state - ioBroker setState object @@ -1751,8 +1752,14 @@ function Adapter(options) { logger.warn(`${this.namespaceLog} State "${id}" has wrong type "${typeof state.val}" but has to be "${obj.common.type}"`); } - // now check min/max if it's a number + // now round step and check min/max if it's a number if (typeof state.val === 'number') { + if (typeof obj.common.step === 'number' && obj.common.step > 0) { + // round to next step + const inv = 1 / obj.common.step; + state.val = Math.round(state.val * inv) / inv; + } + if (obj.common.max !== undefined && state.val > obj.common.max) { logger.warn(`${this.namespaceLog} State "${id}" has value "${state.val}" greater than max "${obj.common.max}"`); } diff --git a/test/lib/io-package.json b/test/lib/io-package.json index 0d71a32110..09220ed395 100644 --- a/test/lib/io-package.json +++ b/test/lib/io-package.json @@ -2,7 +2,9 @@ "common": { "name": "test", "version": "0.1.0", - "title": "test Adapter", + "titleLang": { + "en": "test Adapter" + }, "desc": { "en": "This adapter connect", "de": "This adapter connect", @@ -44,16 +46,10 @@ { "js-controller": ">=0.9.0" } - ], - "config": { - "width ": 1224, - "height": 520 - } + ] }, "native": { "paramA": "", "paramB": "" - }, - "_id": "system.adapter.test", - "type": "adapter" + } } diff --git a/test/lib/testStates.js b/test/lib/testStates.js index dd96fe43c7..5245e5d06b 100644 --- a/test/lib/testStates.js +++ b/test/lib/testStates.js @@ -833,6 +833,33 @@ function register(it, expect, context) { return Promise.resolve(); }); + it(testName + 'Should round to next 5', async () => { + // we test the step attribute here + await context.adapter.setObjectAsync(`${gid}step`, { + common: { + name: 'test1', + type: 'number', + role: 'level', + min: -100, + max: 100, + step: 5 + }, + native: {}, + type: 'state' + }); + + // now the state should be rounded + await context.adapter.setStateAsync(`${gid}step`, 13, true); + + let state = await context.adapter.getStateAsync(`${gid}step`); + expect(state.val).to.equal(15); + + // now with a negative value + await context.adapter.setStateAsync(`${gid}step`, -18, true); + + state = await context.adapter.getStateAsync(`${gid}step`); + expect(state.val).to.equal(-20); + }); // getHistory - cannot be tested }