mirror of
https://github.com/suchmememanyskill/CYD-Klipper.git
synced 2026-03-21 05:33:24 +00:00
Start implementation on file list
This commit is contained in:
@@ -330,7 +330,7 @@ Files KlipperPrinter::get_files()
|
|||||||
Files files_result = {0};
|
Files files_result = {0};
|
||||||
HTTPClient client;
|
HTTPClient client;
|
||||||
LOG_F(("Heap space pre-file-parse: %d bytes\n", esp_get_free_heap_size()));
|
LOG_F(("Heap space pre-file-parse: %d bytes\n", esp_get_free_heap_size()));
|
||||||
std::list<FileSystemFile> files;
|
std::list<KlipperFileSystemFile> files;
|
||||||
|
|
||||||
auto timer_request = millis();
|
auto timer_request = millis();
|
||||||
configure_http_client(client, "/server/files/list", true, 5000);
|
configure_http_client(client, "/server/files/list", true, 5000);
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
char* name;
|
char* name;
|
||||||
float modified;
|
float modified;
|
||||||
} FileSystemFile;
|
} KlipperFileSystemFile;
|
||||||
|
|
||||||
class KlipperPrinter : public BasePrinter
|
class KlipperPrinter : public BasePrinter
|
||||||
{
|
{
|
||||||
@@ -32,7 +32,7 @@ class KlipperPrinter : public BasePrinter
|
|||||||
int parse_macros_count(JsonDocument &in);
|
int parse_macros_count(JsonDocument &in);
|
||||||
PowerDevices parse_power_devices(JsonDocument &in);
|
PowerDevices parse_power_devices(JsonDocument &in);
|
||||||
int parse_power_devices_count(JsonDocument &in);
|
int parse_power_devices_count(JsonDocument &in);
|
||||||
void parse_file_list(JsonDocument &in, std::list<FileSystemFile> &files, int fetch_limit);
|
void parse_file_list(JsonDocument &in, std::list<KlipperFileSystemFile> &files, int fetch_limit);
|
||||||
char *parse_thumbnails(JsonDocument &in);
|
char *parse_thumbnails(JsonDocument &in);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -310,13 +310,13 @@ int KlipperPrinter::parse_power_devices_count(JsonDocument &in)
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
void KlipperPrinter::parse_file_list(JsonDocument &in, std::list<FileSystemFile> &files, int fetch_limit)
|
void KlipperPrinter::parse_file_list(JsonDocument &in, std::list<KlipperFileSystemFile> &files, int fetch_limit)
|
||||||
{
|
{
|
||||||
auto result = in["result"].as<JsonArray>();
|
auto result = in["result"].as<JsonArray>();
|
||||||
|
|
||||||
for (auto file : result)
|
for (auto file : result)
|
||||||
{
|
{
|
||||||
FileSystemFile f = {0};
|
KlipperFileSystemFile f = {0};
|
||||||
const char *path = file["path"];
|
const char *path = file["path"];
|
||||||
float modified = file["modified"];
|
float modified = file["modified"];
|
||||||
auto file_iter = files.begin();
|
auto file_iter = files.begin();
|
||||||
|
|||||||
@@ -269,6 +269,19 @@ bool OctoPrinter::set_power_device_state(const char* device_name, bool state)
|
|||||||
|
|
||||||
Files OctoPrinter::get_files()
|
Files OctoPrinter::get_files()
|
||||||
{
|
{
|
||||||
|
HTTPClient client;
|
||||||
|
JsonDocument filter;
|
||||||
|
JsonDocument doc;
|
||||||
|
configure_http_client(client, "/api/files?recursive=true", true, 5000, printer_config);
|
||||||
|
|
||||||
|
filter["files"][0]["path"] = true;
|
||||||
|
filter["files"][0]["date"] = true;
|
||||||
|
|
||||||
|
if (client.GET() == 200)
|
||||||
|
{
|
||||||
|
auto parseResult = deserializeJson(doc, client.getStream(), DeserializationOption::Filter(filter));
|
||||||
|
}
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
#include "../printer_integration.hpp"
|
#include "../printer_integration.hpp"
|
||||||
#include <HTTPClient.h>
|
#include <HTTPClient.h>
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char* name;
|
||||||
|
float modified;
|
||||||
|
} OctoFileSystemFile;
|
||||||
|
|
||||||
class OctoPrinter : public BasePrinter
|
class OctoPrinter : public BasePrinter
|
||||||
{
|
{
|
||||||
@@ -11,6 +17,7 @@ class OctoPrinter : public BasePrinter
|
|||||||
void parse_printer_state(JsonDocument& in);
|
void parse_printer_state(JsonDocument& in);
|
||||||
void parse_job_state(JsonDocument& in);
|
void parse_job_state(JsonDocument& in);
|
||||||
void parse_error(JsonDocument& in);
|
void parse_error(JsonDocument& in);
|
||||||
|
void OctoPrinter::parse_file_list(JsonDocument &in, std::list<OctoFileSystemFile> &files, int fetch_limit);
|
||||||
|
|
||||||
bool get_request(const char* endpoint, int timeout_ms = 1000, bool stream = true);
|
bool get_request(const char* endpoint, int timeout_ms = 1000, bool stream = true);
|
||||||
bool post_request(const char* endpoint, const char* body, int timeout_ms = 1000, bool stream = false);
|
bool post_request(const char* endpoint, const char* body, int timeout_ms = 1000, bool stream = false);
|
||||||
|
|||||||
@@ -102,3 +102,51 @@ void OctoPrinter::parse_error(JsonDocument& in)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void OctoPrinter::parse_file_list(JsonDocument &in, std::list<OctoFileSystemFile> &files, int fetch_limit)
|
||||||
|
{
|
||||||
|
auto result = in["files"].as<JsonArray>();
|
||||||
|
|
||||||
|
for (auto file : result)
|
||||||
|
{
|
||||||
|
OctoFileSystemFile f = {0};
|
||||||
|
const char *path = file["path"];
|
||||||
|
float modified = file["date"];
|
||||||
|
auto file_iter = files.begin();
|
||||||
|
|
||||||
|
while (file_iter != files.end())
|
||||||
|
{
|
||||||
|
if ((*file_iter).modified < modified)
|
||||||
|
break;
|
||||||
|
|
||||||
|
file_iter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file_iter == files.end() && files.size() >= fetch_limit)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
f.name = (char *)malloc(strlen(path) + 1);
|
||||||
|
if (f.name == NULL)
|
||||||
|
{
|
||||||
|
LOG_LN("Failed to allocate memory");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
strcpy(f.name, path);
|
||||||
|
f.modified = modified;
|
||||||
|
|
||||||
|
if (file_iter != files.end())
|
||||||
|
files.insert(file_iter, f);
|
||||||
|
else
|
||||||
|
files.push_back(f);
|
||||||
|
|
||||||
|
if (files.size() > fetch_limit)
|
||||||
|
{
|
||||||
|
auto last_entry = files.back();
|
||||||
|
|
||||||
|
if (last_entry.name != NULL)
|
||||||
|
free(last_entry.name);
|
||||||
|
|
||||||
|
files.pop_back();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user