Initial commit
This commit is contained in:
728
test/lib/setup.js
Normal file
728
test/lib/setup.js
Normal file
@@ -0,0 +1,728 @@
|
||||
/* jshint -W097 */// jshint strict:false
|
||||
/*jslint node: true */
|
||||
// check if tmp directory exists
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var child_process = require('child_process');
|
||||
var rootDir = path.normalize(__dirname + '/../../');
|
||||
var pkg = require(rootDir + 'package.json');
|
||||
var debug = typeof v8debug === 'object';
|
||||
pkg.main = pkg.main || 'main.js';
|
||||
|
||||
var adapterName = path.normalize(rootDir).replace(/\\/g, '/').split('/');
|
||||
adapterName = adapterName[adapterName.length - 2];
|
||||
var adapterStarted = false;
|
||||
|
||||
function getAppName() {
|
||||
var parts = __dirname.replace(/\\/g, '/').split('/');
|
||||
return parts[parts.length - 3].split('.')[0];
|
||||
}
|
||||
|
||||
var appName = getAppName().toLowerCase();
|
||||
|
||||
var objects;
|
||||
var states;
|
||||
|
||||
var pid = null;
|
||||
|
||||
function copyFileSync(source, target) {
|
||||
|
||||
var targetFile = target;
|
||||
|
||||
//if target is a directory a new file with the same name will be created
|
||||
if (fs.existsSync(target)) {
|
||||
if ( fs.lstatSync( target ).isDirectory() ) {
|
||||
targetFile = path.join(target, path.basename(source));
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
fs.writeFileSync(targetFile, fs.readFileSync(source));
|
||||
}
|
||||
catch (err) {
|
||||
console.log("file copy error: " +source +" -> " + targetFile + " (error ignored)");
|
||||
}
|
||||
}
|
||||
|
||||
function copyFolderRecursiveSync(source, target, ignore) {
|
||||
var files = [];
|
||||
|
||||
var base = path.basename(source);
|
||||
if (base === adapterName) {
|
||||
base = pkg.name;
|
||||
}
|
||||
//check if folder needs to be created or integrated
|
||||
var targetFolder = path.join(target, base);
|
||||
if (!fs.existsSync(targetFolder)) {
|
||||
fs.mkdirSync(targetFolder);
|
||||
}
|
||||
|
||||
//copy
|
||||
if (fs.lstatSync(source).isDirectory()) {
|
||||
files = fs.readdirSync(source);
|
||||
files.forEach(function (file) {
|
||||
if (ignore && ignore.indexOf(file) !== -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
var curSource = path.join(source, file);
|
||||
var curTarget = path.join(targetFolder, file);
|
||||
if (fs.lstatSync(curSource).isDirectory()) {
|
||||
// ignore grunt files
|
||||
if (file.indexOf('grunt') !== -1) return;
|
||||
if (file === 'chai') return;
|
||||
if (file === 'mocha') return;
|
||||
copyFolderRecursiveSync(curSource, targetFolder, ignore);
|
||||
} else {
|
||||
copyFileSync(curSource, curTarget);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (!fs.existsSync(rootDir + 'tmp')) {
|
||||
fs.mkdirSync(rootDir + 'tmp');
|
||||
}
|
||||
|
||||
function storeOriginalFiles() {
|
||||
console.log('Store original files...');
|
||||
var dataDir = rootDir + 'tmp/' + appName + '-data/';
|
||||
|
||||
var f = fs.readFileSync(dataDir + 'objects.json');
|
||||
var objects = JSON.parse(f.toString());
|
||||
if (objects['system.adapter.admin.0'] && objects['system.adapter.admin.0'].common) {
|
||||
objects['system.adapter.admin.0'].common.enabled = false;
|
||||
}
|
||||
if (objects['system.adapter.admin.1'] && objects['system.adapter.admin.1'].common) {
|
||||
objects['system.adapter.admin.1'].common.enabled = false;
|
||||
}
|
||||
|
||||
fs.writeFileSync(dataDir + 'objects.json.original', JSON.stringify(objects));
|
||||
try {
|
||||
f = fs.readFileSync(dataDir + 'states.json');
|
||||
fs.writeFileSync(dataDir + 'states.json.original', f);
|
||||
}
|
||||
catch (err) {
|
||||
console.log('no states.json found - ignore');
|
||||
}
|
||||
}
|
||||
|
||||
function restoreOriginalFiles() {
|
||||
console.log('restoreOriginalFiles...');
|
||||
var dataDir = rootDir + 'tmp/' + appName + '-data/';
|
||||
|
||||
var f = fs.readFileSync(dataDir + 'objects.json.original');
|
||||
fs.writeFileSync(dataDir + 'objects.json', f);
|
||||
try {
|
||||
f = fs.readFileSync(dataDir + 'states.json.original');
|
||||
fs.writeFileSync(dataDir + 'states.json', f);
|
||||
}
|
||||
catch (err) {
|
||||
console.log('no states.json.original found - ignore');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function checkIsAdapterInstalled(cb, counter, customName) {
|
||||
customName = customName || pkg.name.split('.').pop();
|
||||
counter = counter || 0;
|
||||
var dataDir = rootDir + 'tmp/' + appName + '-data/';
|
||||
console.log('checkIsAdapterInstalled...');
|
||||
|
||||
try {
|
||||
var f = fs.readFileSync(dataDir + 'objects.json');
|
||||
var objects = JSON.parse(f.toString());
|
||||
if (objects['system.adapter.' + customName + '.0']) {
|
||||
console.log('checkIsAdapterInstalled: ready!');
|
||||
setTimeout(function () {
|
||||
if (cb) cb();
|
||||
}, 100);
|
||||
return;
|
||||
} else {
|
||||
console.warn('checkIsAdapterInstalled: still not ready');
|
||||
}
|
||||
} catch (err) {
|
||||
|
||||
}
|
||||
|
||||
if (counter > 20) {
|
||||
console.error('checkIsAdapterInstalled: Cannot install!');
|
||||
if (cb) cb('Cannot install');
|
||||
} else {
|
||||
console.log('checkIsAdapterInstalled: wait...');
|
||||
setTimeout(function() {
|
||||
checkIsAdapterInstalled(cb, counter + 1);
|
||||
}, 1000);
|
||||
}
|
||||
}
|
||||
|
||||
function checkIsControllerInstalled(cb, counter) {
|
||||
counter = counter || 0;
|
||||
var dataDir = rootDir + 'tmp/' + appName + '-data/';
|
||||
|
||||
console.log('checkIsControllerInstalled...');
|
||||
try {
|
||||
var f = fs.readFileSync(dataDir + 'objects.json');
|
||||
var objects = JSON.parse(f.toString());
|
||||
if (objects['system.adapter.admin.0']) {
|
||||
console.log('checkIsControllerInstalled: installed!');
|
||||
setTimeout(function () {
|
||||
if (cb) cb();
|
||||
}, 100);
|
||||
return;
|
||||
}
|
||||
} catch (err) {
|
||||
|
||||
}
|
||||
|
||||
if (counter > 20) {
|
||||
console.log('checkIsControllerInstalled: Cannot install!');
|
||||
if (cb) cb('Cannot install');
|
||||
} else {
|
||||
console.log('checkIsControllerInstalled: wait...');
|
||||
setTimeout(function() {
|
||||
checkIsControllerInstalled(cb, counter + 1);
|
||||
}, 1000);
|
||||
}
|
||||
}
|
||||
|
||||
function installAdapter(customName, cb) {
|
||||
if (typeof customName === 'function') {
|
||||
cb = customName;
|
||||
customName = null;
|
||||
}
|
||||
customName = customName || pkg.name.split('.').pop();
|
||||
console.log('Install adapter...');
|
||||
var startFile = 'node_modules/' + appName + '.js-controller/' + appName + '.js';
|
||||
// make first install
|
||||
if (debug) {
|
||||
child_process.execSync('node ' + startFile + ' add ' + customName + ' --enabled false', {
|
||||
cwd: rootDir + 'tmp',
|
||||
stdio: [0, 1, 2]
|
||||
});
|
||||
checkIsAdapterInstalled(function (error) {
|
||||
if (error) console.error(error);
|
||||
console.log('Adapter installed.');
|
||||
if (cb) cb();
|
||||
});
|
||||
} else {
|
||||
// add controller
|
||||
var _pid = child_process.fork(startFile, ['add', customName, '--enabled', 'false'], {
|
||||
cwd: rootDir + 'tmp',
|
||||
stdio: [0, 1, 2, 'ipc']
|
||||
});
|
||||
|
||||
waitForEnd(_pid, function () {
|
||||
checkIsAdapterInstalled(function (error) {
|
||||
if (error) console.error(error);
|
||||
console.log('Adapter installed.');
|
||||
if (cb) cb();
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function waitForEnd(_pid, cb) {
|
||||
if (!_pid) {
|
||||
cb(-1, -1);
|
||||
return;
|
||||
}
|
||||
_pid.on('exit', function (code, signal) {
|
||||
if (_pid) {
|
||||
_pid = null;
|
||||
cb(code, signal);
|
||||
}
|
||||
});
|
||||
_pid.on('close', function (code, signal) {
|
||||
if (_pid) {
|
||||
_pid = null;
|
||||
cb(code, signal);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function installJsController(cb) {
|
||||
console.log('installJsController...');
|
||||
if (!fs.existsSync(rootDir + 'tmp/node_modules/' + appName + '.js-controller') ||
|
||||
!fs.existsSync(rootDir + 'tmp/' + appName + '-data')) {
|
||||
// try to detect appName.js-controller in node_modules/appName.js-controller
|
||||
// travis CI installs js-controller into node_modules
|
||||
if (fs.existsSync(rootDir + 'node_modules/' + appName + '.js-controller')) {
|
||||
console.log('installJsController: no js-controller => copy it from "' + rootDir + 'node_modules/' + appName + '.js-controller"');
|
||||
// copy all
|
||||
// stop controller
|
||||
console.log('Stop controller if running...');
|
||||
var _pid;
|
||||
if (debug) {
|
||||
// start controller
|
||||
_pid = child_process.exec('node ' + appName + '.js stop', {
|
||||
cwd: rootDir + 'node_modules/' + appName + '.js-controller',
|
||||
stdio: [0, 1, 2]
|
||||
});
|
||||
} else {
|
||||
_pid = child_process.fork(appName + '.js', ['stop'], {
|
||||
cwd: rootDir + 'node_modules/' + appName + '.js-controller',
|
||||
stdio: [0, 1, 2, 'ipc']
|
||||
});
|
||||
}
|
||||
|
||||
waitForEnd(_pid, function () {
|
||||
// copy all files into
|
||||
if (!fs.existsSync(rootDir + 'tmp')) fs.mkdirSync(rootDir + 'tmp');
|
||||
if (!fs.existsSync(rootDir + 'tmp/node_modules')) fs.mkdirSync(rootDir + 'tmp/node_modules');
|
||||
|
||||
if (!fs.existsSync(rootDir + 'tmp/node_modules/' + appName + '.js-controller')){
|
||||
console.log('Copy js-controller...');
|
||||
copyFolderRecursiveSync(rootDir + 'node_modules/' + appName + '.js-controller', rootDir + 'tmp/node_modules/');
|
||||
}
|
||||
|
||||
console.log('Setup js-controller...');
|
||||
var __pid;
|
||||
if (debug) {
|
||||
// start controller
|
||||
_pid = child_process.exec('node ' + appName + '.js setup first --console', {
|
||||
cwd: rootDir + 'tmp/node_modules/' + appName + '.js-controller',
|
||||
stdio: [0, 1, 2]
|
||||
});
|
||||
} else {
|
||||
__pid = child_process.fork(appName + '.js', ['setup', 'first', '--console'], {
|
||||
cwd: rootDir + 'tmp/node_modules/' + appName + '.js-controller',
|
||||
stdio: [0, 1, 2, 'ipc']
|
||||
});
|
||||
}
|
||||
waitForEnd(__pid, function () {
|
||||
checkIsControllerInstalled(function () {
|
||||
// change ports for object and state DBs
|
||||
var config = require(rootDir + 'tmp/' + appName + '-data/' + appName + '.json');
|
||||
config.objects.port = 19001;
|
||||
config.states.port = 19000;
|
||||
fs.writeFileSync(rootDir + 'tmp/' + appName + '-data/' + appName + '.json', JSON.stringify(config, null, 2));
|
||||
console.log('Setup finished.');
|
||||
|
||||
copyAdapterToController();
|
||||
|
||||
installAdapter(function () {
|
||||
storeOriginalFiles();
|
||||
if (cb) cb(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
} else {
|
||||
// check if port 9000 is free, else admin adapter will be added to running instance
|
||||
var client = new require('net').Socket();
|
||||
client.connect(9000, '127.0.0.1', function() {
|
||||
console.error('Cannot initiate fisrt run of test, because one instance of application is running on this PC. Stop it and repeat.');
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
setTimeout(function () {
|
||||
client.destroy();
|
||||
if (!fs.existsSync(rootDir + 'tmp/node_modules/' + appName + '.js-controller')) {
|
||||
console.log('installJsController: no js-controller => install from git');
|
||||
|
||||
child_process.execSync('npm install https://git.spacen.net/' + appName + '/' + appName + '.js-controller/tarball/master --prefix ./ --production', {
|
||||
cwd: rootDir + 'tmp/',
|
||||
stdio: [0, 1, 2]
|
||||
});
|
||||
} else {
|
||||
console.log('Setup js-controller...');
|
||||
var __pid;
|
||||
if (debug) {
|
||||
// start controller
|
||||
child_process.exec('node ' + appName + '.js setup first', {
|
||||
cwd: rootDir + 'tmp/node_modules/' + appName + '.js-controller',
|
||||
stdio: [0, 1, 2]
|
||||
});
|
||||
} else {
|
||||
child_process.fork(appName + '.js', ['setup', 'first'], {
|
||||
cwd: rootDir + 'tmp/node_modules/' + appName + '.js-controller',
|
||||
stdio: [0, 1, 2, 'ipc']
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// let npm install admin and run setup
|
||||
checkIsControllerInstalled(function () {
|
||||
var _pid;
|
||||
|
||||
if (fs.existsSync(rootDir + 'node_modules/' + appName + '.js-controller/' + appName + '.js')) {
|
||||
_pid = child_process.fork(appName + '.js', ['stop'], {
|
||||
cwd: rootDir + 'node_modules/' + appName + '.js-controller',
|
||||
stdio: [0, 1, 2, 'ipc']
|
||||
});
|
||||
}
|
||||
|
||||
waitForEnd(_pid, function () {
|
||||
// change ports for object and state DBs
|
||||
var config = require(rootDir + 'tmp/' + appName + '-data/' + appName + '.json');
|
||||
config.objects.port = 19001;
|
||||
config.states.port = 19000;
|
||||
fs.writeFileSync(rootDir + 'tmp/' + appName + '-data/' + appName + '.json', JSON.stringify(config, null, 2));
|
||||
|
||||
copyAdapterToController();
|
||||
|
||||
installAdapter(function () {
|
||||
storeOriginalFiles();
|
||||
if (cb) cb(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
}, 1000);
|
||||
}
|
||||
} else {
|
||||
setTimeout(function () {
|
||||
console.log('installJsController: js-controller installed');
|
||||
if (cb) cb(false);
|
||||
}, 0);
|
||||
}
|
||||
}
|
||||
|
||||
function copyAdapterToController() {
|
||||
console.log('Copy adapter...');
|
||||
// Copy adapter to tmp/node_modules/appName.adapter
|
||||
copyFolderRecursiveSync(rootDir, rootDir + 'tmp/node_modules/', ['.idea', 'test', 'tmp', '.git', appName + '.js-controller']);
|
||||
console.log('Adapter copied.');
|
||||
}
|
||||
|
||||
function clearControllerLog() {
|
||||
var dirPath = rootDir + 'tmp/log';
|
||||
var files;
|
||||
try {
|
||||
if (fs.existsSync(dirPath)) {
|
||||
console.log('Clear controller log...');
|
||||
files = fs.readdirSync(dirPath);
|
||||
} else {
|
||||
console.log('Create controller log directory...');
|
||||
files = [];
|
||||
fs.mkdirSync(dirPath);
|
||||
}
|
||||
} catch(e) {
|
||||
console.error('Cannot read "' + dirPath + '"');
|
||||
return;
|
||||
}
|
||||
if (files.length > 0) {
|
||||
try {
|
||||
for (var i = 0; i < files.length; i++) {
|
||||
var filePath = dirPath + '/' + files[i];
|
||||
fs.unlinkSync(filePath);
|
||||
}
|
||||
console.log('Controller log cleared');
|
||||
} catch (err) {
|
||||
console.error('cannot clear log: ' + err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function clearDB() {
|
||||
var dirPath = rootDir + 'tmp/yunkong2-data/sqlite';
|
||||
var files;
|
||||
try {
|
||||
if (fs.existsSync(dirPath)) {
|
||||
console.log('Clear sqlite DB...');
|
||||
files = fs.readdirSync(dirPath);
|
||||
} else {
|
||||
console.log('Create controller log directory...');
|
||||
files = [];
|
||||
fs.mkdirSync(dirPath);
|
||||
}
|
||||
} catch(e) {
|
||||
console.error('Cannot read "' + dirPath + '"');
|
||||
return;
|
||||
}
|
||||
if (files.length > 0) {
|
||||
try {
|
||||
for (var i = 0; i < files.length; i++) {
|
||||
var filePath = dirPath + '/' + files[i];
|
||||
fs.unlinkSync(filePath);
|
||||
}
|
||||
console.log('Clear sqlite DB');
|
||||
} catch (err) {
|
||||
console.error('cannot clear DB: ' + err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function setupController(cb) {
|
||||
installJsController(function (isInited) {
|
||||
clearControllerLog();
|
||||
clearDB();
|
||||
|
||||
if (!isInited) {
|
||||
restoreOriginalFiles();
|
||||
copyAdapterToController();
|
||||
}
|
||||
// read system.config object
|
||||
var dataDir = rootDir + 'tmp/' + appName + '-data/';
|
||||
|
||||
var objs;
|
||||
try {
|
||||
objs = fs.readFileSync(dataDir + 'objects.json');
|
||||
objs = JSON.parse(objs);
|
||||
}
|
||||
catch (e) {
|
||||
console.log('ERROR reading/parsing system configuration. Ignore');
|
||||
objs = {'system.config': {}};
|
||||
}
|
||||
if (!objs || !objs['system.config']) {
|
||||
objs = {'system.config': {}};
|
||||
}
|
||||
|
||||
if (cb) cb(objs['system.config']);
|
||||
});
|
||||
}
|
||||
|
||||
function startAdapter(objects, states, callback) {
|
||||
if (adapterStarted) {
|
||||
console.log('Adapter already started ...');
|
||||
if (callback) callback(objects, states);
|
||||
return;
|
||||
}
|
||||
adapterStarted = true;
|
||||
console.log('startAdapter...');
|
||||
if (fs.existsSync(rootDir + 'tmp/node_modules/' + pkg.name + '/' + pkg.main)) {
|
||||
try {
|
||||
if (debug) {
|
||||
// start controller
|
||||
pid = child_process.exec('node node_modules/' + pkg.name + '/' + pkg.main + ' --console silly', {
|
||||
cwd: rootDir + 'tmp',
|
||||
stdio: [0, 1, 2]
|
||||
});
|
||||
} else {
|
||||
// start controller
|
||||
pid = child_process.fork('node_modules/' + pkg.name + '/' + pkg.main, ['--console', 'silly'], {
|
||||
cwd: rootDir + 'tmp',
|
||||
stdio: [0, 1, 2, 'ipc']
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(JSON.stringify(error));
|
||||
}
|
||||
} else {
|
||||
console.error('Cannot find: ' + rootDir + 'tmp/node_modules/' + pkg.name + '/' + pkg.main);
|
||||
}
|
||||
if (callback) callback(objects, states);
|
||||
}
|
||||
|
||||
function startController(isStartAdapter, onObjectChange, onStateChange, callback) {
|
||||
if (typeof isStartAdapter === 'function') {
|
||||
callback = onStateChange;
|
||||
onStateChange = onObjectChange;
|
||||
onObjectChange = isStartAdapter;
|
||||
isStartAdapter = true;
|
||||
}
|
||||
|
||||
if (onStateChange === undefined) {
|
||||
callback = onObjectChange;
|
||||
onObjectChange = undefined;
|
||||
}
|
||||
|
||||
if (pid) {
|
||||
console.error('Controller is already started!');
|
||||
} else {
|
||||
console.log('startController...');
|
||||
adapterStarted = false;
|
||||
var isObjectConnected;
|
||||
var isStatesConnected;
|
||||
|
||||
var Objects = require(rootDir + 'tmp/node_modules/' + appName + '.js-controller/lib/objects/objectsInMemServer');
|
||||
objects = new Objects({
|
||||
connection: {
|
||||
"type" : "file",
|
||||
"host" : "127.0.0.1",
|
||||
"port" : 19001,
|
||||
"user" : "",
|
||||
"pass" : "",
|
||||
"noFileCache": false,
|
||||
"connectTimeout": 2000
|
||||
},
|
||||
logger: {
|
||||
silly: function (msg) {
|
||||
console.log(msg);
|
||||
},
|
||||
debug: function (msg) {
|
||||
console.log(msg);
|
||||
},
|
||||
info: function (msg) {
|
||||
console.log(msg);
|
||||
},
|
||||
warn: function (msg) {
|
||||
console.warn(msg);
|
||||
},
|
||||
error: function (msg) {
|
||||
console.error(msg);
|
||||
}
|
||||
},
|
||||
connected: function () {
|
||||
isObjectConnected = true;
|
||||
if (isStatesConnected) {
|
||||
console.log('startController: started!');
|
||||
if (isStartAdapter) {
|
||||
startAdapter(objects, states, callback);
|
||||
} else {
|
||||
if (callback) {
|
||||
callback(objects, states);
|
||||
callback = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
change: onObjectChange
|
||||
});
|
||||
|
||||
// Just open in memory DB itself
|
||||
var States = require(rootDir + 'tmp/node_modules/' + appName + '.js-controller/lib/states/statesInMemServer');
|
||||
states = new States({
|
||||
connection: {
|
||||
type: 'file',
|
||||
host: '127.0.0.1',
|
||||
port: 19000,
|
||||
options: {
|
||||
auth_pass: null,
|
||||
retry_max_delay: 15000
|
||||
}
|
||||
},
|
||||
logger: {
|
||||
silly: function (msg) {
|
||||
console.log(msg);
|
||||
},
|
||||
debug: function (msg) {
|
||||
console.log(msg);
|
||||
},
|
||||
info: function (msg) {
|
||||
console.log(msg);
|
||||
},
|
||||
warn: function (msg) {
|
||||
console.log(msg);
|
||||
},
|
||||
error: function (msg) {
|
||||
console.log(msg);
|
||||
}
|
||||
},
|
||||
connected: function () {
|
||||
isStatesConnected = true;
|
||||
if (isObjectConnected) {
|
||||
console.log('startController: started!!');
|
||||
if (isStartAdapter) {
|
||||
startAdapter(objects, states, callback);
|
||||
} else {
|
||||
if (callback) {
|
||||
callback(objects, states);
|
||||
callback = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
change: onStateChange
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function stopAdapter(cb) {
|
||||
if (!pid) {
|
||||
console.error('Controller is not running!');
|
||||
if (cb) {
|
||||
setTimeout(function () {
|
||||
cb(false);
|
||||
}, 0);
|
||||
}
|
||||
} else {
|
||||
adapterStarted = false;
|
||||
pid.on('exit', function (code, signal) {
|
||||
if (pid) {
|
||||
console.log('child process terminated due to receipt of signal ' + signal);
|
||||
if (cb) cb();
|
||||
pid = null;
|
||||
}
|
||||
});
|
||||
|
||||
pid.on('close', function (code, signal) {
|
||||
if (pid) {
|
||||
if (cb) cb();
|
||||
pid = null;
|
||||
}
|
||||
});
|
||||
|
||||
pid.kill('SIGTERM');
|
||||
}
|
||||
}
|
||||
|
||||
function _stopController() {
|
||||
if (objects) {
|
||||
objects.destroy();
|
||||
objects = null;
|
||||
}
|
||||
if (states) {
|
||||
states.destroy();
|
||||
states = null;
|
||||
}
|
||||
}
|
||||
|
||||
function stopController(cb) {
|
||||
var timeout;
|
||||
if (objects) {
|
||||
console.log('Set system.adapter.' + pkg.name + '.0');
|
||||
objects.setObject('system.adapter.' + pkg.name + '.0', {
|
||||
common:{
|
||||
enabled: false
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
stopAdapter(function () {
|
||||
if (timeout) {
|
||||
clearTimeout(timeout);
|
||||
timeout = null;
|
||||
}
|
||||
|
||||
_stopController();
|
||||
|
||||
if (cb) {
|
||||
cb(true);
|
||||
cb = null;
|
||||
}
|
||||
});
|
||||
|
||||
timeout = setTimeout(function () {
|
||||
timeout = null;
|
||||
console.log('child process NOT terminated');
|
||||
|
||||
_stopController();
|
||||
|
||||
if (cb) {
|
||||
cb(false);
|
||||
cb = null;
|
||||
}
|
||||
pid = null;
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
// Setup the adapter
|
||||
function setAdapterConfig(common, native, instance) {
|
||||
var objects = JSON.parse(fs.readFileSync(rootDir + 'tmp/' + appName + '-data/objects.json').toString());
|
||||
var id = 'system.adapter.' + adapterName.split('.').pop() + '.' + (instance || 0);
|
||||
if (common) objects[id].common = common;
|
||||
if (native) objects[id].native = native;
|
||||
fs.writeFileSync(rootDir + 'tmp/' + appName + '-data/objects.json', JSON.stringify(objects));
|
||||
}
|
||||
|
||||
// Read config of the adapter
|
||||
function getAdapterConfig(instance) {
|
||||
var objects = JSON.parse(fs.readFileSync(rootDir + 'tmp/' + appName + '-data/objects.json').toString());
|
||||
var id = 'system.adapter.' + adapterName.split('.').pop() + '.' + (instance || 0);
|
||||
return objects[id];
|
||||
}
|
||||
|
||||
if (typeof module !== undefined && module.parent) {
|
||||
module.exports.getAdapterConfig = getAdapterConfig;
|
||||
module.exports.setAdapterConfig = setAdapterConfig;
|
||||
module.exports.startController = startController;
|
||||
module.exports.stopController = stopController;
|
||||
module.exports.setupController = setupController;
|
||||
module.exports.stopAdapter = stopAdapter;
|
||||
module.exports.startAdapter = startAdapter;
|
||||
module.exports.installAdapter = installAdapter;
|
||||
module.exports.appName = appName;
|
||||
module.exports.adapterName = adapterName;
|
||||
module.exports.adapterStarted = adapterStarted;
|
||||
}
|
||||
397
test/testMSSQL.js
Normal file
397
test/testMSSQL.js
Normal file
@@ -0,0 +1,397 @@
|
||||
/* jshint -W097 */// jshint strict:false
|
||||
/*jslint node: true */
|
||||
/*jshint expr: true*/
|
||||
var expect = require('chai').expect;
|
||||
var setup = require(__dirname + '/lib/setup');
|
||||
|
||||
var objects = null;
|
||||
var states = null;
|
||||
var onStateChanged = null;
|
||||
var onObjectChanged = null;
|
||||
var sendToID = 1;
|
||||
|
||||
var adapterShortName = setup.adapterName.substring(setup.adapterName.indexOf('.')+1);
|
||||
|
||||
var now = new Date().getTime();
|
||||
|
||||
function checkConnectionOfAdapter(cb, counter) {
|
||||
counter = counter || 0;
|
||||
if (counter > 20) {
|
||||
cb && cb('Cannot check connection');
|
||||
return;
|
||||
}
|
||||
|
||||
states.getState('system.adapter.' + adapterShortName + '.0.alive', function (err, state) {
|
||||
if (err) console.error('MSSQL: ' + err);
|
||||
if (state && state.val) {
|
||||
cb && cb();
|
||||
} else {
|
||||
setTimeout(function () {
|
||||
checkConnectionOfAdapter(cb, counter + 1);
|
||||
}, 1000);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function checkValueOfState(id, value, cb, counter) {
|
||||
counter = counter || 0;
|
||||
if (counter > 20) {
|
||||
cb && cb('Cannot check value Of State ' + id);
|
||||
return;
|
||||
}
|
||||
|
||||
states.getState(id, function (err, state) {
|
||||
if (err) console.error('MSSQL: ' + err);
|
||||
if (value === null && !state) {
|
||||
cb && cb();
|
||||
} else
|
||||
if (state && (value === undefined || state.val === value)) {
|
||||
cb && cb();
|
||||
} else {
|
||||
setTimeout(function () {
|
||||
checkValueOfState(id, value, cb, counter + 1);
|
||||
}, 500);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function sendTo(target, command, message, callback) {
|
||||
onStateChanged = function (id, state) {
|
||||
if (id === 'messagebox.system.adapter.test.0') {
|
||||
callback(state.message);
|
||||
}
|
||||
};
|
||||
|
||||
states.pushMessage('system.adapter.' + target, {
|
||||
command: command,
|
||||
message: message,
|
||||
from: 'system.adapter.test.0',
|
||||
callback: {
|
||||
message: message,
|
||||
id: sendToID++,
|
||||
ack: false,
|
||||
time: (new Date()).getTime()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
describe('Test MSSQL', function() {
|
||||
before('Test MSSQL: Start js-controller', function (_done) {
|
||||
this.timeout(600000); // because of first install from npm
|
||||
setup.adapterStarted = false;
|
||||
|
||||
console.log('Started in TRAVIS: ' + (process.env.TRAVIS && process.env.TRAVIS==='true'));
|
||||
console.log('Started in APPVEYOR: ' + (process.env.APPVEYOR && process.env.APPVEYOR==='True'));
|
||||
|
||||
if (!(process.env.APPVEYOR && process.env.APPVEYOR==='True')) {
|
||||
console.log('MSSQL testing only available in Appveyor on Windows, ignore test run (APPVEYOR:' + JSON.stringify(process.env.APPVEYOR) + ', TRAVIS:' + JSON.stringify(process.env.TRAVIS) + ')');
|
||||
_done();
|
||||
return;
|
||||
}
|
||||
setup.setupController(function () {
|
||||
var config = setup.getAdapterConfig();
|
||||
// enable adapter
|
||||
config.common.enabled = true;
|
||||
config.common.loglevel = 'debug';
|
||||
|
||||
config.native.dbtype = 'mssql';
|
||||
config.native.user = 'sa';
|
||||
config.native.password = 'Password12!';
|
||||
|
||||
setup.setAdapterConfig(config.common, config.native);
|
||||
|
||||
setup.startController(true, function(id, obj) {}, function (id, state) {
|
||||
if (onStateChanged) onStateChanged(id, state);
|
||||
},
|
||||
function (_objects, _states) {
|
||||
objects = _objects;
|
||||
states = _states;
|
||||
objects.setObject('sql.0.memRss', {
|
||||
common: {
|
||||
type: 'number',
|
||||
role: 'state',
|
||||
custom: {
|
||||
"sql.0": {
|
||||
enabled: true,
|
||||
changesOnly: true,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
maxLength: 3,
|
||||
changesMinDelta: 0.5
|
||||
}
|
||||
}
|
||||
},
|
||||
type: 'state'
|
||||
}, _done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Test MSSQL: Check if adapter started', function (done) {
|
||||
this.timeout(60000);
|
||||
if (!(process.env.APPVEYOR && process.env.APPVEYOR==='True')) {
|
||||
done();
|
||||
return;
|
||||
}
|
||||
checkConnectionOfAdapter(function () {
|
||||
now = new Date().getTime();
|
||||
objects.setObject('system.adapter.test.0', {
|
||||
common: {
|
||||
|
||||
},
|
||||
type: 'instance'
|
||||
},
|
||||
function () {
|
||||
states.subscribeMessage('system.adapter.test.0');
|
||||
setTimeout(function () {
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'sql.0.memRss',
|
||||
options: {
|
||||
changesOnly: true,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
changesMinDelta: 0.5,
|
||||
storageType: 'Number'
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'system.adapter.sql.0.memHeapTotal',
|
||||
options: {
|
||||
changesOnly: false,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
storageType: 'String'
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'system.adapter.sql.0.uptime',
|
||||
options: {
|
||||
changesOnly: false,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
storageType: 'Boolean'
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
// wait till adapter receives the new settings
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 20000);
|
||||
});
|
||||
});
|
||||
});
|
||||
}, 10000);
|
||||
});
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Check Enabled Points after Enable', function (done) {
|
||||
this.timeout(20000);
|
||||
if (!(process.env.APPVEYOR && process.env.APPVEYOR==='True')) {
|
||||
done();
|
||||
return;
|
||||
}
|
||||
|
||||
sendTo('sql.0', 'getEnabledDPs', {}, function (result) {
|
||||
console.log(JSON.stringify(result));
|
||||
expect(Object.keys(result).length).to.be.equal(3);
|
||||
expect(result['sql.0.memRss'].enabled).to.be.true;
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 15000);
|
||||
});
|
||||
});
|
||||
it('Test MSSQL: Write values into DB', function (done) {
|
||||
this.timeout(10000);
|
||||
if (!(process.env.APPVEYOR && process.env.APPVEYOR==='True')) {
|
||||
done();
|
||||
return;
|
||||
}
|
||||
|
||||
this.timeout(10000);
|
||||
|
||||
states.setState('sql.0.memRss', {val: 2, ts: now - 20000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: true, ts: now - 10000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 2, ts: now - 5000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 2.2, ts: now - 4000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: '2.5', ts: now - 3000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 3, ts: now - 1000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 'Test', ts: now - 500}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(done, 5000);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
});
|
||||
it('Test MSSQL: Read values from DB using query', function (done) {
|
||||
this.timeout(10000);
|
||||
if (!(process.env.APPVEYOR && process.env.APPVEYOR==='True')) {
|
||||
done();
|
||||
return;
|
||||
}
|
||||
|
||||
sendTo('sql.0', 'query', "SELECT id FROM yunkong2.dbo.datapoints WHERE name='sql.0.memRss'", function (result) {
|
||||
sendTo('sql.0', 'query', 'SELECT * FROM yunkong2.dbo.ts_number WHERE id=' + result.result[0].id, function (result) {
|
||||
console.log('MSSQL: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.be.at.least(5);
|
||||
var found = 0;
|
||||
for (var i = 0; i < result.result.length; i++) {
|
||||
if (result.result[i].val >= 1 && result.result[i].val <= 3) found ++;
|
||||
}
|
||||
expect(found).to.be.equal(6);
|
||||
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 3000);
|
||||
});
|
||||
});
|
||||
});
|
||||
it('Test MSSQL: Read values from DB using GetHistory', function (done) {
|
||||
this.timeout(10000);
|
||||
if (!(process.env.APPVEYOR && process.env.APPVEYOR==='True')) {
|
||||
done();
|
||||
return;
|
||||
}
|
||||
|
||||
sendTo('sql.0', 'getHistory', {
|
||||
id: 'sql.0.memRss',
|
||||
options: {
|
||||
start: now - 30000,
|
||||
limit: 50,
|
||||
count: 50,
|
||||
aggregate: 'none'
|
||||
}
|
||||
}, function (result) {
|
||||
console.log('MSSQL: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.be.at.least(5);
|
||||
var found = 0;
|
||||
for (var i = 0; i < result.result.length; i++) {
|
||||
if (result.result[i].val >= 1 && result.result[i].val <= 3) found ++;
|
||||
}
|
||||
expect(found).to.be.equal(6);
|
||||
|
||||
sendTo('sql.0', 'getHistory', {
|
||||
id: 'sql.0.memRss',
|
||||
options: {
|
||||
start: now - 15000,
|
||||
end: now,
|
||||
limit: 2,
|
||||
count: 2,
|
||||
aggregate: 'none'
|
||||
}
|
||||
}, function (result) {
|
||||
console.log('MSSQL: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.be.equal(2);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Check Datapoint Types', function (done) {
|
||||
this.timeout(5000);
|
||||
if (!(process.env.APPVEYOR && process.env.APPVEYOR==='True')) {
|
||||
done();
|
||||
return;
|
||||
}
|
||||
|
||||
sendTo('sql.0', 'query', "SELECT name, type FROM yunkong2.dbo.datapoints", function (result) {
|
||||
console.log('MSSQL: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.least(3);
|
||||
for (var i = 0; i < result.result.length; i++) {
|
||||
if (result.result[i].name === 'sql.0.memRss') {
|
||||
expect(result.result[i].type).to.be.equal(0);
|
||||
}
|
||||
else if (result.result[i].name === 'system.adapter.sql.0.memHeapTotal') {
|
||||
expect(result.result[i].type).to.be.equal(1);
|
||||
}
|
||||
else if (result.result[i].name === 'system.adapter.sql.0.uptime') {
|
||||
expect(result.result[i].type).to.be.equal(2);
|
||||
}
|
||||
}
|
||||
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 3000);
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Disable Datapoint again', function (done) {
|
||||
this.timeout(5000);
|
||||
if (!(process.env.APPVEYOR && process.env.APPVEYOR==='True')) {
|
||||
done();
|
||||
return;
|
||||
}
|
||||
|
||||
sendTo('sql.0', 'disableHistory', {
|
||||
id: 'sql.0.memRss',
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
setTimeout(done, 2000);
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Check Enabled Points after Disable', function (done) {
|
||||
this.timeout(5000);
|
||||
if (!(process.env.APPVEYOR && process.env.APPVEYOR==='True')) {
|
||||
done();
|
||||
return;
|
||||
}
|
||||
|
||||
sendTo('sql.0', 'getEnabledDPs', {}, function (result) {
|
||||
console.log(JSON.stringify(result));
|
||||
expect(Object.keys(result).length).to.be.equal(2);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
after('Test MSSQL: Stop js-controller', function (done) {
|
||||
this.timeout(6000);
|
||||
if (!(process.env.APPVEYOR && process.env.APPVEYOR==='True')) {
|
||||
done();
|
||||
return;
|
||||
}
|
||||
|
||||
setup.stopController(function (normalTerminated) {
|
||||
console.log('MSSQL: Adapter normal terminated: ' + normalTerminated);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
374
test/testMySQL.js
Normal file
374
test/testMySQL.js
Normal file
@@ -0,0 +1,374 @@
|
||||
/* jshint -W097 */// jshint strict:false
|
||||
/*jslint node: true */
|
||||
/*jshint expr: true*/
|
||||
var expect = require('chai').expect;
|
||||
var setup = require(__dirname + '/lib/setup');
|
||||
|
||||
var objects = null;
|
||||
var states = null;
|
||||
var onStateChanged = null;
|
||||
var onObjectChanged = null;
|
||||
var sendToID = 1;
|
||||
|
||||
var adapterShortName = setup.adapterName.substring(setup.adapterName.indexOf('.')+1);
|
||||
|
||||
var now = new Date().getTime();
|
||||
|
||||
function checkConnectionOfAdapter(cb, counter) {
|
||||
counter = counter || 0;
|
||||
if (counter > 20) {
|
||||
cb && cb('Cannot check connection');
|
||||
return;
|
||||
}
|
||||
|
||||
states.getState('system.adapter.' + adapterShortName + '.0.alive', function (err, state) {
|
||||
if (err) console.error('MySQL: ' + err);
|
||||
if (state && state.val) {
|
||||
cb && cb();
|
||||
} else {
|
||||
setTimeout(function () {
|
||||
checkConnectionOfAdapter(cb, counter + 1);
|
||||
}, 1000);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function checkValueOfState(id, value, cb, counter) {
|
||||
counter = counter || 0;
|
||||
if (counter > 20) {
|
||||
cb && cb('Cannot check value Of State ' + id);
|
||||
return;
|
||||
}
|
||||
|
||||
states.getState(id, function (err, state) {
|
||||
if (err) console.error('MySQL: ' + err);
|
||||
if (value === null && !state) {
|
||||
cb && cb();
|
||||
} else
|
||||
if (state && (value === undefined || state.val === value)) {
|
||||
cb && cb();
|
||||
} else {
|
||||
setTimeout(function () {
|
||||
checkValueOfState(id, value, cb, counter + 1);
|
||||
}, 500);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function sendTo(target, command, message, callback) {
|
||||
onStateChanged = function (id, state) {
|
||||
if (id === 'messagebox.system.adapter.test.0') {
|
||||
callback(state.message);
|
||||
}
|
||||
};
|
||||
|
||||
states.pushMessage('system.adapter.' + target, {
|
||||
command: command,
|
||||
message: message,
|
||||
from: 'system.adapter.test.0',
|
||||
callback: {
|
||||
message: message,
|
||||
id: sendToID++,
|
||||
ack: false,
|
||||
time: (new Date()).getTime()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
describe('Test MySQL', function() {
|
||||
before('Test MySQL: Start js-controller', function (_done) {
|
||||
this.timeout(600000); // because of first install from npm
|
||||
setup.adapterStarted = false;
|
||||
|
||||
setup.setupController(function () {
|
||||
var config = setup.getAdapterConfig();
|
||||
// enable adapter
|
||||
config.common.enabled = true;
|
||||
config.common.loglevel = 'debug';
|
||||
|
||||
config.native.dbtype = 'mysql';
|
||||
config.native.user = 'root';
|
||||
if (process.env.APPVEYOR && process.env.APPVEYOR==='True') {
|
||||
config.native.password = 'Password12!';
|
||||
}
|
||||
|
||||
setup.setAdapterConfig(config.common, config.native);
|
||||
|
||||
setup.startController(true, function(id, obj) {}, function (id, state) {
|
||||
if (onStateChanged) onStateChanged(id, state);
|
||||
},
|
||||
function (_objects, _states) {
|
||||
objects = _objects;
|
||||
states = _states;
|
||||
objects.setObject('sql.0.memRss', {
|
||||
common: {
|
||||
type: 'number',
|
||||
role: 'state',
|
||||
custom: {
|
||||
"sql.0": {
|
||||
enabled: true,
|
||||
changesOnly: true,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
maxLength: 3,
|
||||
changesMinDelta: 0.5
|
||||
}
|
||||
}
|
||||
},
|
||||
type: 'state'
|
||||
}, _done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Test MySQL: Check if adapter started', function (done) {
|
||||
this.timeout(90000);
|
||||
checkConnectionOfAdapter(function () {
|
||||
now = new Date().getTime();
|
||||
objects.setObject('system.adapter.test.0', {
|
||||
common: {
|
||||
|
||||
},
|
||||
type: 'instance'
|
||||
},
|
||||
function () {
|
||||
states.subscribeMessage('system.adapter.test.0');
|
||||
setTimeout(function () {
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'system.adapter.sql.0.memHeapTotal',
|
||||
options: {
|
||||
changesOnly: false,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
storageType: 'String'
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'system.adapter.sql.0.alive',
|
||||
options: {
|
||||
changesOnly: false,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
storageType: 'Boolean'
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'system.adapter.sql.0.uptime',
|
||||
options: {
|
||||
changesOnly: false,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
storageType: false
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
// wait till adapter receives the new settings
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 70000);
|
||||
});
|
||||
});
|
||||
});
|
||||
}, 10000);
|
||||
});
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Check Enabled Points after Enable', function (done) {
|
||||
this.timeout(20000);
|
||||
|
||||
sendTo('sql.0', 'getEnabledDPs', {}, function (result) {
|
||||
console.log(JSON.stringify(result));
|
||||
expect(Object.keys(result).length).to.be.equal(4);
|
||||
expect(result['sql.0.memRss'].enabled).to.be.true;
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 15000);
|
||||
});
|
||||
});
|
||||
it('Test MySQL: Write values into DB', function (done) {
|
||||
this.timeout(10000);
|
||||
|
||||
states.setState('sql.0.memRss', {val: 2, ts: now - 20000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: true, ts: now - 10000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 2, ts: now - 5000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 2.2, ts: now - 4000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 2.3, ts: now - 3500}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: '2.5', ts: now - 3000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 3, ts: now - 1000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 'Test', ts: now - 500}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(done, 5000);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
});
|
||||
it('Test MySQL: Read values from DB using query', function (done) {
|
||||
this.timeout(10000);
|
||||
|
||||
sendTo('sql.0', 'query', 'SELECT id FROM yunkong2.datapoints WHERE name="sql.0.memRss"', function (result) {
|
||||
console.log('MySQL: ' + JSON.stringify(result.result, null, 2));
|
||||
sendTo('sql.0', 'query', 'SELECT * FROM yunkong2.ts_number WHERE id=' + result.result[0].id, function (result) {
|
||||
console.log('MySQL: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.be.at.least(5);
|
||||
var found = 0;
|
||||
var found22 = false;
|
||||
var found23 = false;
|
||||
for (var i = 0; i < result.result.length; i++) {
|
||||
if (result.result[i].val >= 1 && result.result[i].val <= 3) found ++;
|
||||
if (result.result[i].val === 2.2) found22 = true;
|
||||
if (result.result[i].val === 2.3) found23 = true;
|
||||
}
|
||||
expect(found).to.be.equal(6);
|
||||
expect(found22).to.be.false;
|
||||
expect(found23).to.be.true;
|
||||
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 3000);
|
||||
});
|
||||
});
|
||||
});
|
||||
it('Test MySQL: Read values from DB using GetHistory', function (done) {
|
||||
this.timeout(10000);
|
||||
|
||||
sendTo('sql.0', 'getHistory', {
|
||||
id: 'sql.0.memRss',
|
||||
options: {
|
||||
start: now - 30000,
|
||||
limit: 50,
|
||||
count: 50,
|
||||
aggregate: 'none'
|
||||
}
|
||||
}, function (result) {
|
||||
console.log('MySQL: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.be.at.least(5);
|
||||
var found = 0;
|
||||
for (var i = 0; i < result.result.length; i++) {
|
||||
if (result.result[i].val >= 1 && result.result[i].val <= 3) found ++;
|
||||
}
|
||||
expect(found).to.be.equal(6);
|
||||
|
||||
sendTo('sql.0', 'getHistory', {
|
||||
id: 'sql.0.memRss',
|
||||
options: {
|
||||
start: now - 15000,
|
||||
end: now,
|
||||
limit: 2,
|
||||
count: 2,
|
||||
aggregate: 'none'
|
||||
}
|
||||
}, function (result) {
|
||||
console.log('MySQL: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.be.equal(2);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Check Datapoint Types', function (done) {
|
||||
this.timeout(5000);
|
||||
|
||||
sendTo('sql.0', 'query', "SELECT id, name, type FROM yunkong2.datapoints", function (result) {
|
||||
console.log('MySQL: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.least(3);
|
||||
var uptime_id = null;
|
||||
for (var i = 0; i < result.result.length; i++) {
|
||||
if (result.result[i].name === 'sql.0.memRss') {
|
||||
expect(result.result[i].type).to.be.equal(0);
|
||||
}
|
||||
else if (result.result[i].name === 'system.adapter.sql.0.memHeapTotal') {
|
||||
expect(result.result[i].type).to.be.equal(1);
|
||||
}
|
||||
else if (result.result[i].name === 'system.adapter.sql.0.alive') {
|
||||
expect(result.result[i].type).to.be.equal(2);
|
||||
}
|
||||
else if (result.result[i].name === 'system.adapter.sql.0.uptime') {
|
||||
expect(result.result[i].type).to.be.equal(0);
|
||||
uptime_id = result.result[i].id;
|
||||
expect(uptime_id).to.be.not.null;
|
||||
}
|
||||
}
|
||||
|
||||
sendTo('sql.0', 'query', "UPDATE yunkong2.datapoints SET type=NULL WHERE id=" + uptime_id, function (result) {
|
||||
console.log('MySQL: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.affectedRows).to.be.equal(1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Disable Datapoint again', function (done) {
|
||||
this.timeout(5000);
|
||||
|
||||
sendTo('sql.0', 'disableHistory', {
|
||||
id: 'sql.0.memRss',
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
setTimeout(done, 2000);
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Check Enabled Points after Disable', function (done) {
|
||||
this.timeout(5000);
|
||||
|
||||
sendTo('sql.0', 'getEnabledDPs', {}, function (result) {
|
||||
console.log(JSON.stringify(result));
|
||||
expect(Object.keys(result).length).to.be.equal(3);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
after('Test MySQL: Stop js-controller', function (done) {
|
||||
this.timeout(6000);
|
||||
|
||||
setup.stopController(function (normalTerminated) {
|
||||
console.log('MySQL: Adapter normal terminated: ' + normalTerminated);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
482
test/testMySQLDash.js
Normal file
482
test/testMySQLDash.js
Normal file
@@ -0,0 +1,482 @@
|
||||
/* jshint -W097 */// jshint strict:false
|
||||
/*jslint node: true */
|
||||
/*jshint expr: true*/
|
||||
var expect = require('chai').expect;
|
||||
var setup = require(__dirname + '/lib/setup');
|
||||
|
||||
var objects = null;
|
||||
var states = null;
|
||||
var onStateChanged = null;
|
||||
var onObjectChanged = null;
|
||||
var sendToID = 1;
|
||||
|
||||
var adapterShortName = setup.adapterName.substring(setup.adapterName.indexOf('.')+1);
|
||||
|
||||
var now = new Date().getTime();
|
||||
|
||||
function checkConnectionOfAdapter(cb, counter) {
|
||||
counter = counter || 0;
|
||||
if (counter > 20) {
|
||||
cb && cb('Cannot check connection');
|
||||
return;
|
||||
}
|
||||
|
||||
states.getState('system.adapter.' + adapterShortName + '.0.alive', function (err, state) {
|
||||
if (err) console.error('MySQL-with-dash: ' + err);
|
||||
if (state && state.val) {
|
||||
cb && cb();
|
||||
} else {
|
||||
setTimeout(function () {
|
||||
checkConnectionOfAdapter(cb, counter + 1);
|
||||
}, 1000);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function checkValueOfState(id, value, cb, counter) {
|
||||
counter = counter || 0;
|
||||
if (counter > 20) {
|
||||
cb && cb('Cannot check value Of State ' + id);
|
||||
return;
|
||||
}
|
||||
|
||||
states.getState(id, function (err, state) {
|
||||
if (err) console.error('MySQL-with-dash: ' + err);
|
||||
if (value === null && !state) {
|
||||
cb && cb();
|
||||
} else
|
||||
if (state && (value === undefined || state.val === value)) {
|
||||
cb && cb();
|
||||
} else {
|
||||
setTimeout(function () {
|
||||
checkValueOfState(id, value, cb, counter + 1);
|
||||
}, 500);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function sendTo(target, command, message, callback) {
|
||||
onStateChanged = function (id, state) {
|
||||
if (id === 'messagebox.system.adapter.test.0') {
|
||||
callback(state.message);
|
||||
}
|
||||
};
|
||||
|
||||
states.pushMessage('system.adapter.' + target, {
|
||||
command: command,
|
||||
message: message,
|
||||
from: 'system.adapter.test.0',
|
||||
callback: {
|
||||
message: message,
|
||||
id: sendToID++,
|
||||
ack: false,
|
||||
time: (new Date()).getTime()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
describe('Test MySQL-with-dash', function() {
|
||||
before('Test MySQL-with-dash: Start js-controller', function (_done) {
|
||||
this.timeout(600000); // because of first install from npm
|
||||
setup.adapterStarted = false;
|
||||
|
||||
setup.setupController(function () {
|
||||
var config = setup.getAdapterConfig();
|
||||
// enable adapter
|
||||
config.common.enabled = true;
|
||||
config.common.loglevel = 'debug';
|
||||
|
||||
config.native.dbtype = 'mysql';
|
||||
config.native.user = 'root';
|
||||
config.native.dbname = 'io-broker';
|
||||
if (process.env.APPVEYOR && process.env.APPVEYOR==='True') {
|
||||
config.native.password = 'Password12!';
|
||||
}
|
||||
|
||||
setup.setAdapterConfig(config.common, config.native);
|
||||
|
||||
setup.startController(true, function(id, obj) {}, function (id, state) {
|
||||
if (onStateChanged) onStateChanged(id, state);
|
||||
},
|
||||
function (_objects, _states) {
|
||||
objects = _objects;
|
||||
states = _states;
|
||||
objects.setObject('sql.0.memRss', {
|
||||
common: {
|
||||
type: 'number',
|
||||
role: 'state',
|
||||
custom: {
|
||||
"sql.0": {
|
||||
enabled: true,
|
||||
changesOnly: true,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
maxLength: 3,
|
||||
changesMinDelta: 0.5
|
||||
}
|
||||
}
|
||||
},
|
||||
type: 'state'
|
||||
}, _done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Test MySQL-with-dash: Check if adapter started', function (done) {
|
||||
this.timeout(60000);
|
||||
checkConnectionOfAdapter(function () {
|
||||
now = new Date().getTime();
|
||||
objects.setObject('system.adapter.test.0', {
|
||||
common: {
|
||||
|
||||
},
|
||||
type: 'instance'
|
||||
},
|
||||
function () {
|
||||
states.subscribeMessage('system.adapter.test.0');
|
||||
setTimeout(function () {
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'system.adapter.sql.0.memHeapTotal',
|
||||
options: {
|
||||
changesOnly: false,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
storageType: false
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'system.adapter.sql.0.uptime',
|
||||
options: {
|
||||
changesOnly: false,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
storageType: false
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'system.adapter.sql.0.alive',
|
||||
options: {
|
||||
changesOnly: false,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
storageType: false
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
objects.setObject('sql.0.testValue2', {
|
||||
common: {
|
||||
type: 'number',
|
||||
role: 'state'
|
||||
},
|
||||
type: 'state'
|
||||
},
|
||||
function () {
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'sql.0.testValue2',
|
||||
options: {
|
||||
changesOnly: true,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
maxLength: 3,
|
||||
changesMinDelta: 0.5,
|
||||
aliasId: 'sql.0.testValue2-alias'
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
// wait till adapter receives the new settings
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 2000);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}, 10000);
|
||||
});
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Check Enabled Points after Enable', function (done) {
|
||||
this.timeout(20000);
|
||||
|
||||
sendTo('sql.0', 'getEnabledDPs', {}, function (result) {
|
||||
console.log(JSON.stringify(result));
|
||||
expect(Object.keys(result).length).to.be.equal(5);
|
||||
expect(result['sql.0.memRss'].enabled).to.be.true;
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 15000);
|
||||
});
|
||||
});
|
||||
it('Test MySQL-with-dash: Write values into DB', function (done) {
|
||||
this.timeout(10000);
|
||||
|
||||
states.setState('sql.0.memRss', {val: 2, ts: now - 20000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: true, ts: now - 10000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 2, ts: now - 5000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 2.2, ts: now - 4000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: '2.5', ts: now - 3000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 3, ts: now - 1000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 'Test', ts: now - 500}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.testValue2', {val: 1, ts: now - 2000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.testValue2', {val: 3, ts: now - 1000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(done, 5000);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
});
|
||||
it('Test MySQL-with-dash: Read values from DB using query', function (done) {
|
||||
this.timeout(10000);
|
||||
|
||||
sendTo('sql.0', 'query', 'SELECT id FROM `io-broker`.datapoints WHERE name="sql.0.memRss"', function (result) {
|
||||
console.log('MySQL-with-dash: ' + JSON.stringify(result.result, null, 2));
|
||||
sendTo('sql.0', 'query', 'SELECT * FROM `io-broker`.ts_number WHERE id=' + result.result[0].id, function (result) {
|
||||
console.log('MySQL-with-dash: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.be.at.least(5);
|
||||
var found = 0;
|
||||
for (var i = 0; i < result.result.length; i++) {
|
||||
if (result.result[i].val >= 1 && result.result[i].val <= 3) found ++;
|
||||
}
|
||||
expect(found).to.be.equal(6);
|
||||
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 3000);
|
||||
});
|
||||
});
|
||||
});
|
||||
it('Test MySQL-with-dash: Read values from DB using GetHistory', function (done) {
|
||||
this.timeout(10000);
|
||||
|
||||
sendTo('sql.0', 'getHistory', {
|
||||
id: 'sql.0.memRss',
|
||||
options: {
|
||||
start: now - 30000,
|
||||
limit: 50,
|
||||
count: 50,
|
||||
aggregate: 'none'
|
||||
}
|
||||
}, function (result) {
|
||||
console.log('MySQL-with-dash: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.be.at.least(5);
|
||||
var found = 0;
|
||||
for (var i = 0; i < result.result.length; i++) {
|
||||
if (result.result[i].val >= 1 && result.result[i].val <= 3) found ++;
|
||||
}
|
||||
expect(found).to.be.equal(6);
|
||||
|
||||
sendTo('sql.0', 'getHistory', {
|
||||
id: 'sql.0.memRss',
|
||||
options: {
|
||||
start: now - 15000,
|
||||
end: now,
|
||||
limit: 2,
|
||||
count: 2,
|
||||
aggregate: 'none'
|
||||
}
|
||||
}, function (result) {
|
||||
console.log('MySQL-with-dash: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.be.equal(2);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Check Datapoint Types', function (done) {
|
||||
this.timeout(5000);
|
||||
|
||||
sendTo('sql.0', 'query', "SELECT name, type FROM `io-broker`.datapoints", function (result) {
|
||||
console.log('MySQL: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.least(3);
|
||||
for (var i = 0; i < result.result.length; i++) {
|
||||
if (result.result[i].name === 'sql.0.memRss') {
|
||||
expect(result.result[i].type).to.be.equal(0);
|
||||
}
|
||||
else if (result.result[i].name === 'system.adapter.sql.0.memHeapTotal') {
|
||||
expect(result.result[i].type).to.be.equal(0);
|
||||
}
|
||||
else if (result.result[i].name === 'system.adapter.sql.0.alive') {
|
||||
expect(result.result[i].type).to.be.equal(2);
|
||||
}
|
||||
else if (result.result[i].name === 'system.adapter.sql.0.uptime') {
|
||||
expect(result.result[i].type).to.be.equal(0);
|
||||
}
|
||||
}
|
||||
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 3000);
|
||||
});
|
||||
});
|
||||
|
||||
it('Test ' + adapterShortName + ': Read values from DB using GetHistory for aliased testValue2', function (done) {
|
||||
this.timeout(25000);
|
||||
|
||||
sendTo('sql.0', 'getHistory', {
|
||||
id: 'sql.0.testValue2',
|
||||
options: {
|
||||
start: now - 5000,
|
||||
end: now,
|
||||
count: 50,
|
||||
aggregate: 'none'
|
||||
}
|
||||
}, function (result) {
|
||||
console.log(JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.be.equal(2);
|
||||
|
||||
sendTo('sql.0', 'getHistory', {
|
||||
id: 'sql.0.testValue2-alias',
|
||||
options: {
|
||||
start: now - 5000,
|
||||
end: now,
|
||||
count: 50,
|
||||
aggregate: 'none'
|
||||
}
|
||||
}, function (result2) {
|
||||
console.log(JSON.stringify(result2.result, null, 2));
|
||||
expect(result2.result.length).to.be.equal(2);
|
||||
for (var i = 0; i < result2.result.length; i++) {
|
||||
expect(result2.result[i].val).to.be.equal(result.result[i].val);
|
||||
}
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Test ' + adapterShortName + ': Remove Alias-ID', function (done) {
|
||||
this.timeout(5000);
|
||||
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'sql.0.testValue2',
|
||||
options: {
|
||||
aliasId: ''
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
// wait till adapter receives the new settings
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 2000);
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Add Alias-ID again', function (done) {
|
||||
this.timeout(5000);
|
||||
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'sql.0.testValue2',
|
||||
options: {
|
||||
aliasId: 'this.is.a.test-value'
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
// wait till adapter receives the new settings
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 2000);
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Change Alias-ID', function (done) {
|
||||
this.timeout(5000);
|
||||
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'sql.0.testValue2',
|
||||
options: {
|
||||
aliasId: 'this.is.another.test-value'
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
// wait till adapter receives the new settings
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 2000);
|
||||
});
|
||||
});
|
||||
|
||||
it('Test ' + adapterShortName + ': Disable Datapoint again', function (done) {
|
||||
this.timeout(5000);
|
||||
|
||||
sendTo('sql.0', 'disableHistory', {
|
||||
id: 'sql.0.memRss',
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
setTimeout(done, 2000);
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Check Enabled Points after Disable', function (done) {
|
||||
this.timeout(5000);
|
||||
|
||||
sendTo('sql.0', 'getEnabledDPs', {}, function (result) {
|
||||
console.log(JSON.stringify(result));
|
||||
expect(Object.keys(result).length).to.be.equal(4);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
after('Test MySQL-with-dash: Stop js-controller', function (done) {
|
||||
this.timeout(6000);
|
||||
|
||||
setup.stopController(function (normalTerminated) {
|
||||
console.log('MySQL-with-dash: Adapter normal terminated: ' + normalTerminated);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
368
test/testMySQLExisting.js
Normal file
368
test/testMySQLExisting.js
Normal file
@@ -0,0 +1,368 @@
|
||||
/* jshint -W097 */// jshint strict:false
|
||||
/*jslint node: true */
|
||||
/*jshint expr: true*/
|
||||
var expect = require('chai').expect;
|
||||
var setup = require(__dirname + '/lib/setup');
|
||||
|
||||
var objects = null;
|
||||
var states = null;
|
||||
var onStateChanged = null;
|
||||
var onObjectChanged = null;
|
||||
var sendToID = 1;
|
||||
|
||||
var adapterShortName = setup.adapterName.substring(setup.adapterName.indexOf('.')+1);
|
||||
|
||||
var now = new Date().getTime();
|
||||
var now2;
|
||||
if (!now2) now2 = new Date().getTime();
|
||||
|
||||
function checkConnectionOfAdapter(cb, counter) {
|
||||
counter = counter || 0;
|
||||
if (counter > 20) {
|
||||
cb && cb('Cannot check connection');
|
||||
return;
|
||||
}
|
||||
|
||||
states.getState('system.adapter.' + adapterShortName + '.0.alive', function (err, state) {
|
||||
if (err) console.error('MySQL: ' + err);
|
||||
if (state && state.val) {
|
||||
cb && cb();
|
||||
} else {
|
||||
setTimeout(function () {
|
||||
checkConnectionOfAdapter(cb, counter + 1);
|
||||
}, 1000);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function checkValueOfState(id, value, cb, counter) {
|
||||
counter = counter || 0;
|
||||
if (counter > 20) {
|
||||
cb && cb('Cannot check value Of State ' + id);
|
||||
return;
|
||||
}
|
||||
|
||||
states.getState(id, function (err, state) {
|
||||
if (err) console.error('MySQL: ' + err);
|
||||
if (value === null && !state) {
|
||||
cb && cb();
|
||||
} else
|
||||
if (state && (value === undefined || state.val === value)) {
|
||||
cb && cb();
|
||||
} else {
|
||||
setTimeout(function () {
|
||||
checkValueOfState(id, value, cb, counter + 1);
|
||||
}, 500);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function sendTo(target, command, message, callback) {
|
||||
onStateChanged = function (id, state) {
|
||||
if (id === 'messagebox.system.adapter.test.0') {
|
||||
callback(state.message);
|
||||
}
|
||||
};
|
||||
|
||||
states.pushMessage('system.adapter.' + target, {
|
||||
command: command,
|
||||
message: message,
|
||||
from: 'system.adapter.test.0',
|
||||
callback: {
|
||||
message: message,
|
||||
id: sendToID++,
|
||||
ack: false,
|
||||
time: (new Date()).getTime()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
describe('Test MySQL Existing', function() {
|
||||
before('Test MySQL Existing: Start js-controller', function (_done) {
|
||||
this.timeout(600000); // because of first install from npm
|
||||
setup.adapterStarted = false;
|
||||
|
||||
setup.setupController(function () {
|
||||
var config = setup.getAdapterConfig();
|
||||
// enable adapter
|
||||
config.common.enabled = true;
|
||||
config.common.loglevel = 'debug';
|
||||
|
||||
config.native.dbtype = 'mysql';
|
||||
config.native.user = 'root';
|
||||
if (process.env.APPVEYOR && process.env.APPVEYOR==='True') {
|
||||
config.native.password = 'Password12!';
|
||||
}
|
||||
|
||||
setup.setAdapterConfig(config.common, config.native);
|
||||
|
||||
setup.startController(true, function(id, obj) {}, function (id, state) {
|
||||
if (onStateChanged) onStateChanged(id, state);
|
||||
},
|
||||
function (_objects, _states) {
|
||||
objects = _objects;
|
||||
states = _states;
|
||||
_done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Test MySQL Existing: Check if adapter started', function (done) {
|
||||
this.timeout(60000);
|
||||
checkConnectionOfAdapter(function () {
|
||||
now = new Date().getTime();
|
||||
objects.setObject('system.adapter.test.0', {
|
||||
common: {
|
||||
|
||||
},
|
||||
type: 'instance'
|
||||
},
|
||||
function () {
|
||||
states.subscribeMessage('system.adapter.test.0');
|
||||
setTimeout(function() {
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'sql.0.memRss',
|
||||
options: {
|
||||
changesOnly: true,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
changesMinDelta: 0.5,
|
||||
storageType: false
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'system.adapter.sql.0.memHeapTotal',
|
||||
options: {
|
||||
changesOnly: false,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
storageType: 'Number'
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'system.adapter.sql.0.alive',
|
||||
options: {
|
||||
changesOnly: false,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
storageType: false
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'system.adapter.sql.0.uptime',
|
||||
options: {
|
||||
changesOnly: false,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
storageType: false
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
// wait till adapter receives the new settings
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 20000);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}, 10000);
|
||||
});
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Check Enabled Points after Enable', function (done) {
|
||||
this.timeout(20000);
|
||||
|
||||
sendTo('sql.0', 'getEnabledDPs', {}, function (result) {
|
||||
console.log(JSON.stringify(result));
|
||||
expect(Object.keys(result).length).to.be.equal(4);
|
||||
expect(result['sql.0.memRss'].enabled).to.be.true;
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 15000);
|
||||
});
|
||||
});
|
||||
it('Test MySQL Existing: Write values into DB', function (done) {
|
||||
this.timeout(10000);
|
||||
|
||||
states.setState('sql.0.memRss', {val: 2, ts: now - 20000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: true, ts: now - 10000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 2, ts: now - 5000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 2.2, ts: now - 4000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 2.3, ts: now - 3500}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: '2.5', ts: now - 3000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 3, ts: now - 1000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 'Test', ts: now - 500}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(done, 5000);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
});
|
||||
it('Test MySQL Existing: Read values from DB using query', function (done) {
|
||||
this.timeout(10000);
|
||||
|
||||
sendTo('sql.0', 'query', 'SELECT id FROM yunkong2.datapoints WHERE name="sql.0.memRss"', function (result) {
|
||||
console.log('MySQL: ' + JSON.stringify(result.result, null, 2));
|
||||
sendTo('sql.0', 'query', 'SELECT * FROM yunkong2.ts_number WHERE id=' + result.result[0].id, function (result) {
|
||||
console.log('MySQL: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.be.at.least(5);
|
||||
var found = 0;
|
||||
var found22 = false;
|
||||
var found23 = false;
|
||||
for (var i = 0; i < result.result.length; i++) {
|
||||
if (result.result[i].val >= 1 && result.result[i].val <= 3) found ++;
|
||||
if (result.result[i].val === 2.2) found22 = true;
|
||||
if (result.result[i].val === 2.3) found23 = true;
|
||||
}
|
||||
expect(found).to.be.equal(12);
|
||||
expect(found22).to.be.false;
|
||||
expect(found23).to.be.true;
|
||||
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 3000);
|
||||
});
|
||||
});
|
||||
});
|
||||
it('Test MySQL Existing: Read values from DB using GetHistory', function (done) {
|
||||
this.timeout(10000);
|
||||
|
||||
sendTo('sql.0', 'getHistory', {
|
||||
id: 'sql.0.memRss',
|
||||
options: {
|
||||
start: now2 - 30000,
|
||||
limit: 50,
|
||||
count: 50,
|
||||
aggregate: 'none'
|
||||
}
|
||||
}, function (result) {
|
||||
console.log('MySQL: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.be.at.least(5);
|
||||
var found = 0;
|
||||
for (var i = 0; i < result.result.length; i++) {
|
||||
if (result.result[i].val >= 1 && result.result[i].val <= 3) found ++;
|
||||
}
|
||||
expect(found).to.be.equal(12);
|
||||
|
||||
sendTo('sql.0', 'getHistory', {
|
||||
id: 'sql.0.memRss',
|
||||
options: {
|
||||
start: now2 - 15000,
|
||||
end: now,
|
||||
limit: 2,
|
||||
count: 2,
|
||||
aggregate: 'none'
|
||||
}
|
||||
}, function (result) {
|
||||
console.log('MySQL: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.be.equal(2);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Check Datapoint Types', function (done) {
|
||||
this.timeout(5000);
|
||||
|
||||
sendTo('sql.0', 'query', "SELECT name, type FROM yunkong2.datapoints", function (result) {
|
||||
console.log('MySQL: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.least(3);
|
||||
for (var i = 0; i < result.result.length; i++) {
|
||||
if (result.result[i].name === 'sql.0.memRss') {
|
||||
expect(result.result[i].type).to.be.equal(0);
|
||||
}
|
||||
else if (result.result[i].name === 'system.adapter.sql.0.memHeapTotal') {
|
||||
expect(result.result[i].type).to.be.equal(0);
|
||||
}
|
||||
else if (result.result[i].name === 'system.adapter.sql.0.alive') {
|
||||
expect(result.result[i].type).to.be.equal(2);
|
||||
}
|
||||
else if (result.result[i].name === 'system.adapter.sql.0.uptime') {
|
||||
expect(result.result[i].type).to.be.equal(0);
|
||||
}
|
||||
}
|
||||
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 3000);
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Disable Datapoint again', function (done) {
|
||||
this.timeout(5000);
|
||||
|
||||
sendTo('sql.0', 'disableHistory', {
|
||||
id: 'sql.0.memRss',
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
setTimeout(done, 2000);
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Check Enabled Points after Disable', function (done) {
|
||||
this.timeout(5000);
|
||||
|
||||
sendTo('sql.0', 'getEnabledDPs', {}, function (result) {
|
||||
console.log(JSON.stringify(result));
|
||||
expect(Object.keys(result).length).to.be.equal(3);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
after('Test MySQL Existing: Stop js-controller', function (done) {
|
||||
this.timeout(6000);
|
||||
|
||||
setup.stopController(function (normalTerminated) {
|
||||
console.log('MySQL: Adapter normal terminated: ' + normalTerminated);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
369
test/testMySQLExistingNoNulls.js
Normal file
369
test/testMySQLExistingNoNulls.js
Normal file
@@ -0,0 +1,369 @@
|
||||
/* jshint -W097 */// jshint strict:false
|
||||
/*jslint node: true */
|
||||
/*jshint expr: true*/
|
||||
var expect = require('chai').expect;
|
||||
var setup = require(__dirname + '/lib/setup');
|
||||
|
||||
var objects = null;
|
||||
var states = null;
|
||||
var onStateChanged = null;
|
||||
var onObjectChanged = null;
|
||||
var sendToID = 1;
|
||||
|
||||
var adapterShortName = setup.adapterName.substring(setup.adapterName.indexOf('.')+1);
|
||||
|
||||
var now = new Date().getTime();
|
||||
var now2;
|
||||
if (!now2) now2 = new Date().getTime();
|
||||
|
||||
function checkConnectionOfAdapter(cb, counter) {
|
||||
counter = counter || 0;
|
||||
if (counter > 20) {
|
||||
cb && cb('Cannot check connection');
|
||||
return;
|
||||
}
|
||||
|
||||
states.getState('system.adapter.' + adapterShortName + '.0.alive', function (err, state) {
|
||||
if (err) console.error('MySQL: ' + err);
|
||||
if (state && state.val) {
|
||||
cb && cb();
|
||||
} else {
|
||||
setTimeout(function () {
|
||||
checkConnectionOfAdapter(cb, counter + 1);
|
||||
}, 1000);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function checkValueOfState(id, value, cb, counter) {
|
||||
counter = counter || 0;
|
||||
if (counter > 20) {
|
||||
cb && cb('Cannot check value Of State ' + id);
|
||||
return;
|
||||
}
|
||||
|
||||
states.getState(id, function (err, state) {
|
||||
if (err) console.error('MySQL: ' + err);
|
||||
if (value === null && !state) {
|
||||
cb && cb();
|
||||
} else
|
||||
if (state && (value === undefined || state.val === value)) {
|
||||
cb && cb();
|
||||
} else {
|
||||
setTimeout(function () {
|
||||
checkValueOfState(id, value, cb, counter + 1);
|
||||
}, 500);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function sendTo(target, command, message, callback) {
|
||||
onStateChanged = function (id, state) {
|
||||
if (id === 'messagebox.system.adapter.test.0') {
|
||||
callback(state.message);
|
||||
}
|
||||
};
|
||||
|
||||
states.pushMessage('system.adapter.' + target, {
|
||||
command: command,
|
||||
message: message,
|
||||
from: 'system.adapter.test.0',
|
||||
callback: {
|
||||
message: message,
|
||||
id: sendToID++,
|
||||
ack: false,
|
||||
time: (new Date()).getTime()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
describe('Test MySQL Existing No Nulls', function() {
|
||||
before('Test MySQL Existing No Nulls: Start js-controller', function (_done) {
|
||||
this.timeout(600000); // because of first install from npm
|
||||
setup.adapterStarted = false;
|
||||
|
||||
setup.setupController(function () {
|
||||
var config = setup.getAdapterConfig();
|
||||
// enable adapter
|
||||
config.common.enabled = true;
|
||||
config.common.loglevel = 'debug';
|
||||
|
||||
config.native.writeNulls = false;
|
||||
config.native.dbtype = 'mysql';
|
||||
config.native.user = 'root';
|
||||
if (process.env.APPVEYOR && process.env.APPVEYOR==='True') {
|
||||
config.native.password = 'Password12!';
|
||||
}
|
||||
|
||||
setup.setAdapterConfig(config.common, config.native);
|
||||
|
||||
setup.startController(true, function(id, obj) {}, function (id, state) {
|
||||
if (onStateChanged) onStateChanged(id, state);
|
||||
},
|
||||
function (_objects, _states) {
|
||||
objects = _objects;
|
||||
states = _states;
|
||||
_done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Test MySQL Existing No Nulls: Check if adapter started', function (done) {
|
||||
this.timeout(60000);
|
||||
checkConnectionOfAdapter(function () {
|
||||
now = new Date().getTime();
|
||||
objects.setObject('system.adapter.test.0', {
|
||||
common: {
|
||||
|
||||
},
|
||||
type: 'instance'
|
||||
},
|
||||
function () {
|
||||
states.subscribeMessage('system.adapter.test.0');
|
||||
setTimeout(function() {
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'sql.0.memRss',
|
||||
options: {
|
||||
changesOnly: true,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
changesMinDelta: 0.5,
|
||||
storageType: false
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'system.adapter.sql.0.memHeapTotal',
|
||||
options: {
|
||||
changesOnly: false,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
storageType: 'Number'
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'system.adapter.sql.0.alive',
|
||||
options: {
|
||||
changesOnly: false,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
storageType: false
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'system.adapter.sql.0.uptime',
|
||||
options: {
|
||||
changesOnly: false,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
storageType: false
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
// wait till adapter receives the new settings
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 20000);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}, 10000);
|
||||
});
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Check Enabled Points after Enable', function (done) {
|
||||
this.timeout(20000);
|
||||
|
||||
sendTo('sql.0', 'getEnabledDPs', {}, function (result) {
|
||||
console.log(JSON.stringify(result));
|
||||
expect(Object.keys(result).length).to.be.equal(4);
|
||||
expect(result['sql.0.memRss'].enabled).to.be.true;
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 15000);
|
||||
});
|
||||
});
|
||||
it('Test MySQL Existing No Nulls: Write values into DB', function (done) {
|
||||
this.timeout(10000);
|
||||
|
||||
states.setState('sql.0.memRss', {val: 2, ts: now - 20000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: true, ts: now - 10000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 2, ts: now - 5000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 2.2, ts: now - 4000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 2.3, ts: now - 3500}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: '2.5', ts: now - 3000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 3, ts: now - 1000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 'Test', ts: now - 500}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(done, 5000);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
});
|
||||
it('Test MySQL Existing No Nulls: Read values from DB using query', function (done) {
|
||||
this.timeout(10000);
|
||||
|
||||
sendTo('sql.0', 'query', 'SELECT id FROM yunkong2.datapoints WHERE name="sql.0.memRss"', function (result) {
|
||||
console.log('MySQL: ' + JSON.stringify(result.result, null, 2));
|
||||
sendTo('sql.0', 'query', 'SELECT * FROM yunkong2.ts_number WHERE id=' + result.result[0].id, function (result) {
|
||||
console.log('MySQL: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.be.at.least(5);
|
||||
var found = 0;
|
||||
var found22 = false;
|
||||
var found23 = false;
|
||||
for (var i = 0; i < result.result.length; i++) {
|
||||
if (result.result[i].val >= 1 && result.result[i].val <= 3) found ++;
|
||||
if (result.result[i].val === 2.2) found22 = true;
|
||||
if (result.result[i].val === 2.3) found23 = true;
|
||||
}
|
||||
expect(found).to.be.equal(18);
|
||||
expect(found22).to.be.false;
|
||||
expect(found23).to.be.true;
|
||||
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 3000);
|
||||
});
|
||||
});
|
||||
});
|
||||
it('Test MySQL Existing No Nulls: Read values from DB using GetHistory', function (done) {
|
||||
this.timeout(10000);
|
||||
|
||||
sendTo('sql.0', 'getHistory', {
|
||||
id: 'sql.0.memRss',
|
||||
options: {
|
||||
start: now2 - 30000,
|
||||
limit: 50,
|
||||
count: 50,
|
||||
aggregate: 'none'
|
||||
}
|
||||
}, function (result) {
|
||||
console.log('MySQL: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.be.at.least(5);
|
||||
var found = 0;
|
||||
for (var i = 0; i < result.result.length; i++) {
|
||||
if (result.result[i].val >= 1 && result.result[i].val <= 3) found ++;
|
||||
}
|
||||
expect(found).to.be.equal(18);
|
||||
|
||||
sendTo('sql.0', 'getHistory', {
|
||||
id: 'sql.0.memRss',
|
||||
options: {
|
||||
start: now2 - 15000,
|
||||
end: now,
|
||||
limit: 2,
|
||||
count: 2,
|
||||
aggregate: 'none'
|
||||
}
|
||||
}, function (result) {
|
||||
console.log('MySQL: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.be.equal(2);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Check Datapoint Types', function (done) {
|
||||
this.timeout(5000);
|
||||
|
||||
sendTo('sql.0', 'query', "SELECT name, type FROM yunkong2.datapoints", function (result) {
|
||||
console.log('MySQL: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.least(3);
|
||||
for (var i = 0; i < result.result.length; i++) {
|
||||
if (result.result[i].name === 'sql.0.memRss') {
|
||||
expect(result.result[i].type).to.be.equal(0);
|
||||
}
|
||||
else if (result.result[i].name === 'system.adapter.sql.0.memHeapTotal') {
|
||||
expect(result.result[i].type).to.be.equal(0);
|
||||
}
|
||||
else if (result.result[i].name === 'system.adapter.sql.0.alive') {
|
||||
expect(result.result[i].type).to.be.equal(2);
|
||||
}
|
||||
else if (result.result[i].name === 'system.adapter.sql.0.uptime') {
|
||||
expect(result.result[i].type).to.be.equal(0);
|
||||
}
|
||||
}
|
||||
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 3000);
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Disable Datapoint again', function (done) {
|
||||
this.timeout(5000);
|
||||
|
||||
sendTo('sql.0', 'disableHistory', {
|
||||
id: 'sql.0.memRss',
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
setTimeout(done, 2000);
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Check Enabled Points after Disable', function (done) {
|
||||
this.timeout(5000);
|
||||
|
||||
sendTo('sql.0', 'getEnabledDPs', {}, function (result) {
|
||||
console.log(JSON.stringify(result));
|
||||
expect(Object.keys(result).length).to.be.equal(3);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
after('Test MySQL Existing No Nulls: Stop js-controller', function (done) {
|
||||
this.timeout(6000);
|
||||
|
||||
setup.stopController(function (normalTerminated) {
|
||||
console.log('MySQL: Adapter normal terminated: ' + normalTerminated);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
91
test/testPackageFiles.js
Normal file
91
test/testPackageFiles.js
Normal file
@@ -0,0 +1,91 @@
|
||||
/* jshint -W097 */
|
||||
/* jshint strict:false */
|
||||
/* jslint node: true */
|
||||
/* jshint expr: true */
|
||||
var expect = require('chai').expect;
|
||||
var fs = require('fs');
|
||||
|
||||
describe('Test package.json and io-package.json', function() {
|
||||
it('Test package files', function (done) {
|
||||
console.log();
|
||||
|
||||
var fileContentIOPackage = fs.readFileSync(__dirname + '/../io-package.json', 'utf8');
|
||||
var ioPackage = JSON.parse(fileContentIOPackage);
|
||||
|
||||
var fileContentNPMPackage = fs.readFileSync(__dirname + '/../package.json', 'utf8');
|
||||
var npmPackage = JSON.parse(fileContentNPMPackage);
|
||||
|
||||
expect(ioPackage).to.be.an('object');
|
||||
expect(npmPackage).to.be.an('object');
|
||||
|
||||
expect(ioPackage.common.version, 'ERROR: Version number in io-package.json needs to exist').to.exist;
|
||||
expect(npmPackage.version, 'ERROR: Version number in package.json needs to exist').to.exist;
|
||||
|
||||
expect(ioPackage.common.version, 'ERROR: Version numbers in package.json and io-package.json needs to match').to.be.equal(npmPackage.version);
|
||||
|
||||
if (!ioPackage.common.news || !ioPackage.common.news[ioPackage.common.version]) {
|
||||
console.log('WARNING: No news entry for current version exists in io-package.json, no rollback in Admin possible!');
|
||||
console.log();
|
||||
}
|
||||
|
||||
expect(npmPackage.author, 'ERROR: Author in package.json needs to exist').to.exist;
|
||||
expect(ioPackage.common.authors, 'ERROR: Authors in io-package.json needs to exist').to.exist;
|
||||
|
||||
if (ioPackage.common.name.indexOf('template') !== 0) {
|
||||
if (Array.isArray(ioPackage.common.authors)) {
|
||||
expect(ioPackage.common.authors.length, 'ERROR: Author in io-package.json needs to be set').to.not.be.equal(0);
|
||||
if (ioPackage.common.authors.length === 1) {
|
||||
expect(ioPackage.common.authors[0], 'ERROR: Author in io-package.json needs to be a real name').to.not.be.equal('my Name <my@email.com>');
|
||||
}
|
||||
}
|
||||
else {
|
||||
expect(ioPackage.common.authors, 'ERROR: Author in io-package.json needs to be a real name').to.not.be.equal('my Name <my@email.com>');
|
||||
}
|
||||
}
|
||||
else {
|
||||
console.log('WARNING: Testing for set authors field in io-package skipped because template adapter');
|
||||
console.log();
|
||||
}
|
||||
expect(fs.existsSync(__dirname + '/../README.md'), 'ERROR: README.md needs to exist! Please create one with description, detail information and changelog. English is mandatory.').to.be.true;
|
||||
if (!ioPackage.common.titleLang || typeof ioPackage.common.titleLang !== 'object') {
|
||||
console.log('WARNING: titleLang is not existing in io-package.json. Please add');
|
||||
console.log();
|
||||
}
|
||||
if (
|
||||
ioPackage.common.title.indexOf('yunkong2') !== -1 ||
|
||||
ioPackage.common.title.indexOf('yunkong2') !== -1 ||
|
||||
ioPackage.common.title.indexOf('adapter') !== -1 ||
|
||||
ioPackage.common.title.indexOf('Adapter') !== -1
|
||||
) {
|
||||
console.log('WARNING: title contains Adapter or yunkong2. It is clear anyway, that it is adapter for yunkong2.');
|
||||
console.log();
|
||||
}
|
||||
|
||||
if (ioPackage.common.name.indexOf('vis-') !== 0) {
|
||||
if (!ioPackage.common.materialize || !fs.existsSync(__dirname + '/../admin/index_m.html') || !fs.existsSync(__dirname + '/../gulpfile.js')) {
|
||||
console.log('WARNING: Admin3 support is missing! Please add it');
|
||||
console.log();
|
||||
}
|
||||
if (ioPackage.common.materialize) {
|
||||
expect(fs.existsSync(__dirname + '/../admin/index_m.html'), 'Admin3 support is enabled in io-package.json, but index_m.html is missing!').to.be.true;
|
||||
}
|
||||
}
|
||||
|
||||
var licenseFileExists = fs.existsSync(__dirname + '/../LICENSE');
|
||||
var fileContentReadme = fs.readFileSync(__dirname + '/../README.md', 'utf8');
|
||||
if (fileContentReadme.indexOf('## Changelog') === -1) {
|
||||
console.log('Warning: The README.md should have a section ## Changelog');
|
||||
console.log();
|
||||
}
|
||||
expect((licenseFileExists || fileContentReadme.indexOf('## License') !== -1), 'A LICENSE must exist as LICENSE file or as part of the README.md').to.be.true;
|
||||
if (!licenseFileExists) {
|
||||
console.log('Warning: The License should also exist as LICENSE file');
|
||||
console.log();
|
||||
}
|
||||
if (fileContentReadme.indexOf('## License') === -1) {
|
||||
console.log('Warning: The README.md should also have a section ## License to be shown in Admin3');
|
||||
console.log();
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
342
test/testPostgreSQL.js
Normal file
342
test/testPostgreSQL.js
Normal file
@@ -0,0 +1,342 @@
|
||||
/* jshint -W097 */// jshint strict:false
|
||||
/*jslint node: true */
|
||||
/*jshint expr: true*/
|
||||
var expect = require('chai').expect;
|
||||
var setup = require(__dirname + '/lib/setup');
|
||||
|
||||
var objects = null;
|
||||
var states = null;
|
||||
var onStateChanged = null;
|
||||
var onObjectChanged = null;
|
||||
var sendToID = 1;
|
||||
|
||||
var adapterShortName = setup.adapterName.substring(setup.adapterName.indexOf('.')+1);
|
||||
|
||||
var now = new Date().getTime();
|
||||
|
||||
function checkConnectionOfAdapter(cb, counter) {
|
||||
counter = counter || 0;
|
||||
if (counter > 20) {
|
||||
cb && cb('Cannot check connection');
|
||||
return;
|
||||
}
|
||||
|
||||
states.getState('system.adapter.' + adapterShortName + '.0.alive', function (err, state) {
|
||||
if (err) console.error('PostgreSQL: ' + err);
|
||||
if (state && state.val) {
|
||||
cb && cb();
|
||||
} else {
|
||||
setTimeout(function () {
|
||||
checkConnectionOfAdapter(cb, counter + 1);
|
||||
}, 1000);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function checkValueOfState(id, value, cb, counter) {
|
||||
counter = counter || 0;
|
||||
if (counter > 20) {
|
||||
cb && cb('Cannot check value Of State ' + id);
|
||||
return;
|
||||
}
|
||||
|
||||
states.getState(id, function (err, state) {
|
||||
if (err) console.error('PostgreSQL: ' + err);
|
||||
if (value === null && !state) {
|
||||
cb && cb();
|
||||
} else
|
||||
if (state && (value === undefined || state.val === value)) {
|
||||
cb && cb();
|
||||
} else {
|
||||
setTimeout(function () {
|
||||
checkValueOfState(id, value, cb, counter + 1);
|
||||
}, 500);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function sendTo(target, command, message, callback) {
|
||||
onStateChanged = function (id, state) {
|
||||
if (id === 'messagebox.system.adapter.test.0') {
|
||||
callback(state.message);
|
||||
}
|
||||
};
|
||||
|
||||
states.pushMessage('system.adapter.' + target, {
|
||||
command: command,
|
||||
message: message,
|
||||
from: 'system.adapter.test.0',
|
||||
callback: {
|
||||
message: message,
|
||||
id: sendToID++,
|
||||
ack: false,
|
||||
time: (new Date()).getTime()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
describe('Test PostgreSQL', function() {
|
||||
before('Test PostgreSQL: Start js-controller', function (_done) {
|
||||
this.timeout(600000); // because of first install from npm
|
||||
setup.adapterStarted = false;
|
||||
|
||||
setup.setupController(function () {
|
||||
var config = setup.getAdapterConfig();
|
||||
// enable adapter
|
||||
config.common.enabled = true;
|
||||
config.common.loglevel = 'debug';
|
||||
|
||||
config.native.dbtype = 'postgresql';
|
||||
config.native.user = 'postgres';
|
||||
if (process.env.APPVEYOR && process.env.APPVEYOR==='True') {
|
||||
config.native.password = 'Password12!';
|
||||
}
|
||||
|
||||
setup.setAdapterConfig(config.common, config.native);
|
||||
|
||||
setup.startController(true, function(id, obj) {}, function (id, state) {
|
||||
if (onStateChanged) onStateChanged(id, state);
|
||||
},
|
||||
function (_objects, _states) {
|
||||
objects = _objects;
|
||||
states = _states;
|
||||
objects.setObject('sql.0.memRss', {
|
||||
common: {
|
||||
type: 'number',
|
||||
role: 'state',
|
||||
custom: {
|
||||
"sql.0": {
|
||||
enabled: true,
|
||||
changesOnly: true,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
maxLength: 3,
|
||||
changesMinDelta: 0.5
|
||||
}
|
||||
}
|
||||
},
|
||||
type: 'state'
|
||||
}, _done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Test PostgreSQL: Check if adapter started', function (done) {
|
||||
this.timeout(60000);
|
||||
checkConnectionOfAdapter(function () {
|
||||
now = new Date().getTime();
|
||||
objects.setObject('system.adapter.test.0', {
|
||||
common: {
|
||||
|
||||
},
|
||||
type: 'instance'
|
||||
},
|
||||
function () {
|
||||
states.subscribeMessage('system.adapter.test.0');
|
||||
setTimeout(function () {
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'system.adapter.sql.0.memHeapTotal',
|
||||
options: {
|
||||
changesOnly: false,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
storageType: 'String'
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'system.adapter.sql.0.uptime',
|
||||
options: {
|
||||
changesOnly: false,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
storageType: 'Boolean'
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
// wait till adapter receives the new settings
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 20000);
|
||||
});
|
||||
});
|
||||
}, 10000);
|
||||
});
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Check Enabled Points after Enable', function (done) {
|
||||
this.timeout(20000);
|
||||
|
||||
sendTo('sql.0', 'getEnabledDPs', {}, function (result) {
|
||||
console.log(JSON.stringify(result));
|
||||
expect(Object.keys(result).length).to.be.equal(3);
|
||||
expect(result['sql.0.memRss'].enabled).to.be.true;
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 15000);
|
||||
});
|
||||
});
|
||||
it('Test PostgreSQL: Write values into DB', function (done) {
|
||||
this.timeout(10000);
|
||||
|
||||
this.timeout(10000);
|
||||
|
||||
states.setState('sql.0.memRss', {val: 2, ts: now - 20000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: true, ts: now - 10000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 2, ts: now - 5000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 2.2, ts: now - 4000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: '2.5', ts: now - 3000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 3, ts: now - 1000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 'Test', ts: now - 500}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(done, 5000);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
});
|
||||
it('Test PostgreSQL: Read values from DB using query', function (done) {
|
||||
this.timeout(10000);
|
||||
|
||||
sendTo('sql.0', 'query', "SELECT id FROM datapoints WHERE name='sql.0.memRss'", function (result) {
|
||||
sendTo('sql.0', 'query', 'SELECT * FROM ts_number WHERE id=' + result.result[0].id, function (result) {
|
||||
console.log('PostgreSQL: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.be.at.least(5);
|
||||
var found = 0;
|
||||
for (var i = 0; i < result.result.length; i++) {
|
||||
if (result.result[i].val >= 1 && result.result[i].val <= 3) found ++;
|
||||
}
|
||||
expect(found).to.be.equal(6);
|
||||
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 3000);
|
||||
});
|
||||
});
|
||||
});
|
||||
it('Test PostgreSQL: Read values from DB using GetHistory', function (done) {
|
||||
this.timeout(10000);
|
||||
|
||||
sendTo('sql.0', 'getHistory', {
|
||||
id: 'sql.0.memRss',
|
||||
options: {
|
||||
start: now - 30000,
|
||||
limit: 50,
|
||||
count: 50,
|
||||
aggregate: 'none'
|
||||
}
|
||||
}, function (result) {
|
||||
console.log('PostgreSQL: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.be.at.least(5);
|
||||
var found = 0;
|
||||
for (var i = 0; i < result.result.length; i++) {
|
||||
if (result.result[i].val >= 1 && result.result[i].val <= 3) found ++;
|
||||
}
|
||||
expect(found).to.be.equal(6);
|
||||
|
||||
sendTo('sql.0', 'getHistory', {
|
||||
id: 'sql.0.memRss',
|
||||
options: {
|
||||
start: now - 15000,
|
||||
end: now,
|
||||
limit: 2,
|
||||
count: 2,
|
||||
aggregate: 'none'
|
||||
}
|
||||
}, function (result) {
|
||||
console.log('PostgreSQL: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.be.equal(2);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Check Datapoint Types', function (done) {
|
||||
this.timeout(5000);
|
||||
|
||||
sendTo('sql.0', 'query', "SELECT name, type FROM datapoints", function (result) {
|
||||
console.log('PostgreSQL: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.least(3);
|
||||
for (var i = 0; i < result.result.length; i++) {
|
||||
if (result.result[i].name === 'sql.0.memRss') {
|
||||
expect(result.result[i].type).to.be.equal(0);
|
||||
}
|
||||
else if (result.result[i].name === 'system.adapter.sql.0.memHeapTotal') {
|
||||
expect(result.result[i].type).to.be.equal(1);
|
||||
}
|
||||
else if (result.result[i].name === 'system.adapter.sql.0.uptime') {
|
||||
expect(result.result[i].type).to.be.equal(2);
|
||||
}
|
||||
}
|
||||
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 3000);
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Disable Datapoint again', function (done) {
|
||||
this.timeout(5000);
|
||||
|
||||
sendTo('sql.0', 'disableHistory', {
|
||||
id: 'sql.0.memRss',
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
setTimeout(done, 2000);
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Check Enabled Points after Disable', function (done) {
|
||||
this.timeout(5000);
|
||||
|
||||
sendTo('sql.0', 'getEnabledDPs', {}, function (result) {
|
||||
console.log(JSON.stringify(result));
|
||||
expect(Object.keys(result).length).to.be.equal(2);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
after('Test PostgreSQL: Stop js-controller', function (done) {
|
||||
this.timeout(6000);
|
||||
|
||||
setup.stopController(function (normalTerminated) {
|
||||
console.log('PostgreSQL: Adapter normal terminated: ' + normalTerminated);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
355
test/testSQLite.js
Normal file
355
test/testSQLite.js
Normal file
@@ -0,0 +1,355 @@
|
||||
/* jshint -W097 */// jshint strict:false
|
||||
/*jslint node: true */
|
||||
/*jshint expr: true*/
|
||||
var expect = require('chai').expect;
|
||||
var setup = require(__dirname + '/lib/setup');
|
||||
|
||||
var objects = null;
|
||||
var states = null;
|
||||
var onStateChanged = null;
|
||||
var onObjectChanged = null;
|
||||
var sendToID = 1;
|
||||
|
||||
var adapterShortName = setup.adapterName.substring(setup.adapterName.indexOf('.')+1);
|
||||
|
||||
var now = new Date().getTime();
|
||||
|
||||
function checkConnectionOfAdapter(cb, counter) {
|
||||
counter = counter || 0;
|
||||
if (counter > 20) {
|
||||
cb && cb('Cannot check connection');
|
||||
return;
|
||||
}
|
||||
|
||||
states.getState('system.adapter.' + adapterShortName + '.0.alive', function (err, state) {
|
||||
if (err) console.error('SQLite:' + err);
|
||||
if (state && state.val) {
|
||||
cb && cb();
|
||||
} else {
|
||||
setTimeout(function () {
|
||||
checkConnectionOfAdapter(cb, counter + 1);
|
||||
}, 1000);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function checkValueOfState(id, value, cb, counter) {
|
||||
counter = counter || 0;
|
||||
if (counter > 20) {
|
||||
cb && cb('Cannot check value Of State ' + id);
|
||||
return;
|
||||
}
|
||||
|
||||
states.getState(id, function (err, state) {
|
||||
if (err) console.error('SQLite:' + err);
|
||||
if (value === null && !state) {
|
||||
cb && cb();
|
||||
} else
|
||||
if (state && (value === undefined || state.val === value)) {
|
||||
cb && cb();
|
||||
} else {
|
||||
setTimeout(function () {
|
||||
checkValueOfState(id, value, cb, counter + 1);
|
||||
}, 500);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function sendTo(target, command, message, callback) {
|
||||
onStateChanged = function (id, state) {
|
||||
if (id === 'messagebox.system.adapter.test.0') {
|
||||
callback(state.message);
|
||||
}
|
||||
};
|
||||
|
||||
states.pushMessage('system.adapter.' + target, {
|
||||
command: command,
|
||||
message: message,
|
||||
from: 'system.adapter.test.0',
|
||||
callback: {
|
||||
message: message,
|
||||
id: sendToID++,
|
||||
ack: false,
|
||||
time: (new Date()).getTime()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
describe('Test SQLite', function() {
|
||||
before('Test SQLite: Start js-controller', function (_done) {
|
||||
this.timeout(600000); // because of first install from npm
|
||||
setup.adapterStarted = false;
|
||||
|
||||
setup.setupController(function () {
|
||||
var config = setup.getAdapterConfig();
|
||||
// enable adapter
|
||||
config.common.enabled = true;
|
||||
config.common.loglevel = 'debug';
|
||||
|
||||
config.native.dbtype = 'sqlite';
|
||||
|
||||
setup.setAdapterConfig(config.common, config.native);
|
||||
|
||||
setup.startController(true, function(id, obj) {}, function (id, state) {
|
||||
if (onStateChanged) onStateChanged(id, state);
|
||||
},
|
||||
function (_objects, _states) {
|
||||
objects = _objects;
|
||||
states = _states;
|
||||
objects.setObject('sql.0.memRss', {
|
||||
common: {
|
||||
type: 'number',
|
||||
role: 'state',
|
||||
custom: {
|
||||
"sql.0": {
|
||||
enabled: true,
|
||||
changesOnly: true,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
maxLength: 3,
|
||||
changesMinDelta: 0.5
|
||||
}
|
||||
}
|
||||
},
|
||||
type: 'state'
|
||||
}, _done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Test SQLite: Check if adapter started', function (done) {
|
||||
this.timeout(60000);
|
||||
checkConnectionOfAdapter(function () {
|
||||
now = new Date().getTime();
|
||||
objects.setObject('system.adapter.test.0', {
|
||||
common: {
|
||||
|
||||
},
|
||||
type: 'instance'
|
||||
},
|
||||
function () {
|
||||
states.subscribeMessage('system.adapter.test.0');
|
||||
setTimeout(function () {
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'system.adapter.sql.0.memHeapTotal',
|
||||
options: {
|
||||
changesOnly: false,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
storageType: 'String'
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'system.adapter.sql.0.alive',
|
||||
options: {
|
||||
changesOnly: false,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
storageType: 'Boolean'
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
sendTo('sql.0', 'enableHistory', {
|
||||
id: 'system.adapter.sql.0.uptime',
|
||||
options: {
|
||||
changesOnly: false,
|
||||
debounce: 0,
|
||||
retention: 31536000,
|
||||
storageType: false
|
||||
}
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
// wait till adapter receives the new settings
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 20000);
|
||||
});
|
||||
});
|
||||
});
|
||||
}, 10000);
|
||||
});
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Check Enabled Points after Enable', function (done) {
|
||||
this.timeout(20000);
|
||||
|
||||
sendTo('sql.0', 'getEnabledDPs', {}, function (result) {
|
||||
console.log(JSON.stringify(result));
|
||||
expect(Object.keys(result).length).to.be.equal(4);
|
||||
expect(result['sql.0.memRss'].enabled).to.be.true;
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 15000);
|
||||
});
|
||||
});
|
||||
it('Test SQLite: Write values into DB', function (done) {
|
||||
this.timeout(10000);
|
||||
|
||||
this.timeout(10000);
|
||||
|
||||
states.setState('sql.0.memRss', {val: 2, ts: now - 20000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: true, ts: now - 10000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 2, ts: now - 5000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 2.2, ts: now - 4000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: '2.5', ts: now - 3000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 3, ts: now - 1000}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(function () {
|
||||
states.setState('sql.0.memRss', {val: 'Test', ts: now - 500}, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setTimeout(done, 5000);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
});
|
||||
it('Test SQLite: Read values from DB using query', function (done) {
|
||||
this.timeout(10000);
|
||||
|
||||
sendTo('sql.0', 'query', 'SELECT id FROM datapoints WHERE name="sql.0.memRss"', function (result) {
|
||||
sendTo('sql.0', 'query', 'SELECT * FROM ts_number WHERE id=' + result.result[0].id, function (result) {
|
||||
console.log('SQLite:' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.be.at.least(5);
|
||||
var found = 0;
|
||||
for (var i = 0; i < result.result.length; i++) {
|
||||
if (result.result[i].val >= 1 && result.result[i].val <= 3) found ++;
|
||||
}
|
||||
expect(found).to.be.equal(6);
|
||||
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 3000);
|
||||
});
|
||||
});
|
||||
});
|
||||
it('Test SQLite: Read values from DB using GetHistory', function (done) {
|
||||
this.timeout(20000);
|
||||
|
||||
sendTo('sql.0', 'getHistory', {
|
||||
id: 'sql.0.memRss',
|
||||
options: {
|
||||
start: now - 30000,
|
||||
limit: 50,
|
||||
count: 50,
|
||||
aggregate: 'none'
|
||||
}
|
||||
}, function (result) {
|
||||
console.log('SQLite:' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.be.at.least(5);
|
||||
var found = 0;
|
||||
for (var i = 0; i < result.result.length; i++) {
|
||||
if (result.result[i].val >= 1 && result.result[i].val <= 3) found ++;
|
||||
}
|
||||
expect(found).to.be.equal(6);
|
||||
|
||||
sendTo('sql.0', 'getHistory', {
|
||||
id: 'sql.0.memRss',
|
||||
options: {
|
||||
start: now - 15000,
|
||||
end: now,
|
||||
limit: 2,
|
||||
count: 2,
|
||||
aggregate: 'none'
|
||||
}
|
||||
}, function (result) {
|
||||
console.log('SQLite:' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.be.equal(2);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Check Datapoint Types', function (done) {
|
||||
this.timeout(125000);
|
||||
|
||||
setTimeout(function() {
|
||||
sendTo('sql.0', 'query', "SELECT name, type FROM datapoints", function (result) {
|
||||
console.log('SQLite: ' + JSON.stringify(result.result, null, 2));
|
||||
expect(result.result.length).to.least(3);
|
||||
for (var i = 0; i < result.result.length; i++) {
|
||||
if (result.result[i].name === 'sql.0.memRss') {
|
||||
expect(result.result[i].type).to.be.equal(0);
|
||||
}
|
||||
else if (result.result[i].name === 'system.adapter.sql.0.memHeapTotal') {
|
||||
expect(result.result[i].type).to.be.equal(1);
|
||||
}
|
||||
else if (result.result[i].name === 'system.adapter.sql.0.uptime') {
|
||||
expect(result.result[i].type).to.be.equal(0);
|
||||
}
|
||||
else if (result.result[i].name === 'system.adapter.sql.0.alive') {
|
||||
expect(result.result[i].type).to.be.equal(2);
|
||||
}
|
||||
}
|
||||
|
||||
setTimeout(function () {
|
||||
done();
|
||||
}, 3000);
|
||||
});
|
||||
}, 121000);
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Disable Datapoint again', function (done) {
|
||||
this.timeout(5000);
|
||||
|
||||
sendTo('sql.0', 'disableHistory', {
|
||||
id: 'sql.0.memRss',
|
||||
}, function (result) {
|
||||
expect(result.error).to.be.undefined;
|
||||
expect(result.success).to.be.true;
|
||||
setTimeout(done, 2000);
|
||||
});
|
||||
});
|
||||
it('Test ' + adapterShortName + ': Check Enabled Points after Disable', function (done) {
|
||||
this.timeout(5000);
|
||||
|
||||
sendTo('sql.0', 'getEnabledDPs', {}, function (result) {
|
||||
console.log(JSON.stringify(result));
|
||||
expect(Object.keys(result).length).to.be.equal(3);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
after('Test SQLite: Stop js-controller', function (done) {
|
||||
this.timeout(6000);
|
||||
|
||||
setup.stopController(function (normalTerminated) {
|
||||
console.log('SQLite: Adapter normal terminated: ' + normalTerminated);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user