mirror of
https://github.com/dalathegreat/Battery-Emulator.git
synced 2025-10-05 19:42:08 +02:00
display events in reverse chronological order
This commit is contained in:
parent
5dc413462b
commit
2c26085160
2 changed files with 42 additions and 14 deletions
|
@ -1,6 +1,4 @@
|
||||||
#include "events_html.h"
|
#include "events_html.h"
|
||||||
#include <Arduino.h>
|
|
||||||
#include "../utils/events.h"
|
|
||||||
|
|
||||||
const char EVENTS_HTML_START[] = R"=====(
|
const char EVENTS_HTML_START[] = R"=====(
|
||||||
<style>body{background-color:#000;color:#fff}.event-log{display:flex;flex-direction:column}.event{display:flex;flex-wrap:wrap;border:1px solid #fff;padding:10px}.event>div{flex:1;min-width:100px;max-width:90%;word-break:break-word}</style><div style="background-color:#303e47;padding:10px;margin-bottom:10px;border-radius:25px"><div class="event-log"><div class="event" style="background-color:#1e2c33;font-weight:700"><div>Event Type</div><div>Severity</div><div>Last Event</div><div>Count</div><div>Data</div><div>Message</div></div>
|
<style>body{background-color:#000;color:#fff}.event-log{display:flex;flex-direction:column}.event{display:flex;flex-wrap:wrap;border:1px solid #fff;padding:10px}.event>div{flex:1;min-width:100px;max-width:90%;word-break:break-word}</style><div style="background-color:#303e47;padding:10px;margin-bottom:10px;border-radius:25px"><div class="event-log"><div class="event" style="background-color:#1e2c33;font-weight:700"><div>Event Type</div><div>Severity</div><div>Last Event</div><div>Count</div><div>Data</div><div>Message</div></div>
|
||||||
|
@ -12,6 +10,13 @@ const char EVENTS_HTML_END[] = R"=====(
|
||||||
<script>function showEvent(){document.querySelectorAll(".event").forEach(function(e){var n=e.querySelector(".sec-ago");n&&(n.innerText=new Date(new Date().getTime()-1e3*parseInt(n.innerText,10)).toLocaleString())})}function home(){window.location.href="/"}window.onload=function(){showEvent()}</script>
|
<script>function showEvent(){document.querySelectorAll(".event").forEach(function(e){var n=e.querySelector(".sec-ago");n&&(n.innerText=new Date(new Date().getTime()-1e3*parseInt(n.innerText,10)).toLocaleString())})}function home(){window.location.href="/"}window.onload=function(){showEvent()}</script>
|
||||||
)=====";
|
)=====";
|
||||||
|
|
||||||
|
static std::vector<EventData> order_events;
|
||||||
|
|
||||||
|
// Function to compare events by timestamp
|
||||||
|
static bool compareEventsByTimestamp(const EventData& a, const EventData& b) {
|
||||||
|
return a.event_pointer->timestamp > b.event_pointer->timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
String events_processor(const String& var) {
|
String events_processor(const String& var) {
|
||||||
if (var == "X") {
|
if (var == "X") {
|
||||||
String content = "";
|
String content = "";
|
||||||
|
@ -22,30 +27,45 @@ String events_processor(const String& var) {
|
||||||
|
|
||||||
unsigned long timestamp_now = get_current_event_time_secs();
|
unsigned long timestamp_now = get_current_event_time_secs();
|
||||||
|
|
||||||
|
//clear the vector
|
||||||
|
order_events.clear();
|
||||||
|
// Collect all events
|
||||||
for (int i = 0; i < EVENT_NOF_EVENTS; i++) {
|
for (int i = 0; i < EVENT_NOF_EVENTS; i++) {
|
||||||
event_pointer = get_event_pointer((EVENTS_ENUM_TYPE)i);
|
event_pointer = get_event_pointer((EVENTS_ENUM_TYPE)i);
|
||||||
EVENTS_ENUM_TYPE event_handle = static_cast<EVENTS_ENUM_TYPE>(i);
|
if (event_pointer->occurences > 0) {
|
||||||
|
order_events.push_back({static_cast<EVENTS_ENUM_TYPE>(i), event_pointer});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort events by timestamp
|
||||||
|
std::sort(order_events.begin(), order_events.end(), compareEventsByTimestamp);
|
||||||
|
|
||||||
|
// Generate HTML and debug output
|
||||||
|
for (const auto& event : order_events) {
|
||||||
|
EVENTS_ENUM_TYPE event_handle = event.event_handle;
|
||||||
|
event_pointer = event.event_pointer;
|
||||||
#ifdef DEBUG_VIA_USB
|
#ifdef DEBUG_VIA_USB
|
||||||
Serial.println("Event: " + String(get_event_enum_string(event_handle)) +
|
Serial.println("Event: " + String(get_event_enum_string(event_handle)) +
|
||||||
" count: " + String(event_pointer->occurences) + " seconds: " + String(event_pointer->timestamp) +
|
" count: " + String(event_pointer->occurences) + " seconds: " + String(event_pointer->timestamp) +
|
||||||
" data: " + String(event_pointer->data) +
|
" data: " + String(event_pointer->data) +
|
||||||
" level: " + String(get_event_level_string(event_handle)));
|
" level: " + String(get_event_level_string(event_handle)));
|
||||||
#endif
|
#endif
|
||||||
if (event_pointer->occurences > 0) {
|
content.concat("<div class='event'>");
|
||||||
content.concat("<div class='event'>");
|
content.concat("<div>" + String(get_event_enum_string(event_handle)) + "</div>");
|
||||||
content.concat("<div>" + String(get_event_enum_string(event_handle)) + "</div>");
|
content.concat("<div>" + String(get_event_level_string(event_handle)) + "</div>");
|
||||||
content.concat("<div>" + String(get_event_level_string(event_handle)) + "</div>");
|
content.concat("<div class='sec-ago'>" + String(timestamp_now - event_pointer->timestamp) + "</div>");
|
||||||
content.concat("<div class='sec-ago'>" + String(timestamp_now - event_pointer->timestamp) + "</div>");
|
content.concat("<div>" + String(event_pointer->occurences) + "</div>");
|
||||||
content.concat("<div>" + String(event_pointer->occurences) + "</div>");
|
content.concat("<div>" + String(event_pointer->data) + "</div>");
|
||||||
content.concat("<div>" + String(event_pointer->data) + "</div>");
|
content.concat("<div>" + String(get_event_message_string(event_handle)) + "</div>");
|
||||||
content.concat("<div>" + String(get_event_message_string(event_handle)) + "</div>");
|
content.concat("</div>"); // End of event row
|
||||||
content.concat("</div>"); // End of event row
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//clear the vector
|
||||||
|
order_events.clear();
|
||||||
content.concat(FPSTR(EVENTS_HTML_END));
|
content.concat(FPSTR(EVENTS_HTML_END));
|
||||||
return content;
|
return content;
|
||||||
|
return String();
|
||||||
}
|
}
|
||||||
return String();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Script for displaying event log before it gets minified
|
/* Script for displaying event log before it gets minified
|
||||||
|
|
|
@ -2,6 +2,9 @@
|
||||||
#define EVENTS_H
|
#define EVENTS_H
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <vector>
|
||||||
|
#include "../utils/events.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Replaces placeholder with content section in web page
|
* @brief Replaces placeholder with content section in web page
|
||||||
|
@ -11,5 +14,10 @@
|
||||||
* @return String
|
* @return String
|
||||||
*/
|
*/
|
||||||
String events_processor(const String& var);
|
String events_processor(const String& var);
|
||||||
|
// Define a struct to hold event data
|
||||||
|
struct EventData {
|
||||||
|
EVENTS_ENUM_TYPE event_handle;
|
||||||
|
const EVENTS_STRUCT_TYPE* event_pointer;
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue