-
Notifications
You must be signed in to change notification settings - Fork 0
/
vehicle.js
126 lines (109 loc) · 2.94 KB
/
vehicle.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
var exec = require('child_process').exec;
var status = {};
var clients = {}; // Clients list
// Broadcast to all clients
function broadcast(message){
for (var client in clients) clients[client].write(JSON.stringify(message));
}
setInterval(function(){
broadcast(new Date());
},500);
// create sockjs server
var echo = require('sockjs').createServer();
// on new connection event
echo.on('connection', function(conn) {
console.log(conn.remoteAddress + ":" + conn.remotePort + " " + conn.headers.host);
// add this client to clients object
console.log("on connection " + conn.id );
clients[conn.id] = conn;
// on receive new data from client event
conn.on('data', function(message) {
//console.log("on data " + conn.id + message);
var msg = JSON.parse(message);
console.log(msg);
switch (msg.require) {
case 'forward':
exec("gpio write 10 1");
exec("gpio write 14 1");
break;
case 'forward_stop':
exec("gpio write 10 0");
exec("gpio write 14 0");
break;
case 'backward':
exec("gpio write 6 1");
exec("gpio write 11 1");
break;
case 'backward_stop':
exec("gpio write 6 0");
exec("gpio write 11 0");
break;
case 'left_forward':
exec("gpio write 14 1");
break;
case 'left_forward_stop':
exec("gpio write 14 0");
break;
case 'left_backward':
exec("gpio write 6 1");
break;
case 'left_backward_stop':
exec("gpio write 6 0");
break;
case 'right_forward':
exec("gpio write 10 1");
break;
case 'right_forward_stop':
exec("gpio write 10 0");
break;
case 'right_backward':
exec("gpio write 11 1");
break;
case 'right_backward_stop':
exec("gpio write 11 0");
break;
case 'stop':
exec("gpio write 6 0");
exec("gpio write 10 0");
exec("gpio write 11 0");
exec("gpio write 14 0");
break;
case 'clockwise':
exec("gpio write 11 1");
exec("gpio write 14 1");
break;
case 'counterclockwise':
exec("gpio write 6 1");
exec("gpio write 10 1");
break;
default:
break;
}
});
// on connection close event
conn.on('close', function() {
console.log("on close " + conn.id );
delete clients[conn.id];
});
});
// Create an http server
var server = require('http').createServer();
// 2. Static files server
var node_static = require('node-static');
var static_directory = new node_static.Server(__dirname);
server.addListener('request', function(req, res) {
static_directory.serve(req, res); //require('node-static').Server(__dirname).serve(req, res); // index.html
});
server.addListener('upgrade', function(req,res){
res.end();
});
// Integrate SockJS and listen on /echo
echo.installHandlers(server, {prefix:'/echo'});
// Start server
server.listen(8080, '0.0.0.0');
// to initiate GPIO pins
var initGPIO = "gpio mode 6 out; gpio mode 10 out; gpio mode 11 out; gpio mode 14 out; gpio write 6 0; gpio write 10 0; gpio write 11 0; gpio write 14 0";
exec(initGPIO, function(error, stdout, stderr) {
console.log(stdout);
});
// end of file