-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.js
99 lines (87 loc) · 3.56 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* ioBroker sonoff Adapter
*
* (c) 2017-2024 bluefox
*
* MIT License
*/
'use strict';
const utils = require('@iobroker/adapter-core'); // Get common adapter utils
const adapterName = require('./package.json').name.split('.').pop();
const Server = require('./lib/server');
let server = null;
let adapter;
function decrypt(key, value) {
let result = '';
for (let i = 0; i < value.length; ++i) {
result += String.fromCharCode(key[i % key.length].charCodeAt(0) ^ value.charCodeAt(i));
}
return result;
}
function startAdapter(options) {
options = options || {};
Object.assign(options, { name: adapterName });
adapter = new utils.Adapter(options);
adapter.on('ready', () => {
// delete pass if a password set
if (adapter.config.password && adapter.config.pass) {
// delete adapter.config.pass
adapter.getForeignObject(`system.adapter.${adapter.namespace}`, (err, config) => {
delete config.native.pass;
adapter.setForeignObject(config._id, config);
});
} else if (!adapter.config.password && adapter.config.pass) {
// migrate
adapter.getForeignObject('system.config', (err, systemConfig) => {
adapter.getForeignObject(`system.adapter.${adapter.namespace}`, (err, config) => {
if (systemConfig && systemConfig.native && systemConfig.native.secret) {
// noinspection JSUnresolvedVariable
config.native.password = decrypt(systemConfig.native.secret, adapter.config.pass);
} else {
// noinspection JSUnresolvedVariable
config.native.password = decrypt('Zgfr56gFe87jJOM', adapter.config.pass);
}
delete config.native.pass;
adapter.setForeignObject(config._id, config);
});
});
} else {
main();
}
});
adapter.on('unload', cb => {
if (server) {
server.destroy(cb);
server = null;
} else if (typeof cb === 'function') {
cb();
}
});
// called if the subscribed state changes itself
adapter.on('stateChange', (id, state) => {
adapter.log.debug(`stateChange ${id}: ${JSON.stringify(state)}`);
// you can use the ack flag to detect if state is desired or acknowledged
state && !state.ack && server && server.onStateChange(id, state);
});
return adapter;
}
function main() {
adapter.config.TELE_SENSOR = adapter.config.TELE_SENSOR === true || adapter.config.TELE_SENSOR === 'true';
adapter.config.TELE_STATE = adapter.config.TELE_STATE === true || adapter.config.TELE_STATE === 'true';
adapter.config.STAT_RESULT = adapter.config.STAT_RESULT === true || adapter.config.STAT_RESULT === 'true';
adapter.config.OBJ_TREE = adapter.config.OBJ_TREE === true || adapter.config.OBJ_TREE === 'true';
// subscribe for all own variables
adapter.subscribeStates('*');
// read all states and set alive to false
adapter.getStatesOf('', '', (err, states) =>
states && states.length && states.forEach(state =>
state._id.match(/\.alive$/) && adapter.setForeignState(state._id, false, true)));
server = new Server(adapter);
}
// If started as allInOne/compact mode => return function to create instance
if (module && module.parent) {
module.exports = startAdapter;
} else {
// or start the instance directly
startAdapter();
}