|
|
@ -9,7 +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']
|
|
|
|
|
|
|
|
|
|
|
|
//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
|
|
|
@ -101,29 +101,29 @@ let check_devices = setInterval(to => {
|
|
|
|
wss.on('connection', ws => {
|
|
|
|
wss.on('connection', ws => {
|
|
|
|
ws.on('message', message => {
|
|
|
|
ws.on('message', message => {
|
|
|
|
console.log(`Received message => ${message}`)
|
|
|
|
console.log(`Received message => ${message}`)
|
|
|
|
let msg = message.split(";", 2)
|
|
|
|
let msg = message.split(";", 3)
|
|
|
|
if (msg[0] === 'led1') {
|
|
|
|
if (msg[0] === 'device') {
|
|
|
|
if (msg[1] === '') ws.send('error;try again')
|
|
|
|
redis_cli.hget(`device;${msg[1]}`, "ip", function(err, ip) {
|
|
|
|
else if (msg[1] === 'on' || msg[1] === "off"){
|
|
|
|
if(err) console.log(err)
|
|
|
|
let xhr = new XMLHttpRequest();
|
|
|
|
if(ip){
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
let xhr = new XMLHttpRequest();
|
|
|
|
if (xhr.readyState === 4 && xhr.status === 200) {
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
wss.clients.forEach(clients => {
|
|
|
|
if (xhr.readyState === 4 && xhr.status === 200) {
|
|
|
|
clients.send(message)
|
|
|
|
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;
|
|
|
|
}else ws.send('error;try again')
|
|
|
|
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')
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
ws.send('websocket connected')
|
|
|
|
ws.send('websocket connected')
|
|
|
|
//on new connection
|
|
|
|
//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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|