-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
168 lines (139 loc) · 4.28 KB
/
app.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
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
/**
* Module dependencies.
*/
// Load requires
var express = require('express'), routes = require('./routes');
var net = require('net');
var io = require('socket.io');
var TrackingProvider = require('./mongotracking.js').TrackingProvider;
var SSIDProvider = require('./ssidtracking.js').SSIDProvider;
// MySQL
var MySQLClient = require('mysql').Client, mySQLclient = new MySQLClient();
mySQLclient.user = 'root';
mySQLclient.password = 'jaap83';
// client.host='mysqlserver.com'; //only needed if your mysql server isn't on
// your localhost
// client.port='5673'; //only needed if your mysql port isn't 3306
// mySQLclient.connect();
// test
// var query = mySQLclient.query(
// 'INSERT INTO table '+
// 'SET title = ?, text = ?, created = ?',
// ['another entry', 'because 2 entries make a better test', '2010-08-16
// 12:42:15']
// );
// Create servers
var app = module.exports = express.createServer(), io = io.listen(app);
var wss;
net.createServer(function(socket) {
socket.on('connect', function() {
console.log("Client connected")
});
socket.on('listening', function(data) {
console.log("Listening " + data)
});
socket.on('error', function(data) {
console.log(data)
});
socket.on('data', function(data) {
jsonData = JSON.parse(data.toString());
acc = {}
acc.ts = jsonData.ts
wss.emit("acc", acc)
});
socket.on('end', function() {
console.log("server disconnected")
});
}).listen(2500);
// Configuration
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(require('stylus').middleware({
src : __dirname + '/public',
compress : true
}));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function() {
app.use(express.errorHandler({
dumpExceptions : true,
showStack : true
}));
});
app.configure('production', function() {
app.use(express.errorHandler());
});
var trackingProvider = new TrackingProvider('localhost', 27017);
var ssidProvider = new SSIDProvider('localhost', 27017);
// Sockets
var websocketList = [];
wss = io.sockets.on('connection', function(websocket) {
websocketList.push(websocket);
console.log(websocketList.length + " connections");
websocket.on('disconnect', function() {
websocketList.splice(websocketList.indexOf(websocket), 1);
console.log(websocketList.length + " connections");
});
websocket.on('my other event', function(data) {
console.log(data);
});
});
// Routes
// Web interface
app.get('/', routes.index);
app.get('/test', routes.test);
app.get('/transactions', routes.transactions);
app.get('/ssid', function(req, res) {
// do expensive call
var data = {'abc': 12};
res.render('ssid.jade', {
locals : {
title : 'SSID Tracking',
css : ['/stylesheets/layout.css', '/stylesheets/menu.css', '/stylesheets/footer.css'],
scripts : ['//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', '//maps.googleapis.com/maps/api/js?sensor=false', '/socket.io/socket.io.js', '/javascripts/ssid.js'],
data : data,
}
});
});
// handling mobile requests
app.post('/mobile/loc', function(req, res) {
console.log("Received from HTTP POST: " + req.body.data);
my_obj = JSON.parse(req.body.data)
wss.emit("latlng", my_obj)
trackingProvider.save(my_obj, function(error, docs) {
console.log("Lat/lng inserted")
});
res.end("OK");
});
app.post('/mobile/battery', function(req, res) {
console.log("Received Battery: " + req.body.data);
});
app.post('/mobile/ssid', function(req, res) {
my_obj = JSON.parse(req.body.data)
console.log("Received SSID: " + req.body.data);
ssidProvider.save(my_obj, function(error, docs) {
console.log("SSID inserted")
});
ssidProvider.count(function(error, count) {
var reply = {};
reply['count'] = count;
jsonReply = JSON.stringify(reply);
console.log(jsonReply);
res.write(jsonReply);
res.end("");
});
});
console.log("starting server")
app.listen(80, function() {
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});
// Custom web socket events
// latlng --> for GPS/network location updates from mobile to Web UI
// transaction --> new transaction info (completed, cancelled)
// mobile_on --> mobile phone switched on
// mobile_off --> mobile phone switched off
//