-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirecTVSim.js
More file actions
151 lines (116 loc) · 4.43 KB
/
Copy pathdirecTVSim.js
File metadata and controls
151 lines (116 loc) · 4.43 KB
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
/**
* Created by mkahn on 11/16/16.
*/
// Mimics a DirecTV box
var os = require( 'os' );
var colors = require( 'colors' );
var keypress = require('keypress');
var _ = require('lodash');
var myIp = getIPAddresses()[ 0 ]; // brittle, but whatever
var VERBOSE = true;
var Server = require( 'node-ssdp' ).Server;
//Lets require/import the HTTP module
var http = require( 'http' );
//Lets define a port we want to listen to
const PORT = 8080; // This is hardwired into the DirecTV API, so make sure nothing else is on this port on your system
function logv( msg ) {
if ( VERBOSE ) {
console.log( msg );
}
}
function getIPAddresses() {
var interfaces = os.networkInterfaces();
var addresses = [];
for ( var k in interfaces ) {
for ( var k2 in interfaces[ k ] ) {
var address = interfaces[ k ][ k2 ];
if ( address.family === 'IPv4' && !address.internal ) {
addresses.push( address.address );
}
}
}
return addresses;
}
var server = new Server( {
ssdpSig: "Linux/2.6.18.5, UPnP/1.0 DIRECTV JHUPnP/1.0",
location: "http://" + getIPAddresses() + ":9900/upnp/desc.xml"
} );
server.addUSN( 'upnp:rootdevice' );
server.addUSN( 'urn:schemas-upnp-org:device:MediaServer:1' );
server.on( 'advertise-alive', function ( headers ) {
// Expire old devices from your cache.
// Register advertising device somewhere (as designated in http headers heads)
} );
server.on( 'advertise-bye', function ( headers ) {
// Remove specified device from cache.
} );
// start the server
server.start();
process.on( 'exit', function () {
server.stop() // advertise shutting down and stop listening
} )
//Create a server
var httpserver = http.createServer( function ( request, response ) {
var url = request.url;
//Return the goofy XML SSDP description file
if ( url.indexOf( 'xml' ) > -1 ) {
console.log("Dumping XML description back to inquirer");
var fs = require( 'fs' );
fs.readFile( "dtvresponse.xml", "utf-8", function(err, data){
response.end( data );
} );
} else if ( url.indexOf("getTuned")>-1) {
console.log( "Returning tuning info" );
response.end( JSON.stringify( getChannelInfo() ) );
} else if ( url.indexOf("getVersion") > -1 ){
console.log( "Returning uid info" );
response.end( JSON.stringify( { receiverId: "fake-receiver-yo"} ) );
} else if ( url.indexOf("tune") > -1 ){
console.log( "Returning tune info" );
var channel = parseInt(url.substring(url.indexOf("=") + 1));
channelIdx = _.findIndex(channelInfo, {major: channel});
response.end( JSON.stringify( { returnVal: "returning 200"} ) );
} else {
response.writeHead( 400, 'No such route', { 'content-type': 'text/plain' } );
response.end("Bad route, homie!");
}
} );
//Lets start our server
httpserver.listen( PORT, function () {
//Callback triggered when server is successfully listening. Hurray!
console.log( "Server listening on: http://localhost:%s", PORT );
} );
var channelIdx = 0;
function getChannelInfo(){
return channelInfo[channelIdx];
}
var channelInfo = [
{ callsign: "ESPNHD", major: 206, minor: 65535, programId: 36417953, stationId: 2220255, title: "Yoga Caliente" },
{ callsign: "CNNHD", major: 202, minor: 65535, programId: 36417952, stationId: 2220225, title: "News & Stuff" },
{ callsign: "BEIN", major: 620, minor: 65535, programId: 3617953, stationId: 220255, title: "Soccer is Dull" },
{ callsign: "FOX", major: 2, minor: 65535, programId: 3647953, stationId: 20255, title: "FOXy Show" }
];
// make `process.stdin` begin emitting "keypress" events
keypress(process.stdin);
// listen for the "keypress" event
process.stdin.on('keypress', function (ch, key) {
if (key && key.ctrl && key.name == 'c') {
process.exit()
}
if(key && key.name) {
switch (key.name) {
case 'up':
channelIdx = (channelIdx + 1) % channelInfo.length;
console.log('changed channel up'.green);
break;
case 'down':
channelIdx = (channelIdx - 1) < 0 ? channelInfo.length - 1 : channelIdx - 1;
console.log('changed channel down'.green);
break;
default:
process.stdout.write(String(key.sequence));
}
}
});
// process.stdin.setRawMode(true);
// process.stdin.resume();