diff --git a/README.md b/README.md index 64794a48..1762a3cd 100644 --- a/README.md +++ b/README.md @@ -751,10 +751,11 @@ Scripts can be activated and deactivated by controlling of this state with ack=f ## Changelog -### 2.1.1 (2016-05-20) +### 2.1.1 (2016-05-21) * (bluefox) try to fix "Duplicate name" error * (bluefox) modify readFile/wrieFile commands * (gh-god) fix stop of script and unsubscribe +* (paul53) check type of set value and min, max by setState ### 2.1.0 (2016-05-13) * (bluefox) add getHistory command diff --git a/io-package.json b/io-package.json index 339fcab2..44862212 100644 --- a/io-package.json +++ b/io-package.json @@ -8,9 +8,9 @@ "hobbyquaker " ], "news": { - "en": "(bluefox) try to fix Duplicate name error\n(bluefox) add getFile/setFile commands\n(gh-god) fix stop of script and unsubscribe", - "de": "(bluefox) try to fix Duplicate name error\n(bluefox) add getFile/setFile commands\n(gh-god) fix stop of script and unsubscribe", - "ru": "(bluefox) try to fix Duplicate name error\n(bluefox) add getFile/setFile commands\n(gh-god) fix stop of script and unsubscribe" + "en": "(bluefox) try to fix Duplicate name error\n(bluefox) modify writeFile/readFile commands\n(gh-god) fix stop of script and unsubscribe\n(paul53) check type of set value and min, max by setState", + "de": "(bluefox) try to fix Duplicate name error\n(bluefox) modify writeFile/readFile commands\n(gh-god) fix stop of script and unsubscribe\n(paul53) check type of set value and min, max by setState", + "ru": "(bluefox) try to fix Duplicate name error\n(bluefox) modify writeFile/readFile commands\n(gh-god) fix stop of script and unsubscribe\n(paul53) check type of set value and min, max by setState" }, "desc": "Javascript/Coffeescript Script Engine", "platform": "Javascript/Node.js", diff --git a/javascript.js b/javascript.js index bf277d80..d21effdf 100644 --- a/javascript.js +++ b/javascript.js @@ -1442,6 +1442,24 @@ } } + // Check type of state + var common = objects[id] ? objects[id].common : null; + if (common && + common.type && + common.type !== 'mixed' && + common.type !== 'file' && + common.type !== 'json' && + common.type !== typeof state.val + ) { + adapter.log.warn('Wrong type of ' + id + '.state. Please fix, while deprecated and will not work in next versions.'); + //return; + } + // Check min and max of value + if (common && typeof state.val === 'number') { + if (common.min !== undefined && state.val < common.min) state.val = common.min; + if (common.max !== undefined && state.val > common.max) state.val = common.max; + } + if (states[id]) { adapter.setForeignState(id, state, function () { if (typeof callback === 'function') callback(); @@ -1653,6 +1671,62 @@ native = native || {}; + // Check min, max and def values for number + if (common.type !== undefined && common.type === 'number') { + var min = 0; + var max = 0; + var def = 0; + var err; + if (common.min !== undefined) { + min = common.min; + if (typeof min !== 'number') { + min = parseFloat(min); + if (isNaN(min)) { + err = 'Wrong type of ' + id + '.common.min'; + logger.error(err); + if (callback) callback(err); + return; + } else { + common.min = min; + } + } + } + if (common.max !== undefined) { + max = common.max; + if (typeof max !== 'number') { + max = parseFloat(max); + if (isNaN(max)) { + err = 'Wrong type of ' + id + '.common.max'; + logger.error(err); + if (callback) callback(err); + return; + } else { + common.max = max; + } + } + } + if (common.def !== undefined) { + def = common.def; + if (typeof def !== 'number') { + def = parseFloat(def); + if (isNaN(def)) { + err = 'Wrong type of ' + id + '.common.def'; + logger.error(err); + if (callback) callback(err); + return; + } else { + common.def = def; + } + } + } + if (common.min !== undefined && common.max !== undefined && min > max) { + common.max = min; + common.min = max; + } + if (common.def !== undefined && common.min !== undefined && def < min) common.def = min; + if (common.def !== undefined && common.max !== undefined && def > max) common.def = max; + } + if (forceCreation) { adapter.setObject(name, { common: common, diff --git a/test/testFunctions.js b/test/testFunctions.js index 42b622cf..324928b1 100644 --- a/test/testFunctions.js +++ b/test/testFunctions.js @@ -112,7 +112,7 @@ describe('Test JS', function() { this.timeout(5000); checkConnectionOfAdapter(done); }); -/* + it('Test JS: check creation of state', function (done) { this.timeout(2000); // add script @@ -673,7 +673,7 @@ describe('Test JS', function() { objects.setObject(script._id, script, function (err) { expect(err).to.be.not.ok; }); - });*/ + }); it('Test JS: test write file to "javascript"', function (done) { this.timeout(5000);