From ed7767bca30bdda64e7f945fe608f8574592e39d Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Sun, 3 Mar 2019 22:43:45 +0100 Subject: [PATCH 01/13] add adapter-core --- main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.js b/main.js index 147adca..f1cf554 100644 --- a/main.js +++ b/main.js @@ -3,7 +3,7 @@ 'use strict'; const request = require('request-promise'); -const utils = require(__dirname + '/lib/utils'); +const utils = require('@iobroker/adapter-core'); const adapter = new utils.Adapter('etamon'); const xpath = require('xpath'); const xmldom = require('xmldom').DOMParser; From 43314370d1117173a21f4e0385b5f82cdbdbb942 Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Sun, 3 Mar 2019 22:43:45 +0100 Subject: [PATCH 02/13] remove utils.js --- lib/utils.js | 83 ---------------------------------------------------- 1 file changed, 83 deletions(-) delete mode 100644 lib/utils.js diff --git a/lib/utils.js b/lib/utils.js deleted file mode 100644 index c8a0eb7..0000000 --- a/lib/utils.js +++ /dev/null @@ -1,83 +0,0 @@ -'use strict'; - -const fs = require('fs'); -const path = require('path'); - -let controllerDir; -let appName; - -/** - * returns application name - * - * The name of the application can be different and this function finds it out. - * - * @returns {string} - */ - function getAppName() { - const parts = __dirname.replace(/\\/g, '/').split('/'); - return parts[parts.length - 2].split('.')[0]; -} - -/** - * looks for js-controller home folder - * - * @param {boolean} isInstall - * @returns {string} - */ -function getControllerDir(isInstall) { - // Find the js-controller location - const possibilities = [ - 'iobroker.js-controller', - 'ioBroker.js-controller', - ]; - /** @type {string} */ - let controllerPath; - for (const pkg of possibilities) { - try { - const possiblePath = require.resolve(pkg); - if (fs.existsSync(possiblePath)) { - controllerPath = possiblePath; - break; - } - } catch (e) { /* not found */ } - } - if (controllerPath == null) { - if (!isInstall) { - console.log('Cannot find js-controller'); - process.exit(10); - } else { - process.exit(); - } - } - // we found the controller - return path.dirname(controllerPath); -} - -/** - * reads controller base settings - * - * @alias getConfig - * @returns {object} - */ - function getConfig() { - let configPath; - if (fs.existsSync( - configPath = path.join(controllerDir, 'conf', appName + '.json') - )) { - return JSON.parse(fs.readFileSync(configPath, 'utf8')); - } else if (fs.existsSync( - configPath = path.join(controllerDir, 'conf', + appName.toLowerCase() + '.json') - )) { - return JSON.parse(fs.readFileSync(configPath, 'utf8')); - } else { - throw new Error('Cannot find ' + controllerDir + '/conf/' + appName + '.json'); - } -} -appName = getAppName(); -controllerDir = getControllerDir(typeof process !== 'undefined' && process.argv && process.argv.indexOf('--install') !== -1); -const adapter = require(path.join(controllerDir, 'lib/adapter.js')); - -exports.controllerDir = controllerDir; -exports.getConfig = getConfig; -exports.Adapter = adapter; -exports.appName = appName; From 46cc63f7478cfa9c9a6d516949efcb6116801f43 Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Sun, 3 Mar 2019 22:47:01 +0100 Subject: [PATCH 03/13] update package.json --- package.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index c771433..3faa227 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,8 @@ "devDependencies": { "gulp": "^3.9.1", "mocha": "^4.1.0", - "chai": "^4.1.2" + "chai": "^4.1.2", + "@iobroker/adapter-core": "^1.0.3" }, "main": "main.js", "scripts": { @@ -43,4 +44,4 @@ "url": "https://github.com/frankhirsch/ioBroker.etamon/issues" }, "readmeFilename": "README.md" -} +} \ No newline at end of file From 127379dcd724d86eec70c138dcfa0c4080df1c3c Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Sun, 3 Mar 2019 22:52:40 +0100 Subject: [PATCH 04/13] update appveyor.yml --- appveyor.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 84ca0a9..98ae8e8 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,7 +1,6 @@ version: 'test-{build}' environment: matrix: - - nodejs_version: '4' - nodejs_version: '6' - nodejs_version: '8' - nodejs_version: '10' @@ -15,7 +14,7 @@ install: - ps: 'if($NpmVersion -eq 5) { npm install -g npm@5 }' - ps: npm --version - npm install - - npm install winston@2.3.1 + - npm install winston@3.2.1 - 'npm install https://github.com/ioBroker/ioBroker.js-controller/tarball/master --production' test_script: - echo %cd% From 5606fd235b1f011706880302189b8b321b106949 Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Sun, 3 Mar 2019 22:58:54 +0100 Subject: [PATCH 05/13] update testing setup.js --- test/lib/setup.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/lib/setup.js b/test/lib/setup.js index 16857ed..28e5e8b 100644 --- a/test/lib/setup.js +++ b/test/lib/setup.js @@ -311,6 +311,7 @@ function installJsController(cb) { } else { // check if port 9000 is free, else admin adapter will be added to running instance var client = new require('net').Socket(); + client.on('error', () => {}); 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); From 692aed2bce4561bb415f138c3ec0265afaa42f03 Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Sun, 3 Mar 2019 23:22:49 +0100 Subject: [PATCH 06/13] update basic package-file testing --- test/testPackageFiles.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/test/testPackageFiles.js b/test/testPackageFiles.js index c600a60..a63e6bc 100644 --- a/test/testPackageFiles.js +++ b/test/testPackageFiles.js @@ -2,18 +2,20 @@ /* jshint strict:false */ /* jslint node: true */ /* jshint expr: true */ -var expect = require('chai').expect; -var fs = require('fs'); +'use strict'; -describe('Test package.json and io-package.json', function() { - it('Test package files', function (done) { +const expect = require('chai').expect; +const fs = require('fs'); + +describe('Test package.json and io-package.json', () => { + it('Test package files', done => { console.log(); - var fileContentIOPackage = fs.readFileSync(__dirname + '/../io-package.json', 'utf8'); - var ioPackage = JSON.parse(fileContentIOPackage); + const fileContentIOPackage = fs.readFileSync(__dirname + '/../io-package.json', 'utf8'); + const ioPackage = JSON.parse(fileContentIOPackage); - var fileContentNPMPackage = fs.readFileSync(__dirname + '/../package.json', 'utf8'); - var npmPackage = JSON.parse(fileContentNPMPackage); + const fileContentNPMPackage = fs.readFileSync(__dirname + '/../package.json', 'utf8'); + const npmPackage = JSON.parse(fileContentNPMPackage); expect(ioPackage).to.be.an('object'); expect(npmPackage).to.be.an('object'); @@ -31,6 +33,8 @@ describe('Test package.json and io-package.json', function() { 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; + expect(ioPackage.common.license, 'ERROR: License missing in io-package in common.license').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); @@ -61,7 +65,7 @@ describe('Test package.json and io-package.json', function() { console.log(); } - if (ioPackage.common.name.indexOf('vis-') !== 0) { + if (!ioPackage.common.controller && !ioPackage.common.onlyWWW && !ioPackage.common.noConfig) { 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(); @@ -71,8 +75,8 @@ describe('Test package.json and io-package.json', function() { } } - var licenseFileExists = fs.existsSync(__dirname + '/../LICENSE'); - var fileContentReadme = fs.readFileSync(__dirname + '/../README.md', 'utf8'); + const licenseFileExists = fs.existsSync(__dirname + '/../LICENSE'); + const 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(); From b07a206b929d80a59c10baad8b7e7bfcfd293863 Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Mon, 4 Mar 2019 22:37:38 +0100 Subject: [PATCH 07/13] update package.json --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 3faa227..fef524f 100644 --- a/package.json +++ b/package.json @@ -28,13 +28,13 @@ "request": "^2.88.0", "request-promise": "^4.2.2", "xpath": "^0.0.27", - "xmldom": "0.1.27" + "xmldom": "0.1.27", + "@iobroker/adapter-core": "^1.0.3" }, "devDependencies": { "gulp": "^3.9.1", "mocha": "^4.1.0", - "chai": "^4.1.2", - "@iobroker/adapter-core": "^1.0.3" + "chai": "^4.1.2" }, "main": "main.js", "scripts": { From 2ba0e26f0946d89b0f84f1013c6eca481c981412 Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Sun, 10 Mar 2019 23:45:28 +0100 Subject: [PATCH 08/13] migrate testing to new iobroker/testing library --- appveyor.yml | 6 +- package.json | 8 +- test/integration.js | 5 + test/lib/setup.js | 729 --------------------------------------- test/mocha.custom.opts | 2 + test/mocha.setup.js | 14 + test/package.js | 5 + test/testAdapter.js | 140 -------- test/testPackageFiles.js | 95 ----- test/unit.js | 5 + 10 files changed, 40 insertions(+), 969 deletions(-) create mode 100644 test/integration.js delete mode 100644 test/lib/setup.js create mode 100644 test/mocha.custom.opts create mode 100644 test/mocha.setup.js create mode 100644 test/package.js delete mode 100644 test/testAdapter.js delete mode 100644 test/testPackageFiles.js create mode 100644 test/unit.js diff --git a/appveyor.yml b/appveyor.yml index 98ae8e8..180cf47 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -14,11 +14,11 @@ install: - ps: 'if($NpmVersion -eq 5) { npm install -g npm@5 }' - ps: npm --version - npm install - - npm install winston@3.2.1 - - 'npm install https://github.com/ioBroker/ioBroker.js-controller/tarball/master --production' test_script: - echo %cd% - node --version - npm --version - - npm test + - 'npm run test:package' + - 'npm run test:unit' + - 'npm run test:integration' build: 'off' diff --git a/package.json b/package.json index fef524f..1b98bd3 100644 --- a/package.json +++ b/package.json @@ -34,11 +34,15 @@ "devDependencies": { "gulp": "^3.9.1", "mocha": "^4.1.0", - "chai": "^4.1.2" + "chai": "^4.1.2", + "@iobroker/testing": "^1.1.10" }, "main": "main.js", "scripts": { - "test": "node node_modules/mocha/bin/mocha --exit" + "test": "npm run test:package && npm run test:unit", + "test:package": "mocha test/package --exit", + "test:unit": "mocha test/unit --exit", + "test:integration": "mocha test/integration --exit" }, "bugs": { "url": "https://github.com/frankhirsch/ioBroker.etamon/issues" diff --git a/test/integration.js b/test/integration.js new file mode 100644 index 0000000..1b3453e --- /dev/null +++ b/test/integration.js @@ -0,0 +1,5 @@ +const path = require('path'); +const { tests } = require('@iobroker/testing'); + +// Run integration tests - See https://github.com/ioBroker/testing for a detailed explanation and further options +tests.integration(path.join(__dirname, '..')); diff --git a/test/lib/setup.js b/test/lib/setup.js deleted file mode 100644 index 28e5e8b..0000000 --- a/test/lib/setup.js +++ /dev/null @@ -1,729 +0,0 @@ -/* 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.on('error', () => {}); - 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://github.com/' + 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/iobroker-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; -} diff --git a/test/mocha.custom.opts b/test/mocha.custom.opts new file mode 100644 index 0000000..703f749 --- /dev/null +++ b/test/mocha.custom.opts @@ -0,0 +1,2 @@ +--require test/mocha.setup.js +{!(node_modules|test)/**/*.test.js,*.test.js,test/**/test!(PackageFiles|Startup).js} \ No newline at end of file diff --git a/test/mocha.setup.js b/test/mocha.setup.js new file mode 100644 index 0000000..15b9051 --- /dev/null +++ b/test/mocha.setup.js @@ -0,0 +1,14 @@ +// Don't silently swallow unhandled rejections +process.on('unhandledRejection', (e) => { + throw e; +}); + +// enable the should interface with sinon +// and load chai-as-promised and sinon-chai by default +const sinonChai = require('sinon-chai'); +const chaiAsPromised = require('chai-as-promised'); +const { should, use } = require('chai'); + +should(); +use(sinonChai); +use(chaiAsPromised); \ No newline at end of file diff --git a/test/package.js b/test/package.js new file mode 100644 index 0000000..38eacc8 --- /dev/null +++ b/test/package.js @@ -0,0 +1,5 @@ +const path = require('path'); +const { tests } = require('@iobroker/testing'); + +// Validate the package files +tests.packageFiles(path.join(__dirname, '..')); diff --git a/test/testAdapter.js b/test/testAdapter.js deleted file mode 100644 index ae9c289..0000000 --- a/test/testAdapter.js +++ /dev/null @@ -1,140 +0,0 @@ -/* jshint -W097 */// jshint strict:false -/*jslint node: 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); - -function checkConnectionOfAdapter(cb, counter) { - counter = counter || 0; - console.log('Try check #' + counter); - if (counter > 30) { - if (cb) cb('Cannot check connection'); - return; - } - - states.getState('system.adapter.' + adapterShortName + '.0.alive', function (err, state) { - if (err) console.error(err); - if (state && state.val) { - if (cb) cb(); - } else { - setTimeout(function () { - checkConnectionOfAdapter(cb, counter + 1); - }, 1000); - } - }); -} - -function checkValueOfState(id, value, cb, counter) { - counter = counter || 0; - if (counter > 20) { - if (cb) cb('Cannot check value Of State ' + id); - return; - } - - states.getState(id, function (err, state) { - if (err) console.error(err); - if (value === null && !state) { - if (cb) cb(); - } else - if (state && (value === undefined || state.val === value)) { - if (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 ' + adapterShortName + ' adapter', function() { - before('Test ' + adapterShortName + ' adapter: Start js-controller', function (_done) { - this.timeout(600000); // because of first install from npm - - 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; - _done(); - }); - }); - }); - -/* - ENABLE THIS WHEN ADAPTER RUNS IN DEAMON MODE TO CHECK THAT IT HAS STARTED SUCCESSFULLY -*/ - it('Test ' + adapterShortName + ' adapter: Check if adapter started', function (done) { - this.timeout(60000); - checkConnectionOfAdapter(function (res) { - if (res) console.log(res); - expect(res).not.to.be.equal('Cannot check connection'); - objects.setObject('system.adapter.test.0', { - common: { - - }, - type: 'instance' - }, - function () { - states.subscribeMessage('system.adapter.test.0'); - done(); - }); - }); - }); -/**/ - -/* - PUT YOUR OWN TESTS HERE USING - it('Testname', function ( done) { - ... - }); - - You can also use "sendTo" method to send messages to the started adapter -*/ - - after('Test ' + adapterShortName + ' adapter: Stop js-controller', function (done) { - this.timeout(10000); - - setup.stopController(function (normalTerminated) { - console.log('Adapter normal terminated: ' + normalTerminated); - done(); - }); - }); -}); diff --git a/test/testPackageFiles.js b/test/testPackageFiles.js deleted file mode 100644 index a63e6bc..0000000 --- a/test/testPackageFiles.js +++ /dev/null @@ -1,95 +0,0 @@ -/* jshint -W097 */ -/* jshint strict:false */ -/* jslint node: true */ -/* jshint expr: true */ -'use strict'; - -const expect = require('chai').expect; -const fs = require('fs'); - -describe('Test package.json and io-package.json', () => { - it('Test package files', done => { - console.log(); - - const fileContentIOPackage = fs.readFileSync(__dirname + '/../io-package.json', 'utf8'); - const ioPackage = JSON.parse(fileContentIOPackage); - - const fileContentNPMPackage = fs.readFileSync(__dirname + '/../package.json', 'utf8'); - const 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; - - expect(ioPackage.common.license, 'ERROR: License missing in io-package in common.license').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 '); - } - } - else { - expect(ioPackage.common.authors, 'ERROR: Author in io-package.json needs to be a real name').to.not.be.equal('my Name '); - } - } - 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('iobroker') !== -1 || - ioPackage.common.title.indexOf('ioBroker') !== -1 || - ioPackage.common.title.indexOf('adapter') !== -1 || - ioPackage.common.title.indexOf('Adapter') !== -1 - ) { - console.log('WARNING: title contains Adapter or ioBroker. It is clear anyway, that it is adapter for ioBroker.'); - console.log(); - } - - if (!ioPackage.common.controller && !ioPackage.common.onlyWWW && !ioPackage.common.noConfig) { - 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; - } - } - - const licenseFileExists = fs.existsSync(__dirname + '/../LICENSE'); - const 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(); - }); -}); diff --git a/test/unit.js b/test/unit.js new file mode 100644 index 0000000..8e27b3a --- /dev/null +++ b/test/unit.js @@ -0,0 +1,5 @@ +const path = require('path'); +const { tests } = require('@iobroker/testing'); + +// Run unit tests - See https://github.com/ioBroker/testing for a detailed explanation and further options +tests.unit(path.join(__dirname, '..')); From 29f51e1a63fdeb44440b3b803cb8dc54fe74471f Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Thu, 2 May 2019 23:22:37 +0200 Subject: [PATCH 09/13] update appveyor.yml --- appveyor.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/appveyor.yml b/appveyor.yml index 180cf47..8d5fd04 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -4,6 +4,7 @@ environment: - nodejs_version: '6' - nodejs_version: '8' - nodejs_version: '10' + - nodejs_version: '12' platform: - x86 - x64 From d1503bc59d95b1aef23112b1573a869cc4e3a00b Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Sat, 11 May 2019 00:21:11 +0200 Subject: [PATCH 10/13] update appveyor.yml --- appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index 8d5fd04..041df88 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -10,7 +10,7 @@ platform: - x64 clone_folder: 'c:\projects\%APPVEYOR_PROJECT_NAME%' install: - - ps: 'Install-Product node $env:nodejs_version $env:platform' + - ps: 'Update-NodeJsInstallation (Get-NodeJsLatestBuild $env:nodejs_version) $env:platform' - ps: '$NpmVersion = (npm -v).Substring(0,1)' - ps: 'if($NpmVersion -eq 5) { npm install -g npm@5 }' - ps: npm --version From b0073ed9cca5c7792eb3389f48d68008f99e4467 Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Sun, 1 Sep 2019 23:42:35 +0200 Subject: [PATCH 11/13] update appveyor.yml --- appveyor.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index 041df88..e464e78 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,7 +1,6 @@ version: 'test-{build}' environment: matrix: - - nodejs_version: '6' - nodejs_version: '8' - nodejs_version: '10' - nodejs_version: '12' From 30c581fec049ddedc478532141362aef83e7469f Mon Sep 17 00:00:00 2001 From: frankhirsch Date: Thu, 27 Feb 2020 16:52:46 +0100 Subject: [PATCH 12/13] Create README.md --- README.md | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..002a6d5 --- /dev/null +++ b/README.md @@ -0,0 +1,132 @@ +# ioBroker.etamon +ioBroker adapter for ETA heating systems with XML Rest API (touch display) + +> *The development of the adapter has shown issues when running on less performant devices such as NAS devices.* +> *You can still use the adapter if you run iobroker on a common desktop/laptop device.* + +As a consequence I recommend using the following JS implementation - you will just need to map once the proper URI from your ETA device. +``` +var http = require('http'); +var xpath = require('xpath'); +var dom = require('xmldom').DOMParser; + +var etaIP = 'http://192.168.XYZ.XYZ:8080/user/var'; + + // 1. URI + // 2. iobroker Object + // 3. read + // 4. write + // 5. name + // 6. type + // 7. unit + // 8. role + // 9. CCU variable (optional) +var etaVars = [ + ["/120/10251/0/0/12242", "eta.puffer.oben", true, false, "Puffer oben", "number", "°C", "value.temperature", "hm-rega.0.1810"] + ,["/120/10251/0/0/12244", "eta.puffer.unten", true, false, "Puffer unten", "number", "°C", "value.temperature", "hm-rega.0.1811"] + ,["/120/10251/0/0/12207", "eta.puffer.aktion", true, false, "Puffer Aktion", "string", "", "state", "hm-rega.0.1809"] + ,["/120/10251/0/0/12533", "eta.puffer.leistung", true, false, "Puffer Leistung", "number", "KW", "state", "hm-rega.0.5038"] + ,["/120/10251/0/0/12129", "eta.puffer.zustand", true, false, "Puffer Zustand", "string", "", "state", ""] + + ,["/120/10101/0/0/12090", "eta.hk.1.zustand", true, false, "Heizkreis Zustand", "string", "", "state", "hm-rega.0.1812"] + ,["/120/10101/0/0/12241", "eta.hk.1.vorlauf", true, false, "Heizkreis Vorlauf", "number", "°C", "value.temperature", "hm-rega.0.8047"] + ,["/120/10101/0/0/12111", "eta.hk.1.heizkurve", true, false, "Heizkreis Heizkurve", "number", "°C", "value.temperature", "hm-rega.0.8048"] + ,["/120/10101/0/11124/0", "eta.hk.1.pumpe", true, false, "Heizkreis Pumpe", "string", "", "state", "hm-rega.0.1813"] + ,["/120/10101/0/11124/2001", "eta.hk.1.anforderung", true, false, "Heizkreis Anforderung", "string", "", "state", ""] + + ,["/40/10021/0/0/12000", "eta.kessel.aktion", true, false, "Kessel", "string", "", "state", "hm-rega.0.1822"] + ,["/40/10021/0/0/12153", "eta.kessel.vollast", true, false, "Kessel Vollaststunden", "string", "", "state", "hm-rega.0.1814"] + ,["/40/10021/0/0/12016", "eta.kessel.verbrauch", true, false, "Kessel Gesmtverbrauch", "number", "kg", "state", "hm-rega.0.1815"] + ,["/40/10021/0/0/12013", "eta.kessel.aschebox", true, false, "Kessel Verbrauch seit Aschebox", "number", "kg", "state", ""] + ,["/40/10021/0/0/12180", "eta.kessel.druck", true, false, "Kessel Druck", "number", "bar", "state", "hm-rega.0.1820"] + ,["/40/10021/0/0/12001", "eta.kessel.soll", true, false, "Kessel Soll", "number", "°C", "state", "hm-rega.0.5037"] + ,["/40/10021/0/0/12161", "eta.kessel.ist", true, false, "Kessel Ist", "number", "°C", "state", "hm-rega.0.3195"] + ,["/40/10021/0/0/12162", "eta.kessel.abgasgtemp", true, false, "Kessel Abgastemperatur", "number", "°C", "state", "hm-rega.0.5040"] + ,["/40/10021/0/0/12165", "eta.kessel.abgasgeblaese", true, false, "Kessel Abgasgebläse", "number", "U/min", "state", "hm-rega.0.5039"] + ,["/40/10021/0/0/12164", "eta.kessel.restsauerstoff", true, false, "Kessel Restsauerstoff", "number", "%", "state", "hm-rega.0.5041"] + ,["/40/10021/0/0/12080", "eta.kessel.zustand", true, false, "Kessel Zustand", "string", "", "state", "hm-rega.0.1808"] + + ,["/40/10201/0/0/12015", "eta.lager.silo", true, false, "Pellets Silo", "number", "kg", "state", "hm-rega.0.1817"] + ,["/40/10021/0/0/12011", "eta.lager.tag", true, false, "Pellets Tagesbehälter", "number", "kg", "state", "hm-rega.0.9651"] + ,["/40/10241/0/0/12197", "eta.system.aussentemperatur", true, false, "Aussentemperatur", "number", "°C", "state", "hm-rega.0.1821"] +]; + +etaVars.forEach(function(etaVar){ + createState(etaVar[1], 0, { + read: etaVar[2], + write: etaVar[3], + name: etaVar[4], + type: etaVar[5], + unit: etaVar[6], + role: etaVar[7] + }); +}); + +schedule("*/5 * * * *", function () { + pollETA(); +}); + +pollETA(); + +function pollETA() { + // console.log("** Polling ETA Variables"); + etaVars.forEach(function(etaVar){ + http.get(etaIP + etaVar[0], function (http_res) { + + // initialize the container for our data + var data = ""; + + // this event fires many times, each time collecting another piece of the response + http_res.on("data", function (chunk) { + // append this chunk to our growing `data` var + data += chunk; + // console.log("** ETA chunk: " + chunk); + }); + + // this event fires *one* time, after all the `data` events/chunks have been gathered + http_res.on("end", function () { + // console.log("** ETA data: " + data); + + try { + var doc = new dom().parseFromString(data); + var select = xpath.useNamespaces({"eta": "http://www.eta.co.at/rest/v1"}); + + var strValue = (select('//eta:value/@strValue', doc)[0].nodeValue); + var text = (select('//eta:value/text()', doc)[0].nodeValue); + var scaleFactor = (select('//eta:value/@scaleFactor', doc)[0].nodeValue); + var unit = (select('//eta:value/@unit', doc)[0].nodeValue); + var value = ""; + + if (etaVar[5]=="number") { + value = text * 1.0 / scaleFactor; + } else { + value = strValue; + } + + /* console.log("**** ETA " + etaVar[0] + " @strValue: " + strValue); + console.log("**** ETA " + etaVar[0] + " @unit: " + unit); + console.log("**** ETA " + etaVar[0] + " text() : " + text); + console.log("**** ETA " + etaVar[0] + " @scaleFactor: " + scaleFactor); + console.log("** ETA [" + etaVar[4] + "]: " + value + " " + unit); */ + + setState(etaVar[1], value); + + // Schreibe Variablen zu CCU + if(etaVar[8]!="") { + setState(etaVar[8], value); + } + } + catch (e) { + log("ETA: Cannot set data "+ etaVar[2] +":" + e, 'error'); + } + }); + }); + }); + + var dateFormat = require('dateformat'); + var currentdate = new Date(); + log(dateFormat(currentdate, "dd. mmm yyyy hh:MM")); + // optional last update in CCU3 + setState("hm-rega.0.7022", dateFormat(currentdate, "dd. mmm yyyy hh:MM")); +} +``` From 325abe9f45b2ae8c694ddbe6099d0de5ae992ae8 Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Sun, 5 Sep 2021 16:31:58 +0200 Subject: [PATCH 13/13] update appveyor.yml --- appveyor.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index e464e78..76b457f 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,9 +1,9 @@ version: 'test-{build}' environment: matrix: - - nodejs_version: '8' - - nodejs_version: '10' - nodejs_version: '12' + - nodejs_version: '14' + - nodejs_version: '16' platform: - x86 - x64