automatic device registration / redis debugging
This commit is contained in:
parent
c11e2d5849
commit
f115932bc9
@ -2,7 +2,7 @@ version: '3'
|
||||
|
||||
services:
|
||||
node:
|
||||
image: node:alpine
|
||||
image: node
|
||||
container_name: esp-smarthome_node
|
||||
restart: always
|
||||
ports:
|
||||
@ -19,13 +19,15 @@ services:
|
||||
links:
|
||||
- redis
|
||||
redis:
|
||||
image: redis:alpine
|
||||
image: redis
|
||||
container_name: esp-smarthome_redis
|
||||
expose:
|
||||
- 6379
|
||||
ports:
|
||||
- 6379:6379
|
||||
restart: always
|
||||
volumes:
|
||||
- ./redis_data/:/var/lib/redis/
|
||||
- ./redis_data/:/data/
|
||||
- ./redis_conf/:/usr/local/etc/redis/
|
||||
environment:
|
||||
- REDIS_REPLICATION_MODE=master
|
||||
|
@ -32,13 +32,6 @@
|
||||
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) {
|
||||
let error_style = document.getElementById('error-box').style
|
||||
document.getElementById('error-message').innerText = msg
|
||||
|
@ -9,9 +9,7 @@ const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest
|
||||
const host = 'http://127.0.0.1:8080/'
|
||||
const outpath = ['sym', '', 'manifest.json', 'admin', 'stats', 'analytics']
|
||||
|
||||
let led1_status = 'off';
|
||||
|
||||
//redis client
|
||||
//redis clientconsole.log(device.status)
|
||||
const redis_cli = redis.createClient({
|
||||
host: 'redis',
|
||||
port: 6379
|
||||
@ -29,32 +27,18 @@ http.createServer(function (req, res) {
|
||||
let file_type = mime.getType(filename)
|
||||
if (path_split[1] === "connect" && path_split[2] !== "") { //device registration
|
||||
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 => {
|
||||
clients.send("device;"+path_split[3]+";online")
|
||||
})
|
||||
console.log(`device;${path_split[3]} just connected`)
|
||||
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();
|
||||
}
|
||||
fs.readFile(filename, function(err, data) {
|
||||
if (err) {
|
||||
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.write(data);
|
||||
@ -87,24 +71,26 @@ const wss = new ws.Server({
|
||||
});
|
||||
|
||||
//check available devices
|
||||
let checkstat = setInterval(to => {
|
||||
let check_devices = setInterval(to => {
|
||||
redis_cli.keys("device;*", function(err, keys) { if(err) console.log(err)
|
||||
if(keys){ keys.forEach(key => {
|
||||
redis_cli.hgetall(key, function(err, device) { if(err) console.log(err)
|
||||
if (device){
|
||||
let device_available = "online"
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', `http://${device[3]}/info`, true);
|
||||
xhr.timeout = 1000;
|
||||
xhr.ontimeout = function(e) {ws.send('error;device "led1" is offline')}
|
||||
xhr.open('GET', `http://[${device.ip}]/info`, true);
|
||||
xhr.send();
|
||||
setTimeout(to => {if (xhr.readyState !== 4){
|
||||
device_available = "offline"
|
||||
xhr.abort()
|
||||
}
|
||||
if (device_available !== device[1]){
|
||||
wss.clients.forEach(clients => clients.send(`${key};${device_available}`))
|
||||
}}, 2000)
|
||||
setTimeout(to => {
|
||||
if (xhr.readyState !== 4){
|
||||
device_available = "offline"
|
||||
xhr.abort()
|
||||
}
|
||||
if (device_available !== device.status){
|
||||
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(`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
|
||||
|
Loading…
Reference in New Issue
Block a user