-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleitura.js
71 lines (62 loc) · 2.46 KB
/
leitura.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
'use strict';
var express = require('express');
var router = express.Router();
var isNull = require('../script').isNull;
var Database = require('../Database');
const Cryptr = require('cryptr');
const config = require('../config');
const cryptr = new Cryptr(config.security.key);
const moment = require('moment-timezone');
router.get('/', (req, res, next) => {
//console.log(req.session);
/*if (!req.session.user || req.session.user && !req.session.user.logged_in) {
res.redirect('/login');
} else {*/
let limit = 50;
Database.query(`SELECT TOP ${limit} momento, temperatura, umidade FROM leitura`).then(results => {
results = results.recordsets[0];
let data = [['momento', 'temperatura', 'umidade']];
for (let i = 0; i < results.length; i++) {
let row = results[i];
//let momento = moment(row.momento).format('YYYY, MM, DD, HH, mm, ss');
let momento = moment(row.momento).format('HH-mm-ss');
let entry = [momento, row.temperatura, row.umidade];
data.push(entry);
}
res.json(data);
}).catch(error => {
res.status(400).json({message: "error reading database", error: error});
});
});
router.get('/dt', (req, res, next) => {
let limit = 50;
let response = {};
Database.query(`SELECT TOP ${limit} momento, temperatura, umidade FROM leitura ORDER BY id DESC`).then(results => {
results = results.recordsets[0];
response.cols = [
{id: 'momento', label: 'momento', type: 'timeofday'},
{id: 'temperatura', label: 'temperatura', type: 'number'},
{id: 'umidade', label: 'umidade', type: 'number'}
];
let rows = [];
console.log(results);
for (let i = 1; i < results.length; i++) {
let row = results[i];
//let momento = moment(row.momento).format('YYYY, MM, DD, HH, mm, ss');
let momento = moment(row.momento).format('HH-mm-ss').split('-');
let entry = {
c: [{v: momento},
{v: row.temperatura},
{v: row.umidade}
]
};
rows.push(entry);
}
response.rows = rows;
res.json(response);
}).catch(error => {
console.log(error);
res.status(400).json({message: "error reading database", error: error});
});
});
module.exports = router;