-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn4m-yeelight.js
121 lines (102 loc) · 2 KB
/
n4m-yeelight.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const Max = require('max-api')
/** Require the `Bulb` type from the yeelight package */
const { Bulb } = require('yeelight.io')
let bulb = null
/**
* Respond to the `connect` message from Max
*/
Max.addHandler('connect', (ip) => {
/** If bulb hasn'e been instantiated */
if (!bulb) {
bulb = new Bulb(ip)
/** Attach listener to the `connected` event */
bulb.on('connected', (light) => {
Max.post(`Connected to bulb @${light.ip}`)
})
/** Attach listener to the `disconnected` event */
bulb.on('disconnected', (light) => {
Max.post(`Disconnected from bulb @${light.ip}`)
})
/** Attach listener to the `error` event */
bulb.on('error', (light, err) => {
Max.post(`Error "${err.message}" from bulb @${light.ip}`)
})
/** Instantiate bulb */
bulb.connect()
}
})
/**
* Respond to the `disconnect` message from Max
*/
Max.addHandler('disconnect', () => {
/** If there is a bulb */
if (bulb) {
bulb.disconnect()
bulb = null
}
})
/**
* Respond to the `on` message from Max
*/
Max.addHandler('on', () => {
console.log("hi")
if (bulb) {
bulb.onn()
}
})
/**
* Respond to the `off` message from Max
*/
Max.addHandler('off', () => {
if (bulb) {
bulb.off()
}
})
/**
* Respond to the `toggle` message from Max
*/
Max.addHandler('toggle', () => {
try{
if (bulb) {
bulb.toggle()
}
} catch(err){
Max.post(err)
}
})
/**
const { toggle } = require('yeelight.io');
toggle('192.168.86.209', (err) => {
if (err) {
console.error(`error [${err.message}] occured on 192.168.10.227`);
} else {
console.log('toggle 192.168.1.227 success');
}
});
if (bulb){
bulb.toggle()
}
*/
/**
* Respond to the `brightness` message from Max
*/
Max.addHandler('brightness', (value) => {
Max.post(value)
if (bulb) {
Max.post(value)
bulb.brightness(value)
}
})
/**
* Respond to the `color` message from Max
*/
Max.addHandler('color', (r, g, b) => {
Max.post('r:'+ r)
Max.post('g:'+ g)
Max.post('b:'+ b)
Max.post(g)
Max.post(b)
if (bulb) {
bulb.color(r, g, b)
}
})