-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
153 lines (130 loc) · 5.32 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
"use strict";
// "accessories": [{"accessory": "VOLUMIO"}]
var Service, Characteristic;
var request = require("request");
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-volumio", "VOLUMIO", VOLUMIO);
};
function VOLUMIO(log, config) {
this.log = log;
this.name = config.name || "Volumio";
this.stateUrl = "http://volumio.local/api/v1/getstate";
this.volume = {};
this.mute = {};
let url0 = "http://" + this.name.toLowerCase() + ".local/api/v1/commands/?";
this.volume.setUrl = url0 + "cmd=volume&volume=%s";
this.mute.onUrl = url0 + "cmd=volume&volume=mute";
this.mute.offUrl = url0 + "cmd=volume&volume=unmute";
}
VOLUMIO.prototype = {
identify: function (callback) {
this.log("Identify requested!");
callback();
},
getServices: function () {
this.log("Creating information!");
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "Volumio.org")
.setCharacteristic(Characteristic.Model, "Volumio")
.setCharacteristic(Characteristic.SerialNumber, "0-0-0");
this.log("Creating speaker!");
var speakerService = new Service.Speaker(this.name);
this.log("... configuring mute characteristic");
speakerService
.getCharacteristic(Characteristic.Mute)
.on("get", this.getMuteState.bind(this))
.on("set", this.setMuteState.bind(this));
this.log("... adding volume characteristic");
speakerService
.addCharacteristic(new Characteristic.Volume())
.on("get", this.getVolume.bind(this))
.on("set", this.setVolume.bind(this));
return [informationService, speakerService];
},
getMuteState: function (callback) {
this._httpRequest(this.stateUrl, "", "GET", function (error, response, body) {
if (error) {
this.log("getMuteState() failed: %s", error.message);
callback(error);
}
else if (response.statusCode !== 200) {
this.log("getMuteState() request returned http error: %s", response.statusCode);
callback(new Error("getMuteState() returned http error " + response.statusCode));
}
else {
let obj = JSON.parse(body);
let muted = (obj.mute == 'true');
this.log("Speaker is currently %s", muted? "MUTED": "NOT MUTED");
callback(null, muted);
}
}.bind(this));
},
setMuteState: function (muted, callback) {
let url = muted? this.mute.onUrl: this.mute.offUrl;
this._httpRequest(url, "", "GET", function (error, response, body) {
if (error) {
this.log("setMuteState() failed: %s", error.message);
callback(error);
}
else if (response.statusCode !== 200) {
this.log("setMuteState() request returned http error: %s", response.statusCode);
callback(new Error("setMuteState() returned http error " + response.statusCode));
}
else {
this.log("setMuteState() successfully set mute state to %s", muted? "ON": "OFF");
callback(undefined, body);
}
}.bind(this));
},
getVolume: function (callback) {
this._httpRequest(this.stateUrl, "", "GET", function (error, response, body) {
if (error) {
this.log("getVolume() failed: %s", error.message);
callback(error);
}
else if (response.statusCode !== 200) {
this.log("getVolume() request returned http error: %s", response.statusCode);
callback(new Error("getVolume() returned http error " + response.statusCode));
}
else {
let obj = JSON.parse(body);
let volume = parseInt(obj.volume);
this.log("Speaker's volume is at %s %", volume);
callback(null, volume);
}
}.bind(this));
},
setVolume: function (volume, callback) {
let url = this.volume.setUrl.replace("%s", volume);
this._httpRequest(url, "", "GET", function (error, response, body) {
if (error) {
this.log("setVolume() failed: %s", error.message);
callback(error);
}
else if (response.statusCode !== 200) {
this.log("setVolume() request returned http error: %s", response.statusCode);
callback(new Error("setVolume() returned http error " + response.statusCode));
}
else {
this.log("setVolume() successfully set volume to %s", volume);
callback(undefined, body);
}
}.bind(this));
},
_httpRequest: function (url, body, method, callback) {
request(
{
url: url,
body: body,
method: method,
rejectUnauthorized: false
},
function (error, response, body) {
callback(error, response, body);
}
)
}
};