Implement name change

This commit is contained in:
hodasemi 2023-09-21 13:56:03 +02:00
parent ace45e2efe
commit 0c33986628
2 changed files with 41 additions and 58 deletions

View file

@ -41,14 +41,36 @@ async function startup() {
for (let i = 0; i < devices.length; i++) {
let device_id = devices[i][0];
let device_descriptor;
if (devices[i][1] == null) {
device_descriptor = device_id
} else {
device_descriptor = devices[i][1];
}
let row_device = document.createElement('tr');
let device_name_entry = document.createElement('td');
let device_name = document.createElement('label');
device_name.innerText = device_id;
let device_name = document.createElement('input');
device_name.value = device_descriptor;
device_name.readOnly = true;
let device_name_edit = document.createElement('button');
device_name_edit.onclick = async () => {
if (device_name.readOnly) {
device_name.readOnly = false;
device_name.focus();
} else {
device_name.readOnly = true;
await change_device_name(device_id, device_name.value);
}
}
let button_icon = document.createElement('i');
button_icon.className = "fa fa-edit";
device_name_entry.appendChild(device_name);
device_name_edit.appendChild(button_icon);
device_name_entry.appendChild(device_name_edit);
row_device.appendChild(device_name_entry);
const device_status_response = await fetch(
@ -106,9 +128,9 @@ async function startup() {
document.getElementById("main").appendChild(table);
}
async function led_on(plug) {
async function change_plug_state(plug, module, state) {
const response = await fetch(
"/plug/" + plug + "/led_on",
"/plug/" + plug + "/" + module + "_" + state,
{
method: "POST"
}
@ -124,75 +146,35 @@ async function led_on(plug) {
let device_state = JSON.parse(await device_status_response.json());
document.getElementById("led_" + plug).innerHTML = device_state["led"];
document.getElementById(module + "_" + plug).innerHTML = device_state[module];
}
}
async function led_on(plug) {
await change_plug_state(plug, "led", "on")
}
async function led_off(plug) {
const response = await fetch(
"/plug/" + plug + "/led_off",
{
method: "POST"
}
);
if (response.ok) {
const device_status_response = await fetch(
"/plug_state/" + plug,
{
method: "GET"
}
);
let device_state = JSON.parse(await device_status_response.json());
document.getElementById("led_" + plug).innerHTML = device_state["led"];
}
await change_plug_state(plug, "led", "off")
}
async function power_on(plug) {
const response = await fetch(
"/plug/" + plug + "/power_on",
{
method: "POST"
}
);
if (response.ok) {
const device_status_response = await fetch(
"/plug_state/" + plug,
{
method: "GET"
}
);
let device_state = JSON.parse(await device_status_response.json());
document.getElementById("power_" + plug).innerHTML = device_state["power"];
}
await change_plug_state(plug, "power", "on")
}
async function power_off(plug) {
await change_plug_state(plug, "pwoer", "off")
}
async function change_device_name(plug, name) {
const response = await fetch(
"/plug/" + plug + "/power_off",
"/device_name/" + plug + "/" + name,
{
method: "POST"
}
);
if (response.ok) {
const device_status_response = await fetch(
"/plug_state/" + plug,
{
method: "GET"
}
);
let device_state = JSON.parse(await device_status_response.json());
document.getElementById("power_" + plug).innerHTML = device_state["power"];
if (!response.ok) {
console.error(response.body);
}
}

View file

@ -4,6 +4,7 @@
<head>
<title>Smart Homeserver</title>
<link href="/css/index.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>