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