-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
124 lines (104 loc) · 2.78 KB
/
server.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
const express = require('express')
const app = express()
const server = require('http').createServer(app)
const io = require('socket.io')(server)
const env = require('dotenv')
const mongoose = require('mongoose')
const router = express.Router()
const cors = require('cors')
// MONGODB
mongoose.connect('mongodb://localhost/prueba-db', { useNewUrlParser: true, useUnifiedTopology: true } )
.then(db => console.log('DB is connected'))
.catch(err => console.error(err))
// settings
env.config()
app.use(express.json())
app.use(cors())
app.use('/api', router)
//require('./app/events/socket')(io)
let parametros = {
t: [],
h: [],
date: []
}
// const t = []
// const h = []
// const date = []
let socketLed = false
let USERS = {}
io.on('connection', (socket) => {
// connect
console.log(`${socket.id} was connected`)
USERS[socket.id] = socket
// escucha
// disconnect
socket.on('disconnect', () => {
console.log(`${socket.id} was disconnected`)
})
})
setInterval(() => {
// TOOD SEND PACKAGE
for (let i in USERS) {
USERS[i].emit('led', socketLed)
}
}, 1000 / 25)
setInterval(() => {
// TOOD SEND PACKAGE
for (let i in USERS) {
USERS[i].emit('params', parametros)
}
}, 5000)
// ENDPOINTS
//app.use('/api', require('./routes/api'))
const Parametro = require('./app/models/Parametro')
const Led = require('./app/models/Led')
router.get('/parametro', async (req, res) => {
parametros = {
t: [],
h: [],
date: []
}
const params = await Parametro.find().sort({_id:-1}).limit(10)
params.forEach(dato => {
parametros.t.push(dato.temperature)
parametros.h.push(dato.humidity)
parametros.date.push(dato.createdAt)
})
res.json(parametros)
})
router.get('/led', async (req, res) => {
const ledStatus = await Led.find().sort({_id:-1}).limit(1)
res.json(ledStatus[0].ledStatus)
})
router.post('/parametro', async (req, res) => {
const parametro = new Parametro(req.body)
parametro.createdAt = new Date()
await parametro.save()
parametros = {
t: [],
h: [],
date: []
}
params = await Parametro.find().sort({_id:-1}).limit(10)
params.forEach(dato => {
parametros.t.push(dato.temperature)
parametros.h.push(dato.humidity)
parametros.date.push(dato.createdAt)
})
res.json({
status: 'saved'
})
})
router.post('/led', async (req, res) => {
const led = new Led(req.body)
led.createdAt = new Date()
socketLed = led.ledStatus
await led.save()
res.json({
status: led.ledStatus
})
})
server.listen(process.env.SOCKET_PORT, () => {
console.log(`server works in ${process.env.SOCKET_PORT}`)
})
//app.use(express.static(`${__dirname}/dist`))