forked from RoonLabs/roon-extension-devialet-phantom-volume
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
207 lines (180 loc) · 6.94 KB
/
app.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
"use strict";
const ssdp = require('node-ssdp').Client,
upnp = require('node-upnp'),
net = require('net'),
url = require('url')
;
const ssdpclient = new ssdp();
let remotes = {};
function Remote(upnp) {
// console.log(upnp);
this.getVolume = async () => {
return (await upnp.call('RenderingControl', 'GetVolume', {
InstanceID: 0,
Channel: 'Master'
})).CurrentVolume;
};
this.getMute = async () => {
return (await upnp.call('RenderingControl', 'GetMute', {
InstanceID: 0,
Channel: 'Master'
})).CurrentMute;
};
this.setVolume = async (v) => {
return (await upnp.call('RenderingControl', 'SetVolume', {
InstanceID: 0,
Channel: 'Master',
DesiredVolume: v
})).CurrentVolume;
};
this.setMute = async (v) => {
return (await upnp.call('RenderingControl', 'SetMute', {
InstanceID: 0,
Channel: 'Master',
DesiredMute: v ? 1 : 0
})).CurrentMute;
};
}
function Discover(cb) {
ssdpclient.on('response', async function inResponse(headers, code, rinfo) {
if (headers.ST == "urn:schemas-upnp-org:service:RenderingControl:2") {
if (remotes[headers.USN]) return;
let u = new upnp({ url: headers.LOCATION });
let remote = remotes[headers.USN] = new Remote(u);
let desc = await u.getDeviceDescription();
if (desc) {
// console.log("found", desc.UDN, desc.friendlyName,
// desc.manufacturer,
// desc.modelName,
// desc.modelNumber);
if (desc.manufacturer == "Devialet" && desc.modelName == "Devialet UPnP Renderer") {
remote.id = desc.UDN;
remote.name = desc.friendlyName;
cb(remote, 'connected');
const loc = url.parse(headers.LOCATION);
function ping() {
let socket = new net.Socket();
socket.setTimeout(5000);
socket.on("timeout", () => {
if (socket) {
// console.log("pinger", "timeout");
socket.destroy();
socket = undefined;
cb(remote, 'disconnected');
}
});
socket.on("error", err => {
if (socket) {
// console.log("pinger", "error");
socket.destroy();
socket = undefined;
cb(remote, 'disconnected');
}
});
socket.connect(loc.port, loc.hostname, () => {
if (socket) {
// console.log("pinger", "connected");
socket.destroy();
setTimeout(function() { ping(); }, 5000)
}
});
}
setTimeout(function() { ping(); }, 5000)
}
}
// My ancient Phantom (the chrome one) does not support this, so I'm giving
// gup on events coming from Spark/PhantomKnob
// await u.subscribe('RenderingControl', function (a) {
// console.log('XXX', arguments);
// });
}
})
function search() {
ssdpclient.search('urn:schemas-upnp-org:service:RenderingControl:2');
setTimeout(function() { search(); }, 10000)
}
search();
}
// console.log("Searching for Devialet Bridge...")
var RoonApi = require("node-roon-api"),
RoonApiStatus = require('node-roon-api-status'),
RoonApiVolumeControl = require('node-roon-api-volume-control');
var roon = new RoonApi({
extension_id: 'com.roonlabs.devialet.phantom.volume',
display_name: 'Devialet Phantom Volume Control',
display_version: "1.0.0",
publisher: 'Roon Labs, LLC',
email: 'contact@roonlabs.com',
website: 'https://github.com/RoonLabs/roon-extension-devialet-phantom-volume',
});
let devices = 0;
var svc_status = new RoonApiStatus(roon);
var svc_volume_control = new RoonApiVolumeControl(roon);
roon.init_services({
provided_services: [ svc_volume_control, svc_status ]
});
function setup() {
new Discover((r, what) => {
if (r.volume_control) { r.volume_control.destroy(); delete(r.volume_control); }
// console.log(r, what);
if (what == "connected")
ev_connected(r);
else
ev_disconnected(r);
});
svc_status.set_status("Searching...", false);
}
async function ev_connected(r) {
devices++;
svc_status.set_status(`Found ${devices} ${devices == 1 ? "device" : "devices"}`, false);
console.log(r.id);
r.volume_control = svc_volume_control.new_device({
state: {
control_key: r.id,
display_name: `${r.name}`,
volume_type: "number",
volume_min: 1,
volume_max: 100,
volume_value: await r.getVolume(),
volume_step: 1.0,
is_muted: await r.getMute()
},
set_volume: async function (req, mode, value) {
let newvol = mode == "absolute" ? value : ((await r.getVolume()) + value);
if (newvol < this.state.volume_min) newvol = this.state.volume_min;
else if (newvol > this.state.volume_max) newvol = this.state.volume_max;
await r.setVolume(newvol);
ev_volume(r, newvol);
req.send_complete("Success");
},
set_mute: async function (req, action) {
let mute = action == 'on';
if (action == 'toggle')
mute = !(await r.getMute());
r.setMute(mute);
ev_mute(r, mute);
req.send_complete("Success");
}
});
}
function ev_disconnected(r) {
devices--;
if (devices == 0) {
svc_status.set_status("Searching...", false);
} else {
svc_status.set_status(`Found ${devices} ${devices == 1 ? "device" : "devices"}`, false);
}
if (r.volume_control) { r.volume_control.destroy(); delete(r.volume_control); }
}
function ev_volume(r, val) {
// console.log("[Devialet Phantom Volume Extension] received volume change from device:", val);
if (r.volume_control)
r.volume_control.update_state({ volume_value: val });
}
function ev_mute(r, val) {
// console.log("[Devialet Phantom Volume Extension] received volume change from device:", val);
if (r.volume_control)
r.volume_control.update_state({ is_muted: val });
}
setup();
roon.start_discovery();