-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (71 loc) · 2.71 KB
/
index.js
File metadata and controls
87 lines (71 loc) · 2.71 KB
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
var Service, Characteristic;
var Denon = require('./lib/denon');
var inherits = require('util').inherits;
var pollingtoevent = require('polling-to-event');
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-denon-soundmode', 'DenonSoundmode', DenonAVRAccessory);
};
function DenonAVRAccessory(log, config) {
this.log = log;
var that = this;
this.config = config;
this.ipAddr = config['ip'];
this.name = config['name'];
this.inputName = config['inputName'] || null;
this.soundMode = config['soundMode'] || null;
this.doPolling = config['doPolling'] || false;
this.pollingInterval = config['pollingInterval'] || "60";
this.pollingInterval = parseInt(this.pollingInterval)
this.denon = new Denon(this.ipAddr);
this.setAttempt = 0;
this.state = false;
if (this.interval < 10 && this.interval > 100000) {
this.log("polling interval out of range.. disabled polling");
this.doPolling = false;
}
// Status Polling
if (this.doPolling) {
that.log("start polling...");
var statusemitter = pollingtoevent(function(done) {
that.log("do poll..")
that.getIsPlaying( function( error, response) {
that.log("Response:"+response);
done(error, response, this.setAttempt);
});
}, {longpolling:true,interval:that.pollingInterval * 1000,longpollEventName:"statuspoll"});
statusemitter.on("statuspoll", function(data) {
that.state = data;
that.log("poll end, state: "+data);
if (that.switchService ) {
that.switchService.getCharacteristic(Characteristic.On).updateValue(that.state, null, "statuspoll");
}
});
}
}
/**
* Retrieves relevant States from receiver and checks with preconfigured values
* @param callback
*/
DenonAVRAccessory.prototype.getIsPlaying = function (callback) {
this.denon.getStates(function (err, retval) {
if (err) {
this.log('get IsPlaying error: ' + err)
callback(null, false);
} else {
callback(null, (retval.powerState = 'ON') && (retval.inputName.indexOf(this.inputName) >= 0) && (retval.soundMode.indexOf(this.soundMode) >= 0));
}
}.bind(this))
};
DenonAVRAccessory.prototype.getServices = function () {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Name, this.name)
.setCharacteristic(Characteristic.Manufacturer, this.type || 'Denon');
this.switchService = new Service.Switch(this.name);
this.switchService.getCharacteristic(Characteristic.On)
.on('get', this.getIsPlaying.bind(this))
.on('set', this.getIsPlaying.bind(this));
return [informationService, this.switchService];
};