|
|
@ -24,12 +24,31 @@ redis_cli.on("error", function (error) {
|
|
|
|
http.createServer(function (req, res) {
|
|
|
|
http.createServer(function (req, res) {
|
|
|
|
const q = url.parse(req.url, true);
|
|
|
|
const q = url.parse(req.url, true);
|
|
|
|
let filename = "./public" + q.pathname;
|
|
|
|
let filename = "./public" + q.pathname;
|
|
|
|
let path_split = q.pathname.split("/", 3);
|
|
|
|
let path_split = q.pathname.split("/", 4);
|
|
|
|
if (path_split[path_split.length - 1] === "") filename += "/index.html";
|
|
|
|
if (path_split[path_split.length - 1] === "") filename += "/index.html";
|
|
|
|
let file_type = mime.getType(filename)
|
|
|
|
let file_type = mime.getType(filename)
|
|
|
|
if (path_split[1] === "connect") {
|
|
|
|
if (path_split[1] === "connect" && path_split[2] !== "") { //device registration
|
|
|
|
res.writeHead(200, {'Content-Type': file_type});
|
|
|
|
res.writeHead(200, {'Content-Type': file_type});
|
|
|
|
res.write("");
|
|
|
|
redis_cli.hmset("device;" + path_split[3], "status", "online", "ip", path_split[2], "time", Date.now())
|
|
|
|
|
|
|
|
wss.clients.forEach(clients => {
|
|
|
|
|
|
|
|
clients.send("device;"+path_split[3]+";online")
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
res.write("200");
|
|
|
|
|
|
|
|
/*let check = setInterval(to => {
|
|
|
|
|
|
|
|
let xhr = new XMLHttpRequest();
|
|
|
|
|
|
|
|
xhr.open('GET', `http://192.168.1.210/info`, true);
|
|
|
|
|
|
|
|
xhr.timeout = 1000;
|
|
|
|
|
|
|
|
xhr.ontimeout = function(e) {ws.send('error;device "led1" is offline')}
|
|
|
|
|
|
|
|
xhr.send();
|
|
|
|
|
|
|
|
setTimeout(to => {if (xhr.readyState !== 4){
|
|
|
|
|
|
|
|
//ws.send('error;device "led1" took too long to reach')
|
|
|
|
|
|
|
|
wss.clients.forEach(clients => {
|
|
|
|
|
|
|
|
clients.send('device;led1;offline')
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
xhr.abort()
|
|
|
|
|
|
|
|
clearInterval(check)
|
|
|
|
|
|
|
|
}}, 2000)
|
|
|
|
|
|
|
|
}, 5000)*/
|
|
|
|
return res.end();
|
|
|
|
return res.end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fs.readFile(filename, function(err, data) {
|
|
|
|
fs.readFile(filename, function(err, data) {
|
|
|
@ -67,6 +86,32 @@ const wss = new ws.Server({
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//check available devices
|
|
|
|
|
|
|
|
let checkstat = setInterval(to => {
|
|
|
|
|
|
|
|
redis_cli.keys("device;*", function(err, keys) { if(err) console.log(err)
|
|
|
|
|
|
|
|
if(keys){ keys.forEach(key => {
|
|
|
|
|
|
|
|
redis_cli.hgetall(key, function(err, device) { if(err) console.log(err)
|
|
|
|
|
|
|
|
if (device){
|
|
|
|
|
|
|
|
let device_available = "online"
|
|
|
|
|
|
|
|
let xhr = new XMLHttpRequest();
|
|
|
|
|
|
|
|
xhr.open('GET', `http://${device[3]}/info`, true);
|
|
|
|
|
|
|
|
xhr.timeout = 1000;
|
|
|
|
|
|
|
|
xhr.ontimeout = function(e) {ws.send('error;device "led1" is offline')}
|
|
|
|
|
|
|
|
xhr.send();
|
|
|
|
|
|
|
|
setTimeout(to => {if (xhr.readyState !== 4){
|
|
|
|
|
|
|
|
wss.clients.forEach(clients => clients.send(`${key};offline`))
|
|
|
|
|
|
|
|
device_available = "offline"
|
|
|
|
|
|
|
|
xhr.abort()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (device_available !== device[1]){
|
|
|
|
|
|
|
|
wss.clients.forEach(clients => clients.send(`${key};${device_available}`))
|
|
|
|
|
|
|
|
}}, 2000)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
})}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}, 5000)
|
|
|
|
|
|
|
|
|
|
|
|
//WS handler
|
|
|
|
//WS handler
|
|
|
|
wss.on('connection', ws => {
|
|
|
|
wss.on('connection', ws => {
|
|
|
|
ws.on('message', message => {
|
|
|
|
ws.on('message', message => {
|
|
|
@ -81,14 +126,17 @@ wss.on('connection', ws => {
|
|
|
|
wss.clients.forEach(clients => {
|
|
|
|
wss.clients.forEach(clients => {
|
|
|
|
clients.send(message)
|
|
|
|
clients.send(message)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
led1_status = msg[1]
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
xhr.open('GET', `http://192.168.1.210/${msg[1]}`, true);
|
|
|
|
xhr.open('GET', `http://192.168.1.210/${msg[1]}`, true);
|
|
|
|
xhr.timeout = 1000;
|
|
|
|
xhr.timeout = 1000;
|
|
|
|
xhr.ontimeout = function(e) {ws.send('error;device "led1" is offline')}
|
|
|
|
xhr.ontimeout = function(e) {ws.send('error;device "led1" is offline')}
|
|
|
|
xhr.send();
|
|
|
|
xhr.send();
|
|
|
|
setTimeout(to => {if (xhr.readyState !== 4) ws.send('error;device "led1" took too long to reach');xhr.abort()}, 1500)
|
|
|
|
setTimeout(to => {if (xhr.readyState !== 4){
|
|
|
|
|
|
|
|
ws.send('error;device "led1" took too long to reach')
|
|
|
|
|
|
|
|
ws.send('device;led1;offline')
|
|
|
|
|
|
|
|
xhr.abort()
|
|
|
|
|
|
|
|
}}, 1500)
|
|
|
|
}else ws.send('error;try again')
|
|
|
|
}else ws.send('error;try again')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|