-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
108 lines (103 loc) · 4.15 KB
/
Copy pathserver.js
File metadata and controls
108 lines (103 loc) · 4.15 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
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const Sequelize = require('sequelize');
var formidable = require("formidable"),
util = require('util');
var models = require('./models');
var net = require('net');
var tcpGuests = [];
app.set('view engine', 'jade');
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res){
models.Mensaje.findAll({limit: 5, order: [['updatedAt', 'DESC']]}).then(function(mensajes) {
models.Conf.findAll({limit:1,order: [['updatedAt', 'DESC']] }).then(function(conf) {
res.render('index', {
title: 'Pet Center',
mensajes: mensajes,
conf: conf[0]
})
})
})
});
app.post('/',(req,res) => {
procesarForm(req,res);
});
function procesarForm(req,res){
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
models.Conf.create({hora_alarma: fields.input_horaalarma, hora_actual: fields.input_horaactual, racion: fields.selector }).then (
conf => {
console.log(conf.get({plain: true}))
models.Mensaje.create({contenido: "Configuración actualizada"}).then(
msj =>{
console.log(msj.get({plain: true}))
}
)
//Transmitir los cambios de configuracion a la CIAA
for (g in tcpGuests){
var msj = undefined;
if(conf.hora_actual != '' && conf.hora_alarma != ''){
msj = '|A|'+conf.hora_actual+'|'+conf.hora_alarma+'|'+conf.racion+'|||\r\n';
} else if(conf.hora_actual == '' && conf.hora_alarma == '') {
msj = '|D|'+conf.racion+'|||\r\n';
} else if(conf.hora_actual == ''){
msj = '|C|'+conf.hora_alarma+'|'+conf.racion+'|||\r\n';
} else {
msj = '|B|'+conf.hora_actual+'|'+conf.racion+'|||\r\n';
}
tcpGuests[g].write(msj);
}
}
)
});
models.Mensaje.findAll({limit: 5, order: [['updatedAt', 'DESC']]}).then(function(mensajes) {
models.Conf.findAll({limit:1,order: [['updatedAt', 'DESC']] }).then(function(conf) {
res.render('index', {
title: 'Pet Center',
mensajes: mensajes,
conf: conf[0]
})
})
})
}
var server = net.createServer(function(socket) {
socket.setEncoding('utf8');
tcpGuests.push(socket);
socket.on('data',function(data){
//La CIAA envia un mensaje y el servidor lo guarda para mostrarlo en la tabla de eventos.
//console.log(data);
if (data.slice(0, 7) == "getData"){
models.Conf.findAll({limit:1,order: [['updatedAt', 'DESC']] }).then(function(conf) {
var config = conf[0];
for (g in tcpGuests){
var msj = undefined;
if(config.hora_alarma == ''){
msj='|D|'+config.racion+'|||\r\n';
}
else {
msj='|C|'+config.hora_alarma+'|'+config.racion+'|||\r\n';
}
tcpGuests[g].write(msj);
}
});
models.Mensaje.create({contenido: "Se conecto el dispenser"}).then(
msj =>{
console.log(msj.get({plain: true}))
})
} else{
models.Mensaje.create({contenido: data.slice(0, -1)}).then(
msj =>{
console.log(msj.get({plain: true}))
})
}
});
socket.on("error", function(err) {
console.log("Caught flash policy server socket error: ");
console.log(err.stack)
});
}).listen(1337, '192.168.0.205');
http.listen(process.env.PORT ||8090, function(){
console.log('server http en 8090');
});