Files

138 lines
4.0 KiB
JavaScript

async function ap_update()
{
const ssid = document.getElementById('ap-ssid');
const pass = document.getElementById('ap-password');
const ip = document.getElementById('ap-ip');
const gateway = document.getElementById('ap-gateway');
const mask = document.getElementById('ap-mask');
let body = {};
if (ssid.value != "")
{
body.ssid = ssid.value;
if (pass.value === "")
body.pass = "";
else
body.pass = pass.value;
}
if (ip.value != "")
body.ip = ip.value;
if (gateway.value != "")
body.gateway = gateway.value;
if (mask.value != "")
body.mask = mask.value;
const res = await fetch("/settings/ap_update",{
method: "POST",
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: JSON.stringify(body)
});
if (res.ok)
init();
else
alert("Неудплось обновить параметры AP сети");
}
async function sta_update()
{
const ssid = document.getElementById('sta-ssid');
const pass = document.getElementById('sta-password');
const checkbox = document.getElementById('connection-out');
const response = await fetch("/settings/sta_update", {
method: "POST",
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: JSON.stringify({ssid: ssid.value, pass: pass.value, is_conn_out: checkbox.checked})
});
if (!response.ok)
alert("Fail set STA settings");
}
async function admin_update()
{
const new_pass = document.getElementById('new-password');
const confirm_pass = document.getElementById('confirm-password');
if (new_pass.value !== confirm_pass.value)
{
alert("Пароли должны совпадать");
return;
}
const res = await fetch("/settings/admin_update", {
method: "POST",
headers:{
'Content-Type': 'application/json;charset=utf-8'
},
body:JSON.stringify({new_pass: new_pass.value})
});
if (!res.ok)
alert("Ошибка! Пароль не обновился");
}
function updateConnectionStatus(isConnected/*, networkName = ''*/) {
const statusElem = document.getElementById('connection-status');
if (isConnected) {
statusElem.textContent = 'Подключено';
statusElem.className = 'info-value status-connected';
} else {
statusElem.textContent = 'Отключено';
statusElem.className = 'info-value status-disconnected';
ssidElem.textContent = '—';
}
}
async function init()
{
try {
const res = await fetch("/settings/current_settings");
if (res.ok)
{
const json = await res.json();
const current_sta = document.getElementById('current-sta-ssid');
current_sta.textContent = json["sta_ssid"];
const current_ap = document.getElementById('current-ap-ssid');
current_ap.textContent = json["ap_ssid"];
const current_ip = document.getElementById('current-ip');
current_ip.textContent = json["ip"];
const current_gateway = document.getElementById('current-gateway');
current_gateway.textContent = json["gateway"];
const current_mask = document.getElementById('current-mask');
current_mask.textContent = json["mask"];
updateConnectionStatus(json["is_conn_out"]);
}
else
{
alert("Неудалось получть текущие настройки");
}
}
catch (error) {
console.log("Ошибка во время запроса настроек: ", error);
}
}
init();
button = document.querySelector('#ap_update');
button.addEventListener('click', ap_update);
button = document.querySelector('#sta_update');
button.addEventListener('click', sta_update);
button = document.querySelector('#admin_update');
button.addEventListener('click', admin_update);