-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
142 lines (121 loc) · 2.99 KB
/
server.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
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var geolib = require('geolib');
var isOnline = require('is-online');
var y = {latitude:12.9472408, longitude:77.5758543};
var app = express();
server = app.listen(2000, function()
{
console.log('http server running on port 2000');
});
var socketio = require('socket.io');
var io = socketio(server);
var mysqlConnection = require('./js/mysqlConnection.js');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended:true
}));
app.use('/res', express.static(path.join(__dirname, '/js')));
app.use('/libs', express.static(path.join(__dirname, '/node_modules')));
app.use('/icon', express.static(path.join(__dirname,'/icon')));
app.use('/media', express.static(path.join(__dirname,'/media')));
DISTANCE = 1500; //meters
app.get('/', function(req, res)
{
res.status(200);
res.setHeader('Content-Type','text/html');
res.sendFile(path.join(__dirname, '/index1.html'));
});
app.get('/admin', function(req, res)
{
res.status(200);
res.setHeader('Content-Type', 'text/html');
res.sendFile(path.join(__dirname, '/admin.html'));
});
function getData (socket)
{
var query = "SELECT advertisements.* FROM advertisements INNER JOIN clients ON clients.id=advertisements.clientId";
var connection = mysqlConnection.createDBConnection();
connection.query(query, function(err, rows)
{
adloop(rows, socket);
});
}
inCircleAdQue = [];
function adloop (ads, socket) {
var adsLength = ads.length;
var i = 0;
while (i < adsLength){
var clientCoordinates = {};
clientCoordinates.latitude = ads[i].latitude;
clientCoordinates.longitude= ads[i].longitude;
var distance = geolib.getDistance(y, clientCoordinates);
var indexInQue = inCircleAdQue.indexOf(ads[i].id);
if (distance < DISTANCE)
{
if (indexInQue == -1) {
inCircleAdQue.push(ads[i].id);
ads[i].remove = 0; //1 for dequeing
var response = {};
response.noData = 0;
response.ad = ads[i];
socket.emit('mediaUpdate', response);
}
}
else
{
if (indexInQue != -1) {
inCircleAdQue.splice(indexInQue, 1);
ads[i].remove = 1;
var response = {};
response.noData = 0;
response.ad = ads[i];
socket.emit('mediaUpdate', response);
}
console.log(ads[i].filename + 'is out at '+ distance + ' meters ');
}
i++;
if (i==adsLength)
{
//i=0; //code for general ads
console.log("Stop");
}
}
if (adsLength == 0)
{
var response = {};
response.noData = 1;
response.ad = null;
socket.emit('mediaUpdate', response);
}
};
socket = null;
startEventHandler = function()
{
console.log('starting');
isOnline(isOnlineHandler);
};
isOnlineHandler = function(err, isonline)
{
console.log("checking internet status");
if (err) {
console.log(err);
return;
}
if (isonline)
{
getData(socket);
}
else
{
console.log('offline');
}
};
connectionHandler = function(sock)
{
socket = sock;
inCircleAdQue = [];
socket.on("start", startEventHandler);
};
io.on('connection', connectionHandler);