OctoPrint Client reimplemented
parent
dec72ca0a9
commit
9972bd35a0
|
|
@ -1,3 +1,4 @@
|
||||||
|
// Octoprint API: https://docs.octoprint.org/en/master/api/index.html
|
||||||
// ArduinoJSON Assistant: https://arduinojson.org/v6/assistant/
|
// ArduinoJSON Assistant: https://arduinojson.org/v6/assistant/
|
||||||
|
|
||||||
#include "OctoPrintClient.h"
|
#include "OctoPrintClient.h"
|
||||||
|
|
@ -76,15 +77,11 @@ void OctoPrintClient::getPrinterJobResults() {
|
||||||
printerData.state = this->translateState((const char*)(*jsonDoc)["state"]);
|
printerData.state = this->translateState((const char*)(*jsonDoc)["state"]);
|
||||||
|
|
||||||
if (BasePrinterClientImpl::isOperational()) {
|
if (BasePrinterClientImpl::isOperational()) {
|
||||||
this->debugController->printLn("Status: " + printerData.state);
|
this->debugController->printLn("Status: " + this->getStateAsText());
|
||||||
} else {
|
} else {
|
||||||
this->debugController->printLn("Printer Not Operational");
|
this->debugController->printLn("Printer Not Operational");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Req 2
|
// Req 2
|
||||||
jsonDoc = this->jsonRequestClient->requestJson(
|
jsonDoc = this->jsonRequestClient->requestJson(
|
||||||
PRINTER_REQUEST_GET,
|
PRINTER_REQUEST_GET,
|
||||||
|
|
@ -108,86 +105,76 @@ void OctoPrintClient::getPrinterJobResults() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
printerData.progressCompletion = (int)(*jsonDoc)["result"]["status"]["display_status"]["progress"];
|
String printing = (const char*)(*jsonDoc)["state"]["flags"]["printing"];
|
||||||
printerData.toolTemp = (int)(*jsonDoc)["result"]["status"]["extruder"]["temperature"];
|
if (printing == "true") {
|
||||||
printerData.toolTargetTemp = (int)(*jsonDoc)["result"]["status"]["extruder"]["target"];
|
printerData.isPrinting = true;
|
||||||
printerData.bedTemp = (int)(*jsonDoc)["result"]["status"]["heater_bed"]["temperature"];
|
} else {
|
||||||
printerData.bedTargetTemp = (int)(*jsonDoc)["result"]["status"]["heater_bed"]["target"];
|
printerData.isPrinting = false;
|
||||||
float fileProgress = (float)(*jsonDoc)["result"]["status"]["virtual_sdcard"]["progress"];
|
}
|
||||||
printerData.progressFilepos = (const char*)(*jsonDoc)["result"]["status"]["virtual_sdcard"]["file_position"];
|
printerData.toolTemp = (const char*)(*jsonDoc)["temperature"]["tool0"]["actual"];
|
||||||
printerData.estimatedPrintTime = (float)(*jsonDoc)["result"]["status"]["toolhead"]["estimated_print_time"];
|
printerData.toolTargetTemp = (const char*)(*jsonDoc)["temperature"]["tool0"]["target"];
|
||||||
|
printerData.bedTemp = (const char*)(*jsonDoc)["temperature"]["bed"]["actual"];
|
||||||
|
printerData.bedTargetTemp = (const char*)(*jsonDoc)["temperature"]["bed"]["target"];
|
||||||
|
|
||||||
/*
|
|
||||||
printerData.progressPrintTimeLeft : No metadata is available, print duration and progress can be used to calculate the ETA:
|
|
||||||
*/
|
|
||||||
float totalPrintTime = printerData.progressPrintTime.toFloat() / fileProgress;
|
|
||||||
printerData.progressPrintTimeLeft = String(totalPrintTime - printerData.progressPrintTime.toFloat());
|
|
||||||
|
|
||||||
if (BasePrinterClientImpl::isOperational()) {
|
if (BasePrinterClientImpl::isOperational()) {
|
||||||
this->debugController->printLn("Status: " + this->getStateAsText() + " " + printerData.fileName + "(" + printerData.progressCompletion + "%)");
|
this->debugController->printLn("Status: " + this->getStateAsText() + " " + printerData.fileName + "(" + printerData.progressCompletion + "%)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void OctoPrintClient::getPrinterPsuState() {
|
void OctoPrintClient::getPrinterPsuState() {
|
||||||
/*// get the PSU state (if enabled and printer operational)
|
const size_t bufferSize = 364; // according to ArduinoJson assistant
|
||||||
|
DynamicJsonDocument *jsonDoc;
|
||||||
|
|
||||||
|
// get the PSU state (if enabled and printer operational)
|
||||||
if (pollPsu && BasePrinterClientImpl::isOperational()) {
|
if (pollPsu && BasePrinterClientImpl::isOperational()) {
|
||||||
if (!validate()) {
|
if (!validate()) {
|
||||||
printerData.isPSUoff = false; // we do not know PSU state, so assume on.
|
printerData.isPSUoff = false; // we do not know PSU state, so assume on.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String apiPostData = "POST /api/plugin/psucontrol HTTP/1.1";
|
// Req 2
|
||||||
String apiPostBody = "{\"command\":\"getPSUState\"}";
|
jsonDoc = this->jsonRequestClient->requestJson(
|
||||||
WiFiClient printClient = getPostRequest(apiPostData,apiPostBody);
|
PRINTER_REQUEST_POST,
|
||||||
if (printerData.error != "") {
|
this->getInstanceServerTarget(),
|
||||||
printerData.isPSUoff = false; // we do not know PSU state, so assume on.
|
this->getInstanceServerPort(),
|
||||||
return;
|
this->encodedAuth,
|
||||||
}
|
"/api/plugin/psucontrol",
|
||||||
const size_t bufferSize3 = JSON_OBJECT_SIZE(2) + 300;
|
"{\"command\":\"getPSUState\"}",
|
||||||
DynamicJsonDocument jsonBuffer(bufferSize3);
|
bufferSize,
|
||||||
|
true
|
||||||
// Parse JSON object
|
);
|
||||||
DeserializationError error = deserializeJson(jsonBuffer, printClient);
|
if (this->jsonRequestClient->getLastError() != "") {
|
||||||
if (error) {
|
// we do not know PSU state, so assume on.
|
||||||
printerData.isPSUoff = false; // we do not know PSU state, so assume on
|
printerData.isPSUoff = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String psu = (const char*)jsonBuffer["isPSUOn"];
|
String psu = (const char*)(*jsonDoc)["isPSUOn"];
|
||||||
if (psu == "true") {
|
if (psu == "true") {
|
||||||
printerData.isPSUoff = false; // PSU checked and is on
|
printerData.isPSUoff = false; // PSU checked and is on
|
||||||
} else {
|
} else {
|
||||||
printerData.isPSUoff = true; // PSU checked and is off, set flag
|
printerData.isPSUoff = true; // PSU checked and is off, set flag
|
||||||
}
|
}
|
||||||
printClient.stop(); //stop client
|
|
||||||
} else {
|
} else {
|
||||||
printerData.isPSUoff = false; // we are not checking PSU state, so assume on
|
printerData.isPSUoff = false; // we are not checking PSU state, so assume on
|
||||||
} */
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* We translate the avail states
|
* We translate the avail states
|
||||||
* - "standby": No print in progress
|
|
||||||
* - "printing": The printer is currently printing
|
|
||||||
* - "paused": A print in progress has been paused
|
|
||||||
* - "error": The print exited with an error. print_stats.message contains a related error message
|
|
||||||
* - "complete": The last print has completed
|
|
||||||
*/
|
*/
|
||||||
int OctoPrintClient::translateState(String stateText) {
|
int OctoPrintClient::translateState(String stateText) {
|
||||||
stateText.toLowerCase();
|
stateText.toLowerCase();
|
||||||
stateText.trim();
|
stateText.trim();
|
||||||
if (stateText == "standby") {
|
if (stateText == "operational" || stateText == "ready" || stateText == "sdready") {
|
||||||
return PRINTER_STATE_STANDBY;
|
return PRINTER_STATE_STANDBY;
|
||||||
}
|
}
|
||||||
if (stateText == "printing") {
|
if (stateText == "printing" || stateText == "cancelling") {
|
||||||
return PRINTER_STATE_PRINTING;
|
return PRINTER_STATE_PRINTING;
|
||||||
}
|
}
|
||||||
if (stateText == "paused") {
|
if (stateText == "paused") {
|
||||||
return PRINTER_STATE_PAUSED;
|
return PRINTER_STATE_PAUSED;
|
||||||
}
|
}
|
||||||
if (stateText == "complete") {
|
if (stateText == "error" || stateText == "closedorerror") {
|
||||||
return PRINTER_STATE_COMPLETED;
|
|
||||||
}
|
|
||||||
if (stateText == "error") {
|
|
||||||
return PRINTER_STATE_ERROR;
|
return PRINTER_STATE_ERROR;
|
||||||
}
|
}
|
||||||
return PRINTER_STATE_OFFLINE;
|
return PRINTER_STATE_OFFLINE;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue