Qrome - updated code format and spelling

pull/99/head
Chrome Legion 2019-04-11 20:34:15 -07:00
parent ba1cc15b14
commit 2996572d86
1 changed files with 341 additions and 339 deletions

View File

@ -1,339 +1,341 @@
/** The MIT License (MIT) /** The MIT License (MIT)
Copyright (c) 2018 David Payne Copyright (c) 2018 David Payne
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
// Additional Contributions: // Additional Contributions:
/* 15 Jan 2019 : Owen Carter : Add psucontrol query via POST api call */ /* 15 Jan 2019 : Owen Carter : Add psucontrol query via POST api call */
/* 07 April 2019 : Jon Smith : Redesigned this class for Repetier Server */ /* 07 April 2019 : Jon Smith : Redesigned this class for Repetier Server */
#include "RepetierClient.h" #include "RepetierClient.h"
RepetierClient::RepetierClient(String ApiKey, String server, int port, String user, String pass, boolean psu) { RepetierClient::RepetierClient(String ApiKey, String server, int port, String user, String pass, boolean psu) {
updatePrintClient(ApiKey, server, port, user, pass, psu); updatePrintClient(ApiKey, server, port, user, pass, psu);
} }
void RepetierClient::updatePrintClient(String ApiKey, String server, int port, String user, String pass, boolean psu) { void RepetierClient::updatePrintClient(String ApiKey, String server, int port, String user, String pass, boolean psu) {
server.toCharArray(myServer, 100); server.toCharArray(myServer, 100);
myApiKey = ApiKey; myApiKey = ApiKey;
myPort = port; myPort = port;
encodedAuth = ""; encodedAuth = "";
if (user != "") { if (user != "") {
String userpass = user + ":" + pass; String userpass = user + ":" + pass;
base64 b64; base64 b64;
encodedAuth = b64.encode(userpass, true); encodedAuth = b64.encode(userpass, true);
} }
pollPsu = psu; pollPsu = psu;
} }
boolean RepetierClient::validate() { boolean RepetierClient::validate() {
boolean rtnValue = false; boolean rtnValue = false;
printerData.error = ""; printerData.error = "";
if (String(myServer) == "") { if (String(myServer) == "") {
printerData.error += "Server address is required; "; printerData.error += "Server address is required; ";
} }
if (myApiKey == "") { if (myApiKey == "") {
printerData.error += "ApiKey is required; "; printerData.error += "ApiKey is required; ";
} }
if (printerData.error == "") { if (printerData.error == "") {
rtnValue = true; rtnValue = true;
} }
return rtnValue; return rtnValue;
} }
WiFiClient RepetierClient::getSubmitRequest(String apiGetData) { WiFiClient RepetierClient::getSubmitRequest(String apiGetData) {
WiFiClient printClient; WiFiClient printClient;
printClient.setTimeout(5000); printClient.setTimeout(5000);
Serial.println("Getting Repetier Data via GET"); Serial.println("Getting Repetier Data via GET");
Serial.println(apiGetData); Serial.println(apiGetData);
result = ""; result = "";
if (printClient.connect(myServer, myPort)) { //starts client connection, checks for connection if (printClient.connect(myServer, myPort)) { //starts client connection, checks for connection
printClient.println(apiGetData); printClient.println(apiGetData);
printClient.println("Host: " + String(myServer) + ":" + String(myPort)); printClient.println("Host: " + String(myServer) + ":" + String(myPort));
printClient.println("X-Api-Key: " + myApiKey); 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("Connection: close"); printClient.println("Connection: close");
if (printClient.println() == 0) { if (printClient.println() == 0) {
Serial.println("Connection to " + String(myServer) + ":" + String(myPort) + " failed."); Serial.println("Connection to " + String(myServer) + ":" + String(myPort) + " failed.");
Serial.println(); Serial.println();
resetPrintData(); resetPrintData();
printerData.error = "Connection to " + String(myServer) + ":" + String(myPort) + " failed."; printerData.error = "Connection to " + String(myServer) + ":" + String(myPort) + " failed.";
return printClient; return printClient;
} }
} }
else { else {
Serial.println("Connection to Repeteir failed: " + String(myServer) + ":" + String(myPort)); //error message if no client connect Serial.println("Connection to Repetier failed: " + String(myServer) + ":" + String(myPort)); //error message if no client connect
Serial.println(); Serial.println();
resetPrintData(); resetPrintData();
printerData.error = "Connection to Repetier failed: " + String(myServer) + ":" + String(myPort); printerData.error = "Connection to Repetier failed: " + String(myServer) + ":" + String(myPort);
return printClient; return printClient;
} }
// Check HTTP status // Check HTTP status
char status[32] = {0}; char status[32] = {0};
printClient.readBytesUntil('\r', status, sizeof(status)); printClient.readBytesUntil('\r', status, sizeof(status));
if (strcmp(status, "Host: 200 OK") != 0) { if (strcmp(status, "Host: 200 OK") != 0) {
Serial.print(F("Unexpected response: ")); Serial.print(F("Unexpected response: "));
Serial.println(status); Serial.println(status);
printerData.state = ""; printerData.state = "";
printerData.error = "Response: " + String(status); printerData.error = "Response: " + String(status);
return printClient; return printClient;
} }
// Skip HTTP headers // Skip HTTP headers
char endOfHeaders[] = "\r\n\r\n"; char endOfHeaders[] = "\r\n\r\n";
if (!printClient.find(endOfHeaders)) { if (!printClient.find(endOfHeaders)) {
Serial.println(F("Invalid response")); Serial.println(F("Invalid response"));
printerData.error = "Invalid response from " + String(myServer) + ":" + String(myPort); printerData.error = "Invalid response from " + String(myServer) + ":" + String(myPort);
printerData.state = ""; printerData.state = "";
} }
return printClient; return printClient;
} }
void RepetierClient::getPrinterJobResults() { void RepetierClient::getPrinterJobResults() {
if (!validate()) { if (!validate()) {
return; return;
} }
//**** get the Printer Job status //**** get the Printer Job status
String apiGetData = "GET /printer/api/?a=listPrinter"; String apiGetData = "GET /printer/api/?a=listPrinter";
WiFiClient printClient = getSubmitRequest(apiGetData); WiFiClient printClient = getSubmitRequest(apiGetData);
if (printerData.error != "") { if (printerData.error != "") {
return; return;
} }
const size_t bufferSize = JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + 2*JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(6) + 710; const size_t bufferSize = JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + 2*JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(6) + 710;
DynamicJsonBuffer jsonBuffer(bufferSize); DynamicJsonBuffer jsonBuffer(bufferSize);
// Parse JSON object // Parse JSON object
JsonArray& root = jsonBuffer.parseArray(printClient); JsonArray& root = jsonBuffer.parseArray(printClient);
if (!root.success()) { if (!root.success()) {
Serial.println("Repetier Data Parsing failed: " + String(myServer) + ":" + String(myPort)); Serial.println("Repetier Data Parsing failed: " + String(myServer) + ":" + String(myPort));
printerData.error = "Repetier Data Parsing failed: " + String(myServer) + ":" + String(myPort); printerData.error = "Repetier Data Parsing failed: " + String(myServer) + ":" + String(myPort);
printerData.state = ""; printerData.state = "";
return; return;
} }
///Selecting First printer ///Selecting First printer
JsonObject& pr = root[0]; JsonObject& pr = root[0];
//printerData.averagePrintTime = (const char*)pr[""]; //printerData.averagePrintTime = (const char*)pr[""];
printerData.estimatedPrintTime = (const char*)pr["printTime"]; printerData.estimatedPrintTime = (const char*)pr["printTime"];
printerData.fileName = (const char*) pr["job"]; printerData.fileName = (const char*) pr["job"];
printerData.fileSize = (const char*) pr["totalLines"]; printerData.fileSize = (const char*) pr["totalLines"];
//printerData.filamentLength = (const char*) pr[""]; //printerData.filamentLength = (const char*) pr[""];
printerData.state = (const char*) pr["online"]; printerData.state = (const char*) pr["online"];
//printerData.lastPrintTime = (const char*) pr[""]; //printerData.lastPrintTime = (const char*) pr[""];
printerData.progressCompletion = (const char*) pr["done"]; printerData.progressCompletion = (const char*) pr["done"];
printerData.progressFilepos = (const char*) pr["linesSend"]; printerData.progressFilepos = (const char*) pr["linesSend"];
printerData.progressPrintTime = (const char*) pr["printedTimeComp"]; printerData.progressPrintTime = (const char*) pr["printedTimeComp"];
//Figure out Time Left //Figure out Time Left
long timeTot=0; long timeTot=0;
long timeElap=0; long timeElap=0;
long timeLeft=0; long timeLeft=0;
if (printerData.estimatedPrintTime != "" ) if (printerData.estimatedPrintTime != "" ) {
{timeTot = atol(pr["printTime"]);} timeTot = atol(pr["printTime"]);
if (printerData.progressPrintTime != "") }
{timeElap= atol(pr["printedTimeComp"]);} if (printerData.progressPrintTime != "") {
timeLeft = timeTot-timeElap; timeElap= atol(pr["printedTimeComp"]);
printerData.progressPrintTimeLeft =String(timeLeft); }
timeLeft = timeTot-timeElap;
printerData.progressPrintTimeLeft = String(timeLeft);
String printing = (const char*) pr["job"]; String printing = (const char*) pr["job"];
if (printing != "none") { if (printing != "none") {
printerData.isPrinting = true; printerData.isPrinting = true;
}else {printerData.isPrinting=false;} } else {
printerData.isPrinting=false;
Serial.println("PT:"+printerData.progressPrintTime); }
Serial.println("PTC:"+ printerData.estimatedPrintTime);
Serial.println("ST:"+ printerData.lastPrintTime); Serial.println("PT: " + printerData.progressPrintTime);
Serial.println("TimeLeft: "+printerData.progressPrintTimeLeft); Serial.println("PTC: " + printerData.estimatedPrintTime);
Serial.println("ST: " + printerData.lastPrintTime);
if (printerData.isPrinting) Serial.println("TimeLeft: " + printerData.progressPrintTimeLeft);
{ Serial.println("I think I am printing");}
if (printerData.isPrinting) {
if (isOperational()) { Serial.println("I think I am printing");
Serial.println("Status: " + printerData.state); }
} else {
Serial.println("Printer Not Operational"); if (isOperational()) {
} Serial.println("Status: " + printerData.state);
} else {
//**** get the Printer Temps and Stat Serial.println("Printer Not Operational");
apiGetData = "GET /printer/api/?a=stateList"; }
printClient = getSubmitRequest(apiGetData);
if (printerData.error != "") { //**** get the Printer Temps and Stat
return; apiGetData = "GET /printer/api/?a=stateList";
} printClient = getSubmitRequest(apiGetData);
const size_t bufferSize2 = 3*JSON_OBJECT_SIZE(2) + 2*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(9) + 300; if (printerData.error != "") {
DynamicJsonBuffer jsonBuffer2(bufferSize2); return;
}
//Parse JSON object const size_t bufferSize2 = 3*JSON_OBJECT_SIZE(2) + 2*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(9) + 300;
JsonObject& root2 = jsonBuffer2.parseObject(printClient); DynamicJsonBuffer jsonBuffer2(bufferSize2);
//Select printer //Parse JSON object
String slug=(const char*) pr["slug"]; JsonObject& root2 = jsonBuffer2.parseObject(printClient);
JsonObject& pr2 = root2[slug];
//Select printer
if (!root2.success()) { String slug=(const char*) pr["slug"];
printerData.isPrinting = false; JsonObject& pr2 = root2[slug];
printerData.toolTemp = "";
printerData.toolTargetTemp = ""; if (!root2.success()) {
printerData.bedTemp = ""; printerData.isPrinting = false;
printerData.bedTargetTemp = (const char*) pr2["heatBeds"][0]["tempSet"]; printerData.toolTemp = "";
return; printerData.toolTargetTemp = "";
} printerData.bedTemp = "";
printerData.bedTargetTemp = (const char*) pr2["heatBeds"][0]["tempSet"];
return;
printerData.toolTemp = (const char*) pr2["extruder"][0]["tempRead"]; }
printerData.toolTargetTemp = (const char*) pr2["extruder"][0]["tempSet"];
printerData.bedTemp = (const char*) pr2["heatedBeds"][0]["tempRead"]; printerData.toolTemp = (const char*) pr2["extruder"][0]["tempRead"];
printerData.bedTargetTemp = (const char*) pr2["heatedBeds"][0]["tempSet"]; printerData.toolTargetTemp = (const char*) pr2["extruder"][0]["tempSet"];
printerData.bedTemp = (const char*) pr2["heatedBeds"][0]["tempRead"];
if (printerData.isPrinting) { printerData.bedTargetTemp = (const char*) pr2["heatedBeds"][0]["tempSet"];
Serial.println("Status: " + printerData.state + " " + printerData.fileName + "(" + printerData.progressCompletion + "%)");
} if (printerData.isPrinting) {
} Serial.println("Status: " + printerData.state + " " + printerData.fileName + "(" + printerData.progressCompletion + "%)");
}
void RepetierClient::getPrinterPsuState() { }
//**** get the PSU state (if enabled and printer operational)
//Not implemented in Repetier Server AFAIK void RepetierClient::getPrinterPsuState() {
} //**** get the PSU state (if enabled and printer operational)
//Not implemented in Repetier Server AFAIK
// Reset all PrinterData }
void RepetierClient::resetPrintData() {
printerData.averagePrintTime = ""; // Reset all PrinterData
printerData.estimatedPrintTime = ""; void RepetierClient::resetPrintData() {
printerData.fileName = ""; printerData.averagePrintTime = "";
printerData.fileSize = ""; printerData.estimatedPrintTime = "";
printerData.lastPrintTime = ""; printerData.fileName = "";
printerData.progressCompletion = ""; printerData.fileSize = "";
printerData.progressFilepos = ""; printerData.lastPrintTime = "";
printerData.progressPrintTime = ""; printerData.progressCompletion = "";
printerData.progressPrintTimeLeft = ""; printerData.progressFilepos = "";
printerData.state = ""; printerData.progressPrintTime = "";
printerData.toolTemp = ""; printerData.progressPrintTimeLeft = "";
printerData.toolTargetTemp = ""; printerData.state = "";
printerData.filamentLength = ""; printerData.toolTemp = "";
printerData.bedTemp = ""; printerData.toolTargetTemp = "";
printerData.bedTargetTemp = ""; printerData.filamentLength = "";
printerData.isPrinting = false; printerData.bedTemp = "";
printerData.isPSUoff = false; printerData.bedTargetTemp = "";
printerData.error = ""; printerData.isPrinting = false;
} printerData.isPSUoff = false;
printerData.error = "";
String RepetierClient::getAveragePrintTime(){ }
return printerData.averagePrintTime;
} String RepetierClient::getAveragePrintTime(){
return printerData.averagePrintTime;
String RepetierClient::getEstimatedPrintTime() { }
return printerData.estimatedPrintTime;
} String RepetierClient::getEstimatedPrintTime() {
return printerData.estimatedPrintTime;
String RepetierClient::getFileName() { }
return printerData.fileName;
} String RepetierClient::getFileName() {
return printerData.fileName;
String RepetierClient::getFileSize() { }
return printerData.fileSize;
} String RepetierClient::getFileSize() {
return printerData.fileSize;
String RepetierClient::getLastPrintTime(){ }
return printerData.lastPrintTime;
} String RepetierClient::getLastPrintTime(){
return printerData.lastPrintTime;
String RepetierClient::getProgressCompletion() { }
return String(printerData.progressCompletion.toInt());
} String RepetierClient::getProgressCompletion() {
return String(printerData.progressCompletion.toInt());
String RepetierClient::getProgressFilepos() { }
return printerData.progressFilepos;
} String RepetierClient::getProgressFilepos() {
return printerData.progressFilepos;
String RepetierClient::getProgressPrintTime() { }
return printerData.progressPrintTime;
} String RepetierClient::getProgressPrintTime() {
return printerData.progressPrintTime;
String RepetierClient::getProgressPrintTimeLeft() { }
String rtnValue = printerData.progressPrintTimeLeft;
if (getProgressCompletion() == "100") { String RepetierClient::getProgressPrintTimeLeft() {
rtnValue = "0"; // Print is done so this should be 0 this is a fix for OctoPrint String rtnValue = printerData.progressPrintTimeLeft;
} if (getProgressCompletion() == "100") {
return rtnValue; rtnValue = "0"; // Print is done so this should be 0 this is a fix for OctoPrint
} }
return rtnValue;
String RepetierClient::getState() { }
return printerData.state;
} String RepetierClient::getState() {
return printerData.state;
boolean RepetierClient::isPrinting() { }
return printerData.isPrinting;
} boolean RepetierClient::isPrinting() {
return printerData.isPrinting;
boolean RepetierClient::isPSUoff() { }
return printerData.isPSUoff;
} boolean RepetierClient::isPSUoff() {
return printerData.isPSUoff;
boolean RepetierClient::isOperational() { }
boolean operational = false;
if (printerData.state == "Operational" || isPrinting()) { boolean RepetierClient::isOperational() {
operational = true; boolean operational = false;
} if (printerData.state == "Operational" || isPrinting()) {
return operational; operational = true;
} }
return operational;
String RepetierClient::getTempBedActual() { }
return printerData.bedTemp;
} String RepetierClient::getTempBedActual() {
return printerData.bedTemp;
String RepetierClient::getTempBedTarget() { }
return printerData.bedTargetTemp;
} String RepetierClient::getTempBedTarget() {
return printerData.bedTargetTemp;
String RepetierClient::getTempToolActual() { }
return printerData.toolTemp;
} String RepetierClient::getTempToolActual() {
return printerData.toolTemp;
String RepetierClient::getTempToolTarget() { }
return printerData.toolTargetTemp;
} String RepetierClient::getTempToolTarget() {
return printerData.toolTargetTemp;
String RepetierClient::getFilamentLength() { }
return printerData.filamentLength;
} String RepetierClient::getFilamentLength() {
return printerData.filamentLength;
String RepetierClient::getError() { }
return printerData.error;
} String RepetierClient::getError() {
return printerData.error;
String RepetierClient::getValueRounded(String value) { }
float f = value.toFloat();
int rounded = (int)(f+0.5f); String RepetierClient::getValueRounded(String value) {
return String(rounded); float f = value.toFloat();
} int rounded = (int)(f+0.5f);
return String(rounded);
}