|
11 | 11 |
|
12 | 12 | const HarmonyHubDiscover = require('@harmonyhub/discover').Explorer;
|
13 | 13 | const utils = require('@iobroker/adapter-core');
|
14 |
| -const adapter = new utils.Adapter('harmony'); |
15 | 14 | const HarmonyWS = require('harmonyhubws');
|
16 | 15 | const hubs = {};
|
| 16 | +let adapter; |
17 | 17 | let discover;
|
18 |
| -const FORBIDDEN_CHARS = /[\]\[*,;'"`<>\\? ]/g; |
| 18 | +const FORBIDDEN_CHARS = /[\][*,;'"`<>\\? ]/g; |
19 | 19 | const fixId = (id) => id.replace(FORBIDDEN_CHARS, '_');
|
20 | 20 | let manualDiscoverHubs;
|
21 | 21 | let subnet;
|
22 | 22 | let discoverInterval;
|
23 | 23 |
|
24 |
| -adapter.on('stateChange', (id, state) => { |
25 |
| - if (!id || !state || state.ack) { |
26 |
| - return; |
27 |
| - } |
28 |
| - const hub = id.split('.')[2]; |
29 |
| - if (!hubs[hub]) return; |
30 |
| - const semaphore = hubs[hub].semaphore; |
31 |
| - if (semaphore === undefined) { |
32 |
| - adapter.log.warn('state changed in offline hub'); |
33 |
| - return; |
34 |
| - } |
35 |
| - if (semaphore.current > 0) { |
36 |
| - adapter.log.info('hub busy, stateChange delayed: ' + id + '=' + state.val); |
37 |
| - } |
38 |
| - semaphore.take(() => { |
39 |
| - setBlocked(hub, true); |
40 |
| - processStateChange(hub, id, state, () => { |
41 |
| - if (semaphore.current === 1) { |
42 |
| - setBlocked(hub, false); |
43 |
| - } |
44 |
| - semaphore.leave(); |
| 24 | + |
| 25 | +function startAdapter(options) { |
| 26 | + options = options || {}; |
| 27 | + Object.assign(options, { |
| 28 | + name: 'harmony' |
| 29 | + }); |
| 30 | + adapter = new utils.Adapter(options); |
| 31 | + |
| 32 | + adapter.on('stateChange', (id, state) => { |
| 33 | + if (!id || !state || state.ack) { |
| 34 | + return; |
| 35 | + } |
| 36 | + const hub = id.split('.')[2]; |
| 37 | + if (!hubs[hub]) return; |
| 38 | + const semaphore = hubs[hub].semaphore; |
| 39 | + if (semaphore === undefined) { |
| 40 | + adapter.log.warn('state changed in offline hub'); |
| 41 | + return; |
| 42 | + } |
| 43 | + if (semaphore.current > 0) { |
| 44 | + adapter.log.info('hub busy, stateChange delayed: ' + id + '=' + state.val); |
| 45 | + } |
| 46 | + semaphore.take(() => { |
| 47 | + setBlocked(hub, true); |
| 48 | + processStateChange(hub, id, state, () => { |
| 49 | + if (semaphore.current === 1) { |
| 50 | + setBlocked(hub, false); |
| 51 | + } |
| 52 | + semaphore.leave(); |
| 53 | + }); |
45 | 54 | });
|
46 | 55 | });
|
47 |
| -}); |
| 56 | + |
| 57 | + // callback has to be called under any circumstances |
| 58 | + adapter.on('unload', callback => { |
| 59 | + try { |
| 60 | + adapter.log.info('[END] Terminating'); |
| 61 | + if (discover) { |
| 62 | + discover.stop(); |
| 63 | + } // endIf |
| 64 | + discover = null; |
| 65 | + for (const hub in Object.keys(hubs)) { |
| 66 | + clientStop(hub); |
| 67 | + } // endFor |
| 68 | + callback(); |
| 69 | + } catch (e) { |
| 70 | + callback(); |
| 71 | + } // endTryCatch |
| 72 | + }); |
| 73 | + |
| 74 | + adapter.on('ready', () => { |
| 75 | + main(); |
| 76 | + }); |
| 77 | + |
| 78 | + return adapter; |
| 79 | +} // endStartAdapter |
48 | 80 |
|
49 | 81 | function processStateChange(hub, id, state, callback) {
|
50 | 82 | const tmp = id.split('.');
|
@@ -139,27 +171,6 @@ function switchActivity(hub, activityLabel, value, callback) {
|
139 | 171 | }
|
140 | 172 | }
|
141 | 173 |
|
142 |
| -// callback has to be called under any circumstances |
143 |
| -adapter.on('unload', callback => { |
144 |
| - try { |
145 |
| - adapter.log.info('[END] Terminating'); |
146 |
| - if (discover) { |
147 |
| - discover.stop(); |
148 |
| - } // endIf |
149 |
| - discover = null; |
150 |
| - for (const hub in Object.keys(hubs)) { |
151 |
| - clientStop(hub); |
152 |
| - } // endFor |
153 |
| - callback(); |
154 |
| - } catch (e) { |
155 |
| - callback(); |
156 |
| - } // endTryCatch |
157 |
| -}); |
158 |
| - |
159 |
| -adapter.on('ready', () => { |
160 |
| - main(); |
161 |
| -}); |
162 |
| - |
163 | 174 | function main() {
|
164 | 175 | manualDiscoverHubs = adapter.config.devices || [];
|
165 | 176 | subnet = adapter.config.subnet || '255.255.255.255';
|
@@ -574,3 +585,11 @@ function setConnected(hub, bool) {
|
574 | 585 | adapter.setState(hub + '.hubConnected', {val: bool, ack: true});
|
575 | 586 | }
|
576 | 587 | }
|
| 588 | + |
| 589 | +// If started as allInOne/compact mode => return function to create instance |
| 590 | +if (module && module.parent) { |
| 591 | + module.exports = startAdapter; |
| 592 | +} else { |
| 593 | + // or start the instance directly |
| 594 | + startAdapter(); |
| 595 | +} |
0 commit comments