forked from robertvorthman/homebridge-marantz-volume
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (71 loc) · 2.8 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
var request = require('request');
var parseString = require('xml2js').parseString;
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-marantz-volume", "marantz-volume", ReceiverVolume);
}
function ReceiverVolume(log, config) {
this.log = log;
this.name = config['name'] || "Receiver Volume";
this.maxVolume = config['maxVolume'] || 70;
this.host = config['host'];
this.statusUrl = "/goform/formMainZone_MainZoneXml.xml";
this.controlUrl = "/goform/formiPhoneAppVolume.xml";
this.powerState = 0;
if (!this.host) {
this.log.warn('Config is missing host/IP of receiver');
callback(new Error('No host/IP defined.'));
return;
}
}
ReceiverVolume.prototype.getPowerOn = function(callback) {
this.log("Receiver Volume power state is %s", this.powerState);
callback(null, this.powerState);
}
ReceiverVolume.prototype.setPowerOn = function(powerOn, callback) {
this.powerState = powerOn ? 1 : 0;
this.log("Set receiver volume power state to %s", this.powerState);
callback(null);
}
ReceiverVolume.prototype.setBrightness = function(level, callback){
var newVolume = level;
if(level > this.maxVolume){
//enforce maximum volume
newVolume = this.maxVolume;
this.log('Volume %s capped to max volume %s', level, this.maxVolume);
}
//convert volume percentage to relative volume
var relativeVolume = (newVolume - 80).toFixed(1);
request.get('http://' + this.host + this.controlUrl + '?1+' + relativeVolume, function (error, response, body) {
if (!error && response.statusCode == 200) {
callback(null);
} else {
callback(error)
}
});
}
ReceiverVolume.prototype.getBrightness = function(callback) {
request.get('http://' + this.host + this.statusUrl, function (error, response, body) {
var xml = '';
if (!error && response.statusCode == 200) {
parseString(xml + body, function (err, result) {
var volume = parseInt(result.item.MasterVolume[0].value[0]) + 80;
callback(null, volume);
});
}
}.bind(this));
}
ReceiverVolume.prototype.getServices = function() {
var lightbulbService = new Service.Lightbulb(this.name);
lightbulbService
.getCharacteristic(Characteristic.On)
.on('get', this.getPowerOn.bind(this))
.on('set', this.setPowerOn.bind(this));
lightbulbService
.addCharacteristic(new Characteristic.Brightness())
.on('get', this.getBrightness.bind(this))
.on('set', this.setBrightness.bind(this));
return [lightbulbService];
}