|
2 | 2 | const fs = require('node:fs'); |
3 | 3 | const getDate = require('../tools').getDate; |
4 | 4 | const path = require('node:path'); |
| 5 | +const compress = require('../targz').compress; |
5 | 6 |
|
6 | | -function command(options, log, callback) { |
7 | | - let zigbeeInst = []; |
| 7 | +async function command(options, log, callback) { |
| 8 | + const zigbeeInst = []; |
8 | 9 |
|
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; |
10 | 15 |
|
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; |
13 | 19 |
|
14 | | - if (fs.existsSync(pth)) { |
15 | | - let nameSuffix; |
| 20 | + // Determine suffix for the filename |
| 21 | + let nameSuffix = ''; |
16 | 22 | if (options.hostType === 'Slave') { |
17 | | - nameSuffix = options.slaveSuffix ? options.slaveSuffix : ''; |
| 23 | + nameSuffix = options.slaveSuffix || ''; |
18 | 24 | } else { |
19 | | - nameSuffix = options.nameSuffix ? options.nameSuffix : ''; |
| 25 | + nameSuffix = options.nameSuffix || ''; |
20 | 26 | } |
21 | 27 |
|
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 | + ); |
23 | 33 |
|
24 | 34 | options.context.fileNames.push(fileName); |
25 | 35 |
|
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' |
32 | 43 | } |
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}`); |
41 | 55 | } |
42 | | - } else { |
43 | | - options.context.types.push(`zigbee.${i}`); |
44 | | - options.context.done.push(`zigbee.${i}`); |
45 | | - } |
| 56 | + resolve(); |
| 57 | + }); |
46 | 58 | }); |
| 59 | + |
47 | 60 | 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'); |
54 | 61 | } |
| 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); |
55 | 75 | } |
56 | 76 | } |
57 | 77 |
|
|
0 commit comments