-
Notifications
You must be signed in to change notification settings - Fork 5
/
remote.js
92 lines (74 loc) · 1.99 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
'use strict';
/*
Created by David Walsh - https://davidwalsh.name
Happy Streaming!
*/
const readline = require('readline');
const request = require('request');
const Roku = require('node-roku');
const xml2json = require('xml2json');
// Will be populated once a device is found
let address = null;
// Map to this URL: http://******:8060/keypress/{key}
const keyEndpoint = {
// Arrow Keys
left: 'Left',
right: 'Right',
up: 'Up',
down: 'Down',
// Standard Keys
space: 'Play',
backspace: 'Back',
return: 'Select',
// Sequences (shift key)
H: 'home',
R: 'Rev',
F: 'Fwd',
S: 'Search',
E: 'Enter',
// Other
r: 'InstantReplay',
b: 'InfoBackspace'
};
const xmlToObject = xml => {
return JSON.parse(xml2json.toJson(xml));
}
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
console.log('Looking for the (first) Roku...');
// Find the Roku
// TODO: Allow for selection of multiple Rokus; current assuming only one
Roku.find((err, devices) => {
if(err) {
console.log('`roku.find` error: ', err);
process.exit();
}
if(!devices.length) {
console.log('No Roku devices found. Bailing.');
process.exit();
}
address = devices[0];
Roku.getDevice(address, (err, deviceDetail) => {
console.log('Connected to Device: ', xmlToObject(deviceDetail).root.device.friendlyName, ' (', devices[0], ')');
console.log('Press keys to navigate the Roku and select content!');
});
});
// Start the keypress listener
process.stdin.on('keypress', (str, key) => {
// Ignore everything until we're connected
if(address === null) {
return;
}
// "Raw" mode so we must do our own kill switch
if(key.sequence === '\u0003') {
process.exit();
}
// Handle commands
let endpoint = keyEndpoint[key.name] || keyEndpoint[key.sequence] || 'Lit_' + key.name;
// Ignore undefined keypresses (no name or sequence)
if(endpoint === 'Lit_undefined') {
return;
}
// Send command!
request.post(address + '/keypress/' + endpoint);
});