update testing setup.js
This commit is contained in:
+253
-38
@@ -9,6 +9,8 @@ const pkg = require(rootDir + 'package.json');
|
||||
const debug = typeof v8debug === 'object';
|
||||
pkg.main = pkg.main || 'main.js';
|
||||
|
||||
let JSONLDB;
|
||||
|
||||
let adapterName = path.normalize(rootDir).replace(/\\/g, '/').split('/');
|
||||
adapterName = adapterName[adapterName.length - 2];
|
||||
let adapterStarted = false;
|
||||
@@ -18,6 +20,21 @@ function getAppName() {
|
||||
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();
|
||||
|
||||
let objects;
|
||||
@@ -25,6 +42,8 @@ let states;
|
||||
|
||||
let pid = null;
|
||||
|
||||
let systemConfig = null;
|
||||
|
||||
function copyFileSync(source, target) {
|
||||
|
||||
let targetFile = target;
|
||||
@@ -84,11 +103,12 @@ if (!fs.existsSync(rootDir + 'tmp')) {
|
||||
fs.mkdirSync(rootDir + 'tmp');
|
||||
}
|
||||
|
||||
function storeOriginalFiles() {
|
||||
async function storeOriginalFiles() {
|
||||
console.log('Store original files...');
|
||||
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());
|
||||
if (objects['system.adapter.admin.0'] && objects['system.adapter.admin.0'].common) {
|
||||
objects['system.adapter.admin.0'].common.enabled = false;
|
||||
@@ -98,38 +118,84 @@ function storeOriginalFiles() {
|
||||
}
|
||||
|
||||
fs.writeFileSync(dataDir + 'objects.json.original', JSON.stringify(objects));
|
||||
try {
|
||||
f = fs.readFileSync(dataDir + 'states.json');
|
||||
fs.writeFileSync(dataDir + 'states.json.original', f);
|
||||
console.log('Store original objects.json');
|
||||
}
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
console.log('restoreOriginalFiles...');
|
||||
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);
|
||||
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);
|
||||
}
|
||||
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();
|
||||
counter = counter || 0;
|
||||
const dataDir = rootDir + 'tmp/' + appName + '-data/';
|
||||
console.log('checkIsAdapterInstalled...');
|
||||
|
||||
try {
|
||||
if (fs.existsSync(dataDir + 'objects.json')) {
|
||||
const f = fs.readFileSync(dataDir + 'objects.json');
|
||||
const objects = JSON.parse(f.toString());
|
||||
if (objects['system.adapter.' + customName + '.0']) {
|
||||
@@ -141,8 +207,36 @@ function checkIsAdapterInstalled(cb, counter, customName) {
|
||||
} else {
|
||||
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) {
|
||||
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) {
|
||||
@@ -156,12 +250,13 @@ function checkIsAdapterInstalled(cb, counter, customName) {
|
||||
}
|
||||
}
|
||||
|
||||
function checkIsControllerInstalled(cb, counter) {
|
||||
async function checkIsControllerInstalled(cb, counter) {
|
||||
counter = counter || 0;
|
||||
const dataDir = rootDir + 'tmp/' + appName + '-data/';
|
||||
|
||||
console.log('checkIsControllerInstalled...');
|
||||
try {
|
||||
if (fs.existsSync(dataDir + 'objects.json')) {
|
||||
const f = fs.readFileSync(dataDir + 'objects.json');
|
||||
const objects = JSON.parse(f.toString());
|
||||
if (objects['system.certificates']) {
|
||||
@@ -171,6 +266,32 @@ function checkIsControllerInstalled(cb, counter) {
|
||||
}, 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) {
|
||||
|
||||
}
|
||||
@@ -296,13 +417,17 @@ function installJsController(cb) {
|
||||
const config = require(rootDir + 'tmp/' + appName + '-data/' + appName + '.json');
|
||||
config.objects.port = 19001;
|
||||
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));
|
||||
console.log('Setup finished.');
|
||||
|
||||
copyAdapterToController();
|
||||
|
||||
installAdapter(function () {
|
||||
storeOriginalFiles();
|
||||
installAdapter(async function () {
|
||||
await storeOriginalFiles();
|
||||
if (cb) cb(true);
|
||||
});
|
||||
});
|
||||
@@ -359,12 +484,16 @@ function installJsController(cb) {
|
||||
const config = require(rootDir + 'tmp/' + appName + '-data/' + appName + '.json');
|
||||
config.objects.port = 19001;
|
||||
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));
|
||||
|
||||
copyAdapterToController();
|
||||
|
||||
installAdapter(function () {
|
||||
storeOriginalFiles();
|
||||
installAdapter(async function () {
|
||||
await storeOriginalFiles();
|
||||
if (cb) cb(true);
|
||||
});
|
||||
});
|
||||
@@ -445,7 +574,8 @@ function clearDB() {
|
||||
}
|
||||
|
||||
function setupController(cb) {
|
||||
installJsController(function (isInited) {
|
||||
installJsController(async function (isInited) {
|
||||
try {
|
||||
clearControllerLog();
|
||||
clearDB();
|
||||
|
||||
@@ -456,12 +586,12 @@ function setupController(cb) {
|
||||
// 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) {
|
||||
} catch (e) {
|
||||
console.log('ERROR reading/parsing system configuration. Ignore');
|
||||
objs = {'system.config': {}};
|
||||
}
|
||||
@@ -469,22 +599,64 @@ function setupController(cb) {
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function getSecret() {
|
||||
async function getSecret() {
|
||||
var dataDir = rootDir + 'tmp/' + appName + '-data/';
|
||||
|
||||
if (systemConfig) {
|
||||
return systemConfig.native.secret;
|
||||
}
|
||||
if (fs.existsSync(dataDir + 'objects.json')) {
|
||||
let objs;
|
||||
try {
|
||||
var objs = fs.readFileSync(dataDir + 'objects.json');
|
||||
objs = fs.readFileSync(dataDir + 'objects.json');
|
||||
objs = JSON.parse(objs);
|
||||
|
||||
return objs['system.config'].native.secret;
|
||||
} catch (e) {
|
||||
}
|
||||
catch (e) {
|
||||
console.warn("Could not load secret. Reason: " + e);
|
||||
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) {
|
||||
@@ -544,18 +716,26 @@ function startController(isStartAdapter, onObjectChange, onStateChange, callback
|
||||
console.error('Controller is already started!');
|
||||
} else {
|
||||
console.log('startController...');
|
||||
try {
|
||||
const config = require(rootDir + 'tmp/' + appName + '-data/' + appName + '.json');
|
||||
|
||||
adapterStarted = false;
|
||||
let isObjectConnected;
|
||||
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({
|
||||
connection: {
|
||||
'type' : 'file',
|
||||
'host' : '127.0.0.1',
|
||||
'port' : 19001,
|
||||
'user' : '',
|
||||
'pass' : '',
|
||||
'type': config.objects.type,
|
||||
'host': '127.0.0.1',
|
||||
'port': 19001,
|
||||
'user': '',
|
||||
'pass': '',
|
||||
'noFileCache': false,
|
||||
'connectTimeout': 2000
|
||||
},
|
||||
@@ -594,10 +774,14 @@ function startController(isStartAdapter, onObjectChange, onStateChange, callback
|
||||
});
|
||||
|
||||
// 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({
|
||||
connection: {
|
||||
type: 'file',
|
||||
type: config.states.type,
|
||||
host: '127.0.0.1',
|
||||
port: 19000,
|
||||
options: {
|
||||
@@ -638,6 +822,9 @@ function startController(isStartAdapter, onObjectChange, onStateChange, callback
|
||||
},
|
||||
change: onStateChange
|
||||
});
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -721,19 +908,47 @@ function stopController(cb) {
|
||||
}
|
||||
|
||||
// Setup the adapter
|
||||
function setAdapterConfig(common, native, instance) {
|
||||
const objects = JSON.parse(fs.readFileSync(rootDir + 'tmp/' + appName + '-data/objects.json').toString());
|
||||
async function setAdapterConfig(common, native, instance) {
|
||||
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 (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
|
||||
function getAdapterConfig(instance) {
|
||||
const objects = JSON.parse(fs.readFileSync(rootDir + 'tmp/' + appName + '-data/objects.json').toString());
|
||||
async function getAdapterConfig(instance) {
|
||||
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];
|
||||
} 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) {
|
||||
|
||||
Reference in New Issue
Block a user