Skip to content

Commit

Permalink
respect common.step attribute of states (#1260)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
foxriver76 authored Apr 16, 2021
1 parent ab79618 commit feca5eb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
9 changes: 8 additions & 1 deletion lib/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}"`);
}
Expand Down
14 changes: 5 additions & 9 deletions test/lib/io-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -44,16 +46,10 @@
{
"js-controller": ">=0.9.0"
}
],
"config": {
"width ": 1224,
"height": 520
}
]
},
"native": {
"paramA": "",
"paramB": ""
},
"_id": "system.adapter.test",
"type": "adapter"
}
}
27 changes: 27 additions & 0 deletions test/lib/testStates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit feca5eb

Please sign in to comment.