Skip to content

Commit

Permalink
Add exporting CAN logs to .txt feature
Browse files Browse the repository at this point in the history
  • Loading branch information
dalathegreat committed Nov 26, 2024
1 parent db12c78 commit 76d24ea
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Software/src/datalayer/datalayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ typedef struct {
/** array with type of inverter used, for displaying on webserver */
char inverter_protocol[64] = {0};
/** array with incoming CAN messages, for displaying on webserver */
char logged_can_messages[10000] = {0};
char logged_can_messages[50000] = {0};
/** bool, determines if CAN messages should be logged for webserver */
bool can_logging_active = false;

Expand Down
2 changes: 2 additions & 0 deletions Software/src/devboard/webserver/can_logging_html.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ String can_logger_processor(const String& var) {
"monospace; }";
content += "</style>";
content += "<button onclick='refreshPage()'>Refresh data</button> ";
content += "<button onclick='exportLogs()'>Export to .txt</button> ";
content += "<button onclick='stopLoggingAndGoToMainPage()'>Back to main page</button>";

// Start a new block for the CAN messages
Expand Down Expand Up @@ -46,6 +47,7 @@ String can_logger_processor(const String& var) {
// Add JavaScript for navigation
content += "<script>";
content += "function refreshPage(){ location.reload(true); }";
content += "function exportLogs() { window.location.href = '/export_logs'; }";
content += "function stopLoggingAndGoToMainPage() {";
content += " fetch('/stop_logging').then(() => window.location.href = '/');";
content += "}";
Expand Down
28 changes: 28 additions & 0 deletions Software/src/devboard/webserver/webserver.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "webserver.h"
#include <Preferences.h>
#include <ctime>
#include "../../datalayer/datalayer.h"
#include "../../datalayer/datalayer_extended.h"
#include "../../lib/bblanchon-ArduinoJson/ArduinoJson.h"
Expand Down Expand Up @@ -68,6 +69,33 @@ void init_webserver() {
request->send_P(200, "text/plain", "Logging stopped");
});

// Define the handler to export logs
server.on("/export_logs", HTTP_GET, [](AsyncWebServerRequest* request) {
String logs = String(datalayer.system.info.logged_can_messages);
if (logs.length() == 0) {
logs = "No logs available.";
}

// Get the current time
time_t now = time(nullptr);
struct tm timeinfo;
localtime_r(&now, &timeinfo);

// Ensure time retrieval was successful
char filename[32];
if (strftime(filename, sizeof(filename), "canlog_%H-%M-%S.txt", &timeinfo)) {
// Valid filename created
} else {
// Fallback filename if automatic timestamping failed
strcpy(filename, "battery_emulator_can_log.txt");
}

// Use request->send with dynamic headers
AsyncWebServerResponse* response = request->beginResponse(200, "text/plain", logs);
response->addHeader("Content-Disposition", String("attachment; filename=\"") + String(filename) + "\"");
request->send(response);
});

// Route for going to cellmonitor web page
server.on("/cellmonitor", HTTP_GET, [](AsyncWebServerRequest* request) {
if (WEBSERVER_AUTH_REQUIRED && !request->authenticate(http_username, http_password))
Expand Down

0 comments on commit 76d24ea

Please sign in to comment.