Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions _static/webserver-v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ source.addEventListener('log', function (e) {

source.addEventListener('state', function (e) {
const data = JSON.parse(e.data);
document.getElementById(data.id).children[1].innerText = data.state;
console.log('got state', e);
const f = (x) => document.getElementById(x.id).children[1].innerText = x.state;
if (Array.isArray(data))
data.forEach(d => f(d));
else f(data);
});

const states = document.getElementById("states");
let i = 0, row;
for (; row = states.rows[i]; i++) {
if (row.classList.contains("switch")) {
(function(id) {
(function (id) {
row.children[2].children[0].addEventListener('click', function () {
const xhr = new XMLHttpRequest();
xhr.open("POST", '/switch/' + id.substr(7) + '/toggle', true);
Expand All @@ -39,7 +43,7 @@ for (; row = states.rows[i]; i++) {
})(row.id);
}
if (row.classList.contains("fan")) {
(function(id) {
(function (id) {
row.children[2].children[0].addEventListener('click', function () {
const xhr = new XMLHttpRequest();
xhr.open("POST", '/fan/' + id.substr(4) + '/toggle', true);
Expand All @@ -48,7 +52,7 @@ for (; row = states.rows[i]; i++) {
})(row.id);
}
if (row.classList.contains("light")) {
(function(id) {
(function (id) {
row.children[2].children[0].addEventListener('click', function () {
const xhr = new XMLHttpRequest();
xhr.open("POST", '/light/' + id.substr(6) + '/toggle', true);
Expand All @@ -57,7 +61,7 @@ for (; row = states.rows[i]; i++) {
})(row.id);
}
if (row.classList.contains("cover")) {
(function(id) {
(function (id) {
row.children[2].children[0].addEventListener('click', function () {
const xhr = new XMLHttpRequest();
xhr.open("POST", '/cover/' + id.substr(6) + '/open', true);
Expand Down
2 changes: 1 addition & 1 deletion _static/webserver-v1.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.