-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathclient.js
434 lines (374 loc) · 12.9 KB
/
client.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
const http = require('http')
const EventEmitter = require('events')
const {promisify} = require('util')
const xml2js = require('xml2js')
const entities = require('entities')
const debugFactory = require('debug')
const xmlbuilder = require('xmlbuilder')
const universalify = require('./frompromise.js')
const debug = debugFactory('wemo-client')
/** @type {(str, opts) => Promise} */
const parseXml = promisify(xml2js.parseString)
function mapCapabilities (capabilityIds, capabilityValues) {
const ids = capabilityIds.split(',')
const values = capabilityValues.split(',')
const result = {}
ids.forEach((val, index) => {
result[val] = values[index]
})
return result
}
class WemoClient extends EventEmitter {
subscriptions = {}
error
constructor (config) {
super()
this.host = config.host
this.port = config.port
this.deviceType = config.deviceType
this.UDN = config.UDN
this.callbackURL = config.callbackURL
this.device = config
// Create map of services
config.serviceList.service.forEach(function (service) {
this[service.serviceType] = {
serviceId: service.serviceId,
controlURL: service.controlURL,
eventSubURL: service.eventSubURL
}
}, this.services = {})
// Transparently subscribe to serviceType events
// TODO: Unsubscribe from ServiceType when all listeners have been removed.
this.on('newListener', this._onListenerAdded)
}
async soapAction (serviceType, action, body) {
this._verifyServiceSupport(serviceType)
const xml = xmlbuilder.create('s:Envelope', {
version: '1.0',
encoding: 'utf-8',
allowEmpty: true
})
.att('xmlns:s', 'http://schemas.xmlsoap.org/soap/envelope/')
.att('s:encodingStyle', 'http://schemas.xmlsoap.org/soap/encoding/')
.ele('s:Body')
.ele('u:' + action)
.att('xmlns:u', serviceType)
const payload = (body ? xml.ele(body) : xml).end()
const options = {
host: this.host,
port: this.port,
path: this.services[serviceType].controlURL,
method: 'POST',
headers: {
SOAPACTION: '"' + serviceType + '#' + action + '"',
'Content-Type': 'text/xml; charset="utf-8"'
}
}
const response = await WemoClient.request(options, payload).catch(err => {
this.error = err.code
this.emit('error', err)
throw err
})
return response && response['s:Envelope']['s:Body'][`u:${action}Response`]
}
async getEndDevices () {
const parseDeviceInfo = function (data) {
const device = {}
if (data.GroupID) {
// treat device group as it was a single device
device.friendlyName = data.GroupName[0]
device.deviceId = data.GroupID[0]
device.capabilities = mapCapabilities(
data.GroupCapabilityIDs[0],
data.GroupCapabilityValues[0]
)
} else {
// single device
device.friendlyName = data.FriendlyName[0]
device.deviceId = data.DeviceID[0]
device.capabilities = mapCapabilities(
data.CapabilityIDs[0],
data.CurrentState[0]
)
}
// set device type
if (device.capabilities.hasOwnProperty('10008')) {
device.deviceType = 'dimmableLight'
}
if (device.capabilities.hasOwnProperty('10300')) {
device.deviceType = 'colorLight'
}
return device
}
const data = await this.soapAction('urn:Belkin:service:bridge:1', 'GetEndDevices', {
DevUDN: this.UDN,
ReqListType: 'PAIRED_LIST'
})
debug('endDevices raw data', data)
const endDevices = []
const result = await parseXml(data.DeviceLists)
const deviceInfos = result.DeviceLists.DeviceList[0].DeviceInfos[0].DeviceInfo
if (deviceInfos) {
endDevices.push(...deviceInfos.map(parseDeviceInfo))
}
if (result.DeviceLists.DeviceList[0].GroupInfos) {
const groupInfos = result.DeviceLists.DeviceList[0].GroupInfos[0].GroupInfo
endDevices.push(...groupInfos.map(parseDeviceInfo))
}
return endDevices
}
async setDeviceStatus (deviceId, capability, value) {
const deviceStatusList = xmlbuilder.create('DeviceStatus', {
version: '1.0',
encoding: 'utf-8'
}).ele({
IsGroupAction: (deviceId.length === 10) ? 'YES' : 'NO',
DeviceID: deviceId,
CapabilityID: capability,
CapabilityValue: value
}).end()
return this.soapAction('urn:Belkin:service:bridge:1', 'SetDeviceStatus', {
DeviceStatusList: { '#text': deviceStatusList }
})
}
async getDeviceStatus (deviceId) {
const data = await this.soapAction('urn:Belkin:service:bridge:1', 'GetDeviceStatus', {
DeviceIDs: deviceId
})
const result = await parseXml(data.DeviceStatusList, { explicitArray: false })
const deviceStatus = result.DeviceStatusList.DeviceStatus
const capabilities = mapCapabilities(deviceStatus.CapabilityID, deviceStatus.CapabilityValue)
return capabilities
}
async setLightColor (deviceId, red, green, blue) {
const color = WemoClient.rgb2xy(red, green, blue)
return this.setDeviceStatus(deviceId, 10300, color.join(':') + ':0')
}
async setBinaryState (value) {
return this.soapAction('urn:Belkin:service:basicevent:1', 'SetBinaryState', {
BinaryState: value
})
}
async getBinaryState () {
const {BinaryState} = await this.soapAction('urn:Belkin:service:basicevent:1', 'GetBinaryState', null)
return BinaryState
}
async setBrightness (brightness) {
const settings = brightness === 0
? { BinaryState: 0 }
: { brightness }
return this.soapAction('urn:Belkin:service:basicevent:1', 'SetBinaryState', settings)
}
async getBrightness () {
const { brightness } = await this.soapAction('urn:Belkin:service:basicevent:1', 'GetBinaryState', null)
return parseInt(brightness)
}
async setAttributes (attributes) {
const builder = new xml2js.Builder({ rootName: 'attribute', headless: true, renderOpts: { pretty: false } })
const xml_attributes = Object.keys(attributes).map((attribute_key) => {
return builder.buildObject({ name: attribute_key, value: attributes[attribute_key] })
}).join('')
return this.soapAction('urn:Belkin:service:deviceevent:1', 'SetAttributes', {
attributeList: {
'#text': xml_attributes
}
})
}
async getAttributes () {
const data = await this.soapAction('urn:Belkin:service:deviceevent:1', 'GetAttributes', null)
const xml = '<attributeList>' + entities.decodeXML(data.attributeList) + '</attributeList>'
const result = await parseXml(xml, { explicitArray: false })
const attributes = {}
for (const key in result.attributeList.attribute) {
const attribute = result.attributeList.attribute[key]
attributes[attribute.name] = attribute.value
}
return attributes
}
async getInsightParams (cb) {
const data = await this.soapAction('urn:Belkin:service:insight:1', 'GetInsightParams', null).catch(err => {
cb(err)
throw err
})
const params = this._parseInsightParams(data.InsightParams)
cb(null, params.binaryState, params.instantPower, params.insightParams)
return params
}
_parseInsightParams (paramsStr) {
const params = paramsStr.split('|')
return {
binaryState: params[0],
instantPower: params[7],
insightParams: {
ONSince: params[1],
OnFor: params[2],
TodayONTime: params[3],
TodayConsumed: params[8] // power consumer today (mW per minute)
}
}
}
_onListenerAdded (eventName) {
const serviceType = WemoClient.EventServices[eventName]
if (serviceType && this.services[serviceType]) {
this._subscribe(serviceType)
}
}
_subscribe (serviceType) {
this._verifyServiceSupport(serviceType)
if (!this.callbackURL) {
throw new Error('Can not subscribe without callbackURL')
}
if (this.subscriptions[serviceType] && this.subscriptions[serviceType] === 'PENDING') {
debug('subscription still pending')
return
}
const options = {
host: this.host,
port: this.port,
path: this.services[serviceType].eventSubURL,
method: 'SUBSCRIBE',
headers: {
TIMEOUT: 'Second-300'
}
}
if (!this.subscriptions[serviceType]) {
// Initial subscription
this.subscriptions[serviceType] = 'PENDING'
debug('Initial subscription - Device: %s, Service: %s', this.UDN, serviceType)
options.headers.CALLBACK = '<' + this.callbackURL + '/' + this.UDN + '>'
options.headers.NT = 'upnp:event'
} else {
// Subscription renewal
debug('Renewing subscription - Device: %s, Service: %s', this.UDN, serviceType)
options.headers.SID = this.subscriptions[serviceType]
}
const req = http.request(options, res => {
if (res.statusCode === 200) {
// Renew after 150 seconds
this.subscriptions[serviceType] = res.headers.sid
setTimeout(this._subscribe.bind(this), 150 * 1000, serviceType)
} else {
// Try to recover from failed subscription after 2 seconds
debug('Subscription request failed with HTTP %s', res.statusCode)
this.subscriptions[serviceType] = null
setTimeout(this._subscribe.bind(this), 2000, serviceType)
}
})
req.on('error', (err) => {
debug('Subscription error: %s - Device: %s, Service: %s', err.code, this.UDN, serviceType)
this.subscriptions[serviceType] = null
this.error = err.code
this.emit('error', err)
})
req.end()
}
_verifyServiceSupport (serviceType) {
if (!this.services[serviceType]) {
throw new Error('Service ' + serviceType + ' not supported by ' + this.UDN)
}
}
handleCallback (body) {
const handler = {
BinaryState: data => {
this.emit('binaryState', data.substring(0, 1))
},
Brightness: data => {
this.emit('brightness', parseInt(data))
},
StatusChange: data => {
xml2js.parseString(data, { explicitArray: false }, (err, xml) => {
if (!err) {
this.emit('statusChange',
xml.StateEvent.DeviceID._,
xml.StateEvent.CapabilityId,
xml.StateEvent.Value
)
}
})
},
InsightParams: data => {
const params = this._parseInsightParams(data)
this.emit('insightParams', params.binaryState, params.instantPower, params.insightParams)
},
attributeList: data => {
const xml = '<attributeList>' + entities.decodeXML(data) + '</attributeList>'
xml2js.parseString(xml, { explicitArray: true }, (err, result) => {
if (!err) {
// In order to keep the existing event signature this
// triggers an event for every attribute changed.
result.attributeList.attribute.forEach((attribute) => {
this.emit('attributeList',
attribute.name[0],
attribute.value[0],
attribute.prevalue[0],
attribute.ts[0]
)
})
}
})
}
}
xml2js.parseString(body, { explicitArray: false }, function (err, xml) {
if (err) { throw err }
for (const prop in xml['e:propertyset']['e:property']) {
if (handler.hasOwnProperty(prop)) {
handler[prop](xml['e:propertyset']['e:property'][prop])
} else {
debug('Unhandled Event: %s', prop)
}
}
})
}
static async request (options, data) {
const body = await new Promise((rs, rj) => {
const req = http.request(options, res => {
let body = ''
res.setEncoding('utf8')
res.on('error', rj)
res.on('data', chunk => { body += chunk })
res.on('end', () => res.statusCode === 200
? rs(body)
: rj(new Error('HTTP ' + res.statusCode + ': ' + body))
)
})
req.on('error', rj)
if (data) req.write(data)
req.end()
})
return parseXml(body, { explicitArray: false })
}
static rgb2xy (r, g, b) {
// Based on: https://github.com/aleroddepaz/pyhue/blob/master/src/pyhue.py
const X = (0.545053 * r) + (0.357580 * g) + (0.180423 * b)
const Y = (0.212671 * r) + (0.715160 * g) + (0.072169 * b)
const Z = (0.019334 * r) + (0.119193 * g) + (0.950227 * b)
const x = X / (X + Y + Z)
const y = Y / (X + Y + Z)
return [
Math.round(x * 65535),
Math.round(y * 65535)
]
}
}
WemoClient.EventServices = {
insightParams: 'urn:Belkin:service:insight:1',
statusChange: 'urn:Belkin:service:bridge:1',
attributeList: 'urn:Belkin:service:basicevent:1',
binaryState: 'urn:Belkin:service:basicevent:1'
}
for (let x of ['request'])
WemoClient[x] = universalify(WemoClient[x])
for (let x of [
'getAttributes',
'getBinaryState',
'getBrightness',
'getDeviceStatus',
'getEndDevices',
'setAttributes',
'setBinaryState',
'setDeviceStatus',
'setLightColor',
'soapAction'
])
WemoClient.prototype[x] = universalify(WemoClient.prototype[x])
module.exports = WemoClient