-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyeelight.js
151 lines (131 loc) · 3.98 KB
/
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const EventEmitter = require('events');
const net = require('net');
const _ = require('lodash');
const DEFAULT_DURATION = 300;
class Yeelight extends EventEmitter {
constructor(ip, port = 55443) {
super();
this.ip = ip;
this.port = port;
this.state = {};
this.counter = 1;
this.connect();
process.on('exit', () => {
this.client.end();
});
}
connect() {
this.client = new net.Socket();
this.client
.setKeepAlive(true)
.connect(this.port, this.ip, () => this.getState())
.on('data', (data) => {
let json;
try {
json = JSON.parse(data.toString());
} catch (e) {
json = {};
}
const { id, result, error } = json;
if (id) {
this.emit(`req${id}`, result);
}
if (error) {
console.error(id, error);
}
})
.on('error', (error) => {
if (error.code === 'ECONNRESET') {
setTimeout(() => this.connect(), 1000 * 5); // try connecting again after 5 seconds
} else {
console.error(this.ip, error);
process.exit(1);
}
})
.on('close', () => {
clearInterval(this._interval);
})
;
this._interval = setInterval(() => this.getState(), 1000 * 30);
}
getCounter() {
if (this.counter > 10000) {
this.counter = 1;
}
return this.counter++;
}
async send(method, ...params) {
const id = this.getCounter();
const cmd = { id, method, params };
this.client.write(`${JSON.stringify(cmd)}\r\n`);
return await new Promise((resolve) => this.once(`req${id}`, (result) => resolve(result)));
}
setScene(prop, ...params) {
if (prop === 'color') {
const [ rgb, bright ] = params;
this.state.rgb = rgb;
this.state.bright = bright;
} else if (prop === 'hsv') {
const [ hue, sat, bright ] = params;
this.state.hue = hue;
this.state.sat = sat;
this.state.bright = bright;
} else if (prop === 'ct') {
const [ colorTemperature, bright ] = params;
this.state.colorTemperature = colorTemperature;
this.state.bright = bright
}
this.state.power = 'on';
return this.send('set_scene', prop, ...params);
}
async getState() {
const result = await this.send('get_prop', 'power', 'bright', 'ct', 'rgb', 'hue', 'sat', 'color_mode');
if (result) {
const [ power, bright, colorTemperature, rgb, hue, sat, colorMode ] = result;
const stats = {
power,
bright : Number(bright),
colorTemperature : Number(colorTemperature),
rgb : Number(rgb),
hue : Number(hue),
sat : Number(sat),
colorMode,
};
this.state = stats;
}
}
setColorTemperature(ct, effect = 'smooth', duration = DEFAULT_DURATION) {
this.state.colorTemperature = ct;
return this.send('set_ct_abx', ct, effect, duration);
}
setRGB(rgb, effect, duration = DEFAULT_DURATION) {
this.state.rgb = rgb;
return this.send('set_rg', rgb, effect, duration);
}
setHSV(hue, sat, effect = 'smooth', duration = DEFAULT_DURATION) {
this.state.hue = hue;
this.state.sat = sat;
return this.send('set_hsv', hue, sat, effect, duration);
}
setBright(bright, effect = 'smooth', duration = DEFAULT_DURATION) {
const num = Number(bright)
const value = Math.max(1, Math.min(100, _.isNaN(num) ? 100 : num))
this.state.bright = value;
return this.send('set_bright', value, effect, duration);
}
setPower(toggle, effect = 'smooth', duration = DEFAULT_DURATION) {
const value = toggle ? 'on' : 'off'
this.state.power = value;
return this.send('set_power', value, effect, duration);
}
toggle() {
this.state.power = this.state.power === 'on' ? 'off' : 'on';
return this.send('toggle');
}
adjust(action, prop) {
// action = [ 'increase', 'decrease', 'circle' ]
// prop = [ 'bright', 'ct', 'color' ]
return this.send('set_adjust', action, prop);
}
}
module.exports = Yeelight;