homebridge-openwrt-control 0.3.3 → 0.3.5
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/index.js +123 -91
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { join } from 'path';
|
|
2
|
-
import { mkdirSync
|
|
2
|
+
import { mkdirSync } from 'fs';
|
|
3
3
|
import OpenWrt from './src/openwrt.js';
|
|
4
4
|
import Router from './src/router.js';
|
|
5
5
|
import ImpulseGenerator from './src/impulsegenerator.js';
|
|
@@ -7,14 +7,13 @@ import { PluginName, PlatformName } from './src/constants.js';
|
|
|
7
7
|
|
|
8
8
|
class OpenWrtPlatform {
|
|
9
9
|
constructor(log, config, api) {
|
|
10
|
-
// only load if configured
|
|
11
10
|
if (!config || !Array.isArray(config.devices)) {
|
|
12
11
|
log.warn(`No configuration found for ${PluginName}`);
|
|
13
12
|
return;
|
|
14
13
|
}
|
|
14
|
+
|
|
15
15
|
this.accessories = [];
|
|
16
16
|
|
|
17
|
-
//check if prefs directory exist
|
|
18
17
|
const prefDir = join(api.user.storagePath(), 'openWrt');
|
|
19
18
|
try {
|
|
20
19
|
mkdirSync(prefDir, { recursive: true });
|
|
@@ -23,100 +22,133 @@ class OpenWrtPlatform {
|
|
|
23
22
|
return;
|
|
24
23
|
}
|
|
25
24
|
|
|
26
|
-
api.on('didFinishLaunching',
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (logLevel.debug) {
|
|
46
|
-
log.info(`Device: ${host} ${name}, did finish launching.`);
|
|
47
|
-
const safeConfig = {
|
|
48
|
-
...device,
|
|
49
|
-
auth: {
|
|
50
|
-
...device.auth,
|
|
51
|
-
passwd: 'removed',
|
|
52
|
-
},
|
|
53
|
-
mqtt: {
|
|
54
|
-
auth: {
|
|
55
|
-
...device.mqtt?.auth,
|
|
56
|
-
passwd: 'removed',
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
};
|
|
60
|
-
log.info(`Device: ${host} ${name}, Config: ${JSON.stringify(safeConfig, null, 2)}`);
|
|
61
|
-
}
|
|
25
|
+
api.on('didFinishLaunching', () => {
|
|
26
|
+
// Each device is set up independently — a failure in one does not
|
|
27
|
+
// block the others. Promise.allSettled runs all in parallel.
|
|
28
|
+
Promise.allSettled(
|
|
29
|
+
config.devices.map(device =>
|
|
30
|
+
this.setupDevice(device, prefDir, log, api)
|
|
31
|
+
)
|
|
32
|
+
).then(results => {
|
|
33
|
+
results.forEach((result, i) => {
|
|
34
|
+
if (result.status === 'rejected') {
|
|
35
|
+
log.error(`Device[${i}] setup error: ${result.reason?.message ?? result.reason}`);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// ── Per-device setup ──────────────────────────────────────────────────────
|
|
62
43
|
|
|
44
|
+
async setupDevice(device, prefDir, log, api) {
|
|
45
|
+
const { name, host, displayType } = device;
|
|
46
|
+
|
|
47
|
+
if (!name || !host || !displayType) {
|
|
48
|
+
const reason = !name ? 'name missing'
|
|
49
|
+
: !host ? 'host missing'
|
|
50
|
+
: 'display type disabled';
|
|
51
|
+
log.warn(`Device ${host ?? '(no host)'} ${name ?? '(unnamed)'}: ${reason} — will not be published in the Home app`);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const refreshInterval = (device.refreshInterval ?? 5) * 1000;
|
|
56
|
+
|
|
57
|
+
const logLevel = {
|
|
58
|
+
devInfo: device.log?.deviceInfo,
|
|
59
|
+
success: device.log?.success,
|
|
60
|
+
info: device.log?.info,
|
|
61
|
+
warn: device.log?.warn,
|
|
62
|
+
error: device.log?.error,
|
|
63
|
+
debug: device.log?.debug,
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
if (logLevel.debug) {
|
|
67
|
+
log.info(`Device: ${host} ${name}, did finish launching`);
|
|
68
|
+
const safeConfig = {
|
|
69
|
+
...device,
|
|
70
|
+
auth: {
|
|
71
|
+
...device.auth,
|
|
72
|
+
passwd: 'removed',
|
|
73
|
+
},
|
|
74
|
+
mqtt: {
|
|
75
|
+
auth: {
|
|
76
|
+
...device.mqtt?.auth,
|
|
77
|
+
passwd: 'removed',
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
log.info(`Device: ${host} ${name}, config: ${JSON.stringify(safeConfig, null, 2)}`);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// The startup impulse generator retries the full connect cycle
|
|
85
|
+
// every 120 s until it succeeds, then hands off to the openWrt class
|
|
86
|
+
// impulse generator and stops itself.
|
|
87
|
+
const impulseGenerator = new ImpulseGenerator()
|
|
88
|
+
.on('start', async () => {
|
|
63
89
|
try {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
.on('success', msg => logLevel.success && log.success(`Device: ${host}, ${msg}`))
|
|
70
|
-
.on('info', msg => log.info(`Device: ${host} ${name}, ${msg}`))
|
|
71
|
-
.on('debug', msg => log.info(`Device: ${host} ${name}, debug: ${msg}`))
|
|
72
|
-
.on('warn', msg => log.warn(`Device: ${host} ${name}, ${msg}`))
|
|
73
|
-
.on('error', msg => log.error(`Device: ${host} ${name}, ${msg}`));
|
|
74
|
-
|
|
75
|
-
const openWrtInfo = await openWrt.connect();
|
|
76
|
-
if (!openWrtInfo.state) {
|
|
77
|
-
if (logLevel.warn) log.warn(`Device: ${host} ${name}, ${openWrtInfo.info}`);
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
if (logLevel.success) log.success(`Device: ${host} ${name}, ${openWrtInfo.info}`);
|
|
81
|
-
|
|
82
|
-
// start openwrt impulse generator
|
|
83
|
-
await openWrt.impulseGenerator.state(true, [{ name: 'connect', sampling: refreshInterval }], false);
|
|
84
|
-
|
|
85
|
-
const router = new Router(api, device, openWrt, openWrtInfo)
|
|
86
|
-
.on('devInfo', msg => logLevel.devInfo && log.info(msg))
|
|
87
|
-
.on('success', msg => logLevel.success && log.success(`Device: ${host} ${name}, ${msg}`))
|
|
88
|
-
.on('info', msg => log.info(`Device: ${host} ${name}, ${msg}`))
|
|
89
|
-
.on('debug', msg => log.info(`Device: ${host} ${name}, debug: ${msg}`))
|
|
90
|
-
.on('warn', msg => log.warn(`Device: ${host} ${name}, ${msg}`))
|
|
91
|
-
.on('error', msg => log.error(`Device: ${host} ${name}, ${msg}`));
|
|
92
|
-
|
|
93
|
-
const accessory = await router.start();
|
|
94
|
-
if (accessory) {
|
|
95
|
-
api.publishExternalAccessories(PluginName, [accessory]);
|
|
96
|
-
if (logLevel.success) log.success(`Device: ${host} ${name}, Published as external accessory.`);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// stop accessory impulse generator
|
|
100
|
-
await impulseGenerator.state(false);
|
|
101
|
-
} catch (error) {
|
|
102
|
-
if (logLevel.error) log.error(`Device: ${host} ${name}, Start impulse generator error: ${error.message ?? error}, trying again.`);
|
|
103
|
-
}
|
|
104
|
-
})
|
|
105
|
-
.on('state', (state) => {
|
|
106
|
-
if (logLevel.debug) log.info(`Device: ${host} ${name}, Start impulse generator ${state ? 'started' : 'stopped'}.`);
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
// start accessory impulse generator
|
|
110
|
-
await impulseGenerator.state(true, [{ name: 'start', sampling: 120000 }]);
|
|
90
|
+
await this.startDevice(
|
|
91
|
+
device, name, host,
|
|
92
|
+
refreshInterval, logLevel,
|
|
93
|
+
log, api, impulseGenerator
|
|
94
|
+
);
|
|
111
95
|
} catch (error) {
|
|
112
|
-
if (logLevel.error) log.error(`Device: ${host} ${name},
|
|
96
|
+
if (logLevel.error) log.error(`Device: ${host} ${name}, Start impulse generator error: ${error.message ?? error}, trying again.`);
|
|
113
97
|
}
|
|
98
|
+
})
|
|
99
|
+
.on('state', (state) => {
|
|
100
|
+
if (logLevel.debug) log.info(`Device: ${host} ${name}, Start impulse generator ${state ? 'started' : 'stopped'}.`);
|
|
101
|
+
});
|
|
114
102
|
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
});
|
|
103
|
+
await impulseGenerator.state(true, [{ name: 'start', sampling: 120_000 }]);
|
|
118
104
|
}
|
|
119
105
|
|
|
106
|
+
// ── Connect and register accessory for one device ─────────────────────────
|
|
107
|
+
|
|
108
|
+
async startDevice(device, name, host, refreshInterval, logLevel, log, api, impulseGenerator) {
|
|
109
|
+
const openWrt = new OpenWrt(device)
|
|
110
|
+
.on('success', (msg) => logLevel.success && log.success(`Device: ${host}, ${msg}`))
|
|
111
|
+
.on('info', (msg) => log.info(`Device: ${host} ${name}, ${msg}`))
|
|
112
|
+
.on('debug', (msg) => log.info(`Device: ${host} ${name}, debug: ${msg}`))
|
|
113
|
+
.on('warn', (msg) => log.warn(`Device: ${host} ${name}, ${msg}`))
|
|
114
|
+
.on('error', (msg) => log.error(`Device: ${host} ${name}, ${msg}`));
|
|
115
|
+
|
|
116
|
+
// Connect
|
|
117
|
+
const openWrtInfo = await openWrt.connect();
|
|
118
|
+
if (!openWrtInfo.state) {
|
|
119
|
+
if (logLevel.warn) log.warn(`Device: ${host} ${name}, ${openWrtInfo.info}`);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (logLevel.success) log.success(`Device: ${host} ${name}, ${openWrtInfo.info}`);
|
|
123
|
+
|
|
124
|
+
// Register accessory
|
|
125
|
+
await this.registerDevice({ device, name, host, openWrt, openWrtInfo, refreshInterval, logLevel, log, api, impulseGenerator });
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// ── Register a single device as a Homebridge accessory ───────────────────
|
|
129
|
+
|
|
130
|
+
async registerDevice({ device, name, host, openWrt, openWrtInfo, refreshInterval, logLevel, log, api, impulseGenerator }) {
|
|
131
|
+
const router = new Router(api, device, openWrt, openWrtInfo)
|
|
132
|
+
.on('devInfo', (msg) => logLevel.devInfo && log.info(msg))
|
|
133
|
+
.on('success', (msg) => logLevel.success && log.success(`Device: ${host} ${name}, ${msg}`))
|
|
134
|
+
.on('info', (msg) => log.info(`Device: ${host} ${name}, ${msg}`))
|
|
135
|
+
.on('debug', (msg) => log.info(`Device: ${host} ${name}, debug: ${msg}`))
|
|
136
|
+
.on('warn', (msg) => log.warn(`Device: ${host} ${name}, ${msg}`))
|
|
137
|
+
.on('error', (msg) => log.error(`Device: ${host} ${name}, ${msg}`));
|
|
138
|
+
|
|
139
|
+
const accessory = await router.start();
|
|
140
|
+
if (accessory) {
|
|
141
|
+
api.publishExternalAccessories(PluginName, [accessory]);
|
|
142
|
+
if (logLevel.success) log.success(`Device: ${host} ${name}, Published as external accessory.`);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Stop startup generator and hand off to the openWrt class generator
|
|
146
|
+
await impulseGenerator.state(false);
|
|
147
|
+
await openWrt.impulseGenerator.state(true, [{ name: 'connect', sampling: refreshInterval }], false);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// ── Homebridge accessory cache ────────────────────────────────────────────
|
|
151
|
+
|
|
120
152
|
configureAccessory(accessory) {
|
|
121
153
|
this.accessories.push(accessory);
|
|
122
154
|
}
|
|
@@ -124,4 +156,4 @@ class OpenWrtPlatform {
|
|
|
124
156
|
|
|
125
157
|
export default (api) => {
|
|
126
158
|
api.registerPlatform(PluginName, PlatformName, OpenWrtPlatform);
|
|
127
|
-
}
|
|
159
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"displayName": "OpenWrt Control",
|
|
3
3
|
"name": "homebridge-openwrt-control",
|
|
4
|
-
"version": "0.3.
|
|
4
|
+
"version": "0.3.5",
|
|
5
5
|
"description": "Homebridge plugin to control OpenWrt flashed devices.",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "grzegorz914",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"mqtt": "^5.15.1",
|
|
38
|
-
"axios": "^1.
|
|
38
|
+
"axios": "^1.15.0",
|
|
39
39
|
"express": "^5.2.1"
|
|
40
40
|
},
|
|
41
41
|
"keywords": [
|