-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
193 lines (164 loc) · 5.6 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
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
'use strict';
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const mysql = require('mysql');
const bodyParser = require('body-parser');
const sassMiddleware = require('node-sass-middleware');
const morgan = require('morgan');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
//var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
var flash = require('connect-flash');
//var config = require('./config'); // get our config file
const routes_index = require('./routes/index');
const routes_sensors = require('./routes/sensors');
const routes_users = require('./routes/user');
const routes_tiposensors = require('./routes/tiposensor');
import { Sensor } from "./db/db_sensor";
import { TipoSensor } from "./db/db_tiposensor";
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.use(cookieParser());
//
app.use(session({
secret: 'secret',
saveUninitialized: true,
resave: true
}));
app.use(flash());
// Passport init
app.use(passport.initialize());
app.use(passport.session());
/*
================================================================================
RUTAS
================================================================================
*/
app.use('/', routes_index);
app.use('/sensor', routes_sensors);
app.use('/tiposensor', routes_tiposensors);
app.use('/usuarios', routes_users);
/*
================================================================================
MOTOR DE VISTAS
================================================================================
*/
app.set('view engine', 'ejs');
/*
================================================================================
VARIABLES LOCALES EXPRESS
================================================================================
*/
app.locals.moment = require('moment');
app.locals.moment.locale('es');
/*
================================================================================
CARPETAS ESTATICAS DEL SERVIDOR
================================================================================
*/
app.use(express.static('public'));
app.use(express.static('public/assets'));
/*
================================================================================
LAYOUT
Estado: false
Descripcion: Se define la ruta del layout, false si no se usará alguna
================================================================================
*/
app.set('layout', false); // defaults to 'layout'
//app.set('superSecret', config.secret); // secret variable
/*
================================================================================
SEGURIDAD
Express Session
Descripcion: Almacenamiento de mensajes en la sesion con flash
================================================================================
*/
// Global Vars
app.use(function (req, res, next) {
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.error = req.flash('error');
res.locals.user = req.user || null;
next();
});
/*
================================================================================
SOCKET IO
Descripcion: Tiempo real
================================================================================
*/
// Define/initialize our global vars
let notes = []; //Todas las notas registradas
let socketCount = 0; // Cantidad de usuarios conectados
io.sockets.on('connection', function(socket) {
// Socket has connected, increase socket count
socketCount++;
// Let all sockets know how many are connected
io.sockets.emit('users connected', socketCount);
socket.on('disconnect', function() {
// Decrease the socket count on a disconnect, emit
socketCount--;
io.sockets.emit('users connected', socketCount)
});
});
/**
* Consulta database y envia a los clientes cada 2 s
* @return {[type]} [description]
*/
var watch = function() {
new Sensor().getDatosSensores(function(rows) {
new Sensor().getSensores(function(datos) {
const data = {
datosensores: rows,
tiposensores: datos
}
io.sockets.emit('datos datos', data);
});
});
setTimeout(watch, 2000);
}
watch();
/*
Insertar nota en la base de datos
Luego se emite la nota insertada a todos los clientes activos
Despues se envia un json al cliente que realiza la solicitud post
*/
app.post('/add/:item', function(req, res) {
let data = req.params['item'];
data = {
note: data
};
insertNote(data) //Si se inserta correctamente entonces se emite a los clientes conectados el nuevo dato
res.json(data); //Se retorna el varlo
});
/*app.get('/sensor/show_one_graphic/:idSensor', function(req, res, next) {
const idSensor = req.params['idSensor'];
console.log('id: ' + idSensor);
new Sensor().getSensorById(idSensor, function(row) {
if (row) {
res.render('./public/views/sensors/show_one_graphic', {
sensor: row[0]
});
console.log("Entrós app");
io.sockets.emit('datos uno', row);
}
res.redirect('/list');
});
});*/
/*
* Escuchador
* Listen 3000
* */
http.listen(3000, function() {
console.log('listening on *:3000');
});