-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
520 lines (450 loc) · 15.2 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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
var inherits = require('util').inherits;
var request = require('request');
var json5 = require('json5');
var fs = require('fs');
var path = require('path');
var watch = require('node-watch');
var async = require('async');
try {
var uuid = require('hap-nodejs').uuid;
} catch (error) {
var uuid = null;
}
try {
var alsa = require('alsa-monitor');
} catch (error) {
var alsa = null;
}
try {
var loudness = require('loudness');
} catch (error) {
var loudness = null;
}
var Service, Characteristic, UUIDgen;
var devices = [];
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDgen = homebridge.hap.uuid;
homebridge.registerAccessory("homebridge-fidelio", "Fidelio", FidelioAccessory);
};
//
// Fidelio Accessory
//
function FidelioAccessory(log, config) {
this.log = log;
this.config = config;
this.name = config.name;
this.host = config.host;
this.url = 'http://' + this.host + ':8889/';
// Set device information
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, "Philips")
.setCharacteristic(Characteristic.Model, "Fidelio");
switch (config.type) {
case 'lightbulb':
// Add homekit as Lightbulb service
this.service = new Service.Lightbulb(this.name);
this.service
.getCharacteristic(Characteristic.On)
.on('get', this.getOn.bind(this))
.on('set', this.setOn.bind(this));
this.service
.addCharacteristic(Characteristic.Brightness)
.on('get', this.getVolume.bind(this))
.on('set', this.setVolume.bind(this));
break;
case 'fan':
// Add homekit as Fan service
this.service = new Service.Fan(this.name);
this.service
.getCharacteristic(Characteristic.On)
.on('get', this.getOn.bind(this))
.on('set', this.setOn.bind(this));
this.service
.addCharacteristic(Characteristic.RotationSpeed)
.on('get', this.getVolume.bind(this))
.on('set', this.setVolume.bind(this));
break;
case 'switch':
// Add homekit as Switch service
this.service = new Service.Switch(this.name);
this.service
.getCharacteristic(Characteristic.On)
.on('get', this.getOn.bind(this))
.on('set', this.setOn.bind(this));
this.service
.addCharacteristic(Characteristic.Volume)
.on('get', this.getVolume.bind(this))
.on('set', this.setVolume.bind(this));
break;
default:
// Add homekit Switch service
this.service = new Service.Speaker(this.name);
this.config.type = 'speaker';
this.service
.getCharacteristic(Characteristic.Mute)
.on('get', this.getOn.bind(this))
.on('set', this.setOn.bind(this));
this.service
.addCharacteristic(Characteristic.Volume)
.on('get', this.getVolume.bind(this))
.on('set', this.setVolume.bind(this));
}
// Optionally add channel Characteristic
this.channels = config.channels || false;
if (this.channels) {
// Make channel-n Characteristic
channelCharacteristic = makeChannelCharacteristic(this.channels.length);
// Add Characteristic to service
this.service
.addCharacteristic(channelCharacteristic)
.on('get', this.getChannel.bind(this))
.on('set', this.setChannel.bind(this));
}
// Optionally listen for JSON file
this.file = config.file || false;
if(this.file) {
try {
this._initWatch(this.file);
} catch (err) {
this.log('Error: failed to init file watcher: %s', err.message);
this.channelfile = false;
}
}
// Optionally initiate alsa part
this.alsa = config.alsa || false;
if (this.alsa) {
this._initAlsa();
}
// initiate cache settings
if (!config.cache) {
config.cache = false;
}
this.cache = {on: config.cache.on || false,
volume: config.cache.volume || 10,
volumeNeedsUpdate: false,
channel: config.cache.channel || 1,
channelNeedsUpdate: false};
this.log("Added speaker: %s, host: %s", this.name, this.host);
}
FidelioAccessory.prototype.getServices = function() {
return [this.informationService, this.service];
};
FidelioAccessory.prototype._initAlsa = function() {
if (alsa === null) {
this.log('Warning: Module `alsa-monitor` is not found, disabling alsa');
this.alsa = false;
return;
}
if (loudness === null) {
this.log('Warning: Module `loudness` is not found, disabling alsa');
this.alsa = false;
return;
}
alsa.volume.on("change", function () {
var state = {volume: 'alsa'};
this._setState(state, function(error){
if(error) {
this.log("Failed to set alsa state: %s", error.message);
}
}.bind(this));
}.bind(this));
};
// if alsa state changes, run following callback
FidelioAccessory.prototype._getAlsa = function(callback) {
//check if alsa is active
if (!this.alsa) {
return callback(new Error('Alsa not configured'));
}
//check for mute first, then for volume
loudness.getMuted(function (error,muted) {
if (muted === true) {
// If I'm muted set volume to 0
return callback(null, 0);
} else {
loudness.getVolume(function (error, vol) {
if (typeof vol == "number" && !isNaN(vol)) {
// I got a volume, so proceed
return callback(null, vol);
} else {
return callback(error);
}
}.bind(this));
}
}.bind(this));
};
FidelioAccessory.prototype._initWatch = function(filename) {
// We want to watch the whole path, in case the file does not (yet) exist
watch(path.dirname(filename), function(file) {
// Check if we're triggerd for the correct file
if (filename == file) {
// Try to open file
fs.readFile(filename, 'utf8', function(err, data) {
if (err) {
this.log("Error: error reading file: %s", err);
}
try {
var state = json5.parse(data);
this._setState(state, function(error) {
if (error) {
this.log("Error: file state could not be executed: %s", error.message);
}
}.bind(this));
} catch (error) {
this.log("Error: file %s could not be parsed: %s", file, error.message);
}
}.bind(this));
}
}.bind(this));
};
FidelioAccessory.prototype._request = function (url, callback) {
request(url, function (error, response, body) {
if (error) {
callback(error);
return;
}
if (response.statusCode != 200) {
callback(new Error("Invalid HTTP statusCode: " + response.statusCode));
return;
}
callback(error, body);
});
};
FidelioAccessory.prototype._setState = function(state, callback) {
// if no power state is given, get current state first
if (isNaN(state.power)) {
this._getPower(function(error, result) {
// we ignore errors, since we always have cached values
state.power = result+2;
// now recurse into ourselves with the polled power state
this._setState(state, callback);
}.bind(this));
return;
}
// if alsa volume is required get it first
if (state.volume == 'alsa') {
this._getAlsa(function(error, vol) {
if(error) {
return callback(error);
}
state.volume = vol;
// now recurse into ourselves with the polled volume
this._setState(state, callback);
}.bind(this));
return;
}
// if (needs) to be switched off, store values in cache
if (state.power === 0 || state.power === false || state.power == 2) {
if (!isNaN(state.volume)) {
this.cache.volume = state.volume;
this.cache.volumeNeedsUpdate = true;
}
if (!isNaN(state.channel)) {
this.cache.channel = state.channel;
this.cache.channelNeedsUpdate = true;
}
// and switch off
if (state.power === 0 || state.power === false) {
this._getPower(function(error,result){
if(error === null && result == 1) {
// apparently I'm switched on, so switch me off;
this._request(this.url + 'CTRL$STANDBY', function (error, result) {
if(!error) {
this.cache.on = 0;
}
callback(error);
}.bind(this));
return;
}
callback(error);
}.bind(this));
} else {
//I'm already switched off, nothing to do
callback(null);
}
// we're switched off, so we're done
return;
}
// jeah we're on or need to be on :)
// is it just a power on request? No other state changes?
if (isNaN(state.channel) && isNaN(state.volume) && !this.cache.volumeNeedsUpdate && !this.cache.channelNeedsUpdate) {
// switch me on by calling index
var url = this.url + 'index';
request(url, function (error, result) {
if(!error) {
this.cache.on = 1;
}
callback(error);
}.bind(this));
// we're switched on, so we're done
return;
}
//now finally, set state & volume, if applicable
async.parallel([
// If needed, set channel
function (callback){
channel = state.channel || (this.cache.channelNeedsUpdate?this.cache.channel:false);
if (!channel) {
return callback(null);
}
this._doChannelRequest(channel, callback);
}.bind(this),
// If needed, set volume
function (callback) {
if (isNaN(state.volume) && !this.cache.volumeNeedsUpdate ) {
//nothing to do
return callback(null);
}
volume = (!isNaN(state.volume))?state.volume:this.cache.volume;
this._doVolumeRequest(volume, callback);
}.bind(this)],
callback);
};
FidelioAccessory.prototype._doChannelRequest = function(channel, callback) {
if (!this.channels || channel <= 0 || channel > this.channels.length) {
return callback(new Error('Channels not configured or out of bounds'));
}
this.log("Channel: %s (%d)",this.channels[channel-1], channel);
this._request(this.url + this.channels[channel-1], function (error, result) {
if (!error) {
this.cache.on = 1;
this.cache.channel = channel;
this.cache.channelNeedsUpdate = false;
}
callback(error);
}.bind(this));
};
FidelioAccessory.prototype._doVolumeRequest = function(volume, callback) {
if (!(volume >= 0 && volume <= 100)) {
return callback(new Error('Volume out of bounds'));
}
volume = Math.round(volume / 100.0 * 64.0);
this._request(this.url + 'VOLUME$VAL$' + volume, function (error, result) {
if (!error) {
this.cache.on = 1;
this.cache.volume = volume;
this.cache.volumeNeedsUpdate = false;
}
callback(error);
}.bind(this));
};
FidelioAccessory.prototype._getPower = function(callback) {
var url = this.url + 'HOMESTATUS';
this._request(url, function (error, result) {
if (error) {
return callback(error, this.cache.on);
}
var obj = json5.parse(result);
if (obj.command == 'STANDBY') {
var powerOn = !obj.value;
this.cache.on = powerOn;
return callback(null, powerOn);
} else {
return callback(new Error("Invalid HOMESTATUS response: " + result), this.cache.on);
}
}.bind(this));
};
FidelioAccessory.prototype.getOn = function(callback) {
this._getPower(function (error,result) {
if (error) {
this.log('Error: getOn() failed: %s', error.message);
} else {
this.log('Power is currently %d', result);
}
if (this.config.type == 'speaker') {
result = !result;
}
return callback(error, result);
}.bind(this));
};
FidelioAccessory.prototype.setOn = function(on, callback) {
if (this.config.type == 'speaker') {
on = !on;
}
var state = { power: on };
this._setState(state, function(error) {
if (error) {
this.log('Error: setOn failed: %s', error.message);
} else {
this.log('Power is set to %d', on);
}
return callback(error);
}.bind(this));
};
FidelioAccessory.prototype.getMute = function(callback) {
this.getVolume(function(error, vol) {
return callback(error, (vol==0?true:false));
});
};
FidelioAccessory.prototype.setMute = function(callback, value, context) {
};
FidelioAccessory.prototype.getVolume = function(callback) {
var url = this.url + 'ELAPSE';
this._request(url, function (error, result) {
if (error) {
this.log('Error: getVolume() failed: %s', error.message);
return callback(error, this.cache.volume);
}
var obj = json5.parse(result);
if (obj.command == 'ELAPSE') {
var vol = Math.round(obj.volume / 64.0 * 100.0);
this.log('Got current volume: %d%% (%d)', vol, obj.volume);
this.cache.volume = vol;
return callback(null, vol);
} else if (obj.command == 'NOTHING') {
this.log('Returned volume from cache: %d', this.cache.volume);
return callback(null, this.cache.volume);
} else {
var err = "Unknown ELAPSE response: " + result;
return callback(new Error(err));
}
}.bind(this));
};
FidelioAccessory.prototype.setVolume = function(volume, callback) {
var state = { volume: volume };
this._setState(state, function(error){
if (error) {
this.log('Error: setVolume failed: %s', error.message);
} else {
this.log('Volume is set to %d', volume);
}
return callback(error);
}.bind(this));
};
FidelioAccessory.prototype.getChannel = function(callback) {
// Currently I don't know a way to query this information from the speakers,
// so using cache instead.
this.log('Getting channel from cache: %d', this.cache.channel);
callback(null, this.cache.channel);
};
FidelioAccessory.prototype.setChannel = function(channel, callback) {
var state = { power: true, channel: channel };
this._setState(state, function(error){
if (error) {
this.log('Error: setChannel failed: %s', error.message);
} else {
this.log('Channel is set to %d', channel);
}
return callback(error);
}.bind(this));
};
//
// Custom Characteristic for Volume
function makeChannelCharacteristic(count) {
var id = UUIDgen.generate('Channel-' + count);
channelCharacteristic = function() {
Characteristic.call(this, 'Channel', id);
this.setProps({
format: Characteristic.Formats.INT,
maxValue: count,
minValue: 1,
minStep: 1,
perms: [Characteristic.Perms.READ, Characteristic.Perms.WRITE, Characteristic.Perms.NOTIFY]
});
this.value = this.getDefaultValue();
};
inherits(channelCharacteristic, Characteristic);
return channelCharacteristic;
}