2018-05-13 11:37:02 +02:00
|
|
|
const source = new EventSource("/events");
|
|
|
|
|
|
|
|
source.addEventListener('log', function (e) {
|
|
|
|
const log = document.getElementById("log");
|
2022-02-03 19:24:35 +01:00
|
|
|
let log_prefs = [
|
|
|
|
["\u001b[1;31m", 'e'],
|
|
|
|
["\u001b[0;33m", 'w'],
|
|
|
|
["\u001b[0;32m", 'i'],
|
|
|
|
["\u001b[0;35m", 'c'],
|
|
|
|
["\u001b[0;36m", 'd'],
|
|
|
|
["\u001b[0;37m", 'v'],
|
|
|
|
];
|
|
|
|
|
2018-05-13 11:37:02 +02:00
|
|
|
let klass = '';
|
2022-02-03 19:24:35 +01:00
|
|
|
for (const log_pref of log_prefs){
|
|
|
|
if (e.data.startsWith(log_pref[0])) {
|
|
|
|
klass = log_pref[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (klass == ''){
|
2018-05-13 11:37:02 +02:00
|
|
|
log.innerHTML += e.data + '\n';
|
|
|
|
}
|
2022-02-03 19:24:35 +01:00
|
|
|
log.innerHTML += '<span class="' + klass + '">' + e.data.substr(7, e.data.length - 11) + "</span>\n";
|
2018-05-13 11:37:02 +02:00
|
|
|
});
|
|
|
|
|
2022-11-06 07:27:19 +01:00
|
|
|
const actions = [
|
2022-02-03 19:24:35 +01:00
|
|
|
["switch", ["toggle"]],
|
|
|
|
["light", ["toggle"]],
|
|
|
|
["fan", ["toggle"]],
|
|
|
|
["cover", ["open", "close"]],
|
|
|
|
["button", ["press"]],
|
|
|
|
["lock", ["lock", "unlock", "open"]],
|
|
|
|
];
|
2022-11-06 07:27:19 +01:00
|
|
|
const multi_actions = [
|
2022-02-03 19:24:35 +01:00
|
|
|
["select", "option"],
|
|
|
|
["number", "value"],
|
|
|
|
];
|
|
|
|
|
2018-05-13 11:37:02 +02:00
|
|
|
source.addEventListener('state', function (e) {
|
|
|
|
const data = JSON.parse(e.data);
|
|
|
|
document.getElementById(data.id).children[1].innerText = data.state;
|
|
|
|
});
|
|
|
|
|
|
|
|
const states = document.getElementById("states");
|
|
|
|
let i = 0, row;
|
|
|
|
for (; row = states.rows[i]; i++) {
|
2021-09-13 10:07:32 +02:00
|
|
|
if (!row.children[2].children.length) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-02-03 19:24:35 +01:00
|
|
|
|
|
|
|
for (const domain of actions){
|
|
|
|
if (row.classList.contains(domain[0])) {
|
|
|
|
let id = row.id.substr(domain[0].length+1);
|
|
|
|
for (let j=0;j<row.children[2].children.length && j < domain[1].length; j++){
|
|
|
|
row.children[2].children[j].addEventListener('click', function () {
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.open("POST", '/'+domain[0]+'/' + id + '/' + domain[1][j], true);
|
|
|
|
xhr.send();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const domain of multi_actions){
|
|
|
|
if (row.classList.contains(domain[0])) {
|
|
|
|
let id = row.id.substr(domain[0].length+1);
|
2022-01-17 00:32:10 +01:00
|
|
|
row.children[2].children[0].addEventListener('change', function () {
|
|
|
|
const xhr = new XMLHttpRequest();
|
2022-02-03 19:24:35 +01:00
|
|
|
xhr.open("POST", '/'+domain[0]+'/' + id + '/set?'+domain[1]+'=' + encodeURIComponent(this.value), true);
|
2021-12-12 22:30:36 +01:00
|
|
|
xhr.send();
|
|
|
|
});
|
2022-02-03 19:24:35 +01:00
|
|
|
}
|
|
|
|
}
|
2018-05-13 11:37:02 +02:00
|
|
|
}
|