89 lines
2.4 KiB
C++
89 lines
2.4 KiB
C++
#include "upload_status.h"
|
|
#include "flash_fs.h"
|
|
#include <LittleFS.h>
|
|
|
|
static char s_last_upload[65] = "";
|
|
static unsigned long s_last_upload_ms = 0;
|
|
static char s_scan_phase[32] = "idle";
|
|
static bool s_uploading = false;
|
|
|
|
static String json_escape(const String &s)
|
|
{
|
|
String out;
|
|
out.reserve(s.length() + 8);
|
|
for (size_t i = 0; i < s.length(); i++)
|
|
{
|
|
char c = s.charAt(i);
|
|
if (c == '\\') out += "\\\\";
|
|
else if (c == '"') out += "\\\"";
|
|
else if (c == '\n') out += "\\n";
|
|
else if (c == '\r') { /* skip */ }
|
|
else out += c;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
static int count_result_files()
|
|
{
|
|
int n = 0;
|
|
File dir = LittleFS.open(RESULTS_DIR);
|
|
if (!dir || !dir.isDirectory())
|
|
return 0;
|
|
File entry = dir.openNextFile();
|
|
while (entry)
|
|
{
|
|
if (!entry.isDirectory())
|
|
n++;
|
|
entry.close();
|
|
entry = dir.openNextFile();
|
|
}
|
|
dir.close();
|
|
return n;
|
|
}
|
|
|
|
void upload_status_set_phase(const char *phase)
|
|
{
|
|
if (!phase)
|
|
return;
|
|
strncpy(s_scan_phase, phase, sizeof(s_scan_phase) - 1);
|
|
s_scan_phase[sizeof(s_scan_phase) - 1] = '\0';
|
|
}
|
|
|
|
void upload_status_set_uploading(bool uploading)
|
|
{
|
|
s_uploading = uploading;
|
|
}
|
|
|
|
void upload_status_on_upload(const char *filename)
|
|
{
|
|
if (!filename)
|
|
return;
|
|
strncpy(s_last_upload, filename, sizeof(s_last_upload) - 1);
|
|
s_last_upload[sizeof(s_last_upload) - 1] = '\0';
|
|
s_last_upload_ms = millis();
|
|
s_uploading = false;
|
|
}
|
|
|
|
String upload_status_json()
|
|
{
|
|
String os = flash_fs_read_result("os.txt", 64);
|
|
String host = flash_fs_read_result("hostname.txt", 256);
|
|
os.trim();
|
|
host.trim();
|
|
int nl = host.indexOf('\n');
|
|
if (nl > 0)
|
|
host = host.substring(0, nl);
|
|
host.trim();
|
|
|
|
String json = "{";
|
|
json += "\"scan_phase\":\"" + json_escape(String(s_scan_phase)) + "\",";
|
|
json += "\"last_upload\":\"" + json_escape(String(s_last_upload)) + "\",";
|
|
json += "\"last_upload_ms\":" + String(s_last_upload_ms) + ",";
|
|
json += "\"file_count\":" + String(count_result_files()) + ",";
|
|
json += "\"os\":\"" + json_escape(os.length() ? os : "unknown") + "\",";
|
|
json += "\"hostname\":\"" + json_escape(host.length() ? host : "unknown") + "\",";
|
|
json += "\"uploading\":" + String(s_uploading ? "true" : "false");
|
|
json += "}";
|
|
return json;
|
|
}
|