Bringing klipper up (can use ip or hostname, preffer ip)

pull/125/head
Robert Stein 2020-12-14 08:58:55 +01:00
parent 290fea0311
commit 13c04d65e8
10 changed files with 85 additions and 67 deletions

View File

@ -11,8 +11,8 @@ KlipperClient::KlipperClient(GlobalDataController *globalDataController, DebugCo
void KlipperClient::updatePrintClient() { void KlipperClient::updatePrintClient() {
this->globalDataController->getPrinterHostName().toCharArray(this->myServer, 100); this->globalDataController->getPrinterHostName().toCharArray(this->myServer, 100);
myApiKey = this->globalDataController->getPrinterApiKey(); this->globalDataController->getPrinterServer().toCharArray(this->myServerIp, 30);
myPort = this->globalDataController->getPrinterPort(); this->myPort = this->globalDataController->getPrinterPort();
encodedAuth = ""; encodedAuth = "";
if (this->globalDataController->getPrinterAuthUser() != "") { if (this->globalDataController->getPrinterAuthUser() != "") {
String userpass = this->globalDataController->getPrinterAuthUser() + ":" + this->globalDataController->getPrinterAuthPass(); String userpass = this->globalDataController->getPrinterAuthUser() + ":" + this->globalDataController->getPrinterAuthPass();
@ -25,11 +25,8 @@ void KlipperClient::updatePrintClient() {
boolean KlipperClient::validate() { boolean KlipperClient::validate() {
boolean rtnValue = false; boolean rtnValue = false;
printerData.error = ""; printerData.error = "";
if (String(myServer) == "") { if ((String(this->myServer) == "") && (String(this->myServerIp) == "")) {
printerData.error += "Server address is required; "; printerData.error += "Server address or host name is required; ";
}
if (myApiKey == "") {
printerData.error += "ApiKey is required; ";
} }
if (printerData.error == "") { if (printerData.error == "") {
rtnValue = true; rtnValue = true;
@ -40,14 +37,17 @@ boolean KlipperClient::validate() {
WiFiClient KlipperClient::getSubmitRequest(String apiGetData) { WiFiClient KlipperClient::getSubmitRequest(String apiGetData) {
WiFiClient printClient; WiFiClient printClient;
printClient.setTimeout(5000); printClient.setTimeout(5000);
const char *targetServer = this->myServerIp;
if (String(this->myServerIp) == "") {
targetServer = this->myServer;
}
this->debugController->printLn("Getting Klipper Data via GET"); this->debugController->printLn("Getting Klipper Data via GET");
this->debugController->printLn(apiGetData); this->debugController->printLn(apiGetData);
result = ""; result = "";
if (printClient.connect(myServer, myPort)) { //starts client connection, checks for connection if (printClient.connect(targetServer, myPort)) { //starts client connection, checks for connection
printClient.println(apiGetData); printClient.println(apiGetData);
printClient.println("Host: " + String(myServer) + ":" + String(myPort)); printClient.println("Host: " + String(targetServer) + ":" + String(myPort));
printClient.println("X-Api-Key: " + myApiKey);
if (encodedAuth != "") { if (encodedAuth != "") {
printClient.print("Authorization: "); printClient.print("Authorization: ");
printClient.println("Basic " + encodedAuth); printClient.println("Basic " + encodedAuth);
@ -55,18 +55,18 @@ WiFiClient KlipperClient::getSubmitRequest(String apiGetData) {
printClient.println("User-Agent: ArduinoWiFi/1.1"); printClient.println("User-Agent: ArduinoWiFi/1.1");
printClient.println("Connection: close"); printClient.println("Connection: close");
if (printClient.println() == 0) { if (printClient.println() == 0) {
this->debugController->printLn("Connection to " + String(myServer) + ":" + String(myPort) + " failed."); this->debugController->printLn("Connection to " + String(targetServer) + ":" + String(myPort) + " failed.");
this->debugController->printLn(""); this->debugController->printLn("");
resetPrintData(); resetPrintData();
printerData.error = "Connection to " + String(myServer) + ":" + String(myPort) + " failed."; printerData.error = "Connection to " + String(targetServer) + ":" + String(myPort) + " failed.";
return printClient; return printClient;
} }
} }
else { else {
this->debugController->printLn("Connection to Klipper failed: " + String(myServer) + ":" + String(myPort)); //error message if no client connect this->debugController->printLn("Connection to Klipper failed: " + String(targetServer) + ":" + String(myPort)); //error message if no client connect
this->debugController->printLn(""); this->debugController->printLn("");
resetPrintData(); resetPrintData();
printerData.error = "Connection to Klipper failed: " + String(myServer) + ":" + String(myPort); printerData.error = "Connection to Klipper failed: " + String(targetServer) + ":" + String(myPort);
return printClient; return printClient;
} }
@ -85,7 +85,7 @@ WiFiClient KlipperClient::getSubmitRequest(String apiGetData) {
char endOfHeaders[] = "\r\n\r\n"; char endOfHeaders[] = "\r\n\r\n";
if (!printClient.find(endOfHeaders)) { if (!printClient.find(endOfHeaders)) {
this->debugController->printLn("Invalid response"); this->debugController->printLn("Invalid response");
printerData.error = "Invalid response from " + String(myServer) + ":" + String(myPort); printerData.error = "Invalid response from " + String(targetServer) + ":" + String(myPort);
printerData.state = ""; printerData.state = "";
} }
@ -95,18 +95,21 @@ WiFiClient KlipperClient::getSubmitRequest(String apiGetData) {
WiFiClient KlipperClient::getPostRequest(String apiPostData, String apiPostBody) { WiFiClient KlipperClient::getPostRequest(String apiPostData, String apiPostBody) {
WiFiClient printClient; WiFiClient printClient;
printClient.setTimeout(5000); printClient.setTimeout(5000);
const char *targetServer = this->myServerIp;
if (String(this->myServerIp) == "") {
targetServer = this->myServer;
}
this->debugController->printLn("Getting Klipper Data via POST"); this->debugController->printLn("Getting Klipper Data via POST");
this->debugController->printLn(apiPostData + " | " + apiPostBody); this->debugController->printLn(apiPostData + " | " + apiPostBody);
result = ""; result = "";
if (printClient.connect(myServer, myPort)) { //starts client connection, checks for connection if (printClient.connect(targetServer, myPort)) { //starts client connection, checks for connection
printClient.println(apiPostData); printClient.println(apiPostData);
printClient.println("Host: " + String(myServer) + ":" + String(myPort)); printClient.println("Host: " + String(targetServer) + ":" + String(myPort));
printClient.println("Connection: close"); printClient.println("Connection: close");
printClient.println("X-Api-Key: " + myApiKey);
if (encodedAuth != "") { if (encodedAuth != "") {
printClient.print("Authorization: "); printClient.print("Authorization: ");
printClient.println("Basic " + encodedAuth); printClient.println("Basic " + encodedAuth);
} }
printClient.println("User-Agent: ArduinoWiFi/1.1"); printClient.println("User-Agent: ArduinoWiFi/1.1");
printClient.println("Content-Type: application/json"); printClient.println("Content-Type: application/json");
@ -115,18 +118,18 @@ WiFiClient KlipperClient::getPostRequest(String apiPostData, String apiPostBody)
printClient.println(); printClient.println();
printClient.println(apiPostBody); printClient.println(apiPostBody);
if (printClient.println() == 0) { if (printClient.println() == 0) {
this->debugController->printLn("Connection to " + String(myServer) + ":" + String(myPort) + " failed."); this->debugController->printLn("Connection to " + String(targetServer) + ":" + String(myPort) + " failed.");
this->debugController->printLn(""); this->debugController->printLn("");
resetPrintData(); resetPrintData();
printerData.error = "Connection to " + String(myServer) + ":" + String(myPort) + " failed."; printerData.error = "Connection to " + String(targetServer) + ":" + String(myPort) + " failed.";
return printClient; return printClient;
} }
} }
else { else {
this->debugController->printLn("Connection to Klipper failed: " + String(myServer) + ":" + String(myPort)); //error message if no client connect this->debugController->printLn("Connection to Klipper failed: " + String(targetServer) + ":" + String(myPort)); //error message if no client connect
this->debugController->printLn(""); this->debugController->printLn("");
resetPrintData(); resetPrintData();
printerData.error = "Connection to Klipper failed: " + String(myServer) + ":" + String(myPort); printerData.error = "Connection to Klipper failed: " + String(targetServer) + ":" + String(myPort);
return printClient; return printClient;
} }
@ -145,7 +148,7 @@ WiFiClient KlipperClient::getPostRequest(String apiPostData, String apiPostBody)
char endOfHeaders[] = "\r\n\r\n"; char endOfHeaders[] = "\r\n\r\n";
if (!printClient.find(endOfHeaders)) { if (!printClient.find(endOfHeaders)) {
this->debugController->printLn("Invalid response"); this->debugController->printLn("Invalid response");
printerData.error = "Invalid response from " + String(myServer) + ":" + String(myPort); printerData.error = "Invalid response from " + String(targetServer) + ":" + String(myPort);
printerData.state = ""; printerData.state = "";
} }
@ -156,6 +159,11 @@ void KlipperClient::getPrinterJobResults() {
if (!validate()) { if (!validate()) {
return; return;
} }
const char *targetServer = this->myServerIp;
if (String(this->myServerIp) == "") {
targetServer = this->myServer;
}
//**** get the Printer Job status //**** get the Printer Job status
String apiGetData = "GET /printer/objects/query?heater_bed&extruder&webhooks&virtual_sdcard&print_stats&toolhead&display_status"; String apiGetData = "GET /printer/objects/query?heater_bed&extruder&webhooks&virtual_sdcard&print_stats&toolhead&display_status";
WiFiClient printClient = getSubmitRequest(apiGetData); WiFiClient printClient = getSubmitRequest(apiGetData);
@ -168,8 +176,8 @@ void KlipperClient::getPrinterJobResults() {
// Parse JSON object // Parse JSON object
DeserializationError error = deserializeJson(jsonBuffer, printClient); DeserializationError error = deserializeJson(jsonBuffer, printClient);
if (error) { if (error) {
this->debugController->printLn("Klipper Data Parsing failed: " + String(myServer) + ":" + String(myPort)); this->debugController->printLn("Klipper Data Parsing failed: " + String(targetServer) + ":" + String(myPort));
printerData.error = "Klipper Data Parsing failed: " + String(myServer) + ":" + String(myPort); printerData.error = "Klipper Data Parsing failed: " + String(targetServer) + ":" + String(myPort);
printerData.state = ""; printerData.state = "";
printerData.isPrinting = false; printerData.isPrinting = false;
printerData.toolTemp = ""; printerData.toolTemp = "";

View File

@ -9,8 +9,8 @@
class KlipperClient : public BasePrinterClient { class KlipperClient : public BasePrinterClient {
private: private:
char myServer[100]; char myServer[100];
char myServerIp[30];
int myPort = 7125; int myPort = 7125;
String myApiKey = "";
String encodedAuth = ""; String encodedAuth = "";
boolean pollPsu; boolean pollPsu;
const String printerType = "Klipper"; const String printerType = "Klipper";

View File

@ -7,6 +7,7 @@ public:
virtual void preSetup(); virtual void preSetup();
virtual void postSetup(); virtual void postSetup();
virtual void handleUpdate(); virtual void handleUpdate();
virtual void flipDisplayUpdate();
virtual void showBootScreen(); virtual void showBootScreen();
virtual void showApAccessScreen(String apSsid, String apIp); virtual void showApAccessScreen(String apSsid, String apIp);
virtual void showWebserverSplashScreen(bool isEnabled); virtual void showWebserverSplashScreen(bool isEnabled);

View File

@ -20,6 +20,10 @@ void NextionDisplay::handleUpdate() {
} }
void NextionDisplay::flipDisplayUpdate() {
}
void NextionDisplay::showBootScreen() { void NextionDisplay::showBootScreen() {
String command("version.txt="); String command("version.txt=");
command += "\"for " + this->globalDataController->getPrinterClient()->getPrinterType() + " V" + this->globalDataController->getVersion() + "\""; command += "\"for " + this->globalDataController->getPrinterClient()->getPrinterType() + " V" + this->globalDataController->getVersion() + "\"";

View File

@ -18,6 +18,7 @@ public:
void preSetup(); void preSetup();
void postSetup(); void postSetup();
void handleUpdate(); void handleUpdate();
void flipDisplayUpdate();
void showBootScreen(); void showBootScreen();
void showApAccessScreen(String apSsid, String apIp); void showApAccessScreen(String apSsid, String apIp);
void showWebserverSplashScreen(bool isEnabled); void showWebserverSplashScreen(bool isEnabled);

View File

@ -46,6 +46,14 @@ void OledDisplay::handleUpdate() {
this->ui->update(); this->ui->update();
} }
void OledDisplay::flipDisplayUpdate() {
this->ui->init();
if (this->globalDataController->isDisplayInverted()) {
this->oledDisplay->flipScreenVertically(); // connections at top of OLED display
}
this->ui->update();
}
void OledDisplay::showBootScreen() { void OledDisplay::showBootScreen() {
this->oledDisplay->setTextAlignment(TEXT_ALIGN_CENTER); this->oledDisplay->setTextAlignment(TEXT_ALIGN_CENTER);
this->oledDisplay->setContrast(255); // default is 255 this->oledDisplay->setContrast(255); // default is 255

View File

@ -27,6 +27,7 @@ public:
void preSetup(); void preSetup();
void postSetup(); void postSetup();
void handleUpdate(); void handleUpdate();
void flipDisplayUpdate();
void showBootScreen(); void showBootScreen();
void showApAccessScreen(String apSsid, String apIp); void showApAccessScreen(String apSsid, String apIp);
void showWebserverSplashScreen(bool isEnabled); void showWebserverSplashScreen(bool isEnabled);

View File

@ -208,7 +208,7 @@ void GlobalDataController::setDisplayClient(BaseDisplayClient *baseDisplayClient
this->baseDisplayClient = baseDisplayClient; this->baseDisplayClient = baseDisplayClient;
} }
BaseDisplayClient *GlobalDataController::setDisplayClient() { BaseDisplayClient *GlobalDataController::getDisplayClient() {
return this->baseDisplayClient; return this->baseDisplayClient;
} }

View File

@ -64,7 +64,7 @@ public:
TimeClient *getTimeClient(); TimeClient *getTimeClient();
OpenWeatherMapClient *getWeatherClient(); OpenWeatherMapClient *getWeatherClient();
BasePrinterClient *getPrinterClient(); BasePrinterClient *getPrinterClient();
BaseDisplayClient *setDisplayClient(); BaseDisplayClient *getDisplayClient();
String getLastReportStatus(); String getLastReportStatus();
String getVersion(); String getVersion();

View File

@ -323,13 +323,10 @@ void WebServer::handleUpdateConfig() {
this->globalDataController->getPrinterClient()->getPrinterJobResults(); this->globalDataController->getPrinterClient()->getPrinterJobResults();
this->globalDataController->getPrinterClient()->getPrinterPsuState(); this->globalDataController->getPrinterClient()->getPrinterPsuState();
/*if (INVERT_DISPLAY != flipOld) { if (this->globalDataController->isDisplayInverted() != flipOld) {
ui.init(); this->globalDataController->getDisplayClient()->flipDisplayUpdate();
if(INVERT_DISPLAY)
display.flipScreenVertically();
ui.update();
} }
checkDisplay();*/ this->globalDataController->getDisplayClient()->handleUpdate();
this->globalDataController->getTimeClient()->resetLastEpoch(); this->globalDataController->getTimeClient()->resetLastEpoch();
this->redirectHome(); this->redirectHome();
} }
@ -415,11 +412,10 @@ void WebServer::handleConfigure() {
this->server->sendContent(html); this->server->sendContent(html);
} }
else if (printerClient->getPrinterType() == "Klipper") { else if (printerClient->getPrinterType() == "Klipper") {
// TODO: INVALID JAVASCRIPT HERE html = "<script>function testKlipper(){var e=document.getElementById(\"KlipperTest\"),t=document.getElementById(\"PrinterAddress\").value,"
/*html = "<script>function testKlipper(){var e=document.getElementById(\"KlipperTest\"),r=document.getElementById(\"PrinterAddress\").value," "n=document.getElementById(\"PrinterPort\").value;if(e.innerHTML=\"\",\"\"==t||\"\"==n)return e.innerHTML=\"* Address and Port are required\","
"t=document.getElementById(\"PrinterPort\").value;if(\"\"==r||\"\"==t)return e.innerHTML=\"* Address and Port are required\"," "void(e.style.background=\"\");var r=\"http://\"+t+\":\"+n;r+=\"/printer/info\",window.open(r,\"_blank\").focus()}</script>";
"void(e.style.background=\"\");var n=\"http://\"+r+\":\"+t;n+=\"/printer/info"; this->server->sendContent(html);
this->server->sendContent(html); */
} }
else { else {
html = "<script>function testOctoPrint(){var e=document.getElementById(\"OctoPrintTest\"),t=document.getElementById(\"PrinterAddress\").value," html = "<script>function testOctoPrint(){var e=document.getElementById(\"OctoPrintTest\"),t=document.getElementById(\"PrinterAddress\").value,"
@ -429,38 +425,37 @@ void WebServer::handleConfigure() {
} }
String form = "<form class='w3-container' action='/updateconfig' method='get'><h2>Station Config:</h2>" String form = "<form class='w3-container' action='/updateconfig' method='get'><h2>Station Config:</h2>";
"<p><label>" + printerClient->getPrinterType() + " API Key (get from your server)</label>"
"<input class='w3-input w3-border w3-margin-bottom' type='text' name='PrinterApiKey' id='PrinterApiKey' value='%OCTOKEY%' maxlength='60'></p>";
if (printerClient->getPrinterType() != "Klipper") {
form += "<p><label>" + printerClient->getPrinterType() + " API Key (get from your server)</label>"
"<input class='w3-input w3-border w3-margin-bottom' type='text' name='PrinterApiKey' id='PrinterApiKey' value='%PRINTERAPIKEY%' maxlength='60'></p>";
}
if ((printerClient->getPrinterType() == "OctoPrint") || (printerClient->getPrinterType() == "Klipper")) { if ((printerClient->getPrinterType() == "OctoPrint") || (printerClient->getPrinterType() == "Klipper")) {
form += "<p><label>" + printerClient->getPrinterType() + " Host Name</label><input class='w3-input w3-border w3-margin-bottom' type='text' name='PrinterHostName' value='%OCTOHOST%' maxlength='60'></p>"; form += "<p><label>" + printerClient->getPrinterType() + " Host Name</label><input class='w3-input w3-border w3-margin-bottom' type='text' name='PrinterHostName' value='%PRINTERHOST%' maxlength='60'></p>";
} }
form += "<p><label>" + printerClient->getPrinterType() + " Address (do not include http://)</label>" form += "<p><label>" + printerClient->getPrinterType() + " Address (do not include http://)</label>"
"<input class='w3-input w3-border w3-margin-bottom' type='text' name='PrinterAddress' id='PrinterAddress' value='%OCTOADDRESS%' maxlength='60'></p>" "<input class='w3-input w3-border w3-margin-bottom' type='text' name='PrinterAddress' id='PrinterAddress' value='%PRINTERADDRESS%' maxlength='60'></p>"
"<p><label>" + printerClient->getPrinterType() + " Port</label>" "<p><label>" + printerClient->getPrinterType() + " Port</label>"
"<input class='w3-input w3-border w3-margin-bottom' type='text' name='PrinterPort' id='PrinterPort' value='%OCTOPORT%' maxlength='5' onkeypress='return isNumberKey(event)'></p>"; "<input class='w3-input w3-border w3-margin-bottom' type='text' name='PrinterPort' id='PrinterPort' value='%PRINTERPORT%' maxlength='5' onkeypress='return isNumberKey(event)'></p>";
if (printerClient->getPrinterType() == "Repetier") { if ((printerClient->getPrinterType() == "Repetier") || (printerClient->getPrinterType() == "Klipper")) {
form += "<input type='button' value='Test Connection' onclick='testRepetier()'>" form += "<input type='button' value='Test Connection' onclick='test" + printerClient->getPrinterName() + "()'>"
"<input type='hidden' id='selectedPrinter' value='" + printerClient->getPrinterName() + "'><p id='RepetierTest'></p>" "<input type='hidden' id='selectedPrinter' value='" + printerClient->getPrinterName() + "'><p id='" + printerClient->getPrinterName() + "Test'></p>"
"<script>testRepetier();</script>"; "<script>test" + printerClient->getPrinterName() + "();</script>";
} }
else if (printerClient->getPrinterType() == "Klipper") {
form += "<input type='button' value='Test Connection' onclick='testKlipper()'>"
"<input type='hidden' id='selectedPrinter' value='" + printerClient->getPrinterName() + "'><p id='KlipperTest'></p>"
"<script>testKlipper();</script>";
}
else { else {
form += "<input type='button' value='Test Connection and API JSON Response' onclick='testOctoPrint()'><p id='OctoPrintTest'></p>"; form += "<input type='button' value='Test Connection and API JSON Response' onclick='testOctoPrint()'><p id='OctoPrintTest'></p>";
} }
form += "<p><label>" + printerClient->getPrinterType() + " User (only needed if you have haproxy or basic auth turned on)</label><input class='w3-input w3-border w3-margin-bottom' type='text' name='octoUser' value='%OCTOUSER%' maxlength='30'></p>" form += "<p><label>" + printerClient->getPrinterType() + " User (only needed if you have haproxy or basic auth turned on)</label><input class='w3-input w3-border w3-margin-bottom' type='text' name='octoUser' value='%PRINTERUSER%' maxlength='30'></p>"
"<p><label>" + printerClient->getPrinterType() + " Password </label><input class='w3-input w3-border w3-margin-bottom' type='password' name='octoPass' value='%OCTOPASS%'></p>"; "<p><label>" + printerClient->getPrinterType() + " Password </label><input class='w3-input w3-border w3-margin-bottom' type='password' name='octoPass' value='%PRINTERPASS%'></p>";
form.replace("%OCTOKEY%", this->globalDataController->getPrinterApiKey()); form.replace("%PRINTERAPIKEY%", this->globalDataController->getPrinterApiKey());
form.replace("%OCTOHOST%", this->globalDataController->getPrinterHostName()); form.replace("%PRINTERHOST%", this->globalDataController->getPrinterHostName());
form.replace("%OCTOADDRESS%", this->globalDataController->getPrinterServer()); form.replace("%PRINTERADDRESS%", this->globalDataController->getPrinterServer());
form.replace("%OCTOPORT%", String(this->globalDataController->getPrinterPort())); form.replace("%PRINTERPORT%", String(this->globalDataController->getPrinterPort()));
form.replace("%OCTOUSER%", this->globalDataController->getPrinterAuthUser()); form.replace("%PRINTERUSER%", this->globalDataController->getPrinterAuthUser());
form.replace("%OCTOPASS%", this->globalDataController->getPrinterAuthPass()); form.replace("%PRINTERPASS%", this->globalDataController->getPrinterAuthPass());
this->server->sendContent(form); this->server->sendContent(form);
form = FPSTR(CLOCK_FORM); form = FPSTR(CLOCK_FORM);