-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
50 lines (39 loc) · 1.44 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* jshint -W097 */// jshint strict:false, esnext:true
/*jslint node: true */
"use strict";
const PilightWebsocket = require('./lib/PilightWebsocket');
const ioBrokerUtils = require(__dirname + '/lib/utils'); // Get common adapter utils
const adapter = ioBrokerUtils.adapter('pilight');
let pilightWebsocket = null;
// is called when adapter shuts down - callback has to be called under any circumstances!
adapter.on('unload', function (callback) {
try {
adapter.log.info('cleaned everything up...');
if (pilightWebsocket) {
pilightWebsocket.c
}
callback();
} catch (e) {
callback();
}
});
// is called if a subscribed state changes
adapter.on('stateChange', function (id, state) {
// Warning, state can be null if it was deleted
// adapter.log.info('stateChange ' + id + ' ' + JSON.stringify(state));
// id = pilight.0.Plug1.state
let deviceId = id.split('.')[2];
// you can use the ack flag to detect if it is status (true) or command (false)
if (state && !state.ack) {
adapter.log.info('ack is not set!');
pilightWebsocket.setPowerState(deviceId, state.val, function (err) {
if (err) adapter.log(err);
});
}
});
// is called when databases are connected and adapter received configuration.
// start here!
adapter.on('ready', function () {
pilightWebsocket = new PilightWebsocket(adapter);
adapter.subscribeStates('*');
});