var express = require('express');
var socket_io = require("socket.io");

var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var index = require('./routes/index');
var music = require('./routes/music');

var app = express();

var io = socket_io();
app.io = io;

var exec = require('child_process').exec;

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
app.use('/music', music);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

// socket.io events
io.on( "connection", function( socket ) {
    console.log( "Control connected" );

    socket.on("car-navigation", function(value) {
       var direction = value.direction;
       var speed = value.speed;                                                                   
       if(direction == "up") {
           exec("/root/mr_robot/tools/control/write "+speed+","+speed+"#",{silent:false});
       } else if(direction == "down"){
           exec("/root/mr_robot/tools/control/write "+(-speed)+","+(-speed)+"#",{silent:false});
       } else if(direction == "right"){
           exec("/root/mr_robot/tools/control/write "+speed+","+(-speed)+"#",{silent:false});
       } else if(direction == "left"){
           exec("/root/mr_robot/tools/control/write "+(-speed)+","+speed+"#",{silent:false});
       } else if(direction == "stop"){
           exec("/root/mr_robot/tools/control/write 0,0#",{silent:false});
       }

       console.log("Recieved:");
       console.log(value);
       console.log("-------------");
    });

    socket.on("disconnect", function(){
        console.log("Control disconnected");
    });
});

module.exports = app;

@@ -1541,24 +1541,25 @@
 ion;%0A       
+ 
 var speed = 
@@ -1574,76 +1574,11 @@
 eed;
-                                                                   %0A
+%0A%0A 
     
@@ -1611,35 +1611,43 @@
 %0A           
+ 
 exec(%22/
-root
+home/debian
 /mr_robot/to
@@ -1669,25 +1669,33 @@
 te %22
-+
+ + 
 speed
-+%22,%22+
+ + %22,%22 + 
 speed
-+
+ + 
 %22#%22,
@@ -1698,33 +1698,33 @@
 %22#%22,%7Bsilent:
-fals
+tru
 e%7D);%0A
+ 
        %7D els
@@ -1746,14 +1746,15 @@
 == %22
-down
+left
 %22)
+ 
 %7B%0A  
@@ -1758,35 +1758,43 @@
 %0A           
+ 
 exec(%22/
-root
+home/debian
 /mr_robot/to
@@ -1808,33 +1808,35 @@
 trol/write %22
-+
+ + 
 (-speed)
 +%22,%22+(-speed
@@ -1823,30 +1823,33 @@
 (-speed)
-+%22,%22+(-
+ + %22,%22 + 
 speed
-)+
+ + 
 %22#%22,%7Bsil
@@ -1844,36 +1844,35 @@
 d + %22#%22,%7Bsilent:
-fals
+tru
 e%7D);%0A       %7D el
@@ -1859,32 +1859,33 @@
 :true%7D);%0A       
+ 
 %7D else if(direct
@@ -1899,18 +1899,20 @@
 %22right%22)
+ 
 %7B%0A
+ 
         
@@ -1913,36 +1913,43 @@
          exec(%22/
-root
+home/debian
 /mr_robot/tools/
@@ -1967,27 +1967,33 @@
 te %22
-+
+ + 
 speed
-+%22,%22+
+ + %22,%22 + 
 (-speed)
 +%22#%22
@@ -1988,17 +1988,19 @@
 (-speed)
-+
+ + 
 %22#%22,%7Bsil
@@ -1999,33 +1999,33 @@
 %22#%22,%7Bsilent:
-fals
+tru
 e%7D);%0A
+ 
        %7D els
@@ -2047,16 +2047,18 @@
 == %22
-left
+down
 %22)
+ 
 %7B%0A
+ 
     
@@ -2063,36 +2063,43 @@
          exec(%22/
-root
+home/debian
 /mr_robot/tools/
@@ -2117,17 +2117,19 @@
 te %22
-+
+ + 
 (-speed)
 +%22,%22
@@ -2128,19 +2128,28 @@
 eed)
-+%22,%22+
+ + %22,%22 + (-
 speed
-+
+) + 
 %22#%22,
@@ -2152,33 +2152,33 @@
 %22#%22,%7Bsilent:
-fals
+tru
 e%7D);%0A
+ 
        %7D els
@@ -2202,18 +2202,126 @@
  %22stop%22)
-%7B%0A
+ %7B                                                                                                          %0A 
         
@@ -2334,12 +2334,19 @@
 c(%22/
-root
+home/debian
 /mr_
@@ -2385,20 +2385,19 @@
 %7Bsilent:
-fals
+tru
 e%7D);%0A   
@@ -2400,17 +2400,19 @@
 %0A       
-%7D
+ %7D%0A
 %0A%0A      

Update app.js