-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlovelace.js
126 lines (98 loc) · 2.99 KB
/
lovelace.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
/**
* Module Dependencies
* */
var express = require('express');
var app = express();
var http = require('http').Server(app);
var path = require('path');
var bodyParser = require('body-parser');
var io = require('socket.io')(http);
var rethinkdb = require("./rethinkDB.js");
var mongoDB = require("./mongoDB.js");
var pgrouting = require("./pgrouting.js");
/**
* View engine
* */
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(bodyParser.json()); //json as param support
app.use(bodyParser.urlencoded({ extended: false })); //data as param support
app.use(express.static(path.join(__dirname, 'public'))); //Make resources public
app.use('/map', express.static('public')); //Make resources public at mapa/css/xxx
app.use('/cars', express.static('public')); //Make resources public at car/css/xxx
app.use(express.static(path.join(__dirname, 'test'))); //Make test directory public... for... testing....
/**
* Set rethinkDB and mongoDB... and pgrouting as a global resource.
* Someday i'm going to optimize this @_@...
* */
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
req.rethinkdb = rethinkdb;
req.mongoDB = mongoDB;
req.pgrouting = pgrouting;
req.io = io;
next();
});
/**
* Routers controllers
* */
var indexRouter = require("./routes/router.js");
var mapaRouter = require("./routes/map.js");
var carRouter = require("./routes/car.js");
var carsRouter = require("./routes/cars.js");
app.use("/", indexRouter);
app.use("/map", mapaRouter);
app.use("/car", carRouter);
app.use("/cars", carsRouter);
/**
* Socket handlers
* */
io.on('connection', function(socket){
socket.on("carsData", function(){
mongoDB.getCollections(function(collections){
for(var i = 0; i < collections.length; i++){
findAndEmit(collections[i].name, socket);
}
});
});
socket.on("carFeatures", function(idCar) {
mongoDB.getCollectionData({},idCar, function(err, data) {
if(!err){
socket.emit("carFeatures", data);
}
});
});
});
function findAndEmit(idCar, socket){
mongoDB.getCollectionData({collectionProperties: true}, idCar, function(err,data){
if( !err ){
var transferObject = {
idCar: idCar,
data: data[0]
};
socket.emit("carData", transferObject);
}
});
}
/**
*
* Service Configuration
*
* */
app.set('port', process.env.PORT || 16502);
/**
*
* Start
*
* */
http.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
/**
*
* RethinkDB listener.
* When a change happens in the database it's sent through broadcasting with all the new information to the clients.
*
* */
rethinkdb.listenUpdates(io);
});