some fixes

master
adb 4 years ago
parent 68c68ec9a3
commit 17c6683790

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

@ -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,10 +101,11 @@ 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)
if(ip){
let xhr = new XMLHttpRequest(); let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() { xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) { if (xhr.readyState === 4 && xhr.status === 200) {
@ -113,18 +114,17 @@ wss.on('connection', ws => {
}) })
} }
} }
xhr.open('GET', `http://192.168.1.210/${msg[1]}`, true); xhr.open('GET', `http://[${ip}/${msg[1]}`, 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){
ws.send('error;device "led1" took too long to reach') ws.send(`error;device "${msg[1]}" took too long to reach`)
ws.send('device;led1;offline') ws.send(`device;${msg[1]};offline`)
xhr.abort() xhr.abort()
}}, 1500) }}, 1500)
}else ws.send('error;try again')
} }
}) })
}else ws.send('error;try again')
})
ws.send('websocket connected') ws.send('websocket connected')
//on new connection //on new connection
redis_cli.keys("device;*", function(err, keys) { redis_cli.keys("device;*", function(err, keys) {
@ -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