update testing setup.js

This commit is contained in:
Ingo Fischer
2022-01-29 15:27:38 +01:00
parent d042d65588
commit f1376e5e5b
+377 -162
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,26 +103,65 @@ 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 objects = JSON.parse(f.toString()); const f = fs.readFileSync(dataDir + 'objects.json');
if (objects['system.adapter.admin.0'] && objects['system.adapter.admin.0'].common) { const objects = JSON.parse(f.toString());
objects['system.adapter.admin.0'].common.enabled = false; 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; 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));
console.log('Store original objects.json');
} }
fs.writeFileSync(dataDir + 'objects.json.original', JSON.stringify(objects)); if (fs.existsSync(dataDir + 'states.json')) {
try { try {
f = fs.readFileSync(dataDir + 'states.json'); const f = fs.readFileSync(dataDir + 'states.json');
fs.writeFileSync(dataDir + 'states.json.original', f); fs.writeFileSync(dataDir + 'states.json.original', f);
console.log('Store original states.json');
} catch (err) {
console.log('no states.json found - ignore');
}
} }
catch (err) {
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');
} }
} }
@@ -111,38 +169,74 @@ 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')) {
fs.writeFileSync(dataDir + 'objects.json', f); const f = fs.readFileSync(dataDir + 'objects.json.original');
try { fs.writeFileSync(dataDir + 'objects.json', f);
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 {
const f = fs.readFileSync(dataDir + 'objects.json'); if (fs.existsSync(dataDir + 'objects.json')) {
const objects = JSON.parse(f.toString()); const f = fs.readFileSync(dataDir + 'objects.json');
if (objects['system.adapter.' + customName + '.0']) { const objects = JSON.parse(f.toString());
console.log('checkIsAdapterInstalled: ready!'); if (objects['system.adapter.' + customName + '.0']) {
setTimeout(function () { console.log('checkIsAdapterInstalled: ready!');
if (cb) cb(); setTimeout(function () {
}, 100); if (cb) cb();
return; }, 100);
} else { return;
console.warn('checkIsAdapterInstalled: still not ready'); } else {
} console.warn('checkIsAdapterInstalled: still not ready');
} catch (err) { }
} 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('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,20 +250,47 @@ 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 {
const f = fs.readFileSync(dataDir + 'objects.json'); if (fs.existsSync(dataDir + 'objects.json')) {
const objects = JSON.parse(f.toString()); const f = fs.readFileSync(dataDir + 'objects.json');
if (objects['system.certificates']) { const objects = JSON.parse(f.toString());
console.log('checkIsControllerInstalled: installed!'); if (objects['system.certificates']) {
setTimeout(function () { console.log('checkIsControllerInstalled: installed!');
if (cb) cb(); setTimeout(function () {
}, 100); if (cb) cb();
return; }, 100);
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,46 +574,89 @@ function clearDB() {
} }
function setupController(cb) { function setupController(cb) {
installJsController(function (isInited) { installJsController(async function (isInited) {
clearControllerLog(); try {
clearDB(); clearControllerLog();
clearDB();
if (!isInited) { if (!isInited) {
restoreOriginalFiles(); restoreOriginalFiles();
copyAdapterToController(); copyAdapterToController();
}
// read system.config object
const dataDir = rootDir + 'tmp/' + appName + '-data/';
if (fs.existsSync(dataDir + 'objects.json')) {
let 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': {}};
}
systemConfig = 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);
} }
// read system.config object });
const dataDir = rootDir + 'tmp/' + appName + '-data/'; }
async function getSecret() {
var dataDir = rootDir + 'tmp/' + appName + '-data/';
if (systemConfig) {
return systemConfig.native.secret;
}
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.warn("Could not load secret. Reason: " + e);
objs = {'system.config': {}}; return null;
} }
if (!objs || !objs['system.config']) { if (!objs || !objs['system.config']) {
objs = {'system.config': {}}; objs = {'system.config': {}};
} }
if (cb) cb(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();
function getSecret() { let config = db.get('system.config');
var dataDir = rootDir + 'tmp/' + appName + '-data/'; config = config || {};
try { await db.close();
var objs = fs.readFileSync(dataDir + 'objects.json');
objs = JSON.parse(objs);
return objs['system.config'].native.secret; return config.native.secret;
} catch (e) { } else {
console.warn("Could not load secret. Reason: " + e); console.error('read secret: No objects file found in datadir ' + dataDir);
return null;
} }
} }
function encrypt (key, value) { function encrypt (key, value) {
@@ -544,100 +716,115 @@ 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...');
adapterStarted = false; try {
let isObjectConnected; const config = require(rootDir + 'tmp/' + appName + '-data/' + appName + '.json');
let isStatesConnected;
const Objects = require(rootDir + 'tmp/node_modules/' + appName + '.js-controller/lib/objects/objectsInMemServer'); adapterStarted = false;
objects = new Objects({ let isObjectConnected;
connection: { let isStatesConnected;
'type' : 'file',
'host' : '127.0.0.1', // rootDir + 'tmp/node_modules
'port' : 19001, const objPath = require.resolve(`@iobroker/db-objects-${config.objects.type}`, {
'user' : '', paths: [ rootDir + 'tmp/node_modules', rootDir, rootDir + 'tmp/node_modules/' + appName + '.js-controller']
'pass' : '', });
'noFileCache': false, console.log('Objects Path: ' + objPath);
'connectTimeout': 2000 const Objects = require(objPath).Server;
}, objects = new Objects({
logger: { connection: {
silly: function (msg) { 'type': config.objects.type,
console.log(msg); 'host': '127.0.0.1',
'port': 19001,
'user': '',
'pass': '',
'noFileCache': false,
'connectTimeout': 2000
}, },
debug: function (msg) { logger: {
console.log(msg); 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);
}
}, },
info: function (msg) { connected: function () {
console.log(msg); isObjectConnected = true;
}, if (isStatesConnected) {
warn: function (msg) { console.log('startController: started!');
console.warn(msg); if (isStartAdapter) {
}, startAdapter(objects, states, callback);
error: function (msg) { } else {
console.error(msg); if (callback) {
} callback(objects, states);
}, callback = null;
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
change: onObjectChange });
});
// 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}`, {
states = new States({ paths: [ rootDir + 'tmp/node_modules', rootDir, rootDir + 'tmp/node_modules/' + appName + '.js-controller']
connection: { });
type: 'file', console.log('States Path: ' + statePath);
host: '127.0.0.1', const States = require(statePath).Server;
port: 19000, states = new States({
options: { connection: {
auth_pass: null, type: config.states.type,
retry_max_delay: 15000 host: '127.0.0.1',
} port: 19000,
}, options: {
logger: { auth_pass: null,
silly: function (msg) { retry_max_delay: 15000
console.log(msg); }
}, },
debug: function (msg) { logger: {
console.log(msg); 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);
}
}, },
info: function (msg) { connected: function () {
console.log(msg); isStatesConnected = true;
}, if (isObjectConnected) {
warn: function (msg) { console.log('startController: started!!');
console.log(msg); if (isStartAdapter) {
}, startAdapter(objects, states, callback);
error: function (msg) { } else {
console.log(msg); if (callback) {
} callback(objects, states);
}, callback = null;
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
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 (common) objects[id].common = common; if (fs.existsSync(rootDir + 'tmp/' + appName + '-data/objects.json')) {
if (native) objects[id].native = native; const objects = JSON.parse(fs.readFileSync(rootDir + 'tmp/' + appName + '-data/objects.json').toString());
fs.writeFileSync(rootDir + 'tmp/' + appName + '-data/objects.json', JSON.stringify(objects)); if (common) objects[id].common = common;
if (native) objects[id].native = native;
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')) {
return objects[id]; const objects = JSON.parse(fs.readFileSync(rootDir + 'tmp/' + appName + '-data/objects.json').toString());
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) {