-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
104 lines (85 loc) · 2.88 KB
/
Copy pathapp.js
File metadata and controls
104 lines (85 loc) · 2.88 KB
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
const express = require('express');
const bodyParser = require('body-parser');
const { Sequelize } = require('sequelize');
const { QueryTypes } = require('sequelize');
const cors = require('cors');
const cron = require('node-cron');
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getFormattedDate() {
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
function saveBpmAnormal(batimentos, status) {
sequelize.query(
'INSERT INTO BATIMENTOS (BPM, USER_ID, CRIADO_EM, STATUS) VALUES (?, ?, ?, ?)',
{
// depois pegar do usuario que esta logado
replacements: [batimentos, 1 /*userId*/, getFormattedDate(), status != null ? status : null],
type: QueryTypes.INSERT,
}
);
}
// cron.schedule('*/1 * * * * *', () => {
// try {
const randomNumber = getRandomInt(50, 120);
// let status = null;
// if (randomNumber > 100) {
// status = "ACIMA"
// } else if (randomNumber < 60) {
// status = "ABAIXO";
// }
// if (randomNumber > 100 || randomNumber < 60) {
// saveBpmAnormal(randomNumber, status);
// }
// } catch (e) {
// console.log('Erro: ', e);
// return;
// }
// });
const usuarioRoutes = require('./routes/usuarioRoutes');
const servicoRoutes = require('./routes/servicoRoutes');
const batimentosRoutes = require('./routes/batimentosRoutes');
const app = express();
app.use(cors({
origin: '*',
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization'],
exposedHeaders: ['Content-Type', 'Authorization'],
credentials: true
}));
app.use(bodyParser.json());
// Configuração do Sequelize
const sequelize = new Sequelize('PROJETO_DATABASE', 'root', '', {
host: 'localhost',
dialect: 'mysql',
});
// const sequelize = new Sequelize('projeto_database', 'api_user', 'senhaforte123', {
// host: 'localhost',
// dialect: 'mysql'
// });
sequelize.authenticate()
.then(() => {
console.log('✅ Conectado ao MySQL');
return sequelize.sync();
})
.then(() => {
console.log('✅ Tabelas sincronizadas');
})
.catch(err => console.error(' Erro:', err));
app.use(usuarioRoutes);
app.use(servicoRoutes);
app.use(batimentosRoutes);
const port = 3000;
app.listen(port, () => {
console.log(`Servidor rodando em http://localhost:${port}`);
});