some fixes

master
adb 4 years ago
parent 68c68ec9a3
commit 17c6683790

@ -14,6 +14,7 @@
<script>
const wsurl = 'ws://127.0.0.1:8081'
const socket = new WebSocket(wsurl)
let devices;
socket.onopen = () => {
socket.send('new session')
@ -27,6 +28,10 @@
let msg = e.data.split(";", 3)
if (msg[0] === 'error') show_error(msg[1])
else if (msg[0] === 'device') {
if (!devices.includes(msg[1])){
devices.push(msg[1])
e.data
}
if (msg[2] === 'online') document.getElementById(msg[1]).style.display = "block"
else if (msg[2] === 'offline') document.getElementById(msg[1]).style.display = "none"
else document.getElementById('led1_title').innerText = `LED 1 (currently ${msg[1]}):`
@ -48,12 +53,12 @@
<div class="title2" id="led1_title">LED 1:</div>
<button class="btn-green"
style="width: 10rem; position:relative; margin-top: 1rem; margin-left: calc(50% - 5rem);"
onclick="socket.send('led1;on')">
onclick="socket.send('device;led1;on')">
<div class="btn-text">on</div>
</button>
<button class="btn-red"
style="width: 10rem; position:relative; margin-top: 1rem; margin-left: calc(50% - 5rem);"
onclick="socket.send('led1;off')">
onclick="socket.send('device;led1;off')">
<div class="btn-text">off</div>
</button>
</div>

@ -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
}

Loading…
Cancel
Save