@iobroker/legacy-testing 0.2.4 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/package.json +1 -1
  3. package/setup.js +121 -194
package/README.md CHANGED
@@ -11,7 +11,7 @@ Legacy test modules for ioBroker
11
11
 
12
12
  ## Changelog
13
13
  <!-- ### **WORK IN PROGRESS** -->
14
- ### 0.2.4 (2023-05-07)
14
+ ### 0.2.6 (2023-05-07)
15
15
  * (bluefox) Added possibility to install additional adapters at start
16
16
  * (bluefox) Added setOfflineState/getOfflineState
17
17
 
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.2.4",
2
+ "version": "0.2.6",
3
3
  "name": "@iobroker/legacy-testing",
4
4
  "main": "setup.js",
5
5
  "files": [
package/setup.js CHANGED
@@ -8,6 +8,7 @@ const path = require('path');
8
8
  const child_process = require('child_process');
9
9
  const rootDir = path.normalize(`${__dirname}/../../../`);
10
10
  const pkg = require(`${rootDir}package.json`);
11
+ const cp = require('child_process');
11
12
  const debug = typeof v8debug === 'object';
12
13
  pkg.main = pkg.main || 'main.js';
13
14
 
@@ -187,54 +188,26 @@ async function checkIsAdapterInstalled(cb, counter, customAdapterName, customIns
187
188
  customAdapterName = customAdapterName || pkg.name.split('.').pop();
188
189
  counter = counter || 0;
189
190
  const dataDir = `${rootDir}tmp/${appName}-data/`;
190
- console.log('checkIsAdapterInstalled...');
191
+ console.log(`[${customAdapterName}] checkIsAdapterInstalled...`);
191
192
 
192
193
  try {
193
- if (fs.existsSync(`${dataDir}objects.json`)) {
194
- const f = fs.readFileSync(`${dataDir}objects.json`);
195
- const objects = JSON.parse(f.toString());
196
- if (objects[`system.adapter.${customAdapterName}.${customInstance || 0}`]) {
197
- console.log('checkIsAdapterInstalled: ready!');
198
- setTimeout(() => cb && cb(), 100);
199
- return;
200
- } else {
201
- console.warn('checkIsAdapterInstalled: still not ready');
202
- }
203
- } else if (fs.existsSync(`${dataDir}objects.jsonl`)) {
204
- loadJSONLDB();
205
- const db = new JSONLDB(`${dataDir}objects.jsonl`);
206
- try {
207
- await db.open();
208
- } catch (err) {
209
- if (err.message.includes('Failed to lock DB file')) {
210
- console.log('checkIsAdapterInstalled: DB still opened ...');
211
- }
212
- throw err;
213
- }
214
-
215
- const obj = db.get(`system.adapter.${customAdapterName}.${customInstance || 0}`);
216
- await db.close();
217
-
218
- if (obj) {
219
- console.log('checkIsAdapterInstalled: ready!');
220
- setTimeout(() => cb && cb(), 100);
221
- return;
222
- } else {
223
- console.warn('checkIsAdapterInstalled: still not ready');
224
- }
194
+ const obj = await getObject(`system.adapter.${customAdapterName}.${customInstance || 0}`)
195
+ if (obj) {
196
+ console.log(`[${customAdapterName}] checkIsAdapterInstalled: ready!`);
197
+ setTimeout(() => cb && cb(), 100);
198
+ return;
225
199
  } else {
226
- console.error(`checkIsAdapterInstalled: No objects file found in datadir ${dataDir}`);
200
+ console.warn(`[${customAdapterName}] checkIsAdapterInstalled: still not ready`);
227
201
  }
228
-
229
202
  } catch (err) {
230
- console.log(`checkIsAdapterInstalled: catch ${err}`);
203
+ console.log(`[${customAdapterName}] checkIsAdapterInstalled: catch ${err}`);
231
204
  }
232
205
 
233
206
  if (counter > 20) {
234
- console.error('checkIsAdapterInstalled: Cannot install!');
207
+ console.error(`[${customAdapterName}] checkIsAdapterInstalled: Cannot install!`);
235
208
  cb && cb('Cannot install');
236
209
  } else {
237
- console.log('checkIsAdapterInstalled: wait...');
210
+ console.log(`[${customAdapterName}] checkIsAdapterInstalled: wait...`);
238
211
  setTimeout(() => checkIsAdapterInstalled(cb, counter + 1, customAdapterName, customInstance), 1000);
239
212
  }
240
213
  }
@@ -245,37 +218,11 @@ async function checkIsControllerInstalled(cb, counter) {
245
218
 
246
219
  console.log('checkIsControllerInstalled...');
247
220
  try {
248
- if (fs.existsSync(`${dataDir}objects.json`)) {
249
- const f = fs.readFileSync(`${dataDir}objects.json`);
250
- const objects = JSON.parse(f.toString());
251
- if (objects['system.certificates']) {
252
- console.log('checkIsControllerInstalled: installed!');
253
- setTimeout(() => cb && cb(), 100);
254
- return;
255
- }
256
- } else if (fs.existsSync(`${dataDir}objects.jsonl`)) {
257
- loadJSONLDB();
258
- const db = new JSONLDB(`${dataDir}objects.jsonl`);
259
- try {
260
- await db.open();
261
- } catch (err) {
262
- if (err.message.includes('Failed to lock DB file')) {
263
- console.log('checkIsControllerInstalled: DB still opened ...');
264
- }
265
- throw err;
266
- }
267
-
268
- const obj = db.get('system.certificates');
269
- await db.close();
270
-
271
- if (obj) {
272
- console.log('checkIsControllerInstalled: installed!');
273
- setTimeout(() => cb && cb(), 100);
274
- return;
275
- }
276
-
277
- } else {
278
- console.error(`checkIsControllerInstalled: No objects file found in datadir ${dataDir}`);
221
+ const obj = await getObject('system.certificates');
222
+ if (obj) {
223
+ console.log('checkIsControllerInstalled: installed!');
224
+ setTimeout(() => cb && cb(), 100);
225
+ return;
279
226
  }
280
227
  } catch (err) {
281
228
  // ignore
@@ -296,7 +243,12 @@ function installAdapter(customAdapterName, cb) {
296
243
  customAdapterName = null;
297
244
  }
298
245
  customAdapterName = customAdapterName || pkg.name.split('.').pop();
299
- console.log('Install adapter...');
246
+ console.log(`[${customAdapterName}] Install adapter...`);
247
+
248
+ if (customAdapterName.includes('@')) {
249
+ installCustomAdapter(customAdapterName);
250
+ }
251
+
300
252
  const startFile = `node_modules/${appName}.js-controller/${appName}.js`;
301
253
  // make first install
302
254
  if (debug) {
@@ -306,7 +258,7 @@ function installAdapter(customAdapterName, cb) {
306
258
  });
307
259
  checkIsAdapterInstalled(error => {
308
260
  error && console.error(error);
309
- console.log('Adapter installed.');
261
+ console.log(`[${customAdapterName}] Adapter installed.`);
310
262
  cb && cb();
311
263
  }, 0, customAdapterName);
312
264
  } else {
@@ -319,7 +271,7 @@ function installAdapter(customAdapterName, cb) {
319
271
  waitForEnd(_pid, () =>
320
272
  checkIsAdapterInstalled(error => {
321
273
  error && console.error(error);
322
- console.log('Adapter installed.');
274
+ console.log(`[${customAdapterName}] Adapter installed.`);
323
275
  cb && cb();
324
276
  }, 0, customAdapterName));
325
277
  }
@@ -514,6 +466,16 @@ function copyAdapterToController() {
514
466
  console.log('Adapter copied.');
515
467
  }
516
468
 
469
+ function installCustomAdapter(adapterName) {
470
+ if (!fs.existsSync(`${rootDir}tmp/node_modules/${adapterName}`)) {
471
+ console.log(`Install ${adapterName}`);
472
+ cp.execSync(`npm install ${adapterName} --prefix ./ --production`, {
473
+ cwd: `${rootDir}tmp/`,
474
+ stdio: [0, 1, 2],
475
+ });
476
+ }
477
+ }
478
+
517
479
  function clearControllerLog() {
518
480
  const dirPath = `${rootDir}tmp/log`;
519
481
  let files;
@@ -582,79 +544,75 @@ function setupController(preInstalledAdapters, cb) {
582
544
  restoreOriginalFiles();
583
545
  copyAdapterToController();
584
546
  }
585
- // read system.config object
586
- const dataDir = `${rootDir}tmp/${appName}-data/`;
587
-
588
- if (fs.existsSync(`${dataDir}objects.json`)) {
589
- let objs;
590
- try {
591
- objs = fs.readFileSync(`${dataDir}objects.json`);
592
- objs = JSON.parse(objs);
593
- } catch (e) {
594
- console.log('ERROR reading/parsing system configuration. Ignore');
595
- objs = {'system.config': {}};
596
- }
597
- if (!objs || !objs['system.config']) {
598
- objs = {'system.config': {}};
599
- }
600
-
601
- systemConfig = objs['system.config'];
602
- cb && cb(objs['system.config']);
603
- } else if (fs.existsSync(`${dataDir}objects.jsonl`)) {
604
- loadJSONLDB();
605
- const db = new JSONLDB(`${dataDir}objects.jsonl`);
606
- await db.open();
607
-
608
- const config = db.get('system.config');
609
- systemConfig = config || {};
610
-
611
- await db.close();
612
547
 
613
- cb && cb(systemConfig);
614
- } else {
615
- console.error(`read SystemConfig: No objects file found in datadir ${dataDir}`);
616
- }
548
+ // read system.config object
549
+ const config = await getObject('system.config');
550
+ systemConfig = config || {};
551
+ cb && cb(systemConfig);
617
552
  } catch (err) {
618
553
  console.error(`setupController: ${err}`);
619
554
  }
620
555
  });
621
556
  }
622
557
 
623
- async function getSecret() {
624
- const dataDir = `${rootDir}tmp/${appName}-data/`;
625
-
626
- if (systemConfig) {
627
- return systemConfig.native.secret;
628
- }
629
- if (fs.existsSync(`${dataDir}objects.json`)) {
630
- let objs;
558
+ async function getObject(id){
559
+ if (fs.existsSync(`${rootDir}tmp/${appName}-data/objects.json`)) {
560
+ const objects = JSON.parse(fs.readFileSync(`${rootDir}tmp/${appName}-data/objects.json`).toString());
561
+ return objects[id];
562
+ } else if (fs.existsSync(`${rootDir}tmp/${appName}-data/objects.jsonl`)) {
563
+ loadJSONLDB();
564
+ const db = new JSONLDB(`${rootDir}tmp/${appName}-data/objects.jsonl`);
631
565
  try {
632
- objs = fs.readFileSync(`${dataDir}objects.json`);
633
- objs = JSON.parse(objs);
634
- } catch (e) {
635
- console.warn(`Could not load secret. Reason: ${e}`);
636
- return null;
637
- }
638
- if (!objs || !objs['system.config']) {
639
- objs = {'system.config': {}};
566
+ await db.open();
567
+ } catch (err) {
568
+ if (err.message.includes('Failed to lock DB file')) {
569
+ console.log(`getObject: DB still opened ...`);
570
+ }
571
+ throw err;
640
572
  }
641
573
 
642
- return objs['system.config'].native.secre;
643
- } else if (fs.existsSync(`${dataDir}objects.jsonl`)) {
644
- loadJSONLDB();
645
- const db = new JSONLDB(`${dataDir}objects.jsonl`);
646
- await db.open();
574
+ const obj = db.get(id) || {};
575
+ await db.close();
576
+ return obj;
577
+ } else {
578
+ console.error(`setAdapterConfig: No objects file found in datadir ${rootDir}tmp/${appName}-data/`);
579
+ return null;
580
+ }
581
+ }
647
582
 
648
- let config = db.get('system.config');
649
- config = config || {};
583
+ async function setObject(id, obj){
584
+ if (fs.existsSync(`${rootDir}tmp/${appName}-data/objects.json`)) {
585
+ const objects = JSON.parse(fs.readFileSync(`${rootDir}tmp/${appName}-data/objects.json`).toString());
586
+ objects[id] = obj;
587
+ } else if (fs.existsSync(`${rootDir}tmp/${appName}-data/objects.jsonl`)) {
588
+ loadJSONLDB();
589
+ const db = new JSONLDB(`${rootDir}tmp/${appName}-data/objects.jsonl`);
590
+ try {
591
+ await db.open();
592
+ } catch (err) {
593
+ if (err.message.includes('Failed to lock DB file')) {
594
+ console.log(`getObject: DB still opened ...`);
595
+ }
596
+ throw err;
597
+ }
650
598
 
599
+ db.set(id, obj);
651
600
  await db.close();
652
-
653
- return config.native.secret;
601
+ return obj;
654
602
  } else {
655
- console.error(`read secret: No objects file found in datadir ${dataDir}`);
603
+ console.error(`setAdapterConfig: No objects file found in datadir ${rootDir}tmp/${appName}-data/`);
604
+ return null;
656
605
  }
606
+ }
657
607
 
608
+ async function getSecret() {
609
+ const dataDir = `${rootDir}tmp/${appName}-data/`;
610
+
611
+ if (systemConfig) {
612
+ return systemConfig.native.secret;
613
+ }
614
+ systemConfig = await getObject('system.config');
615
+ return systemConfig.native.secret;
658
616
  }
659
617
 
660
618
  function encrypt (key, value) {
@@ -705,22 +663,22 @@ function startCustomAdapter(adapterName, adapterInstance) {
705
663
  console.log(`Adapter ${id} already started ...`);
706
664
  return;
707
665
  }
708
- adaptersStarted[`${id}`] = true;
666
+ adaptersStarted[id] = true;
709
667
  console.log(`startAdapter ${id} ...`);
710
668
  const _pkg = require(`${rootDir}tmp/node_modules/iobroker.${adapterName}/package.json`);
711
669
  if (fs.existsSync(`${rootDir}tmp/node_modules/iobroker.${adapterName}/${_pkg.main || 'main.js'}`)) {
712
670
  try {
713
671
  if (debug) {
714
672
  // start controller
715
- pids[`${pkg.name}.0`] = child_process.exec(`node node_modules/iobroker.${adapterName}/${_pkg.main || 'main.js'} ${adapterInstance} --console silly`, {
673
+ pids[id] = child_process.exec(`node node_modules/iobroker.${adapterName}/${_pkg.main || 'main.js'} ${adapterInstance} --debug --console silly`, {
716
674
  cwd: `${rootDir}tmp`,
717
- stdio: [0, 1, 2]
675
+ stdio: [0, 1, 2],
718
676
  });
719
677
  } else {
720
678
  // start controller
721
- pids[`${pkg.name}.0`] = child_process.fork(`node_modules/iobroker.${adapterName}/${_pkg.main || 'main.js'}`, [adapterInstance, '--console', 'silly'], {
679
+ pids[id] = child_process.fork(`node_modules/iobroker.${adapterName}/${_pkg.main || 'main.js'}`, [adapterInstance, '--debug ', '--console', 'silly'], {
722
680
  cwd: `${rootDir}tmp`,
723
- stdio: [0, 1, 2, 'ipc']
681
+ stdio: [0, 1, 2, 'ipc'],
724
682
  });
725
683
  }
726
684
  } catch (error) {
@@ -877,10 +835,10 @@ function stopAdapter(cb) {
877
835
  }
878
836
 
879
837
  function stopCustomAdapter(adapterName, adapterInstance) {
880
- const id = `${pkg.name}.0`;
838
+ const id = `${adapterName}.${adapterInstance || 0}`;
881
839
  if (!pids[id]) {
882
- console.error('Controller is not running!');
883
- return Promise.resolve()
840
+ console.error(`Adapter instance ${id} is not running!`);
841
+ return Promise.resolve();
884
842
  } else {
885
843
  adaptersStarted[id] = false;
886
844
  return new Promise(resolve => {
@@ -921,8 +879,8 @@ function stopController(cb) {
921
879
  console.log(`Set system.adapter.${pkg.name}.0`);
922
880
  objects.setObject(`system.adapter.${pkg.name}.0`, {
923
881
  common: {
924
- enabled: false
925
- }
882
+ enabled: false,
883
+ },
926
884
  });
927
885
  }
928
886
 
@@ -956,54 +914,20 @@ function stopController(cb) {
956
914
 
957
915
  // Set up the adapter
958
916
  async function setAdapterConfig(common, native, instance, customAdapterName) {
959
- const id = `system.adapter.${(adapterName || customAdapterName).split('.').pop()}.${instance || 0}`;
960
- if (fs.existsSync(`${rootDir}tmp/${appName}-data/objects.json`)) {
961
- const objects = JSON.parse(fs.readFileSync(`${rootDir}tmp/${appName}-data/objects.json`).toString());
962
- if (common) {
963
- objects[id].common = common;
964
- }
965
- if (native) {
966
- objects[id].native = native;
967
- }
968
- fs.writeFileSync(`${rootDir}tmp/${appName}-data/objects.json`, JSON.stringify(objects));
969
- } else if (fs.existsSync(`${rootDir}tmp/${appName}-data/objects.jsonl`)) {
970
- loadJSONLDB();
971
- const db = new JSONLDB(`${rootDir}tmp/${appName}-data/objects.jsonl`);
972
- await db.open();
973
-
974
- const obj = db.get(id);
975
- if (common) {
976
- obj.common = common;
977
- }
978
- if (native) {
979
- obj.native = native;
980
- }
981
- db.set(id, obj);
982
-
983
- await db.close();
984
- } else {
985
- console.error(`setAdapterConfig: No objects file found in datadir ${rootDir}tmp/${appName}-data/`);
917
+ const id = `system.adapter.${(customAdapterName || adapterName).split('.').pop()}.${instance || 0}`;
918
+ const obj = (await getObject(id)) || {};
919
+ if (common) {
920
+ obj.common = common;
986
921
  }
922
+ if (native) {
923
+ obj.native = native;
924
+ }
925
+ await setObject(id, obj);
987
926
  }
988
927
 
989
928
  // Read config of the adapter
990
929
  async function getAdapterConfig(instance, customAdapterName) {
991
- const id = `system.adapter.${(adapterName || customAdapterName).split('.').pop()}.${instance || 0}`;
992
- if (fs.existsSync(`${rootDir}tmp/${appName}-data/objects.json`)) {
993
- const objects = JSON.parse(fs.readFileSync(`${rootDir}tmp/${appName}-data/objects.json`).toString());
994
- return objects[id];
995
- } else if (fs.existsSync(`${rootDir}tmp/${appName}-data/objects.jsonl`)) {
996
- loadJSONLDB();
997
- const db = new JSONLDB(`${rootDir}tmp/${appName}-data/objects.jsonl`);
998
- await db.open();
999
-
1000
- const obj = db.get(id);
1001
-
1002
- await db.close();
1003
- return obj;
1004
- } else {
1005
- console.error(`getAdapterConfig: No objects file found in datadir ${rootDir}tmp/${appName}-data/`);
1006
- }
930
+ return getObject(`system.adapter.${(customAdapterName || adapterName).split('.').pop()}.${instance || 0}`);
1007
931
  }
1008
932
 
1009
933
  async function setOfflineState(id, state) {
@@ -1046,18 +970,21 @@ async function getOfflineState(id) {
1046
970
  if (typeof module !== undefined && module.parent) {
1047
971
  module.exports.getAdapterConfig = getAdapterConfig;
1048
972
  module.exports.setAdapterConfig = setAdapterConfig;
1049
- module.exports.startController = startController;
1050
- module.exports.stopController = stopController;
1051
- module.exports.setupController = setupController;
1052
- module.exports.stopAdapter = stopAdapter;
1053
- module.exports.startAdapter = startAdapter;
1054
- module.exports.installAdapter = installAdapter;
1055
- module.exports.appName = appName;
1056
- module.exports.adapterName = adapterName;
1057
- module.exports.getSecret = getSecret;
1058
- module.exports.encrypt = encrypt;
1059
- module.exports.setOfflineState = setOfflineState;
1060
- module.exports.getOfflineState = getOfflineState;
973
+ module.exports.startController = startController;
974
+ module.exports.stopController = stopController;
975
+ module.exports.setupController = setupController;
976
+ module.exports.stopAdapter = stopAdapter;
977
+ module.exports.startAdapter = startAdapter;
978
+ module.exports.installAdapter = installAdapter;
979
+ module.exports.appName = appName;
980
+ module.exports.adapterName = adapterName;
981
+ module.exports.getSecret = getSecret;
982
+ module.exports.encrypt = encrypt;
983
+ module.exports.setOfflineState = setOfflineState;
984
+ module.exports.getOfflineState = getOfflineState;
1061
985
  module.exports.stopCustomAdapter = stopCustomAdapter;
1062
986
  module.exports.startCustomAdapter = startCustomAdapter;
987
+ module.exports.installCustomAdapter = installCustomAdapter;
988
+ module.exports.getObject = getObject;
989
+ module.exports.setObject = setObject;
1063
990
  }