forked from mwittig/node-yeelight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
201 lines (162 loc) · 4.78 KB
/
index.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// working with IP addresses
var ip = require('ip');
// for string padding and object-getting
var _ = require('lodash');
// inherit event emitter
var util = require('util');
var EventEmitter = require('events').EventEmitter;
util.inherits(Yeelight, EventEmitter);
// udp bits
var dgram = require('dgram');
var socket = dgram.createSocket('udp4');
// yeelight networking
var net = require('net');
function Yeelight() {
EventEmitter.call(this);
process.nextTick(function() {
// listen for messages
socket.on('message', function(message, address) {
// if we sent the message, ignore it
if (ip.address() == address.address) {
return;
}
// handle socket discovery message
this.handleDiscovery(message, address);
}.bind(this));
}.bind(this));
};
// listens on options.port
Yeelight.prototype.listen = function() {
try {
socket.bind(options.port, function() {
socket.setBroadcast(true);
});
this.emit('ready', options.port);
} catch (ex) {
throw ex;
}
};
// discover() sends out a broadcast message to find all available devices.
Yeelight.prototype.discover = function() {
var message = options.discoveryMsg;
this.sendMessage(message, options.multicastAddr);
};
Yeelight.prototype.connect = function(device) {
if (device.connected === false && device.socket === null) {
device.socket = new net.Socket();
device.socket.connect(device.port, device.host, function() {
device.connected = true;
this.emit('deviceconnected', device);
}.bind(this));
}
};
Yeelight.prototype.sendMessage = function(message, address, callback) {
var buffer = new Buffer(message);
socket.send(buffer, 0, buffer.length, options.port, address, function(err, bytes) {
if (err) throw err;
});
};
Yeelight.prototype.handleDiscovery = function(message, address) {
var headers = message.toString().split('\r\n');
var device = {};
// set defaults
device.connected = false;
device.socket = null;
// build device params
for (var i = 0; i < headers.length; i++) {
if (headers[i].indexOf("id:") >= 0)
device.id = headers[i].slice(4);
if (headers[i].indexOf("Location:") >= 0) {
device.location = headers[i].slice(10);
var tmp = device.location.split(':');
device.host = tmp[1].replace('//', '');
device.port = parseInt(tmp[2], 10);
}
if (headers[i].indexOf("power:") >= 0)
device.power = headers[i].slice(7);
if (headers[i].indexOf("bright:") >= 0)
device.brightness = headers[i].slice(8);
if (headers[i].indexOf("model:") >= 0)
device.model = headers[i].slice(7);
if (headers[i].indexOf("hue:") >= 0)
device.hue = headers[i].slice(5);
if (headers[i].indexOf("sat:") >= 0)
device.saturation = headers[i].slice(5);
}
this.addDevice(device);
};
Yeelight.prototype.addDevice = function(device) {
// check if device exists in array
if (_.filter(this.devices, {
id: device.id
}).length > 0) {
// check if existing object is exactly the same as the device we're passing it
if (_.isEqual(device, _.filter(this.devices, {
id: device.id
}))) {
// don't do anything else
return;
}
// get our device from the list
var dev = _.filter(this.devices, {
id: device.id
});
// overwrite the device
dev = device;
this.emit('deviceupdated', device);
}
// if device isn't in list
else {
// push new device into array
this.devices.push(device);
this.emit('deviceadded', device);
}
};
Yeelight.prototype.setPower = function(device, state, speed) {
speed = speed || 300;
var on_off = state === true ? 'on' : 'off';
device.power = on_off;
var request = {
id: 1,
method: 'set_power',
params: [on_off, 'smooth', speed]
};
this.sendCommand(device, request, function(device) {
this.emit('powerupdated', device);
}.bind(this));
};
Yeelight.prototype.setBrightness = function(device, percentage, speed) {
speed = speed || 300;
if (device.power == 'off') {
device.brightness = '0';
this.setPower(device, true, 0);
}
device.brightness = percentage;
var request = {
id: 1,
method: 'set_bright',
params: [percentage, 'smooth', speed]
};
this.sendCommand(device, request, function(device) {
this.emit('brightnessupdated', device);
}.bind(this));
};
Yeelight.prototype.sendCommand = function(device, command, callback) {
if (device.connected === false && device.socket === null) {
console.log('Connection broken ' + device.connected + '\n' + device.socket);
this.emit('devicedisconnected', device);
return;
}
var message = JSON.stringify(command);
device.socket.write(message + '\r\n');
if (typeof callback !== 'undefined') {
callback(device);
}
};
Yeelight.prototype.devices = [];
var options = {
port: 1982,
multicastAddr: '239.255.255.250',
discoveryMsg: 'M-SEARCH * HTTP/1.1\r\nMAN: \"ssdp:discover\"\r\nST: wifi_bulb\r\n'
};
module.exports = Yeelight;