@iobroker/legacy-testing 2.0.1 → 2.1.1
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.
- package/LICENSE +1 -1
- package/README.md +8 -3
- package/build/engineHelper.d.ts +21 -0
- package/build/engineHelper.js +114 -0
- package/build/guiHelper.d.ts +15 -0
- package/build/guiHelper.js +81 -0
- package/build/setup.d.ts +53 -0
- package/build/setup.js +986 -0
- package/build/tests/testPackageFiles.d.ts +1 -0
- package/build/tests/testPackageFiles.js +89 -0
- package/package.json +44 -10
- package/engineHelper.js +0 -141
- package/guiHelper.js +0 -92
- package/setup.js +0 -1062
- package/tests/testPackageFiles.js +0 -157
package/setup.js
DELETED
|
@@ -1,1062 +0,0 @@
|
|
|
1
|
-
/* jshint -W097 */
|
|
2
|
-
/* jshint strict: false */
|
|
3
|
-
/* jslint node: true */
|
|
4
|
-
|
|
5
|
-
// check if tmp directory exists
|
|
6
|
-
const { lstatSync, existsSync, mkdirSync, readFileSync, writeFileSync, readdirSync, unlinkSync } = require('node:fs');
|
|
7
|
-
const { normalize, join, basename } = require('node:path');
|
|
8
|
-
const { execSync, fork, exec } = require('node:child_process');
|
|
9
|
-
|
|
10
|
-
let rootDir = normalize(`${__dirname}/../../../`);
|
|
11
|
-
const debug = typeof v8debug === 'object';
|
|
12
|
-
|
|
13
|
-
let JSONLDB;
|
|
14
|
-
|
|
15
|
-
let adapterName = normalize(rootDir).replace(/\\/g, '/').split('/');
|
|
16
|
-
if (adapterName[adapterName.length - 2] === 'vis-2-widgets-testing') {
|
|
17
|
-
rootDir = normalize(`${__dirname}/../../../../../../`);
|
|
18
|
-
adapterName = normalize(rootDir).replace(/\\/g, '/').split('/');
|
|
19
|
-
}
|
|
20
|
-
adapterName = adapterName[adapterName.length - 2];
|
|
21
|
-
|
|
22
|
-
let pkg;
|
|
23
|
-
|
|
24
|
-
const adaptersStarted = {};
|
|
25
|
-
const pids = {};
|
|
26
|
-
|
|
27
|
-
let objects;
|
|
28
|
-
let states;
|
|
29
|
-
let systemConfig = null;
|
|
30
|
-
|
|
31
|
-
initialize();
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Initialize logic, you can re-trigger it after config has changed
|
|
35
|
-
*/
|
|
36
|
-
function initialize() {
|
|
37
|
-
pkg = require(`${rootDir}package.json`);
|
|
38
|
-
pkg.main = pkg.main || 'main.js';
|
|
39
|
-
|
|
40
|
-
!existsSync(`${rootDir}tmp`) && mkdirSync(`${rootDir}tmp`);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/** Lowercase application name */
|
|
44
|
-
const appName = 'iobroker';
|
|
45
|
-
|
|
46
|
-
function loadJSONLDB() {
|
|
47
|
-
if (!JSONLDB) {
|
|
48
|
-
const dbPath = require.resolve('@alcalzone/jsonl-db', {
|
|
49
|
-
paths: [`${rootDir}tmp/node_modules`, rootDir, `${rootDir}tmp/node_modules/${appName}.js-controller`],
|
|
50
|
-
});
|
|
51
|
-
console.log(`JSONLDB path: ${dbPath}`);
|
|
52
|
-
try {
|
|
53
|
-
const { JsonlDB } = require(dbPath);
|
|
54
|
-
JSONLDB = JsonlDB;
|
|
55
|
-
} catch (err) {
|
|
56
|
-
console.log(`Jsonl require error: ${err}`);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function copyFileSync(source, target) {
|
|
62
|
-
let targetFile = target;
|
|
63
|
-
|
|
64
|
-
//if target is a directory a new file with the same name will be created
|
|
65
|
-
if (existsSync(target) && lstatSync(target).isDirectory()) {
|
|
66
|
-
targetFile = join(target, basename(source));
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
try {
|
|
70
|
-
writeFileSync(targetFile, readFileSync(source));
|
|
71
|
-
} catch (err) {
|
|
72
|
-
console.log(`file copy error: ${source} -> ${targetFile} (error ignored)`);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function copyFolderRecursiveSync(source, target, ignore) {
|
|
77
|
-
let files = [];
|
|
78
|
-
|
|
79
|
-
let base = basename(source);
|
|
80
|
-
if (base === adapterName) {
|
|
81
|
-
base = pkg.name;
|
|
82
|
-
}
|
|
83
|
-
//check if folder needs to be created or integrated
|
|
84
|
-
const targetFolder = join(target, base);
|
|
85
|
-
if (!existsSync(targetFolder)) {
|
|
86
|
-
mkdirSync(targetFolder);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
//copy
|
|
90
|
-
if (lstatSync(source).isDirectory()) {
|
|
91
|
-
files = readdirSync(source);
|
|
92
|
-
files.forEach(file => {
|
|
93
|
-
if (ignore?.includes(file)) {
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const curSource = join(source, file);
|
|
98
|
-
const curTarget = join(targetFolder, file);
|
|
99
|
-
if (lstatSync(curSource).isDirectory()) {
|
|
100
|
-
// ignore grunt files
|
|
101
|
-
if (file.includes('grunt') || file === 'chai' || file === 'mocha') {
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
copyFolderRecursiveSync(curSource, targetFolder, ignore);
|
|
105
|
-
} else {
|
|
106
|
-
copyFileSync(curSource, curTarget);
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
async function storeOriginalFiles() {
|
|
113
|
-
console.log('Store original files...');
|
|
114
|
-
const dataDir = `${rootDir}tmp/${appName}-data/`;
|
|
115
|
-
|
|
116
|
-
if (existsSync(`${dataDir}objects.json`)) {
|
|
117
|
-
const f = readFileSync(`${dataDir}objects.json`);
|
|
118
|
-
const objects = JSON.parse(f.toString());
|
|
119
|
-
if (objects['system.adapter.admin.0'] && objects['system.adapter.admin.0'].common) {
|
|
120
|
-
objects['system.adapter.admin.0'].common.enabled = false;
|
|
121
|
-
}
|
|
122
|
-
if (objects['system.adapter.admin.1'] && objects['system.adapter.admin.1'].common) {
|
|
123
|
-
objects['system.adapter.admin.1'].common.enabled = false;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
writeFileSync(`${dataDir}objects.json.original`, JSON.stringify(objects));
|
|
127
|
-
console.log('Store original objects.json');
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (existsSync(`${dataDir}states.json`)) {
|
|
131
|
-
try {
|
|
132
|
-
const f = readFileSync(`${dataDir}states.json`);
|
|
133
|
-
writeFileSync(`${dataDir}states.json.original`, f);
|
|
134
|
-
console.log('Store original states.json');
|
|
135
|
-
} catch (err) {
|
|
136
|
-
console.log('no states.json found - ignore');
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
if (existsSync(`${dataDir}objects.jsonl`)) {
|
|
141
|
-
loadJSONLDB();
|
|
142
|
-
const db = new JSONLDB(`${dataDir}objects.jsonl`);
|
|
143
|
-
await db.open();
|
|
144
|
-
|
|
145
|
-
const admin0 = db.get('system.adapter.admin.0');
|
|
146
|
-
if (admin0) {
|
|
147
|
-
if (admin0.common) {
|
|
148
|
-
admin0.common.enabled = false;
|
|
149
|
-
db.set('system.adapter.admin.0', admin0);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
const admin1 = db.get('system.adapter.admin.1');
|
|
154
|
-
if (admin1) {
|
|
155
|
-
if (admin1.common) {
|
|
156
|
-
admin1.common.enabled = false;
|
|
157
|
-
db.set('system.adapter.admin.1', admin1);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
await db.close();
|
|
161
|
-
|
|
162
|
-
const f = readFileSync(`${dataDir}objects.jsonl`);
|
|
163
|
-
writeFileSync(`${dataDir}objects.jsonl.original`, f);
|
|
164
|
-
console.log('Store original objects.jsonl');
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (existsSync(`${dataDir}states.jsonl`)) {
|
|
168
|
-
const f = readFileSync(`${dataDir}states.jsonl`);
|
|
169
|
-
writeFileSync(`${dataDir}states.jsonl.original`, f);
|
|
170
|
-
console.log('Store original states.jsonl');
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
function restoreOriginalFiles() {
|
|
175
|
-
console.log('restoreOriginalFiles...');
|
|
176
|
-
const dataDir = `${rootDir}tmp/${appName}-data/`;
|
|
177
|
-
|
|
178
|
-
if (existsSync(`${dataDir}objects.json.original`)) {
|
|
179
|
-
const f = readFileSync(`${dataDir}objects.json.original`);
|
|
180
|
-
writeFileSync(`${dataDir}objects.json`, f);
|
|
181
|
-
}
|
|
182
|
-
if (existsSync(`${dataDir}objects.json.original`)) {
|
|
183
|
-
const f = readFileSync(`${dataDir}states.json.original`);
|
|
184
|
-
writeFileSync(`${dataDir}states.json`, f);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
if (existsSync(`${dataDir}objects.jsonl.original`)) {
|
|
188
|
-
const f = readFileSync(`${dataDir}objects.jsonl.original`);
|
|
189
|
-
writeFileSync(`${dataDir}objects.jsonl`, f);
|
|
190
|
-
}
|
|
191
|
-
if (existsSync(`${dataDir}objects.jsonl.original`)) {
|
|
192
|
-
const f = readFileSync(`${dataDir}states.jsonl.original`);
|
|
193
|
-
writeFileSync(`${dataDir}states.jsonl`, f);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
async function checkIsAdapterInstalled(cb, counter, customAdapterName, customInstance) {
|
|
198
|
-
customAdapterName = customAdapterName || pkg.name.split('.').pop();
|
|
199
|
-
counter = counter || 0;
|
|
200
|
-
console.log(`[${customAdapterName}] checkIsAdapterInstalled...`);
|
|
201
|
-
|
|
202
|
-
try {
|
|
203
|
-
const obj = await getObject(`system.adapter.${customAdapterName}.${customInstance || 0}`);
|
|
204
|
-
if (obj && obj.common) {
|
|
205
|
-
console.log(`[${customAdapterName}] checkIsAdapterInstalled: ready! ${JSON.stringify(obj)}`);
|
|
206
|
-
setTimeout(() => cb && cb(), 100);
|
|
207
|
-
return;
|
|
208
|
-
}
|
|
209
|
-
console.warn(`[${customAdapterName}] checkIsAdapterInstalled: still not ready`);
|
|
210
|
-
} catch (err) {
|
|
211
|
-
console.log(`[${customAdapterName}] checkIsAdapterInstalled: catch ${err}`);
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
if (counter > 20) {
|
|
215
|
-
console.error(`[${customAdapterName}] checkIsAdapterInstalled: Cannot install!`);
|
|
216
|
-
cb && cb('Cannot install');
|
|
217
|
-
} else {
|
|
218
|
-
console.log(`[${customAdapterName}] checkIsAdapterInstalled: wait...`);
|
|
219
|
-
setTimeout(() => checkIsAdapterInstalled(cb, counter + 1, customAdapterName, customInstance), 1000);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
function checkIsAdapterInstalledAsync(counter, customAdapterName, customInstance) {
|
|
224
|
-
return new Promise((resolve, reject) => {
|
|
225
|
-
checkIsAdapterInstalled(
|
|
226
|
-
err => {
|
|
227
|
-
if (err) {
|
|
228
|
-
reject(err);
|
|
229
|
-
} else {
|
|
230
|
-
resolve();
|
|
231
|
-
}
|
|
232
|
-
},
|
|
233
|
-
counter,
|
|
234
|
-
customAdapterName,
|
|
235
|
-
customInstance,
|
|
236
|
-
);
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
async function checkIsControllerInstalled(cb, counter) {
|
|
241
|
-
counter = counter || 0;
|
|
242
|
-
|
|
243
|
-
console.log('checkIsControllerInstalled...');
|
|
244
|
-
try {
|
|
245
|
-
const obj = await getObject('system.certificates');
|
|
246
|
-
if (obj && obj.common) {
|
|
247
|
-
console.log('checkIsControllerInstalled: installed!');
|
|
248
|
-
setTimeout(() => cb && cb(), 100);
|
|
249
|
-
return;
|
|
250
|
-
}
|
|
251
|
-
} catch (err) {
|
|
252
|
-
// ignore
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
if (counter > 20) {
|
|
256
|
-
console.log('checkIsControllerInstalled: Cannot install!');
|
|
257
|
-
cb && cb('Cannot install');
|
|
258
|
-
} else {
|
|
259
|
-
console.log('checkIsControllerInstalled: wait...');
|
|
260
|
-
setTimeout(() => checkIsControllerInstalled(cb, counter + 1), 1000);
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
function checkIsControllerInstalledAsync(counter) {
|
|
265
|
-
return new Promise((resolve, reject) => {
|
|
266
|
-
checkIsControllerInstalled(err => {
|
|
267
|
-
if (err) {
|
|
268
|
-
reject(err);
|
|
269
|
-
} else {
|
|
270
|
-
resolve();
|
|
271
|
-
}
|
|
272
|
-
}, counter);
|
|
273
|
-
});
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
async function installAdapter(customAdapterName, cb) {
|
|
277
|
-
if (typeof customAdapterName === 'function') {
|
|
278
|
-
cb = customAdapterName;
|
|
279
|
-
customAdapterName = null;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
customAdapterName = customAdapterName || pkg.name;
|
|
283
|
-
console.log(`[${customAdapterName}] Install adapter...`);
|
|
284
|
-
|
|
285
|
-
if (customAdapterName.includes('@')) {
|
|
286
|
-
installCustomAdapter(customAdapterName);
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
const startFile = `node_modules/${appName}.js-controller/${appName}.js`;
|
|
290
|
-
// make first install
|
|
291
|
-
if (debug) {
|
|
292
|
-
execSync(`node ${startFile} add ${customAdapterName} --enabled false`, {
|
|
293
|
-
cwd: `${rootDir}tmp`,
|
|
294
|
-
stdio: [0, 1, 2],
|
|
295
|
-
});
|
|
296
|
-
} else {
|
|
297
|
-
// add controller
|
|
298
|
-
const _pid = fork(startFile, ['add', customAdapterName, '--enabled', 'false'], {
|
|
299
|
-
cwd: `${rootDir}tmp`,
|
|
300
|
-
stdio: [0, 1, 2, 'ipc'],
|
|
301
|
-
});
|
|
302
|
-
|
|
303
|
-
await waitForEndAsync(_pid);
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
const name = customAdapterName.split('@')[0].split('.').pop(); // extract from iobroker.adaptername@version => adaptername
|
|
307
|
-
|
|
308
|
-
await checkIsAdapterInstalledAsync(null, name);
|
|
309
|
-
console.log(`[${customAdapterName}] Adapter installed.`);
|
|
310
|
-
cb && cb();
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
function installAdapterAsync(customAdapterName) {
|
|
314
|
-
return new Promise(resolve => installAdapter(customAdapterName, () => resolve()));
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
function waitForEndAsync(_pid) {
|
|
318
|
-
if (!_pid) {
|
|
319
|
-
return { code: -1, signal: -1 };
|
|
320
|
-
} else {
|
|
321
|
-
return new Promise(resolve => {
|
|
322
|
-
_pid.on('exit', (code, signal) => {
|
|
323
|
-
if (_pid) {
|
|
324
|
-
_pid = null;
|
|
325
|
-
resolve({ code, signal });
|
|
326
|
-
}
|
|
327
|
-
});
|
|
328
|
-
_pid.on('close', (code, signal) => {
|
|
329
|
-
if (_pid) {
|
|
330
|
-
_pid = null;
|
|
331
|
-
resolve({ code, signal });
|
|
332
|
-
}
|
|
333
|
-
});
|
|
334
|
-
});
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
async function installJsController(preInstalledAdapters, cb) {
|
|
339
|
-
if (typeof preInstalledAdapters === 'function') {
|
|
340
|
-
cb = preInstalledAdapters;
|
|
341
|
-
preInstalledAdapters = null;
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
console.log('installJsController...');
|
|
345
|
-
if (
|
|
346
|
-
!existsSync(`${rootDir}tmp/node_modules/${appName}.js-controller`) ||
|
|
347
|
-
!existsSync(`${rootDir}tmp/${appName}-data`)
|
|
348
|
-
) {
|
|
349
|
-
// try to detect appName.js-controller in node_modules/appName.js-controller
|
|
350
|
-
// travis CI installs js-controller into node_modules
|
|
351
|
-
if (existsSync(`${rootDir}node_modules/${appName}.js-controller`)) {
|
|
352
|
-
console.log(
|
|
353
|
-
`installJsController: no js-controller => copy it from "${rootDir}node_modules/${appName}.js-controller"`,
|
|
354
|
-
);
|
|
355
|
-
// copy all
|
|
356
|
-
// stop the controller
|
|
357
|
-
console.log('Stop controller if running...');
|
|
358
|
-
let _pid;
|
|
359
|
-
if (debug) {
|
|
360
|
-
// start controller
|
|
361
|
-
_pid = exec(`node ${appName}.js stop`, {
|
|
362
|
-
cwd: `${rootDir}node_modules/${appName}.js-controller`,
|
|
363
|
-
stdio: [0, 1, 2],
|
|
364
|
-
});
|
|
365
|
-
} else {
|
|
366
|
-
_pid = fork(`${appName}.js`, ['stop'], {
|
|
367
|
-
cwd: `${rootDir}node_modules/${appName}.js-controller`,
|
|
368
|
-
stdio: [0, 1, 2, 'ipc'],
|
|
369
|
-
});
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
await waitForEndAsync(_pid);
|
|
373
|
-
// copy all files into
|
|
374
|
-
!existsSync(`${rootDir}tmp`) && mkdirSync(`${rootDir}tmp`);
|
|
375
|
-
!existsSync(`${rootDir}tmp/node_modules`) && mkdirSync(`${rootDir}tmp/node_modules`);
|
|
376
|
-
|
|
377
|
-
if (!existsSync(`${rootDir}tmp/node_modules/${appName}.js-controller`)) {
|
|
378
|
-
console.log('Copy js-controller...');
|
|
379
|
-
copyFolderRecursiveSync(
|
|
380
|
-
`${rootDir}node_modules/${appName}.js-controller`,
|
|
381
|
-
`${rootDir}tmp/node_modules/`,
|
|
382
|
-
);
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
console.log('Setup js-controller...');
|
|
386
|
-
let __pid;
|
|
387
|
-
if (debug) {
|
|
388
|
-
// start controller
|
|
389
|
-
__pid = exec(`node ${appName}.js setup first --console`, {
|
|
390
|
-
cwd: `${rootDir}tmp/node_modules/${appName}.js-controller`,
|
|
391
|
-
stdio: [0, 1, 2],
|
|
392
|
-
});
|
|
393
|
-
} else {
|
|
394
|
-
__pid = fork(`${appName}.js`, ['setup', 'first', '--console'], {
|
|
395
|
-
cwd: `${rootDir}tmp/node_modules/${appName}.js-controller`,
|
|
396
|
-
stdio: [0, 1, 2, 'ipc'],
|
|
397
|
-
});
|
|
398
|
-
}
|
|
399
|
-
await waitForEndAsync(__pid);
|
|
400
|
-
await checkIsControllerInstalledAsync();
|
|
401
|
-
} else {
|
|
402
|
-
// check if port 9000 is free, else admin adapter will be added to running instance
|
|
403
|
-
const { Socket } = require('node:net');
|
|
404
|
-
const client = new Socket();
|
|
405
|
-
client.on('error', () => {
|
|
406
|
-
// ignore
|
|
407
|
-
});
|
|
408
|
-
client.connect(9000, '127.0.0.1', () => {
|
|
409
|
-
console.error(
|
|
410
|
-
'Cannot initiate the first run of test, because one instance of application is running on this PC. Stop it and repeat.',
|
|
411
|
-
);
|
|
412
|
-
process.exit(0);
|
|
413
|
-
});
|
|
414
|
-
|
|
415
|
-
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
416
|
-
client.destroy();
|
|
417
|
-
|
|
418
|
-
if (!existsSync(`${rootDir}tmp/node_modules/${appName}.js-controller`)) {
|
|
419
|
-
console.log('installJsController: no js-controller => install dev build from npm');
|
|
420
|
-
|
|
421
|
-
execSync(
|
|
422
|
-
`npm install ${appName}.js-controller@${process.env.JS_CONTROLLER_VERSION || 'dev'} --prefix ./ --omit=dev`,
|
|
423
|
-
{
|
|
424
|
-
cwd: `${rootDir}tmp/`,
|
|
425
|
-
stdio: [0, 1, 2],
|
|
426
|
-
},
|
|
427
|
-
);
|
|
428
|
-
} else {
|
|
429
|
-
console.log('Setup js-controller...');
|
|
430
|
-
let __pid;
|
|
431
|
-
if (debug) {
|
|
432
|
-
// start controller
|
|
433
|
-
__pid = exec(`node ${appName}.js setup first`, {
|
|
434
|
-
cwd: `${rootDir}tmp/node_modules/${appName}.js-controller`,
|
|
435
|
-
stdio: [0, 1, 2],
|
|
436
|
-
});
|
|
437
|
-
} else {
|
|
438
|
-
__pid = fork(`${appName}.js`, ['setup', 'first'], {
|
|
439
|
-
cwd: `${rootDir}tmp/node_modules/${appName}.js-controller`,
|
|
440
|
-
stdio: [0, 1, 2, 'ipc'],
|
|
441
|
-
});
|
|
442
|
-
}
|
|
443
|
-
await waitForEndAsync(__pid);
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
// let npm install admin and run setup
|
|
447
|
-
await checkIsControllerInstalledAsync();
|
|
448
|
-
let _pid;
|
|
449
|
-
|
|
450
|
-
if (existsSync(`${rootDir}node_modules/${appName}.js-controller/${appName}.js`)) {
|
|
451
|
-
_pid = fork(`${appName}.js`, ['stop'], {
|
|
452
|
-
cwd: `${rootDir}node_modules/${appName}.js-controller`,
|
|
453
|
-
stdio: [0, 1, 2, 'ipc'],
|
|
454
|
-
});
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
await waitForEndAsync(_pid);
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
// change ports for the object and state DBs
|
|
461
|
-
const config = require(`${rootDir}tmp/${appName}-data/${appName}.json`);
|
|
462
|
-
config.objects.port = 19001;
|
|
463
|
-
config.states.port = 19000;
|
|
464
|
-
|
|
465
|
-
writeFileSync(`${rootDir}tmp/${appName}-data/${appName}.json`, JSON.stringify(config, null, 2));
|
|
466
|
-
console.log('Setup finished.');
|
|
467
|
-
|
|
468
|
-
copyAdapterToController();
|
|
469
|
-
if (preInstalledAdapters) {
|
|
470
|
-
for (let p = 0; p < preInstalledAdapters.length; p++) {
|
|
471
|
-
await installAdapterAsync(preInstalledAdapters[p]);
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
await installAdapterAsync();
|
|
476
|
-
await storeOriginalFiles();
|
|
477
|
-
cb && cb(true);
|
|
478
|
-
} else {
|
|
479
|
-
if (preInstalledAdapters) {
|
|
480
|
-
for (let p = 0; p < preInstalledAdapters.length; p++) {
|
|
481
|
-
// if not installed
|
|
482
|
-
const parts = preInstalledAdapters[p].split('@');
|
|
483
|
-
const name = parts[0];
|
|
484
|
-
const version = parts[1];
|
|
485
|
-
if (!existsSync(`${rootDir}tmp/node_modules/${name}`)) {
|
|
486
|
-
await installAdapterAsync(preInstalledAdapters[p]);
|
|
487
|
-
} else {
|
|
488
|
-
const pack = require(`${rootDir}tmp/node_modules/${name}/package.json`);
|
|
489
|
-
if (pack.version !== version) {
|
|
490
|
-
await installAdapterAsync(preInstalledAdapters[p]);
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
}
|
|
495
|
-
setTimeout(() => {
|
|
496
|
-
console.log('installJsController: js-controller installed');
|
|
497
|
-
cb && cb(false);
|
|
498
|
-
}, 0);
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
function copyAdapterToController() {
|
|
503
|
-
console.log('Pack adapter...');
|
|
504
|
-
|
|
505
|
-
existsSync(`${rootDir}${pkg.name}-${pkg.version}.tgz`) && unlinkSync(`${rootDir}${pkg.name}-${pkg.version}.tgz`);
|
|
506
|
-
existsSync(`${rootDir}tmp/${pkg.name}-${pkg.version}.tgz`) &&
|
|
507
|
-
unlinkSync(`${rootDir}tmp/${pkg.name}-${pkg.version}.tgz`);
|
|
508
|
-
|
|
509
|
-
execSync(`npm pack`, {
|
|
510
|
-
cwd: rootDir,
|
|
511
|
-
stdio: [0, 1, 2],
|
|
512
|
-
});
|
|
513
|
-
copyFileSync(`${rootDir}${pkg.name}-${pkg.version}.tgz`, `${rootDir}tmp/${pkg.name}-${pkg.version}.tgz`);
|
|
514
|
-
execSync(`npm install ${pkg.name}-${pkg.version}.tgz --prefix ./ --omit=dev`, {
|
|
515
|
-
cwd: `${rootDir}tmp`,
|
|
516
|
-
stdio: [0, 1, 2],
|
|
517
|
-
});
|
|
518
|
-
console.log('Adapter copied.');
|
|
519
|
-
|
|
520
|
-
// update admin files
|
|
521
|
-
execSync(`node ${rootDir}tmp/node_modules/${appName}.js-controller/iobroker.js upload ${pkg.name.replace('iobroker.', '')}`, {
|
|
522
|
-
cwd: `${rootDir}tmp`,
|
|
523
|
-
stdio: [0, 1, 2],
|
|
524
|
-
});
|
|
525
|
-
|
|
526
|
-
console.log('Adapter uploaded.');
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
function installCustomAdapter(adapterName) {
|
|
530
|
-
if (!existsSync(`${rootDir}tmp/node_modules/${adapterName}`)) {
|
|
531
|
-
console.log(`Install ${adapterName}`);
|
|
532
|
-
execSync(`npm install ${adapterName} --prefix ./ --omit=dev`, {
|
|
533
|
-
cwd: `${rootDir}tmp/`,
|
|
534
|
-
stdio: [0, 1, 2],
|
|
535
|
-
});
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
function clearControllerLog() {
|
|
540
|
-
const dirPath = `${rootDir}tmp/log`;
|
|
541
|
-
let files;
|
|
542
|
-
try {
|
|
543
|
-
if (existsSync(dirPath)) {
|
|
544
|
-
console.log('Clear controller log...');
|
|
545
|
-
files = readdirSync(dirPath);
|
|
546
|
-
} else {
|
|
547
|
-
console.log('Create controller log directory...');
|
|
548
|
-
files = [];
|
|
549
|
-
mkdirSync(dirPath);
|
|
550
|
-
}
|
|
551
|
-
} catch (e) {
|
|
552
|
-
console.error(`Cannot read "${dirPath}"`);
|
|
553
|
-
return;
|
|
554
|
-
}
|
|
555
|
-
if (files.length > 0) {
|
|
556
|
-
try {
|
|
557
|
-
for (let i = 0; i < files.length; i++) {
|
|
558
|
-
const filePath = `${dirPath}/${files[i]}`;
|
|
559
|
-
unlinkSync(filePath);
|
|
560
|
-
}
|
|
561
|
-
console.log('Controller log cleared');
|
|
562
|
-
} catch (err) {
|
|
563
|
-
console.error(`cannot clear log: ${err}`);
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
function clearDB() {
|
|
569
|
-
const dirPath = `${rootDir}tmp/iobroker-data/sqlite`;
|
|
570
|
-
let files;
|
|
571
|
-
try {
|
|
572
|
-
if (existsSync(dirPath)) {
|
|
573
|
-
console.log('Clear sqlite DB...');
|
|
574
|
-
files = readdirSync(dirPath);
|
|
575
|
-
} else {
|
|
576
|
-
console.log('Create controller log directory...');
|
|
577
|
-
files = [];
|
|
578
|
-
mkdirSync(dirPath);
|
|
579
|
-
}
|
|
580
|
-
} catch (e) {
|
|
581
|
-
console.error(`Cannot read "${dirPath}"`);
|
|
582
|
-
return;
|
|
583
|
-
}
|
|
584
|
-
if (files.length > 0) {
|
|
585
|
-
try {
|
|
586
|
-
for (let i = 0; i < files.length; i++) {
|
|
587
|
-
const filePath = `${dirPath}/${files[i]}`;
|
|
588
|
-
unlinkSync(filePath);
|
|
589
|
-
}
|
|
590
|
-
console.log('Clear sqlite DB');
|
|
591
|
-
} catch (err) {
|
|
592
|
-
console.error(`cannot clear DB: ${err}`);
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
/** @typedef {{ rootDir?: string }} LegacyTestingOptions */
|
|
598
|
-
|
|
599
|
-
/**
|
|
600
|
-
* Override options
|
|
601
|
-
*
|
|
602
|
-
* @param {LegacyTestingOptions} options specify attributes which should be overridden
|
|
603
|
-
*/
|
|
604
|
-
function setOptions(options) {
|
|
605
|
-
if (options.rootDir && existsSync(options.rootDir) && rootDir !== options.rootDir) {
|
|
606
|
-
rootDir = options.rootDir;
|
|
607
|
-
initialize();
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
/**
|
|
612
|
-
* Install js-controller and setup the adapter
|
|
613
|
-
*
|
|
614
|
-
* @param {string[]} preInstalledAdapters list of adapters which need to be installed additionally
|
|
615
|
-
* @param {(systemConfig: Record<string, any>) => void} cb callback
|
|
616
|
-
*/
|
|
617
|
-
function setupController(preInstalledAdapters, cb) {
|
|
618
|
-
if (typeof preInstalledAdapters === 'function') {
|
|
619
|
-
cb = preInstalledAdapters;
|
|
620
|
-
preInstalledAdapters = null;
|
|
621
|
-
}
|
|
622
|
-
|
|
623
|
-
installJsController(preInstalledAdapters, async isInitialized => {
|
|
624
|
-
try {
|
|
625
|
-
clearControllerLog();
|
|
626
|
-
clearDB();
|
|
627
|
-
|
|
628
|
-
if (!isInitialized) {
|
|
629
|
-
restoreOriginalFiles();
|
|
630
|
-
copyAdapterToController();
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
// read system.config object
|
|
634
|
-
const config = await getObject('system.config');
|
|
635
|
-
systemConfig = config || {};
|
|
636
|
-
cb && cb(systemConfig);
|
|
637
|
-
} catch (err) {
|
|
638
|
-
console.error(`setupController: ${err}`);
|
|
639
|
-
}
|
|
640
|
-
});
|
|
641
|
-
}
|
|
642
|
-
|
|
643
|
-
async function getObject(id) {
|
|
644
|
-
if (existsSync(`${rootDir}tmp/${appName}-data/objects.json`)) {
|
|
645
|
-
const objects = JSON.parse(readFileSync(`${rootDir}tmp/${appName}-data/objects.json`).toString());
|
|
646
|
-
return objects[id];
|
|
647
|
-
} else if (existsSync(`${rootDir}tmp/${appName}-data/objects.jsonl`)) {
|
|
648
|
-
loadJSONLDB();
|
|
649
|
-
const db = new JSONLDB(`${rootDir}tmp/${appName}-data/objects.jsonl`);
|
|
650
|
-
try {
|
|
651
|
-
await db.open();
|
|
652
|
-
} catch (err) {
|
|
653
|
-
if (err.message.includes('Failed to lock DB file')) {
|
|
654
|
-
console.log(`getObject: DB still opened ...`);
|
|
655
|
-
}
|
|
656
|
-
throw err;
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
const obj = db.get(id) || {};
|
|
660
|
-
await db.close();
|
|
661
|
-
return obj;
|
|
662
|
-
} else {
|
|
663
|
-
console.error(`setAdapterConfig: No objects file found in datadir ${rootDir}tmp/${appName}-data/`);
|
|
664
|
-
return null;
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
async function setObject(id, obj) {
|
|
669
|
-
if (existsSync(`${rootDir}tmp/${appName}-data/objects.json`)) {
|
|
670
|
-
const objects = JSON.parse(readFileSync(`${rootDir}tmp/${appName}-data/objects.json`).toString());
|
|
671
|
-
objects[id] = obj;
|
|
672
|
-
} else if (existsSync(`${rootDir}tmp/${appName}-data/objects.jsonl`)) {
|
|
673
|
-
loadJSONLDB();
|
|
674
|
-
const db = new JSONLDB(`${rootDir}tmp/${appName}-data/objects.jsonl`);
|
|
675
|
-
try {
|
|
676
|
-
await db.open();
|
|
677
|
-
} catch (err) {
|
|
678
|
-
if (err.message.includes('Failed to lock DB file')) {
|
|
679
|
-
console.log(`getObject: DB still opened ...`);
|
|
680
|
-
}
|
|
681
|
-
throw err;
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
db.set(id, obj);
|
|
685
|
-
await db.close();
|
|
686
|
-
return obj;
|
|
687
|
-
} else {
|
|
688
|
-
console.error(`setAdapterConfig: No objects file found in datadir ${rootDir}tmp/${appName}-data/`);
|
|
689
|
-
return null;
|
|
690
|
-
}
|
|
691
|
-
}
|
|
692
|
-
|
|
693
|
-
async function getSecret() {
|
|
694
|
-
if (systemConfig) {
|
|
695
|
-
return systemConfig.native.secret;
|
|
696
|
-
}
|
|
697
|
-
systemConfig = await getObject('system.config');
|
|
698
|
-
return systemConfig.native.secret;
|
|
699
|
-
}
|
|
700
|
-
|
|
701
|
-
function encrypt(key, value) {
|
|
702
|
-
let result = '';
|
|
703
|
-
for (let i = 0; i < value.length; ++i) {
|
|
704
|
-
result += String.fromCharCode(key[i % key.length].charCodeAt(0) ^ value.charCodeAt(i));
|
|
705
|
-
}
|
|
706
|
-
return result;
|
|
707
|
-
}
|
|
708
|
-
|
|
709
|
-
function startAdapter(objects, states, callback) {
|
|
710
|
-
startCustomAdapter();
|
|
711
|
-
callback && callback(objects, states);
|
|
712
|
-
}
|
|
713
|
-
|
|
714
|
-
function startCustomAdapter(adapterName, adapterInstance) {
|
|
715
|
-
adapterInstance = adapterInstance || 0;
|
|
716
|
-
const id = `${adapterName || pkg.name.split('.')[1]}.${adapterInstance}`;
|
|
717
|
-
if (adaptersStarted[id]) {
|
|
718
|
-
console.log(`Adapter ${id} already started ...`);
|
|
719
|
-
return;
|
|
720
|
-
}
|
|
721
|
-
adaptersStarted[id] = true;
|
|
722
|
-
console.log(`startAdapter ${id} ...`);
|
|
723
|
-
const _pkg = adapterName ? require(`${rootDir}tmp/node_modules/iobroker.${adapterName}/package.json`) : pkg;
|
|
724
|
-
adapterName = adapterName || pkg.name.split('.')[1];
|
|
725
|
-
if (existsSync(`${rootDir}tmp/node_modules/iobroker.${adapterName}/${_pkg.main || 'main.js'}`)) {
|
|
726
|
-
try {
|
|
727
|
-
if (debug) {
|
|
728
|
-
// start controller
|
|
729
|
-
pids[id] = exec(
|
|
730
|
-
`node node_modules/iobroker.${adapterName}/${_pkg.main || 'main.js'} ${adapterInstance} --debug --console silly`,
|
|
731
|
-
{
|
|
732
|
-
cwd: `${rootDir}tmp`,
|
|
733
|
-
stdio: [0, 1, 2],
|
|
734
|
-
},
|
|
735
|
-
);
|
|
736
|
-
} else {
|
|
737
|
-
// start controller
|
|
738
|
-
pids[id] = fork(
|
|
739
|
-
`node_modules/iobroker.${adapterName}/${_pkg.main || 'main.js'}`,
|
|
740
|
-
[adapterInstance, '--debug ', '--console', 'silly'],
|
|
741
|
-
{
|
|
742
|
-
cwd: `${rootDir}tmp`,
|
|
743
|
-
stdio: [0, 1, 2, 'ipc'],
|
|
744
|
-
},
|
|
745
|
-
);
|
|
746
|
-
}
|
|
747
|
-
pids[id].on('exit', (code, signal) => {
|
|
748
|
-
if (pids[id]) {
|
|
749
|
-
console.log(`child process terminated 1 due to receipt of signal ${signal}`);
|
|
750
|
-
let resolve = pids[id].customResolve;
|
|
751
|
-
if (resolve) {
|
|
752
|
-
delete pids[id].customResolve;
|
|
753
|
-
}
|
|
754
|
-
pids[id] = null;
|
|
755
|
-
resolve && resolve();
|
|
756
|
-
}
|
|
757
|
-
});
|
|
758
|
-
pids[id].on('close', () => {
|
|
759
|
-
if (pids[id]) {
|
|
760
|
-
let resolve = pids[id].customResolve;
|
|
761
|
-
if (resolve) {
|
|
762
|
-
delete pids[id].customResolve;
|
|
763
|
-
}
|
|
764
|
-
pids[id] = null;
|
|
765
|
-
resolve && resolve();
|
|
766
|
-
}
|
|
767
|
-
});
|
|
768
|
-
} catch (error) {
|
|
769
|
-
console.error(JSON.stringify(error));
|
|
770
|
-
}
|
|
771
|
-
} else {
|
|
772
|
-
console.error(`Cannot find: ${rootDir}tmp/node_modules/iobroker.${adapterName}/${_pkg.main || 'main.js'}`);
|
|
773
|
-
}
|
|
774
|
-
}
|
|
775
|
-
|
|
776
|
-
function startController(isStartAdapter, onObjectChange, onStateChange, callback) {
|
|
777
|
-
if (typeof isStartAdapter === 'function') {
|
|
778
|
-
callback = onStateChange;
|
|
779
|
-
onStateChange = onObjectChange;
|
|
780
|
-
onObjectChange = isStartAdapter;
|
|
781
|
-
isStartAdapter = true;
|
|
782
|
-
}
|
|
783
|
-
|
|
784
|
-
if (onStateChange === undefined) {
|
|
785
|
-
callback = onObjectChange;
|
|
786
|
-
onObjectChange = undefined;
|
|
787
|
-
}
|
|
788
|
-
|
|
789
|
-
if (pids.controller) {
|
|
790
|
-
console.error('Controller is already started!');
|
|
791
|
-
} else {
|
|
792
|
-
pids.controller = true;
|
|
793
|
-
|
|
794
|
-
console.log('startController...');
|
|
795
|
-
try {
|
|
796
|
-
const config = require(`${rootDir}tmp/${appName}-data/${appName}.json`);
|
|
797
|
-
|
|
798
|
-
adaptersStarted[`${pkg.name}.0`] = false;
|
|
799
|
-
let isObjectConnected;
|
|
800
|
-
let isStatesConnected;
|
|
801
|
-
|
|
802
|
-
// rootDir + 'tmp/node_modules
|
|
803
|
-
const objPath = require.resolve(`@iobroker/db-objects-${config.objects.type}`, {
|
|
804
|
-
paths: [`${rootDir}tmp/node_modules`, rootDir, `${rootDir}tmp/node_modules/${appName}.js-controller`],
|
|
805
|
-
});
|
|
806
|
-
console.log(`Objects Path: ${objPath}`);
|
|
807
|
-
const Objects = require(objPath).Server;
|
|
808
|
-
objects = new Objects({
|
|
809
|
-
connection: {
|
|
810
|
-
type: config.objects.type,
|
|
811
|
-
host: '127.0.0.1',
|
|
812
|
-
port: 19001,
|
|
813
|
-
user: '',
|
|
814
|
-
pass: '',
|
|
815
|
-
noFileCache: false,
|
|
816
|
-
connectTimeout: 2000,
|
|
817
|
-
},
|
|
818
|
-
logger: {
|
|
819
|
-
silly: msg => console.log(msg),
|
|
820
|
-
debug: msg => console.log(msg),
|
|
821
|
-
info: msg => console.log(msg),
|
|
822
|
-
warn: msg => console.warn(msg),
|
|
823
|
-
error: msg => console.error(msg),
|
|
824
|
-
},
|
|
825
|
-
connected: () => {
|
|
826
|
-
isObjectConnected = true;
|
|
827
|
-
if (isStatesConnected) {
|
|
828
|
-
console.log('startController: started!');
|
|
829
|
-
if (isStartAdapter) {
|
|
830
|
-
startAdapter(objects, states, callback);
|
|
831
|
-
} else {
|
|
832
|
-
if (callback) {
|
|
833
|
-
callback(objects, states);
|
|
834
|
-
callback = null;
|
|
835
|
-
}
|
|
836
|
-
}
|
|
837
|
-
}
|
|
838
|
-
},
|
|
839
|
-
change: onObjectChange,
|
|
840
|
-
});
|
|
841
|
-
|
|
842
|
-
// Just open in memory DB itself
|
|
843
|
-
const statePath = require.resolve(`@iobroker/db-states-${config.states.type}`, {
|
|
844
|
-
paths: [`${rootDir}tmp/node_modules`, rootDir, `${rootDir}tmp/node_modules/${appName}.js-controller`],
|
|
845
|
-
});
|
|
846
|
-
console.log(`States Path: ${statePath}`);
|
|
847
|
-
const States = require(statePath).Server;
|
|
848
|
-
states = new States({
|
|
849
|
-
connection: {
|
|
850
|
-
type: config.states.type,
|
|
851
|
-
host: '127.0.0.1',
|
|
852
|
-
port: 19000,
|
|
853
|
-
options: {
|
|
854
|
-
auth_pass: null,
|
|
855
|
-
retry_max_delay: 15000,
|
|
856
|
-
},
|
|
857
|
-
},
|
|
858
|
-
logger: {
|
|
859
|
-
silly: function (msg) {
|
|
860
|
-
console.log(msg);
|
|
861
|
-
},
|
|
862
|
-
debug: function (msg) {
|
|
863
|
-
console.log(msg);
|
|
864
|
-
},
|
|
865
|
-
info: function (msg) {
|
|
866
|
-
console.log(msg);
|
|
867
|
-
},
|
|
868
|
-
warn: function (msg) {
|
|
869
|
-
console.log(msg);
|
|
870
|
-
},
|
|
871
|
-
error: function (msg) {
|
|
872
|
-
console.log(msg);
|
|
873
|
-
},
|
|
874
|
-
},
|
|
875
|
-
connected: function () {
|
|
876
|
-
isStatesConnected = true;
|
|
877
|
-
if (isObjectConnected) {
|
|
878
|
-
console.log('startController: started!!');
|
|
879
|
-
if (isStartAdapter) {
|
|
880
|
-
startAdapter(objects, states, callback);
|
|
881
|
-
} else {
|
|
882
|
-
if (callback) {
|
|
883
|
-
callback(objects, states);
|
|
884
|
-
callback = null;
|
|
885
|
-
}
|
|
886
|
-
}
|
|
887
|
-
}
|
|
888
|
-
},
|
|
889
|
-
change: onStateChange,
|
|
890
|
-
});
|
|
891
|
-
} catch (err) {
|
|
892
|
-
console.log(err);
|
|
893
|
-
}
|
|
894
|
-
}
|
|
895
|
-
}
|
|
896
|
-
|
|
897
|
-
async function stopAdapter(cb) {
|
|
898
|
-
await stopCustomAdapter();
|
|
899
|
-
cb && cb();
|
|
900
|
-
}
|
|
901
|
-
|
|
902
|
-
function stopCustomAdapter(adapterName, adapterInstance) {
|
|
903
|
-
const id = `${adapterName || pkg.name.split('.')[1]}.${adapterInstance || 0}`;
|
|
904
|
-
if (!pids[id]) {
|
|
905
|
-
console.error(`Adapter instance ${id} is not running!`);
|
|
906
|
-
return Promise.resolve();
|
|
907
|
-
} else {
|
|
908
|
-
adaptersStarted[id] = false;
|
|
909
|
-
return new Promise(resolve => {
|
|
910
|
-
pids[id].customResolve = resolve;
|
|
911
|
-
pids[id].on('exit', (code, signal) => {
|
|
912
|
-
if (pids[id]) {
|
|
913
|
-
console.log(`child process terminated 3 due to receipt of signal ${signal}`);
|
|
914
|
-
delete pids[id].customResolve;
|
|
915
|
-
pids[id] = null;
|
|
916
|
-
resolve();
|
|
917
|
-
}
|
|
918
|
-
});
|
|
919
|
-
|
|
920
|
-
pids[id].on('close', () => {
|
|
921
|
-
if (pids[id]) {
|
|
922
|
-
delete pids[id].customResolve;
|
|
923
|
-
pids[id] = null;
|
|
924
|
-
resolve();
|
|
925
|
-
}
|
|
926
|
-
});
|
|
927
|
-
|
|
928
|
-
pids[id].kill('SIGTERM');
|
|
929
|
-
});
|
|
930
|
-
}
|
|
931
|
-
}
|
|
932
|
-
|
|
933
|
-
function _stopController() {
|
|
934
|
-
if (objects) {
|
|
935
|
-
objects.destroy();
|
|
936
|
-
objects = null;
|
|
937
|
-
}
|
|
938
|
-
if (states) {
|
|
939
|
-
states.destroy();
|
|
940
|
-
states = null;
|
|
941
|
-
}
|
|
942
|
-
}
|
|
943
|
-
|
|
944
|
-
function stopController(cb) {
|
|
945
|
-
const id = `${pkg.name.split('.')[1]}.0`;
|
|
946
|
-
let timeout;
|
|
947
|
-
if (objects) {
|
|
948
|
-
console.log(`Set system.adapter.${id}`);
|
|
949
|
-
objects.setObject(`system.adapter.${id}`, {
|
|
950
|
-
common: {
|
|
951
|
-
enabled: false,
|
|
952
|
-
},
|
|
953
|
-
});
|
|
954
|
-
}
|
|
955
|
-
|
|
956
|
-
stopAdapter(() => {
|
|
957
|
-
if (timeout) {
|
|
958
|
-
clearTimeout(timeout);
|
|
959
|
-
timeout = null;
|
|
960
|
-
}
|
|
961
|
-
|
|
962
|
-
_stopController();
|
|
963
|
-
|
|
964
|
-
if (cb) {
|
|
965
|
-
cb(true);
|
|
966
|
-
cb = null;
|
|
967
|
-
}
|
|
968
|
-
pids.controller = null;
|
|
969
|
-
});
|
|
970
|
-
|
|
971
|
-
timeout = setTimeout(() => {
|
|
972
|
-
timeout = null;
|
|
973
|
-
console.log('child process NOT terminated');
|
|
974
|
-
|
|
975
|
-
_stopController();
|
|
976
|
-
|
|
977
|
-
if (cb) {
|
|
978
|
-
cb(false);
|
|
979
|
-
cb = null;
|
|
980
|
-
}
|
|
981
|
-
pids.controller = null;
|
|
982
|
-
}, 5000);
|
|
983
|
-
}
|
|
984
|
-
|
|
985
|
-
// Set up the adapter
|
|
986
|
-
async function setAdapterConfig(common, native, instance, customAdapterName) {
|
|
987
|
-
const id = `system.adapter.${(customAdapterName || adapterName).split('.').pop()}.${instance || 0}`;
|
|
988
|
-
const obj = (await getObject(id)) || {};
|
|
989
|
-
if (common) {
|
|
990
|
-
obj.common = common;
|
|
991
|
-
}
|
|
992
|
-
if (native) {
|
|
993
|
-
obj.native = native;
|
|
994
|
-
}
|
|
995
|
-
await setObject(id, obj);
|
|
996
|
-
}
|
|
997
|
-
|
|
998
|
-
// Read config of the adapter
|
|
999
|
-
async function getAdapterConfig(instance, customAdapterName) {
|
|
1000
|
-
return getObject(`system.adapter.${(customAdapterName || adapterName).split('.').pop()}.${instance || 0}`);
|
|
1001
|
-
}
|
|
1002
|
-
|
|
1003
|
-
async function setOfflineState(id, state) {
|
|
1004
|
-
if (existsSync(`${rootDir}tmp/${appName}-data/states.json`)) {
|
|
1005
|
-
const states = JSON.parse(readFileSync(`${rootDir}tmp/${appName}-data/states.json`).toString());
|
|
1006
|
-
states[id] = state;
|
|
1007
|
-
writeFileSync(`${rootDir}tmp/${appName}-data/states.json`, JSON.stringify(objects));
|
|
1008
|
-
} else if (existsSync(`${rootDir}tmp/${appName}-data/states.jsonl`)) {
|
|
1009
|
-
loadJSONLDB();
|
|
1010
|
-
const db = new JSONLDB(`${rootDir}tmp/${appName}-data/states.jsonl`);
|
|
1011
|
-
await db.open();
|
|
1012
|
-
|
|
1013
|
-
db.set(id, state);
|
|
1014
|
-
|
|
1015
|
-
await db.close();
|
|
1016
|
-
} else {
|
|
1017
|
-
console.error(`setAdapterConfig: No objects file found in datadir ${rootDir}tmp/${appName}-data/`);
|
|
1018
|
-
}
|
|
1019
|
-
}
|
|
1020
|
-
|
|
1021
|
-
// Read config of the adapter
|
|
1022
|
-
async function getOfflineState(id) {
|
|
1023
|
-
if (existsSync(`${rootDir}tmp/${appName}-data/states.json`)) {
|
|
1024
|
-
const states = JSON.parse(readFileSync(`${rootDir}tmp/${appName}-data/states.json`).toString());
|
|
1025
|
-
return states[id];
|
|
1026
|
-
}
|
|
1027
|
-
if (existsSync(`${rootDir}tmp/${appName}-data/states.jsonl`)) {
|
|
1028
|
-
loadJSONLDB();
|
|
1029
|
-
const db = new JSONLDB(`${rootDir}tmp/${appName}-data/states.jsonl`);
|
|
1030
|
-
await db.open();
|
|
1031
|
-
|
|
1032
|
-
const state = db.get(id);
|
|
1033
|
-
|
|
1034
|
-
await db.close();
|
|
1035
|
-
return state;
|
|
1036
|
-
}
|
|
1037
|
-
console.error(`getAdapterConfig: No objects file found in datadir ${rootDir}tmp/${appName}-data/`);
|
|
1038
|
-
}
|
|
1039
|
-
|
|
1040
|
-
if (typeof module !== undefined && module.parent) {
|
|
1041
|
-
module.exports.getAdapterConfig = getAdapterConfig;
|
|
1042
|
-
module.exports.setAdapterConfig = setAdapterConfig;
|
|
1043
|
-
module.exports.startController = startController;
|
|
1044
|
-
module.exports.stopController = stopController;
|
|
1045
|
-
module.exports.setupController = setupController;
|
|
1046
|
-
module.exports.stopAdapter = stopAdapter;
|
|
1047
|
-
module.exports.startAdapter = startAdapter;
|
|
1048
|
-
module.exports.installAdapter = installAdapter;
|
|
1049
|
-
module.exports.appName = appName;
|
|
1050
|
-
module.exports.adapterName = adapterName;
|
|
1051
|
-
module.exports.getSecret = getSecret;
|
|
1052
|
-
module.exports.encrypt = encrypt;
|
|
1053
|
-
module.exports.setOfflineState = setOfflineState;
|
|
1054
|
-
module.exports.getOfflineState = getOfflineState;
|
|
1055
|
-
module.exports.stopCustomAdapter = stopCustomAdapter;
|
|
1056
|
-
module.exports.startCustomAdapter = startCustomAdapter;
|
|
1057
|
-
module.exports.installCustomAdapter = installCustomAdapter;
|
|
1058
|
-
module.exports.getObject = getObject;
|
|
1059
|
-
module.exports.setObject = setObject;
|
|
1060
|
-
module.exports.setOptions = setOptions;
|
|
1061
|
-
module.exports.initialize = initialize;
|
|
1062
|
-
}
|