From 9f24e90a9b84197e1444288ee1aab1e2733a413e Mon Sep 17 00:00:00 2001 From: "friedjof@noweck.info" Date: Thu, 23 May 2024 02:37:54 +0200 Subject: [PATCH] add: log view --- data-template/index.html | 4 ++ data-template/script.js | 71 ++++++++++++++++++++++++++- data-template/style.css | 31 ++++++++++++ lib/LoggingManager/LoggingManager.cpp | 34 +++++++++++++ lib/LoggingManager/LoggingManager.h | 1 + src/main.cpp | 13 ++++- 6 files changed, 150 insertions(+), 4 deletions(-) diff --git a/data-template/index.html b/data-template/index.html index 85a5495..da863f0 100644 --- a/data-template/index.html +++ b/data-template/index.html @@ -9,6 +9,10 @@ + +
+ +
🌥 diff --git a/data-template/script.js b/data-template/script.js index 9dcc53b..6c85ed9 100644 --- a/data-template/script.js +++ b/data-template/script.js @@ -48,7 +48,7 @@ function showTimer(id, timer) { row.appendChild(enabled); var popup_button = document.createElement('td'); - popup_button.innerHTML = ''; + popup_button.innerHTML = ''; popup_button.id = 'popup-' + id; row.appendChild(popup_button); @@ -68,7 +68,7 @@ function showTimer(id, timer) { document.getElementById('timers').getElementsByTagName('tbody')[0].appendChild(row); } -function showPopup(id) { +function showPopup() { var overlay = document.createElement('div'); overlay.className = 'overlay'; overlay.onclick = function() { @@ -89,6 +89,73 @@ function showPopup(id) { modal.appendChild(closeButton); + return modal; +} + +function showLogs() { + var modal = showPopup(); + + var text = document.createElement('h2'); + text.className = 'schedule-header'; + text.textContent = 'Logs 📜'; + modal.appendChild(text); + + var body = document.createElement('div'); + body.className = 'log-body'; + modal.appendChild(body); + + // Show IFrame of /logging + var iframe = document.createElement('iframe'); + iframe.src = url + '/logging'; + iframe.className = 'logging-iframe'; + body.appendChild(iframe); + + // Refresh button + var button_div = document.createElement('div'); + button_div.className = 'refresh-div'; + modal.appendChild(button_div); + + var refresh_button = document.createElement('button'); + refresh_button.innerHTML = 'Refresh'; + refresh_button.className = 'refresh-button'; + refresh_button.onclick = function() { + iframe.src = url + '/logging'; + } + button_div.appendChild(refresh_button); + + var link_button = document.createElement('button'); + link_button.innerHTML = 'Open in new tab'; + link_button.className = 'link-button'; + link_button.onclick = function() { + window.open(url + '/logging', '_blank'); + } + button_div.appendChild(link_button); + + var clear_button = document.createElement('button'); + clear_button.innerHTML = 'Clear logs'; + clear_button.className = 'clear-button'; + clear_button.onclick = function() { + fetch(url + '/reset_logs') + .then(response => { + if (!response.ok) { + throw new Error('Network response was not ok'); + } + return response.text(); + }) + .catch((error) => { + console.error('Error:', error); + }); + + iframe.src = url + '/logging'; + } + button_div.appendChild(clear_button); + + document.body.appendChild(modal); +} + +function showWeekdayConfig(id) { + var modal = showPopup(); + var text = document.createElement('h2'); text.className = 'schedule-header'; text.textContent = '⏰ ' + timers[id].time + ' - ' + timers[id].name; diff --git a/data-template/style.css b/data-template/style.css index 8fb8b74..69684cd 100644 --- a/data-template/style.css +++ b/data-template/style.css @@ -539,4 +539,35 @@ input[type="number"] { font-style: italic; display: flex; justify-content: center; +} + +.logging-iframe { + border-radius: 15px; + overflow: hidden; + border: 2px solid black; + box-shadow: #333 0px 0px 10px; + width: 100%; + height: 100%; +} + + +.log-button { + position: fixed; + top: 10px; + left: 10px; + border: none; +} + +.log-body { + width: 80%; + height: 68%; + margin: 20px auto; + border-color: #007bff; + border-radius: 10px; + border-width: 2px; +} + +.refresh-div { + display: flex; + justify-content: center; } \ No newline at end of file diff --git a/lib/LoggingManager/LoggingManager.cpp b/lib/LoggingManager/LoggingManager.cpp index c6f294d..c49a6e4 100644 --- a/lib/LoggingManager/LoggingManager.cpp +++ b/lib/LoggingManager/LoggingManager.cpp @@ -36,6 +36,40 @@ bool LoggingManager::is_initialized() return this->initialized; } +bool LoggingManager::reset_logs() +{ + // check if file exists + if (!LittleFS.exists(this->filename)) + { + File file = LittleFS.open(this->filename, "w"); + file.close(); + + this->file_line_counter = 0; + + this->log(LOG_LEVEL_INFO, "No log file found, new log file was created"); + + return true; + } + + // delete file + if (LittleFS.remove(this->filename)) + { + // create file + File file = LittleFS.open(this->filename, "w"); + file.close(); + + this->file_line_counter = 0; + + this->log(LOG_LEVEL_INFO, "Log file was reset"); + + return true; + } + + this->log(LOG_LEVEL_ERROR, "Log file could not be reset"); + + return false; +} + String LoggingManager::log_string(log_level_t level) { String log_message = "("; diff --git a/lib/LoggingManager/LoggingManager.h b/lib/LoggingManager/LoggingManager.h index 7ef681a..2fb7210 100644 --- a/lib/LoggingManager/LoggingManager.h +++ b/lib/LoggingManager/LoggingManager.h @@ -46,6 +46,7 @@ class LoggingManager void begin(); bool is_initialized(); + bool reset_logs(); void log(log_level_t level, const char *message); void log(log_level_t level, String message); diff --git a/src/main.cpp b/src/main.cpp index 1c6e807..2ddad59 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -182,7 +182,7 @@ void setup() int line_counter = loggingManager.count_log_lines() + 1; int file_line_counter = loggingManager.get_file_line_counter() + 1; - loggingManager.start_seq(LOG_LEVEL_INFO_FILE, "GET /log_lines ["); + loggingManager.start_seq(LOG_LEVEL_INFO, "GET /log_lines ["); loggingManager.append_seq(line_counter); loggingManager.append_seq(", "); loggingManager.append_seq(file_line_counter); @@ -242,7 +242,6 @@ void setup() server.on("/sleep", HTTP_GET, [](AsyncWebServerRequest *request) { loggingManager.log(LOG_LEVEL_INFO, "GET /sleep"); - loggingManager.log(LOG_LEVEL_INFO_FILE, "Schlafmodus aktiviert"); goToSleep(); @@ -311,6 +310,14 @@ void setup() request->send(200); }); + // reset the logs (delete and create new file) + server.on("/reset_logs", HTTP_GET, [](AsyncWebServerRequest *request) + { + loggingManager.log(LOG_LEVEL_INFO, "GET /reset_logs"); + loggingManager.reset_logs(); + + request->send(200); }); + // get autosleep remaining time server.on("/autosleep", HTTP_GET, [](AsyncWebServerRequest *request) { @@ -385,6 +392,8 @@ void goToSleep() // Set next alert alertManager.set_next_alert(); + loggingManager.log(LOG_LEVEL_INFO_FILE, "Go to sleep"); + // go to sleep #if defined(ESP32DEV) esp_deep_sleep_start();