-
Notifications
You must be signed in to change notification settings - Fork 4
/
hsvtt.js
311 lines (268 loc) · 9.41 KB
/
hsvtt.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
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
// Getting all dependencies
const express = require('express');
const app = express();
const mongoose = require('mongoose');
// var postmark = require("postmark")(process.env.POSTMARK_API_KEY);
const geoConst = require('./data/geoConst.json');
const http = require('http').Server(app);
const io = require('socket.io')(http);
const bodyParser = require('body-parser');
const geoUtils = require('./lib/geoutils');
const schedule = require('./lib/cronSchedules');
// Proper logging
const bunyan = require('bunyan');
const log = bunyan.createLogger({ name: 'transitTracks' });
log.info('Bunyan initialized.');
var pastStopSeq = 18;
var nextStopSeq = 0;
var possibleSkip = false;
// ALL the vehicles
var vehicles = [];
// edit
// Setup DB
const mongoUser = process.env.MONGO_USERNAME;
const mongoPass = process.env.MONGO_PASSWORD;
const mongoUrl = 'mongodb:// '+mongoUser+':'+mongoPass+'@ds053164.mongolab.com:53164/hsvtransit';
mongoose.connect(mongoUrl);
// Shuttle/trolly/auto DB setup
const transitSchema = new mongoose.Schema({
id: Number,
long: Number,
lat: Number,
});
const Transit = mongoose.model('Transit', transitSchema);
var allLocations = [];
// Event DB structure
const eventSchema = new mongoose.Schema({
id: Number,
time: String,
date: String,
name: String,
desciption: String,
xcorr: Number,
ycoor: Number,
});
const Event = mongoose.model('Event', eventSchema);
// Statistics DB structure
const statsSchema = new mongoose.Schema({
id: Number,
hits: Number,
});
const Stats = mongoose.model('Stats', statsSchema);
const userConnSchema = new mongoose.Schema({
id: String,
cipaddr: String,
connStart: { type: Date, default: Date.now },
connEnd: Date,
});
const UserConn = mongoose.model('UserConn', userConnSchema);
const userSchema = new mongoose.Schema({
email: String,
name: String,
pass: String,
});
const User = mongoose.model('User', userSchema);
app.set('port', (process.env.PORT || 5000));
// Setting directory structure
app.set('views', __dirname+'/views');
app.set('view engine', 'ejs');
app.use(express.static(__dirname+'/public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Setup socket.io
// app.http().io();
app.get('/', function(req, res) {
res.render('pages/index');
Stats.find({id: 0}, function(err, stat) {
var statContents = stat;
if (statContents[0]) {
statContents[0].hits += 1;
statContents[0].save();
}
});
});
app.get('/test', function(req, res) {
io.emit('location update', [34.73172, -86.58979]);
res.send('Success');
});
app.get('/stats', function(req, res) {
Stats.find({ id: 0 }, function(err, stat) {
if (stat[0]) {
res.send('Hits: ' +stat[0].hits);
} else {
res.send('Error getting stats.');
}
});
});
var latLng = [];
var locations;
var latLongs = {};
function checkStops(curPnt) {
// console.log("checking to see if near stop: nextStop = " + nextStopSeq + " : " + curPnt);
// for each in stop array
var sb = null // --geoUtils.setStopBounds(nextStopSeq -1);
var ns = nextStopSeq;
var len = geoConst.dtStopArray.length - 1;
var advance = false;
var fin = false;
// while (!advance && !fin) {
// var cnt = ns;
for (var i = 0; i < len && !advance; ++i) {
var test_b = geoUtils.setStopBounds(ns - 1);
// console.log("testing: " + ns);
if (geoUtils.contains(curPnt, test_b)) {
// if (nextStopSeq === pastStopSeq + 1 ) {
advance = (nextStopSeq === i);
// } else {
// advance = false;
// }
if (advance) {
pastStopSeq = i;
nextStopSeq = i + 1;
// nextStopBounds = geoUtils.setStopBounds(nextStopSeq -1);
console.log('advancing stop seq = ' + nextStopSeq + ' : ' + pastStopSeq);
var data = {seq:nextStopSeq, route:'Downtown', id:0 }
io.emit('next stop', data);
return;
}
} else {
ns = ns < len ? ++ns : 1;
// console.log("next test: " + ns);
}
// console.log("nextStop now = " + nextStopSeq);
}
}
function locationRecieved(data) {
var returnStr = 'location socket called: ';
var transitId = data.id;
var vehicleFound = false;
console.log('here: ' + data.id + ' : ' + data.lat + ' - ' + data.lon);
for (var i = 0; i < vehicles.length; i++) {
if(vehicles[i]['id']==transitId) {
vehicleFound = true;
if (geoUtils.contains([data.lat,data.lon], geoConst.dtBounds)) {
vehicles[i]['lat'] = data.lat;
vehicles[i]['long'] = data.lon;
// checkStops([vehicles[i]['lat'],vehicles[i]['long']]);
returnStr = returnStr.concat("location updated");
} else {
returnStr = returnStr.concat("location update failed");
console.log('Invalid location update');
}
}
}
if (vehicleFound === false) {
vehicles.push({'id': transitId, 'lat': data.lat, 'long': data.lon});
returnStr = "new bus location added";
console.log(returnStr);
}
return returnStr;
}
// Trolley Service Schedule - Will need schedule for each route
function isTrolleyInactive() {
// would like to extend this to start at 4pm and end at 1am following morning... of course
// that complicates the testing
var trolleyInactive = false; // named the variable for readability
var date = new Date();
date.setHours(date.getHours()); // minus 6 from UTC time - CHANGE for DAYLIGHT/STANDARD TIME
console.log("hour: " + date.getHours() + ", day: " + date.getDay());
if (date.getDay() === 5 && date.getHours() <= 24 && date.getHours() >= 16) {
console.log("first test: " + trolleyInactive);
trolleyInactive = false;
}
if (trolleyInactive && date.getDay() === 6 && ( (date.getHours() <= 24 &&
date.getHours() >= 16) || (date.getHours() === 0))) {
console.log('second test: ' + trolleyInactive);
trolleyInactive = false;
}
if ( trolleyInactive && date.getDay() == 0 && date.getHours() == 0 ) {
console.log("third test: " + trolleyInactive);
trolleyInactive = false;
}
return trolleyInactive;
}
// Everything socket.io related
io.sockets.on('connection', function(socket) {
/*
Object.keys(socket).forEach(function (key) {
console.log("key (socket): " + key + " value: " + (socket)[key]);
console.log("address: " + socket.handshake.address);
});
*/
console.log("id: " + socket.id + " address: " + socket.handshake.address);
// newConnect = new UserConn ( {id: socket.id, cipaddr: socket.handshake.address, connStart: new Date(), connEnd: null} )
// newConnect.save();
// TODO will need an flag set if the connection is from beacon or client user ---
io.emit('made connect', {nextSeq:nextStopSeq,greet:'hello there'}); // sent to client user
// BEACON listerns won't hear anything until sockets on the beacon is implementation-----
socket.on('bus:connect', function(data) {
console.log('bus connected: ' + data.id + " : " + data.pw);
console.log('bus connected: ' + socket.id + " address: " + socket.handshake.address);
});
socket.on('bus:location', function(data) { // bus:location
console.log('location update: ' + data.id + " : " + data.lat + " - " + data.lon);
locationRecieved(data);
// TODO update vehicles here...;
});
// --------------------------------------------------------------------------------------------
socket.on('get location', function( data ) {
// console.log('location update requested ');
// console.log(allLocations);
// if(isTrolleyInactive()) {
if(false) {
console.log('Sending dormant signal');
io.emit('trolley off', [0,0]);
// io.emit('location update', allLocations); need to change this when running simulation
} else {
console.log('Sending coordinates ' + vehicles);
io.emit('location update', vehicles);
}
});
socket.on('disconnect', function() {
console.log('User disconnected');
UserConn.find({id: socket.id}, function( err, uc ) {
returnStr = "updating connection";
if( uc[0] ) {
returnStr = 'Recording user connection diconnect: ' + uc[0].cipaddr + " - ";
uc[0].connEnd = new Date();
uc[0].save();
returnStr = returnStr.concat("db updated");
} else {
returnStr = returnStr.concat("db update failed");
console.log('Invalid credentials in connection update');
}
});
});
});
/*************************************************
*Admin Functionality
*WARNING: Suspending development of section indefinitely
*************************************************/
/*
app.get('/admin', function(req, res) {
var updates = [];
res.render('pages/admin', {messages: updates});
});
app.get('/admin/addevent', function(req, res) {
res.render('pages/eventadd');
});
*/
// Opening server to requests
http.listen(app.get('port'), function() {
console.log('Node app is running on port ', app.get('port'));
var d = new Date();
d.setHours(d.getHours());
log.info('Time: ' + d.getTime() + ', Day: ' + d.getDay() + ', Hour: ' + d.getHours());
});
// --- Test stuff ---------------------------------------
log.info('Trolley Home: ' + geoConst.trolleyHome);
// console.log('read array ' + geoConst.dtStopArray[seq1][1]);
/*
var testsend = require('./lib/sendNotification');
var to = "[email protected]"
var subject = "Message from user on Hop Around Huntsville"
var message = "This is a test message... hoparoundhuntsville on transittracks-dev has fired up"
var response = null;
testsend.send(to, subject, message, response);
*/
// ----------------------------------------------------------------------------------