-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.js
49 lines (40 loc) · 1.4 KB
/
node.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
module.exports = (RED) => {
/**
* @namespace {import('node-red__registry')}
* @member {NodeAPI} RED
*/
function Node(config) {
RED.nodes.createNode(this, config);
this.status({ fill: 'red', shape: 'dot', text: 'offline' });
/**
* @type {Cloud & Node & EventEmitter}
*/
const cloud = RED.nodes.getNode(config.cloud);
if (cloud != null) {
// Initialize Mi Home connection
cloud.init().then(() => {
// Get protocol object and required data
const { connected, country, mihome: protocol /* aqara: protocol */ } = cloud;
if (connected) {
this.status({ fill: 'green', shape: 'dot', text: 'online' });
}
this.on('input', async (msg) => {
if (connected && msg.payload === true) {
// Call some protocol function
const result = await protocol.miioCall('did', 'method', {}, country);
// Or initialize device and call his method,
// see https://github.com/maxinminax/node-mihome#create-device for details
this.send({ payload: result });
}
});
});
cloud.on('connect', async () => {
this.status({ fill: 'green', shape: 'dot', text: 'online' });
});
cloud.on('disconnect', () => {
this.status({ fill: 'red', shape: 'dot', text: 'offline' });
});
}
}
RED.nodes.registerType('mihome-node', Node);
};