-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtester.js
129 lines (103 loc) · 3.02 KB
/
tester.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
/*CONTROLPAD v0.1 by Trevize Daneel
This is a control script for Beaglebone Black
*/
var b = require('bonescript');
//var t = require('./lib/tank.js');
var exec = require('child_process').exec;
var app = require('http').createServer(handler);
var io = require('socket.io').listen(app);
var fs= require('fs');
var path = require('path');
app.listen(9090);
function handler(request,response){
var filePath = '.' + request.url;
if (filePath == './')
filePath = './index.html';
var extname = path.extname(filePath);
var contentType = 'text/html';
switch (extname) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
case '.jpg':
contentType = "image/jpeg";
break;
}
console.log("Serving file:" + filePath + " (" + contentType +")" );
path.exists(filePath, function(exists) {
if (exists) {
fs.readFile(filePath, function(error, content) {
if (error) {
response.writeHead(500);
response.end();
}
else {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
}
});
}
else {
response.writeHead(404);
response.end();
}
});
}
io.sockets.on('connection',function(socket){
socket.on('moveRelative',function(data){
console.log("Socket :" + socket);
console.log("Request :" + data);
//t.moveRelative(data.direction,data.duration);
//t.moveRelative(data.toLowerCase(),3000);
//socket.emit('datastatus','sent');
//socket.broadcast.emit('dataupdate','ok);
});
socket.on('rotateHead',function(data){
console.log("Socket :" + socket);
console.log("Request :" + data);
//t.headRotate(data.angle);
socket.emit('datastatus',"Current angle: " + data.angle);
//socket.broadcast.emit('dataupdate','ok);
});
socket.on('capture',function(data){
exec('capturewebcam', function callback(error, stdout, stderr){
console.log("Error: "+error);
socket.emit('capturestatus','1');
});
getTheNewImage(function(data,size){
socket.emit('firstChunkSent', data, size)
});
});
});
function getTheNewImage(callback){
var filename = 'capture.jpg';
var base64FileSize = fs.stat(filename,function(err,stats){
if (err) { throw err; }
//return stats.size
callback(null,stats.size);
});
var readable = fs.createReadStream(filename, { encoding: 'base64' });
readable.on('readable', function() {
var getImageData = function(){
while (null !== (base64ImageData = readable.read())) {
return base64ImageData
}
}
callback(getImageData(),base64FileSize)
});
readable.on('end', function() {
console.log('there will be no more data.')
});
readable.on('error', function(err) {
console.log('here is the error: '+err)
readable.end(err);
});
}
console.log("System ON");
//t.resetSystem();
//function control(d1){
// t.moveRelative(d1,3000);
//}