update testing setup.js

This commit is contained in:
Ingo Fischer
2022-01-29 15:27:38 +01:00
parent d042d65588
commit f1376e5e5b
+253 -38
View File
@@ -9,6 +9,8 @@ const pkg = require(rootDir + 'package.json');
const debug = typeof v8debug === 'object'; const debug = typeof v8debug === 'object';
pkg.main = pkg.main || 'main.js'; pkg.main = pkg.main || 'main.js';
let JSONLDB;
let adapterName = path.normalize(rootDir).replace(/\\/g, '/').split('/'); let adapterName = path.normalize(rootDir).replace(/\\/g, '/').split('/');
adapterName = adapterName[adapterName.length - 2]; adapterName = adapterName[adapterName.length - 2];
let adapterStarted = false; let adapterStarted = false;
@@ -18,6 +20,21 @@ function getAppName() {
return parts[parts.length - 3].split('.')[0]; return parts[parts.length - 3].split('.')[0];
} }
function loadJSONLDB() {
if (!JSONLDB) {
const dbPath = require.resolve('@alcalzone/jsonl-db', {
paths: [rootDir + 'tmp/node_modules', rootDir, rootDir + 'tmp/node_modules/' + appName + '.js-controller']
});
console.log('JSONLDB path: ' + dbPath);
try {
const { JsonlDB } = require(dbPath);
JSONLDB = JsonlDB;
} catch (err) {
console.log('Jsonl require error: ' + err);
}
}
}
const appName = getAppName().toLowerCase(); const appName = getAppName().toLowerCase();
let objects; let objects;
@@ -25,6 +42,8 @@ let states;
let pid = null; let pid = null;
let systemConfig = null;
function copyFileSync(source, target) { function copyFileSync(source, target) {
let targetFile = target; let targetFile = target;
@@ -84,11 +103,12 @@ if (!fs.existsSync(rootDir + 'tmp')) {
fs.mkdirSync(rootDir + 'tmp'); fs.mkdirSync(rootDir + 'tmp');
} }
function storeOriginalFiles() { async function storeOriginalFiles() {
console.log('Store original files...'); console.log('Store original files...');
const dataDir = rootDir + 'tmp/' + appName + '-data/'; const dataDir = rootDir + 'tmp/' + appName + '-data/';
let f = fs.readFileSync(dataDir + 'objects.json'); if (fs.existsSync(dataDir + 'objects.json')) {
const f = fs.readFileSync(dataDir + 'objects.json');
const objects = JSON.parse(f.toString()); const objects = JSON.parse(f.toString());
if (objects['system.adapter.admin.0'] && objects['system.adapter.admin.0'].common) { if (objects['system.adapter.admin.0'] && objects['system.adapter.admin.0'].common) {
objects['system.adapter.admin.0'].common.enabled = false; objects['system.adapter.admin.0'].common.enabled = false;
@@ -98,38 +118,84 @@ function storeOriginalFiles() {
} }
fs.writeFileSync(dataDir + 'objects.json.original', JSON.stringify(objects)); fs.writeFileSync(dataDir + 'objects.json.original', JSON.stringify(objects));
try { console.log('Store original objects.json');
f = fs.readFileSync(dataDir + 'states.json');
fs.writeFileSync(dataDir + 'states.json.original', f);
} }
catch (err) {
if (fs.existsSync(dataDir + 'states.json')) {
try {
const f = fs.readFileSync(dataDir + 'states.json');
fs.writeFileSync(dataDir + 'states.json.original', f);
console.log('Store original states.json');
} catch (err) {
console.log('no states.json found - ignore'); console.log('no states.json found - ignore');
} }
}
if (fs.existsSync(dataDir + 'objects.jsonl')) {
loadJSONLDB();
const db = new JSONLDB(dataDir + 'objects.jsonl');
await db.open();
const admin0 = db.get('system.adapter.admin.0');
if (admin0) {
if (admin0.common) {
admin0.common.enabled = false;
db.set('system.adapter.admin.0', admin0);
}
}
const admin1 = db.get('system.adapter.admin.1');
if (admin1) {
if (admin1.common) {
admin1.common.enabled = false;
db.set('system.adapter.admin.1', admin1);
}
}
await db.close();
const f = fs.readFileSync(dataDir + 'objects.jsonl');
fs.writeFileSync(dataDir + 'objects.jsonl.original', f);
console.log('Store original objects.jsonl');
}
if (fs.existsSync(dataDir + 'states.jsonl')) {
const f = fs.readFileSync(dataDir + 'states.jsonl');
fs.writeFileSync(dataDir + 'states.jsonl.original', f);
console.log('Store original states.jsonl');
}
} }
function restoreOriginalFiles() { function restoreOriginalFiles() {
console.log('restoreOriginalFiles...'); console.log('restoreOriginalFiles...');
const dataDir = rootDir + 'tmp/' + appName + '-data/'; const dataDir = rootDir + 'tmp/' + appName + '-data/';
let f = fs.readFileSync(dataDir + 'objects.json.original'); if (fs.existsSync(dataDir + 'objects.json.original')) {
const f = fs.readFileSync(dataDir + 'objects.json.original');
fs.writeFileSync(dataDir + 'objects.json', f); fs.writeFileSync(dataDir + 'objects.json', f);
try { }
f = fs.readFileSync(dataDir + 'states.json.original'); if (fs.existsSync(dataDir + 'objects.json.original')) {
const f = fs.readFileSync(dataDir + 'states.json.original');
fs.writeFileSync(dataDir + 'states.json', f); fs.writeFileSync(dataDir + 'states.json', f);
} }
catch (err) {
console.log('no states.json.original found - ignore');
}
if (fs.existsSync(dataDir + 'objects.jsonl.original')) {
const f = fs.readFileSync(dataDir + 'objects.jsonl.original');
fs.writeFileSync(dataDir + 'objects.jsonl', f);
}
if (fs.existsSync(dataDir + 'objects.jsonl.original')) {
const f = fs.readFileSync(dataDir + 'states.jsonl.original');
fs.writeFileSync(dataDir + 'states.jsonl', f);
}
} }
function checkIsAdapterInstalled(cb, counter, customName) { async function checkIsAdapterInstalled(cb, counter, customName) {
customName = customName || pkg.name.split('.').pop(); customName = customName || pkg.name.split('.').pop();
counter = counter || 0; counter = counter || 0;
const dataDir = rootDir + 'tmp/' + appName + '-data/'; const dataDir = rootDir + 'tmp/' + appName + '-data/';
console.log('checkIsAdapterInstalled...'); console.log('checkIsAdapterInstalled...');
try { try {
if (fs.existsSync(dataDir + 'objects.json')) {
const f = fs.readFileSync(dataDir + 'objects.json'); const f = fs.readFileSync(dataDir + 'objects.json');
const objects = JSON.parse(f.toString()); const objects = JSON.parse(f.toString());
if (objects['system.adapter.' + customName + '.0']) { if (objects['system.adapter.' + customName + '.0']) {
@@ -141,8 +207,36 @@ function checkIsAdapterInstalled(cb, counter, customName) {
} else { } else {
console.warn('checkIsAdapterInstalled: still not ready'); console.warn('checkIsAdapterInstalled: still not ready');
} }
} else if (fs.existsSync(dataDir + 'objects.jsonl')) {
loadJSONLDB();
const db = new JSONLDB(dataDir + 'objects.jsonl');
try {
await db.open();
} catch (err) { } catch (err) {
if (err.message.includes('Failed to lock DB file')) {
console.log('checkIsAdapterInstalled: DB still opened ...');
}
throw err;
}
const obj = db.get('system.adapter.' + customName + '.0');
await db.close();
if (obj) {
console.log('checkIsAdapterInstalled: ready!');
setTimeout(function () {
if (cb) cb();
}, 100);
return;
} else {
console.warn('checkIsAdapterInstalled: still not ready');
}
} else {
console.error('checkIsAdapterInstalled: No objects file found in datadir ' + dataDir);
}
} catch (err) {
console.log('checkIsAdapterInstalled: catch ' + err);
} }
if (counter > 20) { if (counter > 20) {
@@ -156,12 +250,13 @@ function checkIsAdapterInstalled(cb, counter, customName) {
} }
} }
function checkIsControllerInstalled(cb, counter) { async function checkIsControllerInstalled(cb, counter) {
counter = counter || 0; counter = counter || 0;
const dataDir = rootDir + 'tmp/' + appName + '-data/'; const dataDir = rootDir + 'tmp/' + appName + '-data/';
console.log('checkIsControllerInstalled...'); console.log('checkIsControllerInstalled...');
try { try {
if (fs.existsSync(dataDir + 'objects.json')) {
const f = fs.readFileSync(dataDir + 'objects.json'); const f = fs.readFileSync(dataDir + 'objects.json');
const objects = JSON.parse(f.toString()); const objects = JSON.parse(f.toString());
if (objects['system.certificates']) { if (objects['system.certificates']) {
@@ -171,6 +266,32 @@ function checkIsControllerInstalled(cb, counter) {
}, 100); }, 100);
return; return;
} }
} else if (fs.existsSync(dataDir + 'objects.jsonl')) {
loadJSONLDB();
const db = new JSONLDB(dataDir + 'objects.jsonl');
try {
await db.open();
} catch (err) {
if (err.message.includes('Failed to lock DB file')) {
console.log('checkIsControllerInstalled: DB still opened ...');
}
throw err;
}
const obj = db.get('system.certificates');
await db.close();
if (obj) {
console.log('checkIsControllerInstalled: installed!');
setTimeout(function () {
if (cb) cb();
}, 100);
return;
}
} else {
console.error('checkIsControllerInstalled: No objects file found in datadir ' + dataDir);
}
} catch (err) { } catch (err) {
} }
@@ -296,13 +417,17 @@ function installJsController(cb) {
const config = require(rootDir + 'tmp/' + appName + '-data/' + appName + '.json'); const config = require(rootDir + 'tmp/' + appName + '-data/' + appName + '.json');
config.objects.port = 19001; config.objects.port = 19001;
config.states.port = 19000; config.states.port = 19000;
// TEST WISE!
//config.objects.type = 'jsonl';
//config.states.type = 'jsonl';
fs.writeFileSync(rootDir + 'tmp/' + appName + '-data/' + appName + '.json', JSON.stringify(config, null, 2)); fs.writeFileSync(rootDir + 'tmp/' + appName + '-data/' + appName + '.json', JSON.stringify(config, null, 2));
console.log('Setup finished.'); console.log('Setup finished.');
copyAdapterToController(); copyAdapterToController();
installAdapter(function () { installAdapter(async function () {
storeOriginalFiles(); await storeOriginalFiles();
if (cb) cb(true); if (cb) cb(true);
}); });
}); });
@@ -359,12 +484,16 @@ function installJsController(cb) {
const config = require(rootDir + 'tmp/' + appName + '-data/' + appName + '.json'); const config = require(rootDir + 'tmp/' + appName + '-data/' + appName + '.json');
config.objects.port = 19001; config.objects.port = 19001;
config.states.port = 19000; config.states.port = 19000;
// TEST WISE!
//config.objects.type = 'jsonl';
//config.states.type = 'jsonl';
fs.writeFileSync(rootDir + 'tmp/' + appName + '-data/' + appName + '.json', JSON.stringify(config, null, 2)); fs.writeFileSync(rootDir + 'tmp/' + appName + '-data/' + appName + '.json', JSON.stringify(config, null, 2));
copyAdapterToController(); copyAdapterToController();
installAdapter(function () { installAdapter(async function () {
storeOriginalFiles(); await storeOriginalFiles();
if (cb) cb(true); if (cb) cb(true);
}); });
}); });
@@ -445,7 +574,8 @@ function clearDB() {
} }
function setupController(cb) { function setupController(cb) {
installJsController(function (isInited) { installJsController(async function (isInited) {
try {
clearControllerLog(); clearControllerLog();
clearDB(); clearDB();
@@ -456,12 +586,12 @@ function setupController(cb) {
// read system.config object // read system.config object
const dataDir = rootDir + 'tmp/' + appName + '-data/'; const dataDir = rootDir + 'tmp/' + appName + '-data/';
if (fs.existsSync(dataDir + 'objects.json')) {
let objs; let objs;
try { try {
objs = fs.readFileSync(dataDir + 'objects.json'); objs = fs.readFileSync(dataDir + 'objects.json');
objs = JSON.parse(objs); objs = JSON.parse(objs);
} } catch (e) {
catch (e) {
console.log('ERROR reading/parsing system configuration. Ignore'); console.log('ERROR reading/parsing system configuration. Ignore');
objs = {'system.config': {}}; objs = {'system.config': {}};
} }
@@ -469,22 +599,64 @@ function setupController(cb) {
objs = {'system.config': {}}; objs = {'system.config': {}};
} }
systemConfig = objs['system.config'];
if (cb) cb(objs['system.config']); if (cb) cb(objs['system.config']);
} else if (fs.existsSync(dataDir + 'objects.jsonl')) {
loadJSONLDB();
const db = new JSONLDB(dataDir + 'objects.jsonl');
await db.open();
let config = db.get('system.config');
systemConfig = config || {};
await db.close();
if (cb) cb(systemConfig);
} else {
console.error('read SystemConfig: No objects file found in datadir ' + dataDir);
}
} catch (err) {
console.error('setupController: ' + err);
}
}); });
} }
function getSecret() { async function getSecret() {
var dataDir = rootDir + 'tmp/' + appName + '-data/'; var dataDir = rootDir + 'tmp/' + appName + '-data/';
if (systemConfig) {
return systemConfig.native.secret;
}
if (fs.existsSync(dataDir + 'objects.json')) {
let objs;
try { try {
var objs = fs.readFileSync(dataDir + 'objects.json'); objs = fs.readFileSync(dataDir + 'objects.json');
objs = JSON.parse(objs); objs = JSON.parse(objs);
}
return objs['system.config'].native.secret; catch (e) {
} catch (e) {
console.warn("Could not load secret. Reason: " + e); console.warn("Could not load secret. Reason: " + e);
return null; return null;
} }
if (!objs || !objs['system.config']) {
objs = {'system.config': {}};
}
return objs['system.config'].native.secre;
} else if (fs.existsSync(dataDir + 'objects.jsonl')) {
loadJSONLDB();
const db = new JSONLDB(dataDir + 'objects.jsonl');
await db.open();
let config = db.get('system.config');
config = config || {};
await db.close();
return config.native.secret;
} else {
console.error('read secret: No objects file found in datadir ' + dataDir);
}
} }
function encrypt (key, value) { function encrypt (key, value) {
@@ -544,18 +716,26 @@ function startController(isStartAdapter, onObjectChange, onStateChange, callback
console.error('Controller is already started!'); console.error('Controller is already started!');
} else { } else {
console.log('startController...'); console.log('startController...');
try {
const config = require(rootDir + 'tmp/' + appName + '-data/' + appName + '.json');
adapterStarted = false; adapterStarted = false;
let isObjectConnected; let isObjectConnected;
let isStatesConnected; let isStatesConnected;
const Objects = require(rootDir + 'tmp/node_modules/' + appName + '.js-controller/lib/objects/objectsInMemServer'); // rootDir + 'tmp/node_modules
const objPath = require.resolve(`@iobroker/db-objects-${config.objects.type}`, {
paths: [ rootDir + 'tmp/node_modules', rootDir, rootDir + 'tmp/node_modules/' + appName + '.js-controller']
});
console.log('Objects Path: ' + objPath);
const Objects = require(objPath).Server;
objects = new Objects({ objects = new Objects({
connection: { connection: {
'type' : 'file', 'type': config.objects.type,
'host' : '127.0.0.1', 'host': '127.0.0.1',
'port' : 19001, 'port': 19001,
'user' : '', 'user': '',
'pass' : '', 'pass': '',
'noFileCache': false, 'noFileCache': false,
'connectTimeout': 2000 'connectTimeout': 2000
}, },
@@ -594,10 +774,14 @@ function startController(isStartAdapter, onObjectChange, onStateChange, callback
}); });
// Just open in memory DB itself // Just open in memory DB itself
const States = require(rootDir + 'tmp/node_modules/' + appName + '.js-controller/lib/states/statesInMemServer'); const statePath = require.resolve(`@iobroker/db-states-${config.states.type}`, {
paths: [ rootDir + 'tmp/node_modules', rootDir, rootDir + 'tmp/node_modules/' + appName + '.js-controller']
});
console.log('States Path: ' + statePath);
const States = require(statePath).Server;
states = new States({ states = new States({
connection: { connection: {
type: 'file', type: config.states.type,
host: '127.0.0.1', host: '127.0.0.1',
port: 19000, port: 19000,
options: { options: {
@@ -638,6 +822,9 @@ function startController(isStartAdapter, onObjectChange, onStateChange, callback
}, },
change: onStateChange change: onStateChange
}); });
} catch (err) {
console.log(err);
}
} }
} }
@@ -721,19 +908,47 @@ function stopController(cb) {
} }
// Setup the adapter // Setup the adapter
function setAdapterConfig(common, native, instance) { async function setAdapterConfig(common, native, instance) {
const objects = JSON.parse(fs.readFileSync(rootDir + 'tmp/' + appName + '-data/objects.json').toString());
const id = 'system.adapter.' + adapterName.split('.').pop() + '.' + (instance || 0); const id = 'system.adapter.' + adapterName.split('.').pop() + '.' + (instance || 0);
if (fs.existsSync(rootDir + 'tmp/' + appName + '-data/objects.json')) {
const objects = JSON.parse(fs.readFileSync(rootDir + 'tmp/' + appName + '-data/objects.json').toString());
if (common) objects[id].common = common; if (common) objects[id].common = common;
if (native) objects[id].native = native; if (native) objects[id].native = native;
fs.writeFileSync(rootDir + 'tmp/' + appName + '-data/objects.json', JSON.stringify(objects)); fs.writeFileSync(rootDir + 'tmp/' + appName + '-data/objects.json', JSON.stringify(objects));
} else if (fs.existsSync(rootDir + 'tmp/' + appName + '-data/objects.jsonl')) {
loadJSONLDB();
const db = new JSONLDB(rootDir + 'tmp/' + appName + '-data/objects.jsonl');
await db.open();
let obj = db.get(id);
if (common) obj.common = common;
if (native) obj.native = native;
db.set(id, obj);
await db.close();
} else {
console.error('setAdapterConfig: No objects file found in datadir ' + rootDir + 'tmp/' + appName + '-data/');
}
} }
// Read config of the adapter // Read config of the adapter
function getAdapterConfig(instance) { async function getAdapterConfig(instance) {
const objects = JSON.parse(fs.readFileSync(rootDir + 'tmp/' + appName + '-data/objects.json').toString());
const id = 'system.adapter.' + adapterName.split('.').pop() + '.' + (instance || 0); const id = 'system.adapter.' + adapterName.split('.').pop() + '.' + (instance || 0);
if (fs.existsSync(rootDir + 'tmp/' + appName + '-data/objects.json')) {
const objects = JSON.parse(fs.readFileSync(rootDir + 'tmp/' + appName + '-data/objects.json').toString());
return objects[id]; return objects[id];
} else if (fs.existsSync(rootDir + 'tmp/' + appName + '-data/objects.jsonl')) {
loadJSONLDB();
const db = new JSONLDB(rootDir + 'tmp/' + appName + '-data/objects.jsonl');
await db.open();
let obj = db.get(id);
await db.close();
return obj;
} else {
console.error('getAdapterConfig: No objects file found in datadir ' + rootDir + 'tmp/' + appName + '-data/');
}
} }
if (typeof module !== undefined && module.parent) { if (typeof module !== undefined && module.parent) {