automatic device registration / redis debugging

master
adb 4 years ago
parent c11e2d5849
commit f115932bc9

@ -2,7 +2,7 @@ version: '3'
services: services:
node: node:
image: node:alpine image: node
container_name: esp-smarthome_node container_name: esp-smarthome_node
restart: always restart: always
ports: ports:
@ -19,13 +19,15 @@ services:
links: links:
- redis - redis
redis: redis:
image: redis:alpine image: redis
container_name: esp-smarthome_redis container_name: esp-smarthome_redis
expose: expose:
- 6379 - 6379
ports:
- 6379:6379
restart: always restart: always
volumes: volumes:
- ./redis_data/:/var/lib/redis/ - ./redis_data/:/data/
- ./redis_conf/:/usr/local/etc/redis/ - ./redis_conf/:/usr/local/etc/redis/
environment: environment:
- REDIS_REPLICATION_MODE=master - REDIS_REPLICATION_MODE=master

@ -32,13 +32,6 @@
else document.getElementById('led1_title').innerText = `LED 1 (currently ${msg[1]}):` else document.getElementById('led1_title').innerText = `LED 1 (currently ${msg[1]}):`
} }
} }
function copy_url() {
document.getElementById('surl-input').select()
document.execCommand("copy")
}
function lurl_submit() {
socket.send('long_url;'+document.getElementById('longurl-input').value)
}
function show_error(msg) { function show_error(msg) {
let error_style = document.getElementById('error-box').style let error_style = document.getElementById('error-box').style
document.getElementById('error-message').innerText = msg document.getElementById('error-message').innerText = msg

@ -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 => {
device_available = "offline" if (xhr.readyState !== 4){
xhr.abort() device_available = "offline"
} xhr.abort()
if (device_available !== device[1]){ }
wss.clients.forEach(clients => clients.send(`${key};${device_available}`)) if (device_available !== device.status){
}}, 2000) wss.clients.forEach(clients => clients.send(`${key};${device_available}`))
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

Loading…
Cancel
Save