-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·222 lines (189 loc) · 8.88 KB
/
server.js
File metadata and controls
executable file
·222 lines (189 loc) · 8.88 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
(function() {
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;
var BotConnection = require('create-oi'),
BotSerial = 'COM4',
express = require('express'),
csrgen = require('csr-gen'),
//fs = require('fs'),
stylus = require('stylus'),
nib = require('nib'),
http = require('http'),
//https = require('http'),
path = require('path'),
os = require('os'),
childProcess = require('child_process'),
open = require('open'),
ifaces = os.networkInterfaces(),
io,
app,
server,
networkCard = 'Wireless Network Connection',
IP,
BotLocal = false,
userConnected = false,
BOT_NAME = 'ETA',
connectedSocket,
timer = 5,
botReady,
botReadyFlag = false;
botReady = function (Bot) {
if (!botReadyFlag) {
botReadyFlag = true;
for (var dev in ifaces) {
var alias = 0;
ifaces[dev].forEach(function (details) {
if (details.family=='IPv4') {
//uncomment console.log to see network cards for variable above
//console.log(dev+(alias?':'+alias:''),details.address);
if (details.address !== '127.0.0.1' || details.address !== undefined && networkCard.indexOf(dev+(alias?':'+alias:''+'')) > -1) {
IP = details.address;
}
++alias;
}
});
}
/*csrgen(IP, {
outputDir: '/',
read: true,
company: 'Quicken Loans',
email: 'nickfaust-doty@quickenloans.com'
}, function(err, keys){
console.log('CSR created!')
console.log('key: '+keys.private);
console.log('csr: '+keys.csr);
});*/
app = express();
var compile = function (str, path) {
return stylus(str).set('filename', path).use(nib());
};
app.set('port', process.env.PORT || 3001);
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
//app.set('key', fs.readFileSync('/privatekey.pem').toString());
//app.set('cert', fs.readFileSync('/certificate.pem').toString());
app.use(stylus.middleware({
src: __dirname + '/public',
compile: compile
}));
app.use('/components', express.static(path.join(__dirname, 'components')));
server = http.createServer(app);
server.listen(app.get('port'));
app.get('/', function (req, res) {
if (!BotLocal) {
BotLocal = true; //once local is connected set it so another is unable display the default page.
res.render('index');
} else if (BotLocal && !userConnected) { //Only allow one connection at a time.
userConnected = true; //once user is connected set it so another is unable display the default page.
res.render('control');
} else if (BotLocal && userConnected) {
res.render('multiple'); //Even though a user connection is there display something to keep user informed
}
});
io = require('socket.io').listen(server, { log: false, secure: false });
console.log('Remote Control page for ' + BOT_NAME + ' located at http://' + IP + ":" + app.get('port') + '/control ');
console.log('A browser will appear in ' + timer + ' seconds to initiate video stream for ' + BOT_NAME + '.');
/*setTimeout(function () {
childProcess.exec('start chrome --kiosk "http://localhost:' + app.get('port') + '"');
}, timer * 1000); //1000 as setIntervel is in milleseconds*/
setTimeout(function () {
open('http://localhost:' + app.get('port'));
}, timer * 1000); //1000 as setIntervel is in milleseconds
io.sockets.on('connection', function (socket) {
connectedSocket = socket;
socket.on('disconnect', function () {
if (connectedSocket.handshake.address.address === '127.0.0.1') {
BotLocal = false;
} else {
userConnected = false;
}
});
socket.emit('BotReady');
socket.on('BotCommand', function (command) {
var action = command.action,
direction = command.direction ? command.direction : null,
speed = command.speed,
message = '';
if (direction === 'back') {
speed = speed * -1;
}
if (direction === 'right' || direction === 'left') {
speed = speed - 100;
}
if (direction === 'right') {
speed = speed * -1;
}
if (action === 'stop') {
message = 'No Commands present, Stopping ' + BOT_NAME;
console.log(message);
socket.emit('BotMessages', message);
return Bot.drive(0, 0);
} else if (action !== 'dock') {
message = 'Moving Bot: ' + direction;
console.log(message);
socket.emit('BotMessages', message);
return Bot[action](speed,0);
} else if (action === 'dock') {
message = 'Attempting to Dock Bot.';
console.log(message);
socket.emit('BotMessages', message);
return Bot.dock();
}
});
});
var bumpHndlr = function (bumperEvt) {
var message,
speed,
Bot = this;
message = 'Obstacle detected! Repositioning!';
console.log(message);
connectedSocket.emit('BotBump', message);
speed = 200;
// temporarily disable further bump events
// getting multiple bump events while one is in progress
// will cause weird interleaving of our Bot behavior
Bot.off('bump');
// backup a bit
Bot.drive(-speed, 0);
//wait to stabalize
Bot.wait(1000);
// turn based on which bumper sensor got hit
switch(bumperEvt.which) {
case 'forward':
message += ' Obstacle was forward. '+ BOT_NAME + ' will back up some.';
console.log(message);
connectedSocket.emit('BotBump', message);
Bot.drive(-150, 0);
Bot.wait(250);
break;
case 'left':
message += ' Obstacle was on the left. ' + BOT_NAME + ' automatically turning right to avoid obstacle.';
console.log(message);
connectedSocket.emit('BotBump', message);
Bot.rotate(-speed); // turn right
Bot.wait(400);
break;
case 'right':
message += ' Obstacle was on the right. ' + BOT_NAME + ' automatically turning left to avoid obstacle.';
console.log(message);
connectedSocket.emit('BotBump', message);
Bot.rotate(speed); // turn left
Bot.wait(400);
break;
}
Bot.drive(0, 0);
message += ' Repositioning Done! ' + BOT_NAME + ' Ready!';
console.log(message);
connectedSocket.emit('BotBump', message);
// turn handler back on
Bot.on('bump', bumpHndlr);
};
Bot.on('bump', bumpHndlr);
}
}
BotConnection.init({ serialport: BotSerial });
BotConnection.on('ready', function () {
botReady(this);
});
}).call(this);