|
|
|
@ -9,7 +9,7 @@ const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest
|
|
|
|
|
const host = 'http://127.0.0.1:8080/'
|
|
|
|
|
const outpath = ['sym', '', 'manifest.json', 'admin', 'stats', 'analytics']
|
|
|
|
|
|
|
|
|
|
//redis clientconsole.log(device.status)
|
|
|
|
|
//redis client
|
|
|
|
|
const redis_cli = redis.createClient({
|
|
|
|
|
host: 'redis',
|
|
|
|
|
port: 6379
|
|
|
|
@ -101,29 +101,29 @@ let check_devices = setInterval(to => {
|
|
|
|
|
wss.on('connection', ws => {
|
|
|
|
|
ws.on('message', message => {
|
|
|
|
|
console.log(`Received message => ${message}`)
|
|
|
|
|
let msg = message.split(";", 2)
|
|
|
|
|
if (msg[0] === 'led1') {
|
|
|
|
|
if (msg[1] === '') ws.send('error;try again')
|
|
|
|
|
else if (msg[1] === 'on' || msg[1] === "off"){
|
|
|
|
|
let xhr = new XMLHttpRequest();
|
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
|
if (xhr.readyState === 4 && xhr.status === 200) {
|
|
|
|
|
wss.clients.forEach(clients => {
|
|
|
|
|
clients.send(message)
|
|
|
|
|
})
|
|
|
|
|
let msg = message.split(";", 3)
|
|
|
|
|
if (msg[0] === 'device') {
|
|
|
|
|
redis_cli.hget(`device;${msg[1]}`, "ip", function(err, ip) {
|
|
|
|
|
if(err) console.log(err)
|
|
|
|
|
if(ip){
|
|
|
|
|
let xhr = new XMLHttpRequest();
|
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
|
if (xhr.readyState === 4 && xhr.status === 200) {
|
|
|
|
|
wss.clients.forEach(clients => {
|
|
|
|
|
clients.send(message)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
xhr.open('GET', `http://[${ip}/${msg[1]}`, true);
|
|
|
|
|
xhr.send();
|
|
|
|
|
setTimeout(to => {if (xhr.readyState !== 4){
|
|
|
|
|
ws.send(`error;device "${msg[1]}" took too long to reach`)
|
|
|
|
|
ws.send(`device;${msg[1]};offline`)
|
|
|
|
|
xhr.abort()
|
|
|
|
|
}}, 1500)
|
|
|
|
|
}
|
|
|
|
|
xhr.open('GET', `http://192.168.1.210/${msg[1]}`, 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')
|
|
|
|
|
ws.send('device;led1;offline')
|
|
|
|
|
xhr.abort()
|
|
|
|
|
}}, 1500)
|
|
|
|
|
}else ws.send('error;try again')
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}else ws.send('error;try again')
|
|
|
|
|
})
|
|
|
|
|
ws.send('websocket connected')
|
|
|
|
|
//on new connection
|
|
|
|
@ -137,19 +137,3 @@ wss.on('connection', ws => {
|
|
|
|
|
})}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
//random key
|
|
|
|
|
function get_key(length) {
|
|
|
|
|
let forbidden = false; let output = ''
|
|
|
|
|
let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
|
|
|
|
do{
|
|
|
|
|
for (let i = 0; i < length; i++ )
|
|
|
|
|
output += characters.charAt(Math.floor(Math.random() * characters.length))
|
|
|
|
|
for (let i = 0; i < output.length; i++) if (output === outpath[i]) forbidden = true
|
|
|
|
|
redis_cli.hget("surl;"+output, "url", function(err, obj) {
|
|
|
|
|
if(err) console.log(err)
|
|
|
|
|
if(obj) forbidden = true
|
|
|
|
|
})
|
|
|
|
|
} while (forbidden)
|
|
|
|
|
return output
|
|
|
|
|
}
|
|
|
|
|