Skip to content

Commit 589a0fe

Browse files
committed
(simatec) Fix Zigbee Backup
1 parent 28734ef commit 589a0fe

File tree

1 file changed

+54
-34
lines changed

1 file changed

+54
-34
lines changed

lib/scripts/34-zigbee.js

Lines changed: 54 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,76 @@
22
const fs = require('node:fs');
33
const getDate = require('../tools').getDate;
44
const path = require('node:path');
5+
const compress = require('../targz').compress;
56

6-
function command(options, log, callback) {
7-
let zigbeeInst = [];
7+
async function command(options, log, callback) {
8+
const zigbeeInst = [];
89

9-
const compress = require('../targz').compress;
10+
try {
11+
for (let i = 0; i <= 10; i++) {
12+
// Check if zigbee adapter instance exists
13+
const obj = await options.adapter.getForeignObjectAsync(`system.adapter.zigbee.${i}`);
14+
if (!obj) continue;
1015

11-
for (let i = 0; i <= 5; i++) {
12-
let pth = path.join(options.path, `zigbee_${i}`);
16+
// Check if corresponding folder exists
17+
const pth = path.join(options.path, `zigbee_${i}`);
18+
if (!fs.existsSync(pth)) continue;
1319

14-
if (fs.existsSync(pth)) {
15-
let nameSuffix;
20+
// Determine suffix for the filename
21+
let nameSuffix = '';
1622
if (options.hostType === 'Slave') {
17-
nameSuffix = options.slaveSuffix ? options.slaveSuffix : '';
23+
nameSuffix = options.slaveSuffix || '';
1824
} else {
19-
nameSuffix = options.nameSuffix ? options.nameSuffix : '';
25+
nameSuffix = options.nameSuffix || '';
2026
}
2127

22-
const fileName = path.join(options.backupDir, `zigbee.${i}_${getDate()}${nameSuffix ? `_${nameSuffix}` : ''}_backupiobroker.tar.gz`);
28+
// Construct backup filename
29+
const fileName = path.join(
30+
options.backupDir,
31+
`zigbee.${i}_${getDate()}${nameSuffix ? `_${nameSuffix}` : ''}_backupiobroker.tar.gz`
32+
);
2333

2434
options.context.fileNames.push(fileName);
2535

26-
compress({
27-
src: pth,
28-
dest: fileName,
29-
tar: {
30-
ignore: function (name) {
31-
return path.extname(name) === '.gz' // ignore .tar.gz files when packing
36+
// Run compression and wait for it to finish
37+
await new Promise((resolve) => {
38+
compress({
39+
src: pth,
40+
dest: fileName,
41+
tar: {
42+
ignore: (name) => path.extname(name) === '.gz'
3243
}
33-
}
34-
}, (err, stdout, stderr) => {
35-
if (err) {
36-
options.context.errors.zigbee = err.toString();
37-
stderr && log.error(stderr);
38-
if (callback) {
39-
callback(err, stderr);
40-
callback = null;
44+
}, (err, stdout, stderr) => {
45+
if (err) {
46+
options.context.errors.zigbee = err.toString();
47+
if (stderr) log.error(stderr);
48+
if (callback) {
49+
callback(err, stderr);
50+
callback = null;
51+
}
52+
} else {
53+
options.context.types.push(`zigbee.${i}`);
54+
options.context.done.push(`zigbee.${i}`);
4155
}
42-
} else {
43-
options.context.types.push(`zigbee.${i}`);
44-
options.context.done.push(`zigbee.${i}`);
45-
}
56+
resolve();
57+
});
4658
});
59+
4760
zigbeeInst.push(`zigbee.${i}`);
48-
if (i === 5) {
49-
zigbeeInst.length ? log.debug(`found zigbee database: ${zigbeeInst}`) : log.warn('no zigbee database found!!');
50-
}
51-
} else if (!fs.existsSync(pth) && i === 5) {
52-
zigbeeInst.length ? log.debug(`found zigbee database: ${zigbeeInst}`) : log.warn('no zigbee database found!!');
53-
callback && callback(null, 'done');
5461
}
62+
63+
// Log summary
64+
if (zigbeeInst.length) {
65+
log.debug(`Found zigbee databases: ${zigbeeInst.join(', ')}`);
66+
} else {
67+
log.warn('No zigbee databases found!');
68+
}
69+
70+
// Final callback
71+
if (callback) callback(null, 'done');
72+
} catch (err) {
73+
log.error(`Error during zigbee backup: ${err.message}`);
74+
if (callback) callback(err);
5575
}
5676
}
5777

0 commit comments

Comments
 (0)