Skip to content

Commit 25e2526

Browse files
committed
(bluefox) tests for states
1 parent 1674b9c commit 25e2526

File tree

7 files changed

+604
-91
lines changed

7 files changed

+604
-91
lines changed

lib/adapter.js

Lines changed: 148 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2855,7 +2855,9 @@ function Adapter(options) {
28552855
}
28562856
if (differ) {
28572857
that.outputCount++;
2858-
that.states.setState(id, state, callback);
2858+
that.states.setState(id, state, function (err) {
2859+
if (typeof callback === 'function') callback(null, id, false);
2860+
});
28592861
} else {
28602862
if (typeof callback === 'function') callback(null, id, true);
28612863
}
@@ -3293,11 +3295,11 @@ function Adapter(options) {
32933295
if (err) {
32943296
if (typeof callback === 'function') callback(err);
32953297
} else {
3296-
that._setStateChangedHelper(id, state, callback);
3298+
_setStateChangedHelper(id, state, callback);
32973299
}
32983300
});
32993301
} else {
3300-
that._setStateChangedHelper(id, state, callback);
3302+
_setStateChangedHelper(id, state, callback);
33013303
}
33023304
};
33033305

@@ -3433,11 +3435,11 @@ function Adapter(options) {
34333435
if (err) {
34343436
if (typeof callback === 'function') callback(err);
34353437
} else {
3436-
that._setStateChangedHelper(id, state, callback);
3438+
_setStateChangedHelper(id, state, callback);
34373439
}
34383440
});
34393441
} else {
3440-
that._setStateChangedHelper(id, state, callback);
3442+
_setStateChangedHelper(id, state, callback);
34413443
}
34423444
};
34433445

@@ -3531,7 +3533,48 @@ function Adapter(options) {
35313533
}
35323534
};
35333535

3534-
// normally only foreign history has interest, so there is no getHistory and getForeignHistory
3536+
/**
3537+
* Read historian data for states of any instance or system state.
3538+
*
3539+
* This function can read values from history adapters like: history, sql, influxdb. It expects the full path of object ID.
3540+
* Normally only foreign history has interest, so there is no getHistory and getForeignHistory
3541+
*
3542+
* Possible options:
3543+
*
3544+
* - instance - (optional) name of instance, where to read the historian data, e.g. 'history.0', 'sql.1'. By default will be taken from system settings.
3545+
* - start - (optional) time in ms - new Date().getTime()', by default is (now - 1 week)
3546+
* - end - (optional) time in ms - new Date().getTime()', by default is (now + 5000 seconds)
3547+
* - step - (optional) used in aggregate (m4, max, min, average, total) step in ms of intervals
3548+
* - count - number of values if aggregate is 'onchange' or number of intervals if other aggregate method. Count will be ignored if step is set.
3549+
* - from - if from field should be included in answer
3550+
* - ack - if ack field should be included in answer
3551+
* - q - if q field should be included in answer
3552+
* - addId - if id field should be included in answer
3553+
* - limit - do not return more entries than limit
3554+
* - ignoreNull - if null values should be include (false), replaced by last not null value (true) or replaced with 0 (0)
3555+
* - sessionId - (optional) identifier of request, will be returned back in the answer
3556+
* - aggregate - aggregate method:
3557+
* - minmax - used special algorithm. Splice the whole time range in small intervals and find for every interval max, min, start and end values.
3558+
* - max - Splice the whole time range in small intervals and find for every interval max value and use it for this interval (nulls will be ignored).
3559+
* - min - Same as max, but take minimal value.
3560+
* - average - Same as max, but take average value.
3561+
* - total - Same as max, but calculate total value.
3562+
* - count - Same as max, but calculate number of values (nulls will be calculated).
3563+
* - none - No aggregation at all. Only raw values in given period.
3564+
*
3565+
* @alias getHistory
3566+
* @memberof Adapter
3567+
* @param {string} id object ID of the state.
3568+
* @param {object} options see function description
3569+
* @param {function} callback return result
3570+
* <pre><code>
3571+
* function (error, result, step, sessionId) {
3572+
* if (error) adapter.log.error('Cannot read value: ' + err);
3573+
* }
3574+
* </code></pre>
3575+
*
3576+
* See possible attributes of the state in @setState explanation
3577+
*/
35353578
that.getHistory = function getHistory(id, options, callback) {
35363579
options = options || {};
35373580
options.end = options.end || (new Date()).getTime() + 5000000;
@@ -3804,8 +3847,29 @@ function Adapter(options) {
38043847
});
38053848
};
38063849

3807-
that.subscribeForeignStates = function subscribeForeignStates(pattern, options) {
3850+
/**
3851+
* Subscribe for changes on all states of all adapters (and system states), that pass the pattern
3852+
*
3853+
* Allows to Subscribe on changes all states of all instances according to pattern. E.g. to read all states of 'adapterName.X' instance use:
3854+
* <pre><code>
3855+
* adapter.subscribeForeignStates('adapterName.X.*');
3856+
* </code></pre>
3857+
*
3858+
* @alias subscribeForeignStates
3859+
* @memberof Adapter
3860+
* @param {string} pattern string in form 'adapter.0.*' or like this. It can be array of IDs too.
3861+
* @param {object} options optional argument to describe the user context
3862+
* @param {function} callback return result function (err) {}
3863+
*/
3864+
that.subscribeForeignStates = function subscribeForeignStates(pattern, options, callback) {
38083865
if (!pattern) pattern = '*';
3866+
if (typeof options === 'function') {
3867+
callback = options;
3868+
options = null;
3869+
}
3870+
3871+
// Todo check rights for options
3872+
38093873
autoSubscribeOn(function () {
38103874
// compare if this pattern for one of autosubscribe adapters
38113875
for (var s = 0; s < that.autoSubscribe.length; s++) {
@@ -3829,13 +3893,36 @@ function Adapter(options) {
38293893
}
38303894
}
38313895

3832-
that.states.subscribe(pattern, options);
3833-
})
3896+
that.states.subscribe(pattern, callback);
3897+
});
38343898
};
38353899

3836-
that.unsubscribeForeignStates = function unsubscribeForeignStates(pattern, options) {
3900+
/**
3901+
* Unsubscribe for changes for given pattern
3902+
*
3903+
* This function allows to unsubsrcibe from changes. The pattern must be equal to requested one.
3904+
*
3905+
* <pre><code>
3906+
* adapter.subscribeForeignStates('adapterName.X.*');
3907+
* adapter.unsubscribeForeignStates('adapterName.X.abc*'); // This will not work
3908+
* adapter.unsubscribeForeignStates('adapterName.X.*'); // Valid unsubscribe
3909+
* </code></pre>
3910+
*
3911+
* @alias unsubscribeForeignStates
3912+
* @memberof Adapter
3913+
* @param {string} pattern string in form 'adapter.0.*'. Must be the same as subscribe.
3914+
* @param {object} options optional argument to describe the user context
3915+
* @param {function} callback return result function (err) {}
3916+
*/
3917+
that.unsubscribeForeignStates = function unsubscribeForeignStates(pattern, options, callback) {
38373918
if (!pattern) pattern = '*';
3838-
3919+
3920+
// Todo check rights for options
3921+
if (typeof options === 'function') {
3922+
callback = options;
3923+
options = null;
3924+
}
3925+
38393926
if (that.autoSubscribe) {
38403927
for (var s = 0; s < that.autoSubscribe.length; s++) {
38413928
if (pattern === '*' || pattern.substring(0, that.autoSubscribe[s].length + 1) === that.autoSubscribe[s] + '.') {
@@ -3869,25 +3956,68 @@ function Adapter(options) {
38693956
}
38703957
}
38713958

3872-
that.states.unsubscribe(pattern, options);
3959+
that.states.unsubscribe(pattern, callback);
38733960
};
38743961

3875-
that.subscribeStates = function subscribeStates(pattern, options) {
3962+
/**
3963+
* Subscribe for changes on all states of this instance, that pass the pattern
3964+
*
3965+
* Allows to Subscribe on changes all states of current adapter according to pattern. To read all states of current adapter use:
3966+
* <pre><code>
3967+
* adapter.subscribeStates('*'); // subscribe for all states of this adapter
3968+
* </code></pre>
3969+
*
3970+
* @alias subscribeStates
3971+
* @memberof Adapter
3972+
* @param {string} pattern string in form 'adapter.0.*' or like this. It can be array of IDs too.
3973+
* @param {object} options optional argument to describe the user context
3974+
* @param {function} callback return result function (err) {}
3975+
*/
3976+
that.subscribeStates = function subscribeStates(pattern, options, callback) {
3977+
// Todo check rights for options
3978+
if (typeof options === 'function') {
3979+
callback = options;
3980+
options = null;
3981+
}
3982+
38763983
// Exception. Threat the '*' case automatically
38773984
if (!pattern || pattern === '*') {
3878-
that.states.subscribe(that.namespace + '.*', options);
3985+
that.states.subscribe(that.namespace + '.*', callback);
38793986
} else {
38803987
pattern = that._fixId(pattern, 'state');
3881-
that.states.subscribe(pattern, options);
3988+
that.states.subscribe(pattern, callback);
38823989
}
38833990
};
38843991

3885-
that.unsubscribeStates = function unsubscribeStates(pattern, options) {
3992+
/**
3993+
* Unsubscribe for changes for given pattern for own states.
3994+
*
3995+
* This function allows to unsubsrcibe from changes. The pattern must be equal to requested one.
3996+
*
3997+
* <pre><code>
3998+
* adapter.subscribeForeignStates('*');
3999+
* adapter.unsubscribeForeignStates('abc*'); // This will not work
4000+
* adapter.unsubscribeForeignStates('*'); // Valid unsubscribe
4001+
* </code></pre>
4002+
*
4003+
* @alias unsubscribeStates
4004+
* @memberof Adapter
4005+
* @param {string} pattern string in form 'adapter.0.*'. Must be the same as subscribe.
4006+
* @param {object} options optional argument to describe the user context
4007+
* @param {function} callback return result function (err) {}
4008+
*/
4009+
that.unsubscribeStates = function unsubscribeStates(pattern, options, callback) {
4010+
// Todo check rights for options
4011+
if (typeof options === 'function') {
4012+
callback = options;
4013+
options = null;
4014+
}
4015+
38864016
if (!pattern || pattern === '*') {
3887-
that.states.unsubscribe(that.namespace + '.*', options);
4017+
that.states.unsubscribe(that.namespace + '.*', callback);
38884018
} else {
38894019
pattern = that._fixId(pattern, 'state');
3890-
that.states.unsubscribe(pattern, options);
4020+
that.states.unsubscribe(pattern, callback);
38914021
}
38924022
};
38934023

lib/setup/setupInstall.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ function Install(options) {
189189
console.log('host.' + hostname + ' unzip ' + tmpFile);
190190

191191
// Extract files into tmp/
192-
extractFiles(tmpFile, path.join(__dirname + '/../../tmp/', name), function (err) {
192+
extractFiles(tmpFile, path.join(__dirname + '/../../tmp/', name), function (/* err */) {
193193
// Find out the first directory
194194
var dirs = fs.readdirSync(__dirname + '/../../tmp/' + name);
195195
if (dirs.length) {
@@ -279,7 +279,7 @@ function Install(options) {
279279
if (!options.useSystemNpm) {
280280
console.log(cmd);
281281
tools.initNpm(cwd, function (er, npm) {
282-
npm.commands.install([npmUrl], function (er, data) {
282+
npm.commands.install([npmUrl], function (er /*, data */) {
283283
if (er) {
284284
console.error('host.' + hostname + ' Cannot install ' + npmUrl + ': ' + er);
285285
processExit(25);
@@ -300,7 +300,7 @@ function Install(options) {
300300
if (debug || (yargs && yargs.argv && yargs.argv.debug)) {
301301
child.stdout.pipe(process.stdout);
302302
}
303-
child.on('exit', function (code, signal) {
303+
child.on('exit', function (code /* , signal */) {
304304
if (code) {
305305
console.error('host.' + hostname + ' Cannot install ' + npmUrl + ': ' + code);
306306
processExit(25);
@@ -399,7 +399,7 @@ function Install(options) {
399399

400400
if (version !== null) {
401401
if (!semver) semver = require('semver');
402-
var iopkg = JSON.parse(fs.readFileSync(__dirname + '/../../package.json'));
402+
// var iopkg = JSON.parse(fs.readFileSync(__dirname + '/../../package.json'));
403403
if (!semver.satisfies(objs.rows[t].value.common.version, version)) {
404404
console.error('host.' + hostname + ' Invalid version of "' + dName + '". Installed "' + objs.rows[t].value.common.version + '", required "' + version);
405405
processExit(30);
@@ -451,7 +451,7 @@ function Install(options) {
451451
_callback(adapter);
452452
} else {
453453
var obj = objs.pop();
454-
objects.extendObject(obj._id, obj, function (err, res) {
454+
objects.extendObject(obj._id, obj, function (err /* , res */) {
455455
if (err) {
456456
console.error('host.' + hostname + ' error setObject ' + obj._id + ' ' + err);
457457
processExit(17);
@@ -780,7 +780,7 @@ function Install(options) {
780780
function setObjs() {
781781
if (objs.length > 0) {
782782
var obj = objs.pop();
783-
objects.setObject(obj._id, obj, function (err, res) {
783+
objects.setObject(obj._id, obj, function (err /*, res */) {
784784
if (err) {
785785
console.error('host.' + hostname + ' error: ' + err);
786786
} else {
@@ -789,7 +789,7 @@ function Install(options) {
789789
setTimeout(setObjs, 25);
790790
});
791791
} else {
792-
objects.setObject(instanceObj._id, instanceObj, function (err, res) {
792+
objects.setObject(instanceObj._id, instanceObj, function (err /* , res */) {
793793
if (err) {
794794
console.error('host.' + hostname + ' error: ' + err);
795795
} else {
@@ -1156,7 +1156,7 @@ function Install(options) {
11561156
if (!delState.length) {
11571157
if (callback) callback(adapter, instance);
11581158
} else {
1159-
states.delState(delState.pop(), function (err) {
1159+
states.delState(delState.pop(), function (/* err */) {
11601160
setTimeout(delStates, 0);
11611161
});
11621162
}
@@ -1177,7 +1177,7 @@ function Install(options) {
11771177
if (!delObj.length) {
11781178
setTimeout(startDeleteStates, 50);
11791179
} else {
1180-
objects.delObject(delObj.pop(), function (err) {
1180+
objects.delObject(delObj.pop(), function (/* err */) {
11811181
setTimeout(delObjects, 0);
11821182
});
11831183
}
@@ -1261,7 +1261,6 @@ function Install(options) {
12611261
} else {
12621262
if (doc.rows.length !== 0) {
12631263
var count = 0;
1264-
var name = adapter + '.' + instance;
12651264
for (var i = 0; i < doc.rows.length; i++) {
12661265
if (doc.rows[i] && doc.rows[i].value && doc.rows[i].value._id &&
12671266
(adapterRegex.test(doc.rows[i].value._id) || sysAdapterRegex.test(doc.rows[i].value._id))) {
@@ -1285,7 +1284,6 @@ function Install(options) {
12851284
console.log('host.' + hostname + ' no states ' + adapter + ' found');
12861285
} else {
12871286
var count = 0;
1288-
var name = adapter + '.' + instance;
12891287
for (var i = 0; i < doc.rows.length; i++) {
12901288
if (doc.rows[i] && doc.rows[i].value && doc.rows[i].value._id &&
12911289
(adapterRegex.test(doc.rows[i].value._id) || sysAdapterRegex.test(doc.rows[i].value._id))) {

0 commit comments

Comments
 (0)