-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
276 lines (224 loc) · 10 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
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
"use strict";
var debug = require('debug')('roon-extension-harmony'),
debug_keepalive = require('debug')('roon-extension-harmony:keepalive'),
RoonApi = require('node-roon-api'),
RoonApiStatus = require('node-roon-api-status'),
RoonApiSettings = require('node-roon-api-settings'),
RoonApiSourceControl = require('node-roon-api-source-control'),
Discover = require('harmonyhubjs-discover'),
Harmony = require('harmonyhubjs-client'),
PortFinder = require('portfinder');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
const STATE_HUB_OFF = 0;
const STATE_ACTIVITY_STARTING = 1;
const STATE_ACTIVITY_STARTED = 2;
const STATE_HUB_TURNING_OFF = 3;
/**
* Creates a new HarmonyHub instance.
*
* @constructor
*/
function HarmonyHub(name, address) {
this.name = name;
this.address = address;
EventEmitter.call(this)
}
/**
* Creates a Harmony client to talk to this HarmonyHub.
*/
HarmonyHub.prototype.createClient = function() {
debug("HarmonyHub.createClient: Creating Harmony client for %s (%s)", this.name, this.address);
var self = this;
var result = Harmony(this.address);
return result.then((client) => {
client._xmppClient.connection.socket.setTimeout(0);
client._xmppClient.connection.socket.setKeepAlive(true, 10000);
client.on('stateDigest', stateDigest => {
self.emit('stateDigest', stateDigest);
});
return result;
});
}
HarmonyHub.prototype.toString = function() {
return this.name;
}
util.inherits(HarmonyHub, EventEmitter);
/**
* Creates a new HarmonyDiscovery intances. This class will keep track of all the Harmony hubs that are discovered on
* the networks.
*
* @constructor
*/
function HarmonyDiscovery() {
var self = this
self.hubs = [];
PortFinder.getPortPromise()
.then((port) => {
debug("Harmony Discovery - listening on port \'%d\'", port);
self._discover = new Discover(port);
self._discover.on('update', function (hubs) {
debug("received update event from harmonyhubjs-discover. there are \'%d\' hubs: %O", hubs.length, hubs);
self.hubs = hubs.map((entry) => { return new HarmonyHub(entry.friendlyName, entry.ip); });
});
self._discover.start();
})
.catch((error) => {
debug("HarmonyDiscovery - Failed to find available port.", activity.id);
console.log(error);
});
}
var harmonyDiscovery = new HarmonyDiscovery();
var roon = new RoonApi({
extension_id: 'org.pruessmann.roon.logitech.harmony',
display_name: 'Logitech Harmony',
display_version: '0.0.8',
publisher: 'Doc Bobo',
email: '[email protected]',
website: 'https://github.com/docbobo/roon-extension-harmony',
});
var harmony = {
activities: {},
};
var mysettings = roon.load_config("settings") || {
name: "",
hostname: ""
};
function make_layout(settings) {
var l = {
values: settings,
layout: [],
has_error: false
};
var hubs = harmonyDiscovery.hubs.map((entry) => {
return { title: entry.name, value: entry.address }
});
l.layout.push({
type: "dropdown",
title: "Harmony Hub",
subtitle: "Select a Harmony Hub from the list of hubs discovered on the local network.",
values: hubs,
setting: "hostname",
});
return l;
}
var svc_settings = new RoonApiSettings(roon, {
get_settings: function(cb) {
cb(make_layout(mysettings));
},
save_settings: function(req, isdryrun, settings) {
let l = make_layout(settings.values);
req.send_complete(l.has_error ? "NotValid" : "Success", { settings: l });
if (!isdryrun && !l.has_error) {
var old_hostname = mysettings.hostname;
var old_name = mysettings.name;
mysettings = l.values;
mysettings.name = harmonyDiscovery.hubs.find((entry) => { return entry.address == mysettings.hostname }).name;
svc_settings.update_settings(l);
if (old_hostname != mysettings.hostname || old_name != mysettings.name) {
setup_harmony_connection(new HarmonyHub(mysettings.name, mysettings.hostname));
roon.save_config("settings", mysettings);
}
}
}
});
var svc_status = new RoonApiStatus(roon);
var svc_source_control = new RoonApiSourceControl(roon);
roon.init_services({
provided_services: [ svc_status, svc_settings, svc_source_control ]
});
function setup_harmony_connection(harmonyHub) {
debug("setup_harmony_connection(%O)", harmonyHub);
if (!harmonyHub.name || !harmonyHub.address) {
svc_status.set_status("Not configured, please check settings.", true);
} else {
if (harmony.client) {
harmony.client.then((harmonyClient) => { harmonyClient.end() });
delete(harmony.client);
}
harmony.client = harmonyHub.createClient();
harmony.client.then((harmonyClient) => {
harmonyClient.getCurrentActivity().then((currentActivityId) => {
debug("Retrieving Activities....");
harmonyClient.getActivities().then((activities) => {
activities.forEach((activity) => {
if (activity.type === 'PowerOff') {
return;
}
if (harmony.activities[activity.id]) {
debug("Activity '%s' already registered as Source.", activity.id);
harmony.activities[activity.id].update_state({ status: currentActivityId == activity.id ? "selected" : "standby" });
return;
} else {
debug("Creating Source Control Service for '%s'", activity.label);
var device = {
state: {
control_key: activity.id,
display_name: activity.label,
supports_standby: true,
status: currentActivityId == activity.id ? "selected" : "standby",
},
convenience_switch: function (req) {
debug("convenience_switch (%s)", activity.id);
harmonyClient.startActivity(activity.id).then(() => {
debug("convenience_switch (%s) - Succeeded.", activity.id);
req.send_complete("Success");
}).catch((error) => {
debug("convenience_switch (%s) - Failed.", activity.id);
console.log(error);
req.send_complete("Failure");
});
},
standby: function (req) {
debug("standby()");
harmonyClient.turnOff().then(() => {
debug("standby() - Succeeded.");
req.send_complete("Success");
}).catch((error) => {
debug("standby() - Failed.");
console.log(error);
req.send_complete("Failure");
});
}
};
harmonyHub.on('stateDigest', stateDigest => {
debug("stateDigest[%s]: state for '%s' => %s", activity.label, stateDigest.activityId, stateDigest.activityStatus);
var status;
switch (stateDigest.activityStatus) {
case STATE_ACTIVITY_STARTING:
case STATE_ACTIVITY_STARTED:
status = stateDigest.activityId == activity.id ? "selected" : "standby";
break;
default:
status = "standby";
}
if (device.status != status) {
device.status = status;
debug("Calling update_state");
harmony.activities[activity.id].update_state({ status: status });
}
});
harmony.activities[activity.id] = svc_source_control.new_device(device);
}
});
harmonyClient.keepalive = setInterval(() => {
harmonyClient.getCurrentActivity().then((val) => {
debug_keepalive("Keep-Alive: getCurrentActivity() == %s", val);
});
}, 30000);
harmonyClient._xmppClient.on('offline', () => {
debug("Harmony Hub went offline...");
setup_harmony_connection(harmonyHub);
});
svc_status.set_status("Connected to Harmony Hub ''" + harmonyHub.name + "''", false);
});
});
}).catch((error) => {
debug("setup_harmony_connection: Error during setup. Retrying...")
console.log(error);
setup_harmony_connection(harmonyHub);
});
}
}
setup_harmony_connection(new HarmonyHub(mysettings.name, mysettings.hostname));
roon.start_discovery();