-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrouting_server.js
More file actions
330 lines (273 loc) · 10.4 KB
/
Copy pathrouting_server.js
File metadata and controls
330 lines (273 loc) · 10.4 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* HTTP INTERFACE FOR VEHICLE ROUTING VISUALISATION
*
* This is a node.js application that provides an interface
* for the vehicle routing visualiser that uses Google Maps
* to draw routes, jobs and vehicles.
*
* This server is used to allow for Google Maps API calls without
* interacting directly with the page or using other server side
* technology that needs to perform a page refresh in order to update
* the visualisation state.
*
* We use web sockets to establish a direct connection between the
* HTML client page displaying the map and the server that serves as
* a generic RESTFULL-like interface to this map.
*
* author: Davide Nunes
* webpage: http://davidenunes.com
*/
var application_root = __dirname,
express = require("express"),
path = require("path");
var app = express();
var pendingHttpResponse;
//Configure express framework web app
app.configure(function(){
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(application_root, "public")));
app.use(express.errorHandler({dumpExceptions: true, showStack: true}));
});
/**********************************************************************
* CHECK THE CONNECTINO TO THE API
*
* dummy method returns OK if http://localhost:6969/api is requested
***********************************************************************/
app.get('/api', function (request, result){
result.send('OK');
});
/**********************************************************************
* SET MAP CENTER
* query string should be something like:
* "http://localhost:6969/api/set/mapcenter/2,4"
***********************************************************************/
app.get('/api/set/mapcenter/:lat/:lng', function (req, res){
console.log('mapcenter set');
io.sockets.emit('setMapCenter', req.params.lat, req.params.lng);
res.send('OK');
});
/**********************************************************************
* ADD A MARKER TO THE MAP
*
* Adds a generic marker to the map
***********************************************************************/
app.get('/api/add/marker/:id/:color/:lat/:lng/:title', function(req, res){
var id = req.params.id;
var color = req.params.color;
var lat = req.params.lat;
var lng = req.params.lng;
var title = req.params.title;
console.log(req);
io.sockets.emit('addMarker', id, color, lat, lng, title);
res.send('OK');
});
/**********************************************************************
* ADD A WORKER TO THE MAP
*
* Places a Worker Marker in a given (latitude, longitude)
* with a given name.
***********************************************************************/
app.get('/api/add/worker/:id/:lat/:lng/:name', function(req, res){
var id = req.params.id;
var lat = req.params.lat;
var lng = req.params.lng;
var name = req.params.name;
console.log(req);
io.sockets.emit('addWorker', id, lat, lng, name);
res.send('OK');
});
/**********************************************************************
* ADD A JOB TO THE MAP
*
* Places a Worker Marker in a given (latitude, longitude) with a
* given title or name
***********************************************************************/
app.get('/api/add/job/:id/:lat/:lng/:title', function(req, res){
var id = req.params.id;
var lat = req.params.lat;
var lng = req.params.lng;
var title = req.params.title;
console.log(req);
io.sockets.emit('addJob', id, lat, lng, title);
res.send('OK');
});
/**********************************************************************
* ADD A DEPOT
*
* Adds a depot marker to the map in a given (latitude, longitude)
* with a given ID and name or title
***********************************************************************/
app.get('/api/add/depot/:id/:lat/:lng/:title', function(req, res){
var id = req.params.id;
var lat = req.params.lat;
var lng = req.params.lng;
var title = req.params.title;
console.log(req);
io.sockets.emit('addDepot', id, lat, lng, title);
res.send('OK');
});
/**********************************************************************
* REMOVE DEPOT
*
* Removes a depot from the map by ID
***********************************************************************/
app.get('/api/remove/depot/:id', function(req, res){
var id = req.params.id;
io.sockets.emit('removeDepot', id);
res.send('OK');
});
/**********************************************************************
* REMOVE A PREVIOUSLY ADDED JOB
*
* Removes a job marker with a given id
***********************************************************************/
app.get('/api/remove/job/:id', function(req, res){
var id = req.params.id;
io.sockets.emit('removeJob', id);
res.send('OK');
});
/**********************************************************************
* COMPLETE A PREVIOUSLY ADDED JOB
*
* Changes a job marker to display its status as completed
***********************************************************************/
app.get('/api/set/job/done/:id', function(req, res){
var id = req.params.id;
io.sockets.emit('setJobDone', id);
res.send('OK');
});
/**********************************************************************
* REMOVE A PREVIOUSLY ADDED WORKER
*
* Removes a worker marker with a given id
***********************************************************************/
app.get('/api/remove/worker/:id', function(req, res){
var id = req.params.id;
io.sockets.emit('removeWorker', id);
res.send('OK');
});
/**********************************************************************
* CLEAR MAP
*
* removes all the markers and routes from the map
***********************************************************************/
app.get('/api/remove/all', function(req, res){
io.sockets.emit('removeAll');
res.send('OK');
});
/**********************************************************************
* MOVE WORKER ON A ROUTE
*
* moves a worker along an existing route updating its position
*
* params:
*
* workerID - an id for the worker marker previously added
* routeID - an if for a previously added route
* distance - the distance travelled by the worker since the begining
* of the route in meters
*
* example: the worker moved 20 meters in the route 1
* note: this is not comulative. It allways places a worker in the route
* counting a distance from the starting point.
*
***********************************************************************/
app.get('/api/update/worker/position/:workerID/:routeID/:distance', function(req, res){
var workerID = req.params.workerID;
var routeID = req.params.routeID;
var distance = req.params.distance;
io.sockets.emit('moveWorker', workerID, routeID, distance);
res.send('OK');
});
/**********************************************************************
* GET WORKER POSITION
*
* This might be usefull to know the exact (latitude, longitude) of a
* worker using the Google Maps API.
*
* Note: This is the only method that performs a query to the
* visualisation client HTML page. The client should only be used to
* draw the state of a vehicle routing simulation, nevertheless, this
* can be usefull to know where exactly the workers are in a route
* if necessary.
***********************************************************************/
app.get('/api/get/worker/position/:workerID', function(req,res){
var workerID = req.params.workerID;
io.sockets.emit('getWorkerPosition', workerID);
pendingHttpResponse = res;
});
/**********************************************************************
* SET WORKER POSITION
*
* Sets a worker marker position to a given (latitude, longitude)
***********************************************************************/
app.get('/api/set/worker/position/:workerID/:lat/:lng', function(req, res){
var workerID = req.params.workerID;
var lat = req.params.lat;
var lng = req.params.lng;
io.sockets.emit('setWorkerPosition', workerID, lat, lng);
res.send('OK');
});
/************************************************************************************
* ADD A ROUTE TO THE MAP
*
* Adds a route to the map with a given id, starting coordinates and ending coordinates
* the route is drawn according to the google API.
*
* ex, http://localhost:6969/api/add/route/1/38.75609130/-9.15650370/38.757/-9.15651
************************************************************************************/
app.get('/api/add/route/:id/:lat1/:lng1/:lat2/:lng2', function(req,res){
var id = req.params.id;
var lat1 = req.params.lat1;
var lng1 = req.params.lng1;
var lat2 = req.params.lat2;
var lng2 = req.params.lng2;
io.sockets.emit('createRoute', id, lat1, lng1, lat2, lng2);
res.send('OK');
});
/**********************************************************************
* REMOVE A PREVIOUSLY ADDED ROUTE
*
* Remove a route previously added to the map with a given ID
***********************************************************************/
app.get('/api/remove/route/:id', function(req, res){
var id = req.params.id;
io.sockets.emit('removeRoute', id);
res.send('OK');
});
/**
* the express framework and socket.io are running at the same time
* in this case, the express framework supplies the basic HTTP interface
* for this server. The socket.io supplies the capability to communicate
* with the HTML visualisation client via web sockets.
*/
//start the server on a given port
var server = app.listen(6969);
//starts socket.io with the previous server instance
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
/**
* Used to receive a response from the visualisation
* client regarding the worker position on a route.
*/
socket.on('workerPosition',
function(lat,lng){
pendingHttpResponse.send(lat+";"+lng);
});
});