-
Notifications
You must be signed in to change notification settings - Fork 18
/
remote.js
439 lines (402 loc) · 13.8 KB
/
remote.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
'use strict';
var alexa = require('alexa-app'),
HarmonyUtils = require('harmony-hub-util'),
harmony_clients = {},
conf = require('./remote_conf.js'),
Q = require('q'),
hub_ip = conf.hub_ip,
app_id = conf.app_id,
MAX_ACTIVITY_WAIT_TIME_MS = 15000;
// Define an alexa-app
var app = new alexa.app('remote');
app.id = conf.app_id
app.launch(function(req, res) {
console.log("Launching the application");
});
function execCmdDF(hutils, is_device, dev_or_act, cmd, cnt, fn, res) {
console.log("execCmd called with cnt = " + cnt + " is_dev " + is_device +
" dev/act " + dev_or_act + " cmd = " + cmd);
if (cnt === 0) {
fn(res);
hutils.end();
return;
}
hutils.executeCommand(is_device, dev_or_act, cmd).then(function (res) {
console.log(cnt + ". Command " + cmd + " to device/activity " +
dev_or_act + " was executed with result : " + res);
if (res) {
setTimeout(function () {
execCmdDF(hutils, is_device, dev_or_act, cmd, cnt - 1, fn, res);
}, 100);
}
}, function(err) {
console.log("ERROR Occured " + err);
console.log(" stack " + err.stack);
});
}
function execCmd(dev, cmd, cnt, fn, res) {
new HarmonyUtils(hub_ip).then(function (hutil) {
execCmdDF(hutil, true, dev, cmd, cnt, fn, res);
});
}
function execCmdCurrentActivity(cmd, cnt, fn, res) {
new HarmonyUtils(hub_ip).then(function (hutils) {
hutils.readCurrentActivity().then(function (current_activity) {
execCmdDF(hutils, false, current_activity, cmd, cnt, fn, res);
});
});
}
/**
* Waits for a specific activity to be the current activity
* (assumes the activity has already been executed).
*
* @param {string} hutils - The hutils to use
* @param {string} act - The activity to wait for
* @param {number} max_wait_timestamp - The timestamp to give up on waiting
* @returns deferred promise
*/
function waitForActivity(hutils, act, max_wait_timestamp) {
var deferred = Q.defer(),
wait_interval = 3000;
hutils.readCurrentActivity().then(function (current_activity) {
if (current_activity != act) {
if (Date.now() > max_wait_timestamp) {
deferred.reject('Max wait time exceeded waiting for ' + act);
return;
}
console.log(act + ' is not the current activity yet, waiting another ' + wait_interval + 'ms ...');
setTimeout(function () {
waitForActivity(hutils, act, max_wait_timestamp).then(function (res) {
deferred.resolve(res);
}, function (err) {
deferred.reject(err);
});
}, wait_interval);
} else {
console.log(act + ' is now the current activity');
deferred.resolve(true);
}
}, function (err) {
deferred.reject(err);
});
return deferred.promise;
}
/**
* Executes a command for a specific activity, executing and waiting
* for that activity if needed.
*
* @param {string} act - The activity the command should be executed under
* @param {string} cmd - The command to execute
* @param {string} cnt - The count
*/
function execActivityCmd(act, cmd, cnt) {
new HarmonyUtils(hub_ip).then(function (hutils) {
hutils.readCurrentActivity().then(function (current_activity) {
if (current_activity != act) {
// Need to switch activities and wait
execActivity(act, function (res) {
waitForActivity(hutils, act, Date.now() + MAX_ACTIVITY_WAIT_TIME_MS).then(function (res) {
execCmdCurrentActivity(cmd, 1, function (res) {
hutils.end();
console.log('Command executed with result : ' + res);
});
}, function (err) {
console.error(err);
hutils.end();
});
});
} else {
console.log(act + ' is already the current activity, executing command');
execCmdCurrentActivity(cmd, 1, function (res) {
console.log('Command executed with result : ' + res);
hutils.end();
});
}
});
});
}
function execActivity(act, fn) {
new HarmonyUtils(hub_ip).then(function (hutils) {
hutils.executeActivity(act).then(function (res) {
fn(res);
});
});
}
app.pre = function(req, res, type) {
if (req.applicationId !== app_id) {
console.log(" Received and invalid applicaiton ID " + req.applicationId);
res.fail("Invalid applicationId");
}
};
app.intent('IncreaseVolume',
{
"slots" : {'AMOUNT' : 'NUMBER'},
"utterances" : ["{increase|} volume {by|} {1-9|AMOUNT}"]
},
function (req, res) {
var amt = parseInt(req.slot('AMOUNT'), 10);
if (isNaN(amt)) {
amt = 1;
}
res.say('Increasing volume by ' + amt);
console.log('Increasing volume by ' + amt);
execCmdCurrentActivity('Volume,Volume Up', amt, function (res) {
console.log("Command Volume UP was executed with result : " + res);
});
});
app.intent('DecreaseVolume',
{
"slots" : {'AMOUNT' : 'NUMBER'},
"utterances" : ["{decrease volume|reduce volume|down volume|volume down} {by|} {1-9|AMOUNT}"]
},
function (req, res) {
var amt = parseInt(req.slot('AMOUNT'), 10);
if (isNaN(amt)) {
amt = 1;
}
res.say('Decreasing volume by ' + amt);
console.log('Decreasing volume by ' + amt);
execCmdCurrentActivity('Volume,Volume Down', amt, function (res) {
console.log("Command Volume Down was executed with result : " + res);
});
});
app.intent('MuteVolume',
{
"slots" : {},
"utterances" : ["{mute|quiet|shut up|unmute}"]
},
function (req, res) {
res.say('Muting!');
console.log('Muting!');
execCmdCurrentActivity('Volume,Mute', 1, function (res) {
console.log("Command Mute executed with result : " + res);
});
});
app.intent('IncreaseTVVolume',
{
"slots" : {'AMOUNT' : 'NUMBER'},
"utterances" : ["{increase|} TV volume by {1-9|AMOUNT}"]
},
function (req, res) {
var amt = parseInt(req.slot('AMOUNT'), 10);
if (isNaN(amt)) {
amt = 1;
}
res.say('Increasing TV volume by ' + amt);
console.log('Increasing volume by ' + amt);
execCmd('TV', 'VolumeUp', amt, function (res) {
console.log("Command Volume UP was executed with result : " + res);
});
});
app.intent('DecreaseTVVolume',
{
"slots" : {'AMOUNT' : 'NUMBER'},
"utterances" : ["{decrease TV volume|reduce TV volume} by {1-9|AMOUNT}"]
},
function (req, res) {
var amt = parseInt(req.slot('AMOUNT'), 10);
if (isNaN(amt)) {
amt = 1;
}
res.say('Decreasing TV volume by ' + amt);
console.log('Decreasing volume by ' + amt);
execCmd('TV', 'VolumeDown', amt, function (res) {
console.log("Command Volume Down was executed with result : " + res);
});
});
app.intent('MuteTVVolume',
{
"slots" : {},
"utterances" : ["{mute|unmute} {TV|telivision}"]
},
function (req, res) {
res.say('Muting TV!');
console.log('Muting!');
execCmd('TV', 'Mute', 1, function (res) {
console.log("Command Mute executed with result : " + res);
});
});
app.intent('TurnOffTV',
{
"slots" : {},
"utterances" : ["{turn the TV off|turn TV off}"]
},
function (req, res) {
res.say('Turning TV off!');
console.log('Turning TV off!');
execCmd('TV', 'PowerOff', 1, function (res) {
console.log("Command TV PowerOff executed with result : " + res);
});
});
app.intent('TurnOnTV',
{
"slots" : {},
"utterances" : ["{turn on the TV|turn the TV on|turn on TV|turn TV on}"]
},
function (req, res) {
res.say('Turning TV on!');
console.log('Turning TV on!');
execCmd('TV', 'PowerOn', 1, function (res) {
console.log("Command TV PowerOn executed with result : " + res);
});
});
app.intent('TurnOffAmplifier',
{
"slots" : {},
"utterances" : ["{turn the amplifer off|turn amplifier off}"]
},
function (req, res) {
res.say('Turning amplifer off!');
console.log('Turning amplifier off!');
execCmd('Amplifier', 'PowerToggle', 1, function (res) {
console.log("Command for amplifer PowerToggle executed with result : " + res);
});
});
app.intent('TurnOnAmplifier',
{
"slots" : {},
"utterances" : ["{turn on the amplifier|turn the amplifier on}"]
},
function (req, res) {
res.say('Toggle power on the amplifier!');
console.log('Turning amplifier on!');
execCmd('Amplifier', 'PowerToggle', 1, function (res) {
console.log("Command Amplifier PowerToggle executed with result : " + res);
});
});
app.intent('AmplifierInputNext',
{
"slots" : {},
"utterances" : ["{select next amplifier input}"]
},
function (req, res) {
res.say('selecting next input on amplifier!');
console.log('Selecting next amplifier input!');
execCmd('Amplifier', 'InputNext', 1, function (res) {
console.log("Command Amplifier InputNext executed with result : " + res);
});
});
app.intent('SelectChromeCast',
{
"slots" : {},
"utterances" : ["{to|} {select|} {chrome cast|chromecast}"]
},
function (req, res) {
res.say('Selecting Chromecast!');
console.log('Selecting Chromecast!');
execCmd('TV', 'InputHdmi3', 1, function (res) {
console.log("Command TV InputHdmi3 executed with result : " + res);
});
});
app.intent('SelectTivo',
{
"slots" : {},
"utterances" : ["{to|} select tivo"]
},
function (req, res) {
res.say('Selecting tivo!');
console.log('Selecting tivo!');
execCmd('TV', 'InputHdmi2', 1, function (res) {
console.log("Command TV InputHdmi2 executed with result : " + res);
});
});
app.intent('SelectPlaystation',
{
"slots" : {},
"utterances" : ["{select|} {playstation}"]
},
function (req, res) {
res.say('Selecting ps4!');
console.log('Selecting ps4!');
execCmd('TV', 'InputHdmi1', 1, function (res) {
console.log("Command TV InputHdmi1 executed with result : " + res);
});
});
app.intent('TurnOff',
{
"slots" : {},
"utterances" : ["{shutdown|good night|power everything off|power off everything|turn everything off|turn off everything|shut down}"]
},
function (req, res) {
res.say('Turning off everything!');
console.log('Turning off everythign!');
execActivity('PowerOff', function (res) {
console.log("Command to PowerOff executed with result : " + res);
});
});
app.intent('Movie',
{
"slots" : {},
"utterances" : ["{movie|start movie|watch movie}"]
},
function (req, res) {
res.say('Turning on Movie Mode!');
console.log('Turning on Movie Mode!');
execActivity('Watch a Movie', function (res) {
console.log("Command to Watch a Movie executed with result : " + res);
});
});
app.intent('TIVO',
{
"slots" : {},
"utterances" : ["{tivo|start tivo|watch tivo}"]
},
function (req, res) {
res.say('Turning on Tivo Mode!');
console.log('Turning on Tivo Mode!');
execActivity('Watch Tivo', function (res) {
console.log("Command to Watch Tivo executed with result : " + res);
});
});
app.intent('Music',
{
"slots" : {},
"utterances" : ["{music|start music}"]
},
function (req, res) {
res.say('Turning on Music Mode!');
console.log('Turning on Music Mode!');
execActivity('Listen to Digital Music', function (res) {
console.log("Command to Music executed with result : " + res);
});
});
/**
* Creates an intent function for a specific channel configuration
*
* @param {object} channel - The channel configuration to create the function for
* @returns {function} The channel intent function
*/
function getChannelFunction(channel) {
return function (req, res) {
res.say('Starting to ' + channel.utterance_name + '!');
console.log('Starting to ' + channel.utterance_name + '!');
var cmd = [], channel_chars = channel.channel.split(""), j;
for (j = 0; j < channel_chars.length; j++) {
cmd[j] = 'NumericBasic,' + channel_chars[j];
}
execActivityCmd(channel.activity, cmd, 1);
}
}
if (conf.channels) {
// Iterate through the configured channels and create intents for them
var channel_index;
for (channel_index = 0; channel_index < conf.channels.length; channel_index++) {
var channel = conf.channels[channel_index];
// Build an intent name
var intent = channel.activity.replace(" ", "");
intent = intent.charAt(0).toUpperCase() + intent.slice(1);
var utterance = channel.utterance_name.replace(" ", "");
utterance = utterance.charAt(0).toUpperCase() + utterance.slice(1);
intent = intent + utterance;
app.intent(intent,
{
"slots" : {},
"utterances" : ["{to|} " + channel.utterance_name]
},
getChannelFunction(channel));
console.log('Added intent ' + intent +
' with utterance ' + channel.utterance_name +
' which triggers channel ' + channel.channel );
}
}
module.exports = app;