/
webApp.js
122 lines (104 loc) · 3.58 KB
/
webApp.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
/**
* Created by spolu on 02.06.2016.
*/
let express = require('express');
let path = require('path');
let log = require('libs/log')(module);
let http = require('http');
let config = require('config');
let routes = require('./routes/index');
let Terminal = require('models/terminal').Terminal;
let pubsub = require('libs/pubsub');
class WebApp {
constructor() {
this._onSocketConnection = this._onSocketConnection.bind(this);
this._server = null;
this._socket = null;
this._app = express();
this._app.engine("ejs", require('ejs-locals'));
this._app.set('views', path.join(__dirname, 'views'));
this._app.set('view engine', 'ejs');
this._app.use(express.static(path.join(__dirname, 'public')));
this._app.use('/', routes);
this._updateRxCount = this._updateRxCount.bind(this);
this._updateTxCount = this._updateTxCount.bind(this);
this._updateRxCountDel = this._updateRxCountDel.bind(this);
this._updateConnectionStatus = this._updateConnectionStatus.bind(this);
pubsub.on('rxElemData', this._updateRxCount);
pubsub.on('txElemData', this._updateTxCount);
pubsub.on('rxElemDataDel', this._updateRxCountDel);
pubsub.on('connectionStatus', this._updateConnectionStatus);
}
_updateRxCountDel(data) {
if (this._socket) {
this._socket.emit('updateRxCountDel', data);
}
}
_updateRxCount(data) {
if (this._socket) {
this._socket.emit('updateRxCount', data);
}
}
_updateTxCount(data) {
if (this._socket) {
this._socket.emit('updateTxCount', data);
}
}
_updateConnectionStatus(data) {
if (this._socket) {
this._socket.emit('updateConnectionStatus', data);
}
}
_addRowToTable(term) {
let data = {};
data.termId = term.terminalId[3] << 24 | term.terminalId[2] << 16 |
term.terminalId[1] << 8 | term.terminalId[0];
data.rxElemCount = term.elem.length;
data.txElemCount = 0;
if (this._socket) {
this._socket.emit('addToTable', data);
}
}
createServer() {
this._server = http.createServer(this._app);
this._server.listen(config.get('webPort'));
this._server.on('error', this._onError.bind(this));
this._server.on('listening', this._onListening.bind(this));
}
_onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
let bind = 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
log.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
log.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
_onListening() {
let addr = this._server.address();
let bind = 'port ' + addr.port;
log.info('Listening on ' + bind);
this._io = require('socket.io')(this._server);
this._io.on("connection", this._onSocketConnection);
}
_onSocketConnection(socket) {
log.info('socket.io user connected');
this._socket = socket;
Terminal.find({}, function(err, terminals) {
terminals.forEach(function(term) {
this._addRowToTable(term);
}.bind(this));
}.bind(this));
}
}
module.exports = WebApp;