96 lines
3.6 KiB
JavaScript
96 lines
3.6 KiB
JavaScript
// Setze den Wert des Darkmode-Schalters beim Laden der Seite, sobald das Element existiert
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
fetch('/api/config')
|
|
.then(response => response.json())
|
|
.then(config => {
|
|
function setSwitch() {
|
|
const el = document.getElementById('darkmode_switch');
|
|
if (el) {
|
|
if (config && config.default && (config.default.darkmode === true || config.default.darkmode === 'true')) {
|
|
el.checked = true;
|
|
} else {
|
|
el.checked = false;
|
|
}
|
|
} else {
|
|
// Falls das Element noch nicht existiert, erneut versuchen
|
|
setTimeout(setSwitch, 50);
|
|
}
|
|
}
|
|
setSwitch();
|
|
});
|
|
});
|
|
async function saveSettings() {
|
|
// Default (für [default])
|
|
const darkmode = document.getElementById('darkmode_switch')?.checked ?? null;
|
|
const defaultConfig = {
|
|
loglevel: document.getElementById('loglevel')?.value || null,
|
|
darkmode: darkmode
|
|
};
|
|
|
|
// Modbus
|
|
const modbus = {
|
|
host: document.getElementById('modbus_host').value,
|
|
port: parseInt(document.getElementById('modbus_port').value, 10),
|
|
max_coils_addr: parseInt(document.getElementById('modbus_max_coils_addr').value, 10) || null,
|
|
max_input_addr: parseInt(document.getElementById('modbus_max_input_addr').value, 10) || null,
|
|
max_holding_addr: parseInt(document.getElementById('modbus_max_holding_addr').value, 10) || null,
|
|
modbus_coils: null,
|
|
modbus_input_register: null,
|
|
modbus_holding_register: null
|
|
};
|
|
|
|
// MQTT
|
|
const mqtt = {
|
|
broker: document.getElementById('mqtt_broker').value,
|
|
port: parseInt(document.getElementById('mqtt_port').value, 10),
|
|
user: document.getElementById('mqtt_user').value || null,
|
|
password: document.getElementById('mqtt_password').value || null,
|
|
button_circulation: document.getElementById('mqtt_button_circulation').value || null
|
|
};
|
|
|
|
// InfluxDB
|
|
const influxdb = {
|
|
url: document.getElementById('influxdb_url').value,
|
|
token: document.getElementById('influxdb_token').value,
|
|
bucket: document.getElementById('influxdb_bucket').value,
|
|
org: document.getElementById('influxdb_org').value,
|
|
location: document.getElementById('influxdb_location').value || null,
|
|
measurement: document.getElementById('influxdb_measurement').value || null
|
|
};
|
|
|
|
const settings = {
|
|
default: defaultConfig,
|
|
modbus,
|
|
mqtt,
|
|
influxdb
|
|
};
|
|
|
|
try {
|
|
const response = await fetch('/api/save-settings', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(settings)
|
|
});
|
|
|
|
const messageDiv = document.getElementById('message');
|
|
if (response.ok) {
|
|
messageDiv.className = 'message success';
|
|
messageDiv.textContent = '✓ Einstellungen erfolgreich gespeichert!';
|
|
} else {
|
|
messageDiv.className = 'message error';
|
|
messageDiv.textContent = '✗ Fehler beim Speichern der Einstellungen!';
|
|
}
|
|
|
|
setTimeout(() => {
|
|
messageDiv.style.display = 'none';
|
|
}, 3000);
|
|
} catch (error) {
|
|
const messageDiv = document.getElementById('message');
|
|
messageDiv.className = 'message error';
|
|
messageDiv.textContent = '✗ Verbindungsfehler!';
|
|
}
|
|
}
|
|
|