-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_miio.js
52 lines (37 loc) · 1.3 KB
/
test_miio.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
var keypress = require('keypress');
// make `process.stdin` begin emitting "keypress" events
keypress(process.stdin);
const miio = require('miio');
async function doIt() {
// Resolve a device, specifying the token (see below for how to get the token)
const device = await miio.device({ address: '192.168.1.213', token: '35f04e7163788ce50c92e5eee0bb9d22' })
// console.log('Connected to', device)
// console.log("Children: ", device.children())
// console.log("Done");
const children = device.children();
for (const child of children) {
console.log("Child: ", child.miioModel);
// console.dir(child);
if (child.miioModel == 'lumi.switch') {
console.log("Configuring switch...");
child.on('action', event => console.log('Action', event.action, 'with data', event.data));
}
if (child.miioModel == 'lumi.plug') {
console.log("Found the plug!");
let isOn = await child.power();
console.log("Is on: " + isOn);
process.stdin.on('keypress', function (ch, key) {
console.log('got "keypress"', key);
if (key && key.name == 'enter') {
isOn = !isOn
child.setPower(isOn);
// process.stdin.pause();
}
});
}
}
// device.children().forEach(child => {
// console.log("Found child: ", child)
// })
}
doIt();