-
Notifications
You must be signed in to change notification settings - Fork 9
/
cli.js
executable file
·122 lines (108 loc) · 2.75 KB
/
cli.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
#!/usr/bin/env node
/*
* This script implements a node.js/JavaScript version of the hdhomerun_config
* utility that ships with the libhdhomerun C library.
*/
var hdhr = require('../index.js');
var util = require('util');
var fs = require('fs');
var channelscan = require('./channelscan.js');
var vidstream = require('./vidstream.js');
var rtp = require('./rtp-readable.js');
var argv = process.argv;
var argc = argv.length - 2;
function usage() {
console.log('Usage:\n' +
'\tcli.js discover\n' +
'\tcli.js <id> get help\n' +
'\tcli.js <id> get <item>\n' +
'\tcli.js <id> set <item> <value>\n' +
'\tcli.js <id> scan <tuner> [<filename>]\n' +
'\tcli.js <id> save <tuner> [<filename>]\n');
process.exit(0);
}
if ((argc == 0) || (argv[2].match(/^[-]*help/)))
usage();
if (argv[2] == 'discover') {
hdhr.discover(function (err, res) {
if (err) throw new Error('discover error');
res.forEach(function (dev) {
console.log('hdhomerun device %s found at %s',
dev.device_id, dev.device_ip);
});
process.exit(0);
});
} else if (argv[2].match(/^[0-9a-fA-F]{8}$/)) {
hdhr.discover(argv[2], function (err, res) {
if (err) throw err;
var device = hdhr.create(res[0]);
switch (argv[3]) {
case ('get'):
device.get(argv[4], function (err, res) {
if (err) throw err;
console.log(res.value)
process.exit(0);
});
break;
case ('set'):
device.set(argv[4], argv[5], function (err, res) {
if (err) throw err;
process.exit(0);
});
break;
case ('scan'):
if (argv[4]) {
var scanner = channelscan.create(
{device: device, tuner: argv[4]});
scanner.on('found', function (channel) {
console.log('--- channel found ---');
console.log(channel.status);
console.log(channel.streaminfo);
});
scanner.on('done', function (num_found) {
console.log('found %d channels',
num_found);
process.exit(0);
});
scanner.scan();
} else {
usage();
}
break;
case ('save'):
if (argv[4] && argv[5]) {
var outfile = fs.createWriteStream(argv[5]);
var progress = 0;
var sock = vidstream.start(device, argv[4]);
var filter;
sock.once('listening', function () {
filter = rtp.createRTPStream(sock);
filter.pipe(outfile);
filter.on('error', function (err) {
util.print('n');
});
});
var progress = setInterval(function () {
util.print('.');
}, 1000);
process.on('SIGINT', function () {
clearInterval(progress);
vidstream.stop(device, argv[4],
function () {
outfile.end();
console.log('%d bytes received',
outfile.bytesWritten);
process.exit(0);
});
});
} else {
usage();
}
break;
default:
usage();
}
});
} else {
usage();
}