This commit is contained in:
parent
afa8e93495
commit
6d3e550914
3 changed files with 90 additions and 26 deletions
|
@ -105,7 +105,7 @@ def get_api_lock(id):
|
||||||
@app.post('/api/lock/<id>')
|
@app.post('/api/lock/<id>')
|
||||||
@require_authz
|
@require_authz
|
||||||
def post_api_lock(id):
|
def post_api_lock(id):
|
||||||
return ccujack.lock_unlock(request.json["id"], None)
|
return ccujack.lock_unlock(id, request.json["locking"])
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(host='localhost', port=3000, server=GeventWebSocketServer, debug=config.debug, quiet=not config.debug)
|
app.run(host='localhost', port=3000, server=GeventWebSocketServer, debug=config.debug, quiet=not config.debug)
|
||||||
|
|
|
@ -1,3 +1,29 @@
|
||||||
/*
|
/*
|
||||||
* foo
|
* foo
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
.lock-line {
|
||||||
|
display: flex;
|
||||||
|
background: #ddd;
|
||||||
|
padding: 20px 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.lock-name {
|
||||||
|
min-width: 20em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.lock-line button {
|
||||||
|
min-width: 10em;
|
||||||
|
margin: 0.1em 0.5em;
|
||||||
|
padding: 0.1em 0.5em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.lock-line .lock-status {
|
||||||
|
background: black;
|
||||||
|
color: white;
|
||||||
|
border-radius: 1em;
|
||||||
|
padding: 0.5em;
|
||||||
|
margin: 0 1em;
|
||||||
|
font-size: 1.2em;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
|
@ -1,14 +1,40 @@
|
||||||
(function () {
|
(function () {
|
||||||
let update_button = function (lock, e) {
|
let update_button = function (lock) {
|
||||||
if (lock.status === "UNKNOWN") {
|
let state = document.getElementById(`lock__state__${lock.id}`);
|
||||||
e.innerText = lock.name + " ???";
|
let button_lock = document.getElementById(`lock__lock__${lock.id}`);
|
||||||
e.disabled = true;
|
let button_unlock = document.getElementById(`lock__unlock__${lock.id}`);
|
||||||
|
if (lock.status === "LOCKED") {
|
||||||
|
state.innerHTML = "🔒";
|
||||||
|
button_lock.disabled = true;
|
||||||
|
button_unlock.disabled = false;
|
||||||
|
} else if (lock.status === "UNLOCKED") {
|
||||||
|
state.innerHTML = "🔓";
|
||||||
|
button_lock.disabled = false;
|
||||||
|
button_unlock.disabled = true;
|
||||||
} else {
|
} else {
|
||||||
e.innerText = (lock.status === "LOCKED" ? "Unlock" : "Lock") + " " + lock.name;
|
state.innerText = "?"
|
||||||
e.disabled = false;
|
button_lock.disabled = true;
|
||||||
|
button_unlock.disabled = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let lock_button_add_event_handler = function (e, id, locking) {
|
||||||
|
e.addEventListener("click", (ev) => {
|
||||||
|
ev.target.disabled = true;
|
||||||
|
fetch(`/api/lock/${id}`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
locking: locking,
|
||||||
|
})
|
||||||
|
}).then(res => res.json()).then((data) => {
|
||||||
|
//
|
||||||
|
}).catch(error => console.error('Error:', error));
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
function connect() {
|
function connect() {
|
||||||
const ws = new WebSocket("/ws");
|
const ws = new WebSocket("/ws");
|
||||||
ws.addEventListener("message", (event) => {
|
ws.addEventListener("message", (event) => {
|
||||||
|
@ -16,7 +42,7 @@
|
||||||
// console.log(m)
|
// console.log(m)
|
||||||
for (let lock of m.locks) {
|
for (let lock of m.locks) {
|
||||||
let e = document.getElementById("lock__" + lock.id);
|
let e = document.getElementById("lock__" + lock.id);
|
||||||
update_button(lock, e);
|
update_button(lock);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ws.addEventListener("close", (ev) => {
|
ws.addEventListener("close", (ev) => {
|
||||||
|
@ -34,24 +60,36 @@
|
||||||
fetch("/api/lock").then((res) => res.json()).then((data) => {
|
fetch("/api/lock").then((res) => res.json()).then((data) => {
|
||||||
let buttons = document.getElementById("locks");
|
let buttons = document.getElementById("locks");
|
||||||
for (let lock of data.locks) {
|
for (let lock of data.locks) {
|
||||||
let e = document.createElement("button");
|
let div = document.createElement("div");
|
||||||
|
buttons.appendChild(div);
|
||||||
|
div.id = `lock__line__${lock.id}`;
|
||||||
|
div.classList.add("lock-line");
|
||||||
|
let e = document.createElement("div");
|
||||||
|
e.innerText = `${lock.name} `;
|
||||||
|
e.classList.add("lock-name");
|
||||||
|
div.appendChild(e);
|
||||||
|
let state = document.createElement("span");
|
||||||
|
state.id = `lock__state__${lock.id}`;
|
||||||
|
state.classList.add("lock-status");
|
||||||
|
state.innerText = "?";
|
||||||
|
e.prepend(state);
|
||||||
|
|
||||||
|
e = document.createElement("button");
|
||||||
e.name = lock.id;
|
e.name = lock.id;
|
||||||
e.id = "lock__" + lock.id;
|
e.id = `lock__lock__${lock.id}`;
|
||||||
update_button(lock, e);
|
e.disabled = true;
|
||||||
e.addEventListener("click", (e) => {
|
e.innerText = "Lock";
|
||||||
fetch(`/api/lock/${lock.id}`, {
|
lock_button_add_event_handler(e, lock.id, true);
|
||||||
method: "POST",
|
div.appendChild(e);
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
e = document.createElement("button");
|
||||||
},
|
e.name = lock.id;
|
||||||
body: JSON.stringify({
|
e.id = `lock__unlock__${lock.id}`;
|
||||||
id: lock.id,
|
e.disabled = true;
|
||||||
})
|
e.innerText = "Unlock";
|
||||||
}).then(res => res.json()).then((data) => {
|
lock_button_add_event_handler(e, lock.id, false);
|
||||||
//
|
div.appendChild(e);
|
||||||
}).catch(error => console.error('Error:', error));
|
update_button(lock);
|
||||||
})
|
|
||||||
buttons.appendChild(e);
|
|
||||||
}
|
}
|
||||||
}).catch(error => console.error('Error:', error));
|
}).catch(error => console.error('Error:', error));
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue