|
|
@ -9,9 +9,7 @@ const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest
|
|
|
|
const host = 'http://127.0.0.1:8080/'
|
|
|
|
const host = 'http://127.0.0.1:8080/'
|
|
|
|
const outpath = ['sym', '', 'manifest.json', 'admin', 'stats', 'analytics']
|
|
|
|
const outpath = ['sym', '', 'manifest.json', 'admin', 'stats', 'analytics']
|
|
|
|
|
|
|
|
|
|
|
|
let led1_status = 'off';
|
|
|
|
//redis clientconsole.log(device.status)
|
|
|
|
|
|
|
|
|
|
|
|
//redis client
|
|
|
|
|
|
|
|
const redis_cli = redis.createClient({
|
|
|
|
const redis_cli = redis.createClient({
|
|
|
|
host: 'redis',
|
|
|
|
host: 'redis',
|
|
|
|
port: 6379
|
|
|
|
port: 6379
|
|
|
@ -29,32 +27,18 @@ http.createServer(function (req, res) {
|
|
|
|
let file_type = mime.getType(filename)
|
|
|
|
let file_type = mime.getType(filename)
|
|
|
|
if (path_split[1] === "connect" && path_split[2] !== "") { //device registration
|
|
|
|
if (path_split[1] === "connect" && path_split[2] !== "") { //device registration
|
|
|
|
res.writeHead(200, {'Content-Type': file_type});
|
|
|
|
res.writeHead(200, {'Content-Type': file_type});
|
|
|
|
redis_cli.hmset("device;" + path_split[3], "status", "online", "ip", path_split[2], "time", Date.now())
|
|
|
|
redis_cli.hmset("device;" + path_split[3], "status", "online", "ip", req.connection.remoteAddress, "time", Date.now())
|
|
|
|
wss.clients.forEach(clients => {
|
|
|
|
wss.clients.forEach(clients => {
|
|
|
|
clients.send("device;"+path_split[3]+";online")
|
|
|
|
clients.send("device;"+path_split[3]+";online")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
console.log(`device;${path_split[3]} just connected`)
|
|
|
|
res.write("200");
|
|
|
|
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) {
|
|
|
|
if (err) {
|
|
|
|
if (err) {
|
|
|
|
res.writeHead(404, {'Content-Type': "text/html"});
|
|
|
|
res.writeHead(404, {'Content-Type': "text/html"});
|
|
|
|
return res.end("404 Not Found");
|
|
|
|
return res.end("404 Not Found"+ req.connection.remoteAddress);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res.writeHead(200, {'Content-Type': file_type});
|
|
|
|
res.writeHead(200, {'Content-Type': file_type});
|
|
|
|
res.write(data);
|
|
|
|
res.write(data);
|
|
|
@ -87,24 +71,26 @@ const wss = new ws.Server({
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
//check available devices
|
|
|
|
//check available devices
|
|
|
|
let checkstat = setInterval(to => {
|
|
|
|
let check_devices = setInterval(to => {
|
|
|
|
redis_cli.keys("device;*", function(err, keys) { if(err) console.log(err)
|
|
|
|
redis_cli.keys("device;*", function(err, keys) { if(err) console.log(err)
|
|
|
|
if(keys){ keys.forEach(key => {
|
|
|
|
if(keys){ keys.forEach(key => {
|
|
|
|
redis_cli.hgetall(key, function(err, device) { if(err) console.log(err)
|
|
|
|
redis_cli.hgetall(key, function(err, device) { if(err) console.log(err)
|
|
|
|
if (device){
|
|
|
|
if (device){
|
|
|
|
let device_available = "online"
|
|
|
|
let device_available = "online"
|
|
|
|
let xhr = new XMLHttpRequest();
|
|
|
|
let xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('GET', `http://${device[3]}/info`, true);
|
|
|
|
xhr.open('GET', `http://[${device.ip}]/info`, true);
|
|
|
|
xhr.timeout = 1000;
|
|
|
|
|
|
|
|
xhr.ontimeout = function(e) {ws.send('error;device "led1" is offline')}
|
|
|
|
|
|
|
|
xhr.send();
|
|
|
|
xhr.send();
|
|
|
|
setTimeout(to => {if (xhr.readyState !== 4){
|
|
|
|
setTimeout(to => {
|
|
|
|
|
|
|
|
if (xhr.readyState !== 4){
|
|
|
|
device_available = "offline"
|
|
|
|
device_available = "offline"
|
|
|
|
xhr.abort()
|
|
|
|
xhr.abort()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (device_available !== device[1]){
|
|
|
|
if (device_available !== device.status){
|
|
|
|
wss.clients.forEach(clients => clients.send(`${key};${device_available}`))
|
|
|
|
wss.clients.forEach(clients => clients.send(`${key};${device_available}`))
|
|
|
|
}}, 2000)
|
|
|
|
redis_cli.hmset(key, "status", device_available, "ip", device.ip, "time", Date.now())
|
|
|
|
|
|
|
|
console.log(`${key} is now ${device_available}`)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}, 2000)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})}
|
|
|
|
})}
|
|
|
@ -140,7 +126,16 @@ wss.on('connection', ws => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
ws.send('websocket connected')
|
|
|
|
ws.send('websocket connected')
|
|
|
|
ws.send(`led1;${led1_status}`)
|
|
|
|
//on new connection
|
|
|
|
|
|
|
|
redis_cli.keys("device;*", function(err, keys) {
|
|
|
|
|
|
|
|
if (err) console.log(err)
|
|
|
|
|
|
|
|
if (keys) { keys.forEach(key => {
|
|
|
|
|
|
|
|
redis_cli.hget(key, "status", function (err, status) {
|
|
|
|
|
|
|
|
if (err) console.log(err)
|
|
|
|
|
|
|
|
if (status) ws.send(`${key};${status}`)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
})}
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
//random key
|
|
|
|
//random key
|
|
|
|