-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
101 lines (81 loc) · 3.29 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
/****************************************************************************
**
** www.celtab.org.br
**
** Copyright (C) 2013
** Gustavo Valiati <[email protected]>
** Luis Valdes <[email protected]>
** Thiago R. M. Bitencourt <[email protected]>
**
** This file is part of the FishBook project
**
** 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; version 2
** of the License.
**
** 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, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
****************************************************************************/
var express = require('express')
, app = express() // Web framework to handle routing requests
, cons = require('consolidate') // Templating library adapter for Express
, MongoClient = require('mongodb').MongoClient // Driver for connecting to MongoDB
, routes = require('./routes'), // Routes for our application
path = require('path');
// Socket.io
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io').listen(server, {log: false});
MongoClient.connect('mongodb://localhost:27017/rfidmonitor', function(err, db) {
"use strict";
if(err) throw err;
// Register our templating engine
app.engine('html', cons.swig);
app.set('view engine', 'jade');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(path.join(__dirname, 'public')));
// Port
app.set('port', process.env.PORT || 8082);
// Express middleware to populate 'req.cookies' so we can access cookies
app.use(express.cookieParser());
// Express middleware to populate 'req.body' so we can access POST variables
app.use(express.bodyParser());
// When in dev print pretty html
if ('development' == app.get('env')) {
app.use(express.logger('dev'));
app.locals.pretty = true;
}
process.on('uncaughtException', function (err) {
console.error(err.stack);
console.log("Node NOT Exiting...");
});
// Application routes
routes(app, db);
// Server Socket TCP/IP
var collectorPointServer = require('./core/CollectorPointServer')(io, db);
// SocketIO
io.sockets.on('connection', function (socket) {
collectorPointServer.ServerEmitter.on('rfiddata', function (data) {
socket.emit('rfiddata',data);
});
collectorPointServer.ServerEmitter.on('collectors_status', function (data) {
socket.emit('collectors_status',data);
});
socket.on('end', function (data) {
console.log('Disconnect: '+data);
});
});
server.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
// Start listeting TCP/IP connections
collectorPointServer.start();
});