diff --git a/src/Global/GlobalDataController.cpp b/src/Global/GlobalDataController.cpp index 90993f8..97bf49b 100644 --- a/src/Global/GlobalDataController.cpp +++ b/src/Global/GlobalDataController.cpp @@ -395,6 +395,37 @@ PrinterDataStruct *GlobalDataController::addPrinterSetting() { return retStruct; } +/** + * @brief Creates an new entry in printer setting table + * @return PrinterDataStruct* The new struct for the entry + */ +bool GlobalDataController::removePrinterSettingByIdx(int idx) { + if (this->printersCnt <= idx) { + return false; + } + int tSize = this->printersCnt - 1; + int mallocSize = this->printersCnt - 1; + if (mallocSize <= 0) { + mallocSize = 1; + } + PrinterDataStruct *newStruct = (PrinterDataStruct *)malloc(mallocSize * sizeof(PrinterDataStruct)); + memset(newStruct, 0, mallocSize * sizeof(PrinterDataStruct)); + if (tSize > 0) { + int eCnt = 0; + for(int i=0; iprintersCnt; i++) { + if (i == idx) { + continue; + } + memcpy(&newStruct[eCnt], &this->printers[i], sizeof(PrinterDataStruct)); + eCnt++; + } + this->printersCnt = eCnt; + free(this->printers); + this->printers = newStruct; + } + return true; +} + /** * @brief Return a printer state as readable text * @param printerHandle Handle to printer data diff --git a/src/Global/GlobalDataController.h b/src/Global/GlobalDataController.h index 18dd142..0933e67 100644 --- a/src/Global/GlobalDataController.h +++ b/src/Global/GlobalDataController.h @@ -15,10 +15,12 @@ #include "EspController.h" static const char ERROR_MESSAGES_ERR1[] PROGMEM = "[ERR1] Printer for update not found!"; +static const char ERROR_MESSAGES_ERR2[] PROGMEM = "[ERR1] Printer for deletion not found!"; static const char OK_MESSAGES_SAVE1[] PROGMEM = "[OK] Printer successfully saved"; static const char OK_MESSAGES_SAVE2[] PROGMEM = "[OK] Weather api data successfully saved"; static const char OK_MESSAGES_SAVE3[] PROGMEM = "[OK] Station data successfully saved"; +static const char OK_MESSAGES_DELETEPRINTER[] PROGMEM = "[OK] Printer successfully removed"; /** * @brief Handles all needed data for all instances @@ -67,6 +69,7 @@ public: int getRegisteredPrinterClientsNum(); PrinterDataStruct *getPrinterSettings(); PrinterDataStruct *addPrinterSetting(); + bool removePrinterSettingByIdx(int idx); int getNumPrinters(); String getPrinterStateAsText(PrinterDataStruct *printerHandle); String getPrinterClientType(PrinterDataStruct *printerHandle); diff --git a/src/Network/WebServer.cpp b/src/Network/WebServer.cpp index 426d358..2f9bb52 100644 --- a/src/Network/WebServer.cpp +++ b/src/Network/WebServer.cpp @@ -21,7 +21,7 @@ void WebServer::setup() { this->server->on("/configurestation/update", []() { obj->handleUpdateStation(); }); this->server->on("/configureprinter/show", []() { obj->handleConfigurePrinter(); }); this->server->on("/configureprinter/edit", []() { obj->handleUpdatePrinter(); }); - this->server->on("/configureprinter/delete", []() { obj->handleConfigurePrinter(); }); + this->server->on("/configureprinter/delete", []() { obj->handleDeletePrinter(); }); this->server->on("/configureweather/show", []() { obj->handleConfigureWeather(); }); this->server->on("/configureweather/update", []() { obj->handleUpdateWeather(); }); this->server->on("/configuresensor/show", []() { obj->handleConfigureSensor(); }); @@ -452,26 +452,39 @@ void WebServer::handleUpdatePrinter() { this->redirectTarget("/configureprinter/show"); return; } + this->globalDataController->getSystemSettings()->lastError = ""; // Set data MemoryHelper::stringToChar(this->server->arg("e-tname"), targetPrinter->customName, 20); targetPrinter->apiType = this->server->arg("e-tapi").toInt(); - MemoryHelper::stringToChar("", targetPrinter->apiKey, 60); + MemoryHelper::stringToChar(this->server->arg("e-tapikey"), targetPrinter->apiKey, 60); MemoryHelper::stringToChar(this->server->arg("e-taddr"), targetPrinter->remoteAddress, 60); targetPrinter->remotePort = this->server->arg("e-tport").toInt(); targetPrinter->hasPsuControl = this->server->hasArg("e-tpsu"); targetPrinter->basicAuthNeeded = this->server->hasArg("e-tapipw"); MemoryHelper::stringToChar(this->server->arg("e-tapiuser"), targetPrinter->basicAuthUsername, 30); MemoryHelper::stringToChar(this->server->arg("e-tapipass"), targetPrinter->basicAuthPassword, 60); + this->globalDataController->getSystemSettings()->lastOk = FPSTR(OK_MESSAGES_SAVE1); - - //http://192.168.0.239/configureprinter/show?id=0&e-tname=asdasd&e-tapi=Klipper&e-taddr=123.213.123.121&e-tport=80&e-tapipw=on&e-tapiuser=admin&e-tapipass=admin - - this->globalDataController->writeSettings(); this->redirectTarget("/configureprinter/show"); } +/** + * @brief Delete single configuration for Printer + */ +void WebServer::handleDeletePrinter() { + int targetPrinterId = this->server->arg("id").toInt() - 1; + if (this->globalDataController->removePrinterSettingByIdx(targetPrinterId)) { + this->globalDataController->getSystemSettings()->lastOk = FPSTR(OK_MESSAGES_DELETEPRINTER); + this->globalDataController->getSystemSettings()->lastError = ""; + this->globalDataController->writeSettings(); + } else { + this->globalDataController->getSystemSettings()->lastError = FPSTR(ERROR_MESSAGES_ERR2); + } + this->redirectTarget("/configureprinter/show"); +} + /** * @brief Send station configuration page to client */ diff --git a/src/Network/WebServer.h b/src/Network/WebServer.h index 89d23c3..bdf4625 100644 --- a/src/Network/WebServer.h +++ b/src/Network/WebServer.h @@ -32,6 +32,7 @@ public: void handleConfigurePrinter(); void handleUpdatePrinter(); + void handleDeletePrinter(); void handleConfigureStation(); void handleUpdateStation(); void handleConfigureWeather(); diff --git a/src/Network/WebserverMemoryVariables.cpp b/src/Network/WebserverMemoryVariables.cpp index 1e18996..368f7a0 100644 --- a/src/Network/WebserverMemoryVariables.cpp +++ b/src/Network/WebserverMemoryVariables.cpp @@ -79,8 +79,6 @@ void WebserverMemoryVariables::sendHeader( * @param globalDataController Access to global data */ void WebserverMemoryVariables::sendFooter(ESP8266WebServer *server, GlobalDataController *globalDataController) { - - WebserverMemoryVariables::sendModalDanger( server, "resetSettingsModal", @@ -145,6 +143,7 @@ void WebserverMemoryVariables::sendWeatherConfigForm(ESP8266WebServer *server, G FPSTR(WEATHER_FORM3_ID), FPSTR(WEATHER_FORM3_LABEL), globalDataController->getWeatherSettings()->apiKey, + "", 60, "", false, @@ -156,6 +155,7 @@ void WebserverMemoryVariables::sendWeatherConfigForm(ESP8266WebServer *server, G FPSTR(WEATHER_FORM4_ID), globalDataController->getWeatherClient()->getCity(0) + FPSTR(WEATHER_FORM4_LABEL), String(globalDataController->getWeatherSettings()->cityId), + "", 120, "onkeypress='return isNumberKey(event)'", false, @@ -230,6 +230,7 @@ void WebserverMemoryVariables::sendStationConfigForm(ESP8266WebServer *server, G FPSTR(STATION_CONFIG_FORM6_ID), FPSTR(STATION_CONFIG_FORM6_LABEL), String(globalDataController->getClockSettings()->utcOffset), + "", 120, "onkeypress='return isNumberKey(event)'", false, @@ -251,6 +252,7 @@ void WebserverMemoryVariables::sendStationConfigForm(ESP8266WebServer *server, G FPSTR(STATION_CONFIG_FORM8_ID), FPSTR(STATION_CONFIG_FORM8_LABEL), globalDataController->getSystemSettings()->webserverUsername, + "", 20, "", false, @@ -263,6 +265,7 @@ void WebserverMemoryVariables::sendStationConfigForm(ESP8266WebServer *server, G FPSTR(STATION_CONFIG_FORM9_ID), FPSTR(STATION_CONFIG_FORM9_LABEL), globalDataController->getSystemSettings()->webserverPassword, + "", 120, "", true, @@ -278,11 +281,21 @@ void WebserverMemoryVariables::sendStationConfigForm(ESP8266WebServer *server, G * @param server Send out instancce * @param globalDataController Access to global data */ -void WebserverMemoryVariables::sendPrinterConfigForm(ESP8266WebServer *server, GlobalDataController *globalDataController) { - server->sendContent(FPSTR(CONFPRINTER_FORM_START)); - +void WebserverMemoryVariables::sendPrinterConfigForm(ESP8266WebServer *server, GlobalDataController *globalDataController) { int totalPrinters = globalDataController->getNumPrinters(); PrinterDataStruct *printerConfigs = globalDataController->getPrinterSettings(); + + // Show all errors if printers have one + for(int i=0; isendContent(errorBlock); + } + } + + // Show printers + server->sendContent(FPSTR(CONFPRINTER_FORM_START)); for(int i=0; isendContent(FPSTR(CONFPRINTER_FORM_END)); @@ -347,6 +372,7 @@ void WebserverMemoryVariables::sendPrinterConfigFormAEModal(ESP8266WebServer *se FPSTR(CONFPRINTER_FORM_ADDEDIT1_ID), FPSTR(CONFPRINTER_FORM_ADDEDIT1_LABEL), id > 0 ? String(forPrinter->customName) : "", + FPSTR(CONFPRINTER_FORM_ADDEDIT1_PH), 20, "", false, @@ -358,16 +384,18 @@ void WebserverMemoryVariables::sendPrinterConfigFormAEModal(ESP8266WebServer *se FPSTR(CONFPRINTER_FORM_ADDEDIT2_ID), FPSTR(CONFPRINTER_FORM_ADDEDIT2_LABEL), "", - "", + "onchange='apiTypeSelect(\"" + String(FPSTR(CONFPRINTER_FORM_ADDEDIT2_ID)) + "\", \"apacapi-" + String(id) + "\")'", optionData, false, String(id) ); + WebserverMemoryVariables::rowExtraClass = "data-sh='apacapi-" + String(id) + "'"; WebserverMemoryVariables::sendFormInput( server, FPSTR(CONFPRINTER_FORM_ADDEDIT3_ID), FPSTR(CONFPRINTER_FORM_ADDEDIT3_LABEL), id > 0 ? String(forPrinter->apiKey) : "", + FPSTR(CONFPRINTER_FORM_ADDEDIT3_PH), 60, "", false, @@ -379,6 +407,7 @@ void WebserverMemoryVariables::sendPrinterConfigFormAEModal(ESP8266WebServer *se FPSTR(CONFPRINTER_FORM_ADDEDIT4_ID), FPSTR(CONFPRINTER_FORM_ADDEDIT4_LABEL), id > 0 ? String(forPrinter->remoteAddress) : "", + FPSTR(CONFPRINTER_FORM_ADDEDIT4_PH), 60, "", false, @@ -390,6 +419,7 @@ void WebserverMemoryVariables::sendPrinterConfigFormAEModal(ESP8266WebServer *se FPSTR(CONFPRINTER_FORM_ADDEDIT5_ID), FPSTR(CONFPRINTER_FORM_ADDEDIT5_LABEL), id > 0 ? String(forPrinter->remotePort) : "80", + "", 5, "onkeypress='return isNumberKey(event)'", false, @@ -411,6 +441,7 @@ void WebserverMemoryVariables::sendPrinterConfigFormAEModal(ESP8266WebServer *se FPSTR(STATION_CONFIG_FORM7_ID), FPSTR(STATION_CONFIG_FORM7_LABEL), id > 0 ? String(forPrinter->basicAuthUsername) : "", + FPSTR(CONFPRINTER_FORM_ADDEDIT7_PH), 30, "", false, @@ -423,6 +454,7 @@ void WebserverMemoryVariables::sendPrinterConfigFormAEModal(ESP8266WebServer *se FPSTR(STATION_CONFIG_FORM8_ID), FPSTR(STATION_CONFIG_FORM8_LABEL), id > 0 ? String(forPrinter->basicAuthPassword) : "", + FPSTR(CONFPRINTER_FORM_ADDEDIT8_PH), 120, "", true, @@ -548,6 +580,7 @@ void WebserverMemoryVariables::sendFormCheckboxEvent( * @param formId Form id/name * @param label Text for label head * @param value Value in field + * @param placeholder Placeholder text for input field * @param maxLen Max text len in input field * @param events Extra events for input field * @param isPassword True if password field @@ -559,6 +592,7 @@ void WebserverMemoryVariables::sendFormInput( String formId, String label, String value, + String placeholder, int maxLen, String events, bool isPassword, @@ -571,7 +605,7 @@ void WebserverMemoryVariables::sendFormInput( form.replace("%VALUE%", value); form.replace("%MAXLEN%", String(maxLen)); form.replace("%EVENTS%", events); - + form.replace("%PLACEHOLDER%", placeholder); if (isPassword) { form.replace("%FIELDTYPE%", "password"); } @@ -661,11 +695,11 @@ void WebserverMemoryVariables::sendForm( if (uniqueId.length() > 0) { formElement.replace("id='" + formId + "'", "id='" + formId + "-" + uniqueId + "'"); formElement.replace("for='" + formId + "'", "for='" + formId + "-" + uniqueId + "'"); + formElement.replace("\"" + formId + "\"", "\"" + formId + "-" + uniqueId + "\""); } if (inRow) { String rowStartData = FPSTR(FORM_ITEM_ROW_START); rowStartData.replace("%ROWEXTRACLASS%", WebserverMemoryVariables::rowExtraClass); - WebserverMemoryVariables::rowExtraClass = ""; server->sendContent(rowStartData); formElement.replace("%ROWEXT%", FPSTR(FORM_ITEM_ROW_EXT)); formElement.replace("%DIVEXTRACLASS%", ""); @@ -673,6 +707,7 @@ void WebserverMemoryVariables::sendForm( formElement.replace("%ROWEXT%", ""); formElement.replace("%DIVEXTRACLASS%", WebserverMemoryVariables::rowExtraClass); } + WebserverMemoryVariables::rowExtraClass = ""; server->sendContent(formElement); if (inRow) { server->sendContent(FPSTR(FORM_ITEM_ROW_END)); diff --git a/src/Network/WebserverMemoryVariables.h b/src/Network/WebserverMemoryVariables.h index 4ae666a..a4f0486 100644 --- a/src/Network/WebserverMemoryVariables.h +++ b/src/Network/WebserverMemoryVariables.h @@ -29,7 +29,7 @@ static const char FORM_ITEM_CHECKBOX_OFF[] PROGMEM = " deactivated"; static const char FORM_ITEM_INPUT[] PROGMEM = "
" "" - "" + "" "
"; static const char FORM_ITEM_SELECT_START[] PROGMEM = "
" @@ -103,7 +103,7 @@ static const char HEADER_BLOCK2[] PROGMEM = "" "" "
" ""; @@ -148,9 +148,6 @@ static const char HEADER_BLOCK_ERROR[] PROGMEM = "
%ERRORMSG%

" "
" "
" - "" ""; static const char HEADER_BLOCK_OK[] PROGMEM = "" "" "" "" - "" + "" "" ""; @@ -183,10 +180,13 @@ static const char FOOTER_BLOCK[] PROGMEM = "


" static const char GLOBAL_TEXT_WARNING[] PROGMEM = "WARNING"; static const char GLOBAL_TEXT_ABORT[] PROGMEM = "Abort"; static const char GLOBAL_TEXT_RESET[] PROGMEM = "Reset"; +static const char GLOBAL_TEXT_DELETE[] PROGMEM = "Delete"; static const char GLOBAL_TEXT_TRESET[] PROGMEM = "Reset settings"; static const char GLOBAL_TEXT_CRESET[] PROGMEM = "Do you want to reset to default settings?"; static const char GLOBAL_TEXT_TFWIFI[] PROGMEM = "Reset wifi"; static const char GLOBAL_TEXT_CFWIFI[] PROGMEM = "Do you want to reset wifi to default settings?"; +static const char GLOBAL_TEXT_TDPRINTER[] PROGMEM = "Delete printer"; +static const char GLOBAL_TEXT_CDPRINTER[] PROGMEM = "Do you want to delete the printer configuration "%PRINTERNAME%"?"; /** * Controls for update firmware/filesystem @@ -377,7 +377,7 @@ static const char CONFPRINTER_FORM_ROW[] PROGMEM = "" "" "" "
  • " - " - - - + .bx--table-column-menu { + width: 3.25rem + } + .menitem { + padding: 6px 1rem; + font-size: .875rem; + font-weight: 600; + line-height: 1.29; + letter-spacing: .16px; + display: flex; + justify-content: space-between; + text-decoration: none; + color: #c6c6c6 + } + + + + + + - -