Skip to content

Commit

Permalink
Add min fetcher for octoprint
Browse files Browse the repository at this point in the history
  • Loading branch information
suchmememanyskill committed Dec 15, 2024
1 parent 2f1b009 commit 6aa7463
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 14 deletions.
50 changes: 47 additions & 3 deletions CYD-Klipper/src/core/octoprint/octoprint_printer_integration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ bool OctoPrinter::fetch()
request_consecutive_fail_count = 0;
JsonDocument doc;
deserializeJson(doc, client.getStream());
parse_printer_state(doc);
parse_printer_status(doc);

doc.clear();
configure_http_client(client2, "/api/job", true, 1000, printer_config);
Expand Down Expand Up @@ -216,12 +216,56 @@ bool OctoPrinter::fetch()

PrinterDataMinimal OctoPrinter::fetch_min()
{
return {};
PrinterDataMinimal min = {};
min.success = true;
min.print_progress = 0;
min.power_devices = 0;
min.state = PrinterState::PrinterStateOffline;

{
HTTPClient client;
int http_code = 0;
configure_http_client(client, "/api/printer", true, 1000, printer_config);
int http_code = client.GET();

if (http_code == 200)
{
JsonDocument doc;
deserializeJson(doc, client.getStream());
min.state = parse_printer_state(doc);
}
else if (http_code == 409)
{
min.state = PrinterState::PrinterStateError;
return min;
}
else
{
return min;
}
}

{
HTTPClient client;
configure_http_client(client, "/api/job", true, 1000, printer_config);

if (client.GET() == 200)
{
JsonDocument doc;
deserializeJson(doc, client.getStream());
min.print_progress = parse_job_state_progress(doc);
}
else
{
min.state = PrinterState::PrinterStateError;
}
}

return min;
}

void OctoPrinter::disconnect()
{

}

const char* MACRO_AUTOLEVEL = "Auto-Level (G28+G29)";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ class OctoPrinter : public BasePrinter
bool no_printer = false;
unsigned char request_consecutive_fail_count{};

void parse_printer_state(JsonDocument& in);
void parse_printer_status(JsonDocument& in);
PrinterState parse_printer_state(JsonDocument& in);
void parse_job_state(JsonDocument& in);
float parse_job_state_progress(JsonDocument& in);
void parse_error(JsonDocument& in);
void OctoPrinter::parse_file_list(JsonDocument &in, std::list<OctoFileSystemFile> &files, int fetch_limit);
void parse_file_list(JsonDocument &in, std::list<OctoFileSystemFile> &files, int fetch_limit);

bool get_request(const char* endpoint, int timeout_ms = 1000);
void init_ui_panels();
Expand Down
29 changes: 20 additions & 9 deletions CYD-Klipper/src/core/octoprint/octoprint_printer_parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
#include "octoprint_printer_integration.hpp"
#include <ArduinoJson.h>

void OctoPrinter::parse_printer_state(JsonDocument& in)
PrinterState OctoPrinter::parse_printer_state(JsonDocument& in)
{
auto flags = in["state"]["flags"];
auto text = in["state"]["text"];

bool cancelling = flags["cancelling"];
bool closedOrError = flags["closedOrError"];
bool error = flags["error"];
Expand All @@ -21,25 +19,32 @@ void OctoPrinter::parse_printer_state(JsonDocument& in)

if (printing || resuming)
{
printer_data.state = PrinterState::PrinterStatePrinting;
return PrinterState::PrinterStatePrinting;
}
else if (pausing || paused)
{
printer_data.state = PrinterState::PrinterStatePaused;
return PrinterState::PrinterStatePaused;
}
else if (cancelling || finishing || ready)
{
printer_data.state = PrinterState::PrinterStateIdle;
return PrinterState::PrinterStateIdle;
}
else

return PrinterState::PrinterStateError;
}

void OctoPrinter::parse_printer_status(JsonDocument& in)
{
auto text = in["state"]["text"];
printer_data.state = parse_printer_state(in);

if (printer_data.state == PrinterState::PrinterStateError)
{
if (text != NULL && (printer_data.state_message == NULL || strcmp(printer_data.state_message, text)))
{
printer_data.state_message = (char *)malloc(strlen(text) + 1);
strcpy(printer_data.state_message, text);
}

printer_data.state = PrinterState::PrinterStateError;
}

auto temperature = in["temperature"];
Expand Down Expand Up @@ -88,6 +93,12 @@ void OctoPrinter::parse_job_state(JsonDocument& in)
printer_data.remaining_time_s = progress["printTimeLeft"];
}

float OctoPrinter::parse_job_state_progress(JsonDocument& in)
{
float completion = in["progress"]["completion"];
return completion / 100;
}

void OctoPrinter::parse_error(JsonDocument& in)
{
const char* error = in["error"];
Expand Down

0 comments on commit 6aa7463

Please sign in to comment.