diff --git a/paramod.yaml b/paramod.yaml index 13aa6ba..5213458 100644 --- a/paramod.yaml +++ b/paramod.yaml @@ -1,5 +1,6 @@ default: loglevel: "DEBUG" + darkmode: true mqtt: broker: "192.168.178.2" diff --git a/src/config.rs b/src/config.rs index 7284ac4..0e7a9fa 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,21 @@ use serde::{Deserialize, Serialize}; use std::collections::HashMap; +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct DefaultConfig { + pub loglevel: Option, + pub darkmode: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ModbusConfig { + pub host: String, + pub port: u16, + pub max_coils_addr: Option, + pub max_input_addr: Option, + pub max_holding_addr: Option, +} + #[derive(Debug, Serialize, Deserialize, Clone)] pub struct MqttConfig { pub broker: String, @@ -30,20 +45,6 @@ pub struct ModbusRegisterConfig { pub comment: Option, } -#[derive(Debug, Serialize, Deserialize, Clone)] -pub struct ModbusConfig { - pub host: String, - pub port: u16, - pub max_coils_addr: Option, - pub max_input_addr: Option, - pub max_holding_addr: Option, -} - -#[derive(Debug, Serialize, Deserialize, Clone)] -pub struct DefaultConfig { - pub loglevel: Option, -} - #[derive(Debug, Serialize, Deserialize, Clone)] pub struct AppConfig { pub default: DefaultConfig, diff --git a/src/main.rs b/src/main.rs index 20f742a..90045ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,3 @@ - use actix_web::{web, App, HttpResponse, HttpServer, Result}; use actix_files as actix_fs; use std::sync::{Mutex, Arc}; @@ -12,9 +11,6 @@ mod modbus; use crate::config::{AppConfig, ModbusRegisterConfig, ModbusValueMaps}; - -// ...existing code... - struct AppState { config: Mutex, value_maps: Arc>, @@ -44,9 +40,6 @@ impl AppState { } } - -// ...existing code... - async fn index(data: web::Data) -> Result { let config = data.config.lock().unwrap(); let value_maps = data.value_maps.lock().unwrap(); @@ -184,6 +177,11 @@ async fn save_settings( Ok(HttpResponse::Ok().body("success")) } +async fn get_config(data: web::Data) -> HttpResponse { + let config = data.config.lock().unwrap(); + HttpResponse::Ok().json(&*config) +} + #[actix_web::main] async fn main() -> std::io::Result<()> { @@ -215,6 +213,7 @@ async fn main() -> std::io::Result<()> { .route("/settings", web::get().to(settings_page)) .route("/api/save", web::post().to(save_table)) .route("/api/save-settings", web::post().to(save_settings)) + .route("/api/config", web::get().to(get_config)) .service(actix_fs::Files::new("/static", "./static")) }) .bind("0.0.0.0:8080")? diff --git a/static/script.js b/static/script.js index 3739c6f..da594c6 100644 --- a/static/script.js +++ b/static/script.js @@ -1,11 +1,15 @@ // Sortiere die Tabelle nach Adresse (addr) beim Laden der Seite document.addEventListener('DOMContentLoaded', function() { - // Darkmode-Status aus localStorage übernehmen - if (localStorage.getItem('darkmode') === 'true') { - document.body.classList.add('darkmode'); - } else { - document.body.classList.remove('darkmode'); - } + // Darkmode-Status aus Konfiguration übernehmen + fetch('/api/config') + .then(response => response.json()) + .then(config => { + if (config && config.default && config.default.darkmode === true) { + document.body.classList.add('darkmode'); + } else { + document.body.classList.remove('darkmode'); + } + }); const tableBody = document.getElementById('tableBody'); if (tableBody) { // Extrahiere alle Zeilen und deren addr diff --git a/static/settings.js b/static/settings.js index 16f2493..9f6a08f 100644 --- a/static/settings.js +++ b/static/settings.js @@ -1,4 +1,44 @@ +// 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, @@ -18,28 +58,11 @@ async function saveSettings() { measurement: document.getElementById('influxdb_measurement').value || null }; - // 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 - }; - - // Default (für [default]) - const defaultConfig = { - loglevel: document.getElementById('loglevel')?.value || null - }; - const settings = { default: defaultConfig, + modbus, mqtt, - influxdb, - modbus + influxdb }; try { @@ -70,16 +93,3 @@ async function saveSettings() { } } -// Darkmode Toggle -document.addEventListener('DOMContentLoaded', function() { - const btn = document.getElementById('darkmode-toggle'); - if (!btn) return; - // Initialer Zustand aus LocalStorage - if (localStorage.getItem('darkmode') === 'true') { - document.body.classList.add('darkmode'); - } - btn.addEventListener('click', function() { - document.body.classList.toggle('darkmode'); - localStorage.setItem('darkmode', document.body.classList.contains('darkmode')); - }); -}); diff --git a/templates/settings.html b/templates/settings.html index 55c2c60..168f202 100644 --- a/templates/settings.html +++ b/templates/settings.html @@ -41,9 +41,37 @@ - -
- +
+
+ + +
+ + +
+

Modbus Konfiguration

+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
@@ -99,30 +127,6 @@ -
-

Modbus Konfiguration

-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
-