-
Notifications
You must be signed in to change notification settings - Fork 17
/
app.js
147 lines (129 loc) · 3.9 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
/**
* Module dependencies.
*/
var express = require('express'),
routes = require('./routes'),
socket = require('./routes/socket.js'),
config = require('./config.js'),
app = module.exports = express(),
server = require('http').createServer(app)
, https = require('https')
, path = require('path')
, io = require('socket.io').listen(server)
, spawn = require('child_process').spawn
, omx = require('omxcontrol')
, EventEmitter = require( "events" ).EventEmitter;
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
app.engine('html', require('ejs').renderFile);
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/public'));
app.use(app.router);
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', routes.index);
app.get('/partials/:name', routes.partials);
// redirect all others to the index (HTML5 history)
app.get('*', routes.index);
// Socket.io Communication
//Run and pipe shell script output
function run_shell(cmd, args, cb, end) {
var spawn = require('child_process').spawn,
child = spawn(cmd, args),
me = this;
child.stdout.on('readable', function () { cb(me, child.stdout); });
child.stdout.on('end', end);
}
var ss;
//Socket.io Server
io.sockets.on('connection', function (socket) {
socket.on("screen", function(data){
socket.type = "screen";
ss = socket;
console.log("Screen ready...");
});
socket.on("remote", function(data){
socket.type = "remote";
console.log("Remote ready...");
});
socket.on("controll", function(data){
console.log(data);
if(socket.type === "remote"){
if(data.action === "tap"){
if(ss != undefined){
ss.emit("controlling", {action:"enter"});
}
}
else if(data.action === "swipeLeft"){
if(ss != undefined){
ss.emit("controlling", {action:"goLeft"});
}
}
else if(data.action === "swipeRight"){
if(ss != undefined){
ss.emit("controlling", {action:"goRight"});
}
}
}
});
socket.on("log", function(data){
console.log(data);
});
socket.on("video", function(data){
if( data.action === "play"){
console.log(data.video_id);
var id = data.video_id,
url = "http://www.youtube.com/watch?v="+id;
var runShell = new run_shell('youtube-dl',['-gf', '18/22/34/35/37', url],
function (me, stdout) {
me.stdout = stdout.read().toString().replace(/[\r\n]/g, "");
socket.emit("video",{output: "loading"});
omx.start(me.stdout);
omx.ev.on('omx_status', function(data) {
socket.emit("video",{status: data, video_id:id});
});
},
function (me) {
socket.emit("video",{status: "now_playing", video_id:id});
});
}
if( data.action == "stream") {
var id = data.video_id,
url = "https://api.put.io/v2/files/"+id+"/mp4/stream/?oauth_token="+config.settings.PUTIO_KEY;
var options = {
host: 'api.put.io',
port: 443,
path: '/v2/files/'+id+'/stream?oauth_token='+config.settings.PUTIO_KEY,
method: 'GET'
};
var req = https.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
omx.start(res.headers.location);
});
req.end();
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
}
if (data.action == "pause") {
omx.sendKey('p');
}
if (data.action == 'quit') {
omx.sendKey('q');
}
});
});
// Start server
server.listen(3000, function(){
console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});