Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
129 lines (94 loc) · 3.55 KB
/
Copy pathapp.js
File metadata and controls
129 lines (94 loc) · 3.55 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
/**
* Module dependencies.
*/
var express = require('express')
, app = express()
, http = require('http')
, server = http.createServer(app)
, path = require('path')
, routes = require('./routes')
, user = require('./routes/user')
, io = require('socket.io').listen(server)
var clients = {};
var connectCounter = 0;
var spawn = require('child_process').spawn;
var appHome = "/home/pi/astromesh/"
//var appHome = "/Users/cbgrey/Development/Personal/Droids/astromesh-r2pi/"
var driveCommand = appHome + "adafruit/Adafruit_PWM_Servo_Driver/r2drive.py";
var stopCommand = appHome + "adafruit/Adafruit_PWM_Servo_Driver/r2drivestop.py";
var testCommand = appHome + "adafruit/Adafruit_PWM_Servo_Driver/mactest.py";
server.listen(7777,"0.0.0.0") ;
console.log('Server running at http://127.0.0.1:7777/');
app.configure(function(){
app.set('port', process.env.PORT || 7777);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
app.get('/users', user.list);
var driveController = spawn(testCommand);
driveController.stdin.write('Beginning test.');
// TODO: Should this be the main interval loop of a short time
// like 100ms and then trigger different events at different time
// periods. ping at 1 second. check for heartbeat at 500ms etc.
setInterval(function() {
ts = Math.round(new Date().getTime() / 1000);
console.log('[Sending PING. ' + ts + ' ]' + '[ ' + connectCounter + ' clients connected.]');
io.sockets.emit('ping',ts);
},500);
io.sockets.on('connection',function(socket) {
console.log('Connection Received. Socket ID: ' + socket.id);
clients[socket.id] = socket;
connectCounter++;
socket.on('pong',function(data) {
console.log('[Received PONG.]');
});
socket.on('subscribe', function(roomNumber) {
socket.join(roomNumber);
console.log('Socket joined room: ' + roomNumber)
//sendConfiguration(socket,roomNumber);
});
socket.on('unsubscribe', function(roomNumber) {
socket.leave(roomNumber);
console.log('Socket left room: ' + roomNumber)
//sendERROR(socket,"Something went very very wrong!");
});
socket.on('configuration', function(data) {
// Make sure the client is connected to a room. If not, send error.
console.log('----Config--Config--Config--Config----');
//console.log('Socket requested configuration. Will send this data: ' + JSON.stringify(roomData[data]) + ' For room ' + data);
//sendConfiguration(socket,data);
});
socket.on('drive', function(data) {
console.log('---> DRIVING. Direction: ' + data.direction + " Speed: " + data.speed);
if (data.speed != 0 ) {
commandString = "drive:" + data.speed + ":" + data.direction + "\n";
driveController.stdin.write(commandString);
}else{
console.log('STOP DRIVING. \n');
driveController.stdin.write("drive:0:0" + "\n");
}
});
socket.on('EMERGENCYSTOP', function(data) {
console.log('---> EMERGENCY STOP STOP STOP STOP');
driveController.stdin.write("EMERGENCYSTOP:STOP" + "\n");
});
socket.on('dome', function(data) {
console.log('---> DOME ROTATING. Direction: ' + data.direction);
});
socket.on('playSound', function(data) {
console.log('Playing Sound: ' + data.name);
});
socket.on('disconnect', function () {
connectCounter--;
});
});