-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
188 lines (158 loc) · 4.98 KB
/
server.js
File metadata and controls
188 lines (158 loc) · 4.98 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
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
var webSocketServer = require('ws').Server;
const express = require("express");
let devicesList = {};
let guid = () => {
let s4 = () => {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
//return id of format 'aaaaaaaa'-'aaaa'-'aaaa'-'aaaa'-'aaaaaaaaaaaa'
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}
//App setup
const PORT = 5000;
const app = express();
const server = app.listen(PORT,()=>{
console.log(`Listening on port ${PORT}`);
console.log(`http://localhost:${PORT}`);
});
//Static files
app.use(express.static("./public"));
//Socket
var socket = new webSocketServer({ port: 3000 });
//Sirve para poder manejar los eventos como si se tratara de la libreria de Socket.io
let eventhandler = {};
let io = {};
io.on = (event,callback)=>{
eventhandler[event] = callback;
}
//Devuelve el socket basado en el id
io.findSocket = (id)=>{
let soc;
socket.clients.forEach(client=>{
if(client.id == id){
soc = client;
}
});
return soc;
}
//Envia mensaje a todos los usuarios
io.sendMessageUsers = (event,data)=>{
socket.clients.forEach(client=>{
if(client.user){
client.send(JSON.stringify({
event:event,
data:data
}))
}
});
}
//Envia mensaje a cualquier cliente basandose en el id
io.sendMessage = (event,id,data)=>{
socket.clients.forEach(client=>{
if(client.id == id){
client.send(JSON.stringify({
event:event,
data:data
}))
}
});
}
//Envia mensaje a los ESP basandose en id
io.sendMessageEsp = (event,id,data)=>{
socket.clients.forEach(client=>{
if(client.id == id){
client.send(`${event+data}`)
}
});
}
//io.on manejan los mensaje enviados por los clientes
io.on("new user", (data)=>{
data.socket.user = true;
console.log("usuario conectado");
data.socket.tipo = 1;
io.sendMessage("user info",data.socket.id,{id:data.socket.id,devices:devicesList});
});
io.on("new device",(data)=>{
data.socket.user = false;
data.socket.esp = false;
console.log("dispositivo conectado");
data.socket.tipo = 2;
devicesList[data.socket.id] = {
name: data.data.name,
estado:"online",
encendido: false,
tipo: data.data.tipo
}
io.sendMessage("device info",data.socket.id,{id:data.socket.id,encendido:devicesList[data.socket.id].encendido});
io.sendMessageUsers("update devices",devicesList);
// console.log(JSON.stringify(data, null, 2));
});
io.on("new device esp",(data)=>{
data.socket.user = false;
data.socket.esp = true;
console.log("dispositivo conectado");
data.socket.tipo = 2;
devicesList[data.socket.id] = {
name: data.data.name,
estado:"online",
encendido: false,
tipo: data.data.tipo
}
io.sendMessageEsp("0",data.socket.id,'f');
io.sendMessageUsers("update devices",devicesList);
// console.log(JSON.stringify(data, null, 2));
});
io.on("change color",(data)=>{
if(io.findSocket(data.data.id).esp)
io.sendMessageEsp("2",data.data.id,data.data.color);
else
io.sendMessage("2",data.data.id,data.data.color);
});
io.on("active device",(data)=>{
if(io.findSocket(data.data.id).esp){
io.sendMessageEsp("1",data.data.id,data.data.comando);
}
else
io.sendMessage("1",data.data.id,data.data.comando);
})
io.on("resDevice",(data)=>{
devicesList[data.socket.id].encendido = data.data;
io.sendMessageUsers("update devices",devicesList);
});
io.on("ping",(data)=>{
console.log("ping")
data.socket.isAlive = true;
})
//Verifica que los dispositivos no esten deconectados
const interval = setInterval(()=>{
socket.clients.forEach(ws => {
if(ws.esp){
if (ws.isAlive === false){
return ws.terminate();
}
ws.isAlive = false;
}
});
}, 5000);
//Escucha si un dispositivo se ha conectado al servidor
socket.on('connection', function (socketObject) {
console.log("Dispositivo conectado");
socketObject.id = guid();
socketObject.user = false;
//Escucha si un dispositivo a enviado un mensaje
socketObject.on('message', function (message) {
//Para que el manejador de eventos funciones la respuesta debe ser un json con por lo menos el atributo event y el atributo data
eventhandler[JSON.parse(message).event]({idSocket: socketObject.id,socket:socketObject,data:JSON.parse(message).data});
});
//Escuecha si un dispositivo se ha desconectado
socketObject.on('close', function (c, d) {
console.log(`El usuario: ${socketObject.id} se desconecto`);
if(socketObject.tipo == 2){
delete devicesList[socketObject.id];
io.sendMessageUsers("update devices",devicesList);
}
});
});
console.log('Server started');