From 75e37d1053fbf19bf62daae2765fba67d8c9a4f7 Mon Sep 17 00:00:00 2001 From: Robert Stein Date: Mon, 21 Dec 2020 19:35:40 +0100 Subject: [PATCH] Optiomize flash usage, continue work on printer config --- src/Clients/BasePrinterClient.h | 3 +- src/Clients/BasePrinterClientImpl.h | 1 + src/Clients/DuetClient.cpp | 22 +- src/Clients/DuetClient.h | 1 + src/Clients/KlipperClient.cpp | 25 +- src/Clients/KlipperClient.h | 1 + src/Clients/OctoPrintClient.h | 1 + src/Clients/PrinterDataStruct.h | 6 +- src/Clients/RepetierClient.h | 1 + src/Global/GlobalDataController.cpp | 18 + src/Global/GlobalDataController.h | 8 +- src/Global/SystemDataStruct.h | 1 + src/Network/WebServer.cpp | 8 +- src/Network/WebserverMemoryVariables.cpp | 539 +++++++++++++++--- src/Network/WebserverMemoryVariables.h | 283 ++++----- src/Sensors/BME280.cpp | 0 src/Sensors/BME280.h | 1 + src/Sensors/BME680.cpp | 0 src/Sensors/BME680.h | 1 + src/Sensors/HTU21D.cpp | 0 src/Sensors/HTU21D.h | 1 + src/Sensors/SensorClient.h | 2 + .../html-ui}/configureprinter.html | 9 +- .../html-ui}/configurestation.html | 0 .../html-ui}/configureweather.html | 0 {html-ui => templates/html-ui}/index.html | 0 {html-ui => templates/html-ui}/upload.html | 0 {nextion-ui => templates/nextion-ui}/Bahn.zi | Bin .../nextion-ui}/SegoeUi16.zi | Bin .../nextion-ui}/SegoeUi16B.zi | Bin .../nextion-ui}/SegoeUi20.zi | Bin .../nextion-ui}/SegoeUi20B.zi | Bin .../nextion-ui}/SegoeUi24.zi | Bin .../nextion-ui}/SegoeUi24B.zi | Bin .../nextion-ui}/SegoeUi64BNum.zi | Bin .../nextion-ui}/SegoeUiBlack96Temp.zi | Bin .../nextion-ui}/hmi-data.HMI | Bin .../nextion-ui}/icons/211.png | Bin .../nextion-ui}/icons/212.png | Bin .../nextion-ui}/icons/230.png | Bin .../nextion-ui}/icons/231.png | Bin .../nextion-ui}/icons/500d.png | Bin .../nextion-ui}/icons/500n.png | Bin .../nextion-ui}/icons/501.png | Bin .../nextion-ui}/icons/502.png | Bin .../nextion-ui}/icons/600d.png | Bin .../nextion-ui}/icons/600n.png | Bin .../nextion-ui}/icons/601.png | Bin .../nextion-ui}/icons/602.png | Bin .../nextion-ui}/icons/611.png | Bin .../nextion-ui}/icons/721d.png | Bin .../nextion-ui}/icons/721n.png | Bin .../nextion-ui}/icons/741.png | Bin .../nextion-ui}/icons/800d.png | Bin .../nextion-ui}/icons/800n.png | Bin .../nextion-ui}/icons/801d.png | Bin .../nextion-ui}/icons/801n.png | Bin .../nextion-ui}/icons/802d.png | Bin .../nextion-ui}/icons/802n.png | Bin .../nextion-ui}/icons/803.png | Bin .../nextion-ui}/icons/804.png | Bin .../nextion-ui}/icons/air-quality.png | Bin .../nextion-ui}/icons/bed.png | Bin .../nextion-ui}/icons/extr.png | Bin .../nextion-ui}/icons/humidity.png | Bin .../nextion-ui}/icons/pressure.png | Bin .../nextion-ui}/icons/sand-clock.png | Bin .../nextion-ui}/icons/temperature.png | Bin .../nextion-ui}/img/BG_Blue_1.png | Bin .../img/ShowWeatherAndClock - AirQuality.png | Bin .../img/ShowWeatherAndClock - Blank.png | Bin .../img/ShowWeatherAndClock - Humidity.png | Bin .../img/ShowWeatherAndClock - Pressure.png | Bin .../img/ShowWeatherAndClock - Temp.png | Bin .../nextion-ui}/img/Welcome.png | Bin .../nextion-ui}/img/weather_cloud.png | Bin .../nextion-ui}/ui-template-design.xd | Bin 77 files changed, 672 insertions(+), 260 deletions(-) create mode 100644 src/Sensors/BME280.cpp create mode 100644 src/Sensors/BME280.h create mode 100644 src/Sensors/BME680.cpp create mode 100644 src/Sensors/BME680.h create mode 100644 src/Sensors/HTU21D.cpp create mode 100644 src/Sensors/HTU21D.h create mode 100644 src/Sensors/SensorClient.h rename {html-ui => templates/html-ui}/configureprinter.html (99%) rename {html-ui => templates/html-ui}/configurestation.html (100%) rename {html-ui => templates/html-ui}/configureweather.html (100%) rename {html-ui => templates/html-ui}/index.html (100%) rename {html-ui => templates/html-ui}/upload.html (100%) rename {nextion-ui => templates/nextion-ui}/Bahn.zi (100%) rename {nextion-ui => templates/nextion-ui}/SegoeUi16.zi (100%) rename {nextion-ui => templates/nextion-ui}/SegoeUi16B.zi (100%) rename {nextion-ui => templates/nextion-ui}/SegoeUi20.zi (100%) rename {nextion-ui => templates/nextion-ui}/SegoeUi20B.zi (100%) rename {nextion-ui => templates/nextion-ui}/SegoeUi24.zi (100%) rename {nextion-ui => templates/nextion-ui}/SegoeUi24B.zi (100%) rename {nextion-ui => templates/nextion-ui}/SegoeUi64BNum.zi (100%) rename {nextion-ui => templates/nextion-ui}/SegoeUiBlack96Temp.zi (100%) rename {nextion-ui => templates/nextion-ui}/hmi-data.HMI (100%) rename {nextion-ui => templates/nextion-ui}/icons/211.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/212.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/230.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/231.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/500d.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/500n.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/501.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/502.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/600d.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/600n.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/601.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/602.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/611.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/721d.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/721n.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/741.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/800d.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/800n.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/801d.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/801n.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/802d.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/802n.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/803.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/804.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/air-quality.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/bed.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/extr.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/humidity.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/pressure.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/sand-clock.png (100%) rename {nextion-ui => templates/nextion-ui}/icons/temperature.png (100%) rename {nextion-ui => templates/nextion-ui}/img/BG_Blue_1.png (100%) rename {nextion-ui => templates/nextion-ui}/img/ShowWeatherAndClock - AirQuality.png (100%) rename {nextion-ui => templates/nextion-ui}/img/ShowWeatherAndClock - Blank.png (100%) rename {nextion-ui => templates/nextion-ui}/img/ShowWeatherAndClock - Humidity.png (100%) rename {nextion-ui => templates/nextion-ui}/img/ShowWeatherAndClock - Pressure.png (100%) rename {nextion-ui => templates/nextion-ui}/img/ShowWeatherAndClock - Temp.png (100%) rename {nextion-ui => templates/nextion-ui}/img/Welcome.png (100%) rename {nextion-ui => templates/nextion-ui}/img/weather_cloud.png (100%) rename {nextion-ui => templates/nextion-ui}/ui-template-design.xd (100%) diff --git a/src/Clients/BasePrinterClient.h b/src/Clients/BasePrinterClient.h index 7a8add6..b2f7422 100644 --- a/src/Clients/BasePrinterClient.h +++ b/src/Clients/BasePrinterClient.h @@ -17,6 +17,7 @@ public: virtual void updatePrintClient(PrinterDataStruct *printerData) = 0; virtual String getClientType() = 0; virtual boolean isValidConfig(PrinterDataStruct *printerData) = 0; + virtual boolean clientNeedApiKey() = 0; /** * @brief Reset all dynamic variables for printer @@ -36,7 +37,7 @@ public: MemoryHelper::stringToChar("", printerData->fileName, 60); printerData->fileSize = 0; printerData->lastPrintTime = 0; - MemoryHelper::stringToChar("", printerData->progressCompletion, 60); + printerData->progressCompletion = 0; printerData->progressFilepos = 0; printerData->progressPrintTime = 0; printerData->progressPrintTimeLeft = 0; diff --git a/src/Clients/BasePrinterClientImpl.h b/src/Clients/BasePrinterClientImpl.h index a02243e..bc0f916 100644 --- a/src/Clients/BasePrinterClientImpl.h +++ b/src/Clients/BasePrinterClientImpl.h @@ -16,6 +16,7 @@ public: BasePrinterClientImpl(String clientType, GlobalDataController *globalDataController, DebugController *debugController, JsonRequestClient *jsonRequestClient); void getPrinterJobResults(PrinterDataStruct *printerData) {}; void getPrinterPsuState(PrinterDataStruct *printerData) {}; + boolean clientNeedApiKey() { return false; }; void updatePrintClient(PrinterDataStruct *printerData); String getClientType(); boolean isOperational(PrinterDataStruct *printerData); diff --git a/src/Clients/DuetClient.cpp b/src/Clients/DuetClient.cpp index bf293fb..9be3bdf 100644 --- a/src/Clients/DuetClient.cpp +++ b/src/Clients/DuetClient.cpp @@ -85,23 +85,21 @@ void DuetClient::getPrinterJobResults(PrinterDataStruct *printerData) { return; } - //printerData.filamentLength = (const char*)(*jsonDoc)["result"]["status"]["job"]["print_stats"]["filament_used"]; - //printerData.progressPrintTime = (const char*)(*jsonDoc)["printDuration"]; - //printerData.fileName = (const char*)(*jsonDoc)["result"]["status"]["print_stats"]["filename"]; - //printerData.progressCompletion = (int)(*jsonDoc)["fractionPrinted"]; + printerData->filamentLength = (float)(*jsonDoc)["result"]["status"]["job"]["print_stats"]["filament_used"]; + printerData->progressPrintTime = (float)(*jsonDoc)["printDuration"]; + MemoryHelper::stringToChar( + (const char*)(*jsonDoc)["result"]["status"]["print_stats"]["filename"], + printerData->fileName, + 60 + ); + printerData->progressCompletion = (int)(*jsonDoc)["fractionPrinted"]; printerData->toolTemp = (int)(*jsonDoc)["temps"]["current"][1]; printerData->toolTargetTemp = (int)(*jsonDoc)["temps"]["tools"]["active"][0][0]; printerData->bedTemp = (int)(*jsonDoc)["temps"]["bed"]["current"]; printerData->bedTargetTemp = (int)(*jsonDoc)["temps"]["bed"]["active"]; - float fileProgress = (float)(*jsonDoc)["fractionPrinted"]; - //printerData.progressFilepos = (const char*)(*jsonDoc)["filePosition"]; + printerData->progressFilepos = (int)(*jsonDoc)["filePosition"]; printerData->estimatedPrintTime = (float)(*jsonDoc)["file"]; - - /* - 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()); + printerData->progressPrintTimeLeft = (float)(*jsonDoc)["timesLeft"]["file"]; if (this->isOperational(printerData)) { this->debugController->printLn("Status: " diff --git a/src/Clients/DuetClient.h b/src/Clients/DuetClient.h index e855cd5..65a6459 100644 --- a/src/Clients/DuetClient.h +++ b/src/Clients/DuetClient.h @@ -14,6 +14,7 @@ public: DuetClient(GlobalDataController *globalDataController, DebugController *debugController, JsonRequestClient *jsonRequestClient); void getPrinterJobResults(PrinterDataStruct *printerData) override; void getPrinterPsuState(PrinterDataStruct *printerData) override; + boolean clientNeedApiKey() override { return false; }; private: static int translateState(String stateText); diff --git a/src/Clients/KlipperClient.cpp b/src/Clients/KlipperClient.cpp index a2a30aa..a91fe66 100644 --- a/src/Clients/KlipperClient.cpp +++ b/src/Clients/KlipperClient.cpp @@ -48,9 +48,13 @@ void KlipperClient::getPrinterJobResults(PrinterDataStruct *printerData) { } printerData->state = KlipperClient::translateState((const char*)(*jsonDoc)["result"]["status"]["print_stats"]["state"]); - //printerData.filamentLength = (const char*)(*jsonDoc)["result"]["status"]["job"]["print_stats"]["filament_used"]; - //printerData.progressPrintTime = (const char*)(*jsonDoc)["result"]["status"]["print_stats"]["print_duration"]; - //printerData.fileName = (const char*)(*jsonDoc)["result"]["status"]["print_stats"]["filename"]; + printerData->filamentLength = (float)(*jsonDoc)["result"]["status"]["job"]["print_stats"]["filament_used"]; + printerData->progressPrintTime = (float)(*jsonDoc)["result"]["status"]["print_stats"]["print_duration"]; + MemoryHelper::stringToChar( + (const char*)(*jsonDoc)["result"]["status"]["print_stats"]["filename"], + printerData->fileName, + 60 + ); if (this->isOperational(printerData)) { this->debugController->printLn("Status: " + this->globalDataController->getPrinterStateAsText(printerData)); @@ -87,21 +91,20 @@ void KlipperClient::getPrinterJobResults(PrinterDataStruct *printerData) { return; } - //printerData.progressCompletion = (int)(*jsonDoc)["result"]["status"]["display_status"]["progress"]; + float progressCompletion = (float)(*jsonDoc)["result"]["status"]["display_status"]["progress"]; printerData->toolTemp = (int)(*jsonDoc)["result"]["status"]["extruder"]["temperature"]; printerData->toolTargetTemp = (int)(*jsonDoc)["result"]["status"]["extruder"]["target"]; printerData->bedTemp = (int)(*jsonDoc)["result"]["status"]["heater_bed"]["temperature"]; printerData->bedTargetTemp = (int)(*jsonDoc)["result"]["status"]["heater_bed"]["target"]; float fileProgress = (float)(*jsonDoc)["result"]["status"]["virtual_sdcard"]["progress"]; - //printerData.progressFilepos = (const char*)(*jsonDoc)["result"]["status"]["virtual_sdcard"]["file_position"]; + printerData->progressFilepos = (int)(*jsonDoc)["result"]["status"]["virtual_sdcard"]["file_position"]; printerData->estimatedPrintTime = (float)(*jsonDoc)["result"]["status"]["toolhead"]["estimated_print_time"]; + printerData->progressPrintTime = (float)(*jsonDoc)["result"]["status"]["print_stats"]["print_duration"]; + + float totalPrintTime = printerData->progressPrintTime / fileProgress; + printerData->progressPrintTimeLeft = totalPrintTime - printerData->progressPrintTime; + printerData->progressCompletion = progressCompletion * 100; - /* - 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 (this->isOperational(printerData)) { this->debugController->printLn("Status: " + this->globalDataController->getPrinterStateAsText(printerData) + " " diff --git a/src/Clients/KlipperClient.h b/src/Clients/KlipperClient.h index 4654a6a..4fe8e48 100644 --- a/src/Clients/KlipperClient.h +++ b/src/Clients/KlipperClient.h @@ -14,6 +14,7 @@ public: KlipperClient(GlobalDataController *globalDataController, DebugController *debugController, JsonRequestClient *jsonRequestClient); void getPrinterJobResults(PrinterDataStruct *printerData) override; void getPrinterPsuState(PrinterDataStruct *printerData) override; + boolean clientNeedApiKey() override { return false; }; private: static int translateState(String stateText); diff --git a/src/Clients/OctoPrintClient.h b/src/Clients/OctoPrintClient.h index 64096e9..5755708 100644 --- a/src/Clients/OctoPrintClient.h +++ b/src/Clients/OctoPrintClient.h @@ -14,6 +14,7 @@ public: OctoPrintClient(GlobalDataController *globalDataController, DebugController *debugController, JsonRequestClient *jsonRequestClient); void getPrinterJobResults(PrinterDataStruct *printerData) override; void getPrinterPsuState(PrinterDataStruct *printerData) override; + boolean clientNeedApiKey() override { return true; }; private: static int translateState(String stateText); diff --git a/src/Clients/PrinterDataStruct.h b/src/Clients/PrinterDataStruct.h index 44e9f63..2b2b758 100644 --- a/src/Clients/PrinterDataStruct.h +++ b/src/Clients/PrinterDataStruct.h @@ -32,10 +32,10 @@ typedef struct { char fileName[60]; int fileSize; int lastPrintTime; - char progressCompletion[60]; + int progressCompletion; int progressFilepos; - int progressPrintTime; - int progressPrintTimeLeft; + float progressPrintTime; + float progressPrintTimeLeft; int state; float toolTemp; float toolTargetTemp; diff --git a/src/Clients/RepetierClient.h b/src/Clients/RepetierClient.h index b1349bd..3296fb8 100644 --- a/src/Clients/RepetierClient.h +++ b/src/Clients/RepetierClient.h @@ -14,6 +14,7 @@ public: RepetierClient(GlobalDataController *globalDataController, DebugController *debugController, JsonRequestClient *jsonRequestClient); void getPrinterJobResults(PrinterDataStruct *printerData) override; void getPrinterPsuState(PrinterDataStruct *printerData) override; + boolean clientNeedApiKey() override { return true; }; private: static int translateState(String stateText); diff --git a/src/Global/GlobalDataController.cpp b/src/Global/GlobalDataController.cpp index a3094a2..90993f8 100644 --- a/src/Global/GlobalDataController.cpp +++ b/src/Global/GlobalDataController.cpp @@ -438,6 +438,24 @@ void GlobalDataController::registerPrinterClient(int id, BasePrinterClient *base this->basePrinterClients[id] = basePrinterClient; } +/** + * @brief Get all registred printer clients + * + * @return BasePrinterClient** + */ +BasePrinterClient** GlobalDataController::getRegisteredPrinterClients() { + return this->basePrinterClients; +} + +/** + * @brief Get number of registred printer clients + * + * @return int + */ +int GlobalDataController::getRegisteredPrinterClientsNum() { + return this->basePrinterCount; +} + /** * @brief Get viewable type of client for printer * @param printerHandle Handle to printer data diff --git a/src/Global/GlobalDataController.h b/src/Global/GlobalDataController.h index 0a6f105..18dd142 100644 --- a/src/Global/GlobalDataController.h +++ b/src/Global/GlobalDataController.h @@ -14,7 +14,11 @@ #include "../../include/MemoryHelper.h" #include "EspController.h" -static const char ERROR_MESSAGES_ERR1[] PROGMEM = "[ERR1] Printer fo update not found!"; +static const char ERROR_MESSAGES_ERR1[] PROGMEM = "[ERR1] Printer for update 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"; /** * @brief Handles all needed data for all instances @@ -59,6 +63,8 @@ public: bool resetConfig(); void registerPrinterClient(int id, BasePrinterClient *basePrinterClient); + BasePrinterClient** getRegisteredPrinterClients(); + int getRegisteredPrinterClientsNum(); PrinterDataStruct *getPrinterSettings(); PrinterDataStruct *addPrinterSetting(); int getNumPrinters(); diff --git a/src/Global/SystemDataStruct.h b/src/Global/SystemDataStruct.h index 56bd0f3..752d1f0 100644 --- a/src/Global/SystemDataStruct.h +++ b/src/Global/SystemDataStruct.h @@ -13,4 +13,5 @@ typedef struct { int clockWeatherResyncMinutes; String version; String lastError; + String lastOk; } SystemDataStruct; diff --git a/src/Network/WebServer.cpp b/src/Network/WebServer.cpp index bed7166..426d358 100644 --- a/src/Network/WebServer.cpp +++ b/src/Network/WebServer.cpp @@ -463,19 +463,15 @@ void WebServer::handleUpdatePrinter() { 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->getSystemSettings()->lastError = FPSTR(ERROR_MESSAGES_ERR1); - this->globalDataController->writeSettings(); this->redirectTarget("/configureprinter/show"); } - - - /** * @brief Send station configuration page to client */ @@ -518,6 +514,7 @@ void WebServer::handleUpdateStation() { this->globalDataController->getDisplayClient()->handleUpdate(); this->globalDataController->getTimeClient()->resetLastEpoch(); + this->globalDataController->getSystemSettings()->lastOk = FPSTR(OK_MESSAGES_SAVE3); this->redirectHome(); } @@ -551,6 +548,7 @@ void WebServer::handleUpdateWeather() { this->globalDataController->getDisplayClient()->handleUpdate(); this->globalDataController->getTimeClient()->resetLastEpoch(); + this->globalDataController->getSystemSettings()->lastOk = FPSTR(OK_MESSAGES_SAVE2); this->redirectHome(); } diff --git a/src/Network/WebserverMemoryVariables.cpp b/src/Network/WebserverMemoryVariables.cpp index b1324f3..1e18996 100644 --- a/src/Network/WebserverMemoryVariables.cpp +++ b/src/Network/WebserverMemoryVariables.cpp @@ -1,5 +1,7 @@ #include "WebserverMemoryVariables.h" +String WebserverMemoryVariables::rowExtraClass = ""; + /** * @brief Send out header for webpage * @param server Send out instancce @@ -63,6 +65,12 @@ void WebserverMemoryVariables::sendHeader( errorBlock.replace("%ERRORMSG%", globalDataController->getSystemSettings()->lastError); server->sendContent(errorBlock); } + if (globalDataController->getSystemSettings()->lastOk.length() > 0) { + String okBlock = FPSTR(HEADER_BLOCK_OK); + okBlock.replace("%OKMSG%", globalDataController->getSystemSettings()->lastOk); + server->sendContent(okBlock); + globalDataController->getSystemSettings()->lastOk = ""; + } } /** @@ -71,6 +79,28 @@ void WebserverMemoryVariables::sendHeader( * @param globalDataController Access to global data */ void WebserverMemoryVariables::sendFooter(ESP8266WebServer *server, GlobalDataController *globalDataController) { + + + WebserverMemoryVariables::sendModalDanger( + server, + "resetSettingsModal", + FPSTR(GLOBAL_TEXT_WARNING), + FPSTR(GLOBAL_TEXT_TRESET), + FPSTR(GLOBAL_TEXT_CRESET), + FPSTR(GLOBAL_TEXT_ABORT), + FPSTR(GLOBAL_TEXT_RESET), + "onclick='openUrl(\"/systemreset\")'" + ); + WebserverMemoryVariables::sendModalDanger( + server, + "resetWifiModal", + FPSTR(GLOBAL_TEXT_WARNING), + FPSTR(GLOBAL_TEXT_TFWIFI), + FPSTR(GLOBAL_TEXT_CFWIFI), + FPSTR(GLOBAL_TEXT_ABORT), + FPSTR(GLOBAL_TEXT_RESET), + "onclick='openUrl(\"/forgetwifi\")'" + ); server->sendContent(String(FPSTR(FOOTER_BLOCK))); server->sendContent(""); server->client().stop(); @@ -97,34 +127,53 @@ void WebserverMemoryVariables::sendWeatherConfigForm(ESP8266WebServer *server, G server, FPSTR(WEATHER_FORM1_ID), globalDataController->getWeatherSettings()->show, - FPSTR(WEATHER_FORM1_LABEL) + FPSTR(WEATHER_FORM1_LABEL), + true, + "" ); WebserverMemoryVariables::sendFormCheckbox( server, FPSTR(WEATHER_FORM2_ID), globalDataController->getWeatherSettings()->isMetric, FPSTR(WEATHER_FORM2_LABEL_ON), - FPSTR(WEATHER_FORM2_LABEL_OFF) + FPSTR(WEATHER_FORM2_LABEL_OFF), + true, + "" ); - - String form = FPSTR(WEATHER_FORM3); - form.replace("%WEATHERKEY%", globalDataController->getWeatherSettings()->apiKey); - server->sendContent(form); - - form = FPSTR(WEATHER_FORM4); - form.replace("%CITY1%", String(globalDataController->getWeatherSettings()->cityId)); - form.replace("%CITYNAME1%", globalDataController->getWeatherClient()->getCity(0)); - server->sendContent(form); - - form = FPSTR(WEATHER_FORM_OPTIONS); - form.replace( - ">"+String(globalDataController->getWeatherSettings()->lang)+"<", - " selected>"+String(globalDataController->getWeatherSettings()->lang)+"<" + WebserverMemoryVariables::sendFormInput( + server, + FPSTR(WEATHER_FORM3_ID), + FPSTR(WEATHER_FORM3_LABEL), + globalDataController->getWeatherSettings()->apiKey, + 60, + "", + false, + true, + "" ); - server->sendContent(form); - - form = FPSTR(WEATHER_FORM5); - server->sendContent(form); + WebserverMemoryVariables::sendFormInput( + server, + FPSTR(WEATHER_FORM4_ID), + globalDataController->getWeatherClient()->getCity(0) + FPSTR(WEATHER_FORM4_LABEL), + String(globalDataController->getWeatherSettings()->cityId), + 120, + "onkeypress='return isNumberKey(event)'", + false, + true, + "" + ); + WebserverMemoryVariables::sendFormSelect( + server, + FPSTR(WEATHER_FORM5_ID), + FPSTR(WEATHER_FORM5_LABEL), + String(globalDataController->getWeatherSettings()->lang), + "", + FPSTR(WEATHER_FORM5_OPTIONS), + true, + "" + ); + WebserverMemoryVariables::sendFormSubmitButton(server, true); + server->sendContent(FPSTR(WEATHER_FORM_END)); } /** @@ -138,52 +187,90 @@ void WebserverMemoryVariables::sendStationConfigForm(ESP8266WebServer *server, G server, FPSTR(STATION_CONFIG_FORM1_ID), globalDataController->getClockSettings()->show, - FPSTR(STATION_CONFIG_FORM1_LABEL) + FPSTR(STATION_CONFIG_FORM1_LABEL), + true, + "" ); WebserverMemoryVariables::sendFormCheckbox( server, FPSTR(STATION_CONFIG_FORM2_ID), globalDataController->getClockSettings()->is24h, - FPSTR(STATION_CONFIG_FORM2_LABEL) + FPSTR(STATION_CONFIG_FORM2_LABEL), + true, + "" ); WebserverMemoryVariables::sendFormCheckbox( server, FPSTR(STATION_CONFIG_FORM3_ID), globalDataController->getSystemSettings()->invertDisplay, - FPSTR(STATION_CONFIG_FORM3_LABEL) + FPSTR(STATION_CONFIG_FORM3_LABEL), + true, + "" ); WebserverMemoryVariables::sendFormCheckbox( server, FPSTR(STATION_CONFIG_FORM4_ID), globalDataController->getSystemSettings()->useLedFlash, - FPSTR(STATION_CONFIG_FORM4_LABEL) + FPSTR(STATION_CONFIG_FORM4_LABEL), + true, + "" ); - - String form = FPSTR(STATION_CONFIG_FORM5); - String options = FPSTR(STATION_CONFIG_FORM5OPT); - options.replace( - ">"+String(globalDataController->getSystemSettings()->clockWeatherResyncMinutes)+"<", - " selected>"+String(globalDataController->getSystemSettings()->clockWeatherResyncMinutes)+"<" + WebserverMemoryVariables::sendFormSelect( + server, + FPSTR(STATION_CONFIG_FORM5_ID), + FPSTR(STATION_CONFIG_FORM5_LABEL), + String(globalDataController->getSystemSettings()->clockWeatherResyncMinutes), + "", + FPSTR(STATION_CONFIG_FORM5_OPTIONS), + true, + "" + ); + WebserverMemoryVariables::sendFormInput( + server, + FPSTR(STATION_CONFIG_FORM6_ID), + FPSTR(STATION_CONFIG_FORM6_LABEL), + String(globalDataController->getClockSettings()->utcOffset), + 120, + "onkeypress='return isNumberKey(event)'", + false, + true, + "" ); - form.replace("%OPTIONS%", options); - server->sendContent(form); - - form = FPSTR(STATION_CONFIG_FORM6); - form.replace("%UTCOFFSET%", String(globalDataController->getClockSettings()->utcOffset)); - server->sendContent(form); - WebserverMemoryVariables::sendFormCheckboxEvent( server, FPSTR(STATION_CONFIG_FORM7_ID), globalDataController->getSystemSettings()->hasBasicAuth, FPSTR(STATION_CONFIG_FORM7_LABEL), - "showhide(this, 'uspw')" + "showhide('isBasicAuth', 'uspw')", + true, + "" ); - - form = FPSTR(STATION_CONFIG_FORM8); - form.replace("%USERID%", globalDataController->getSystemSettings()->webserverUsername); - form.replace("%STATIONPASSWORD%", globalDataController->getSystemSettings()->webserverPassword); - server->sendContent(form); + WebserverMemoryVariables::rowExtraClass = "data-sh='uspw'"; + WebserverMemoryVariables::sendFormInput( + server, + FPSTR(STATION_CONFIG_FORM8_ID), + FPSTR(STATION_CONFIG_FORM8_LABEL), + globalDataController->getSystemSettings()->webserverUsername, + 20, + "", + false, + true, + "" + ); + WebserverMemoryVariables::rowExtraClass = "data-sh='uspw'"; + WebserverMemoryVariables::sendFormInput( + server, + FPSTR(STATION_CONFIG_FORM9_ID), + FPSTR(STATION_CONFIG_FORM9_LABEL), + globalDataController->getSystemSettings()->webserverPassword, + 120, + "", + true, + true, + "" + ); + WebserverMemoryVariables::sendFormSubmitButton(server, true); + server->sendContent(FPSTR(STATION_CONFIG_FORM_END)); } /** @@ -213,46 +300,138 @@ void WebserverMemoryVariables::sendPrinterConfigForm(ESP8266WebServer *server, G // Generate all modals for(int i=0; i"; -static const char CONFPRINTER_FORM_ROW_ONLINE[] PROGMEM = "
" - "Online" - "
"; -*/ - - - + WebserverMemoryVariables::sendPrinterConfigFormAEModal(server, 0, NULL, globalDataController); server->sendContent(FPSTR(CONFPRINTER_FORM_END)); } -void WebserverMemoryVariables::sendPrinterConfigFormAEModal(ESP8266WebServer *server, int id, PrinterDataStruct *forPrinter) { - String printerEditModal = FPSTR(CONFPRINTER_FORM_ADDEDIT1); - printerEditModal.replace("%ID%", String(id)); +/** + * @brief Modal for printer edit/add + * + * @param server + * @param id + * @param forPrinter + * @param globalDataController Access to global data + */ +void WebserverMemoryVariables::sendPrinterConfigFormAEModal(ESP8266WebServer *server, int id, PrinterDataStruct *forPrinter, GlobalDataController *globalDataController) { + + String modalData = FPSTR(CONFPRINTER_FORM_ADDEDIT_START); + modalData.replace("%ID%", String(id)); if (id == 0) { - printerEditModal.replace("%TITLE%", FPSTR(CONFPRINTER_FORM_ADDEDIT_TA)); + modalData.replace("%TITLE%", FPSTR(CONFPRINTER_FORM_ADDEDIT_TA)); } else { - printerEditModal.replace("%TITLE%", FPSTR(CONFPRINTER_FORM_ADDEDIT_TE)); + modalData.replace("%TITLE%", FPSTR(CONFPRINTER_FORM_ADDEDIT_TE)); } - - if (forPrinter == NULL) { - printerEditModal.replace("%NAME%", ""); - printerEditModal.replace("%TARGETADDR%", ""); - printerEditModal.replace("%TARGETPORT%", "80"); - } else { - printerEditModal.replace("%NAME%", String(forPrinter->customName)); - printerEditModal.replace("%TARGETADDR%", String(forPrinter->remoteAddress)); - printerEditModal.replace("%TARGETPORT%", String(forPrinter->remotePort)); + server->sendContent(modalData); + + String optionData = ""; + BasePrinterClient** printerInstances = globalDataController->getRegisteredPrinterClients(); + for (int i=0; igetRegisteredPrinterClientsNum(); i++) { + if (printerInstances[i] == NULL) { + continue; + } + optionData += ""; } - - server->sendContent(printerEditModal); + WebserverMemoryVariables::sendFormInput( + server, + FPSTR(CONFPRINTER_FORM_ADDEDIT1_ID), + FPSTR(CONFPRINTER_FORM_ADDEDIT1_LABEL), + id > 0 ? String(forPrinter->customName) : "", + 20, + "", + false, + false, + String(id) + ); + WebserverMemoryVariables::sendFormSelect( + server, + FPSTR(CONFPRINTER_FORM_ADDEDIT2_ID), + FPSTR(CONFPRINTER_FORM_ADDEDIT2_LABEL), + "", + "", + optionData, + false, + String(id) + ); + WebserverMemoryVariables::sendFormInput( + server, + FPSTR(CONFPRINTER_FORM_ADDEDIT3_ID), + FPSTR(CONFPRINTER_FORM_ADDEDIT3_LABEL), + id > 0 ? String(forPrinter->apiKey) : "", + 60, + "", + false, + false, + String(id) + ); + WebserverMemoryVariables::sendFormInput( + server, + FPSTR(CONFPRINTER_FORM_ADDEDIT4_ID), + FPSTR(CONFPRINTER_FORM_ADDEDIT4_LABEL), + id > 0 ? String(forPrinter->remoteAddress) : "", + 60, + "", + false, + false, + String(id) + ); + WebserverMemoryVariables::sendFormInput( + server, + FPSTR(CONFPRINTER_FORM_ADDEDIT5_ID), + FPSTR(CONFPRINTER_FORM_ADDEDIT5_LABEL), + id > 0 ? String(forPrinter->remotePort) : "80", + 5, + "onkeypress='return isNumberKey(event)'", + false, + false, + String(id) + ); + WebserverMemoryVariables::sendFormCheckboxEvent( + server, + FPSTR(CONFPRINTER_FORM_ADDEDIT6_ID), + id > 0 ? forPrinter->basicAuthNeeded : true, + FPSTR(CONFPRINTER_FORM_ADDEDIT6_LABEL), + "showhide('" + String(FPSTR(CONFPRINTER_FORM_ADDEDIT6_ID)) + "-" + String(id) + "', 'apac-" + String(id) + "')", + false, + String(id) + ); + WebserverMemoryVariables::rowExtraClass = "data-sh='apac-" + String(id) + "'"; + WebserverMemoryVariables::sendFormInput( + server, + FPSTR(STATION_CONFIG_FORM7_ID), + FPSTR(STATION_CONFIG_FORM7_LABEL), + id > 0 ? String(forPrinter->basicAuthUsername) : "", + 30, + "", + false, + false, + String(id) + ); + WebserverMemoryVariables::rowExtraClass = "data-sh='apac-" + String(id) + "'"; + WebserverMemoryVariables::sendFormInput( + server, + FPSTR(STATION_CONFIG_FORM8_ID), + FPSTR(STATION_CONFIG_FORM8_LABEL), + id > 0 ? String(forPrinter->basicAuthPassword) : "", + 120, + "", + true, + false, + String(id) + ); + modalData = FPSTR(CONFPRINTER_FORM_ADDEDIT_END); + modalData.replace("%ID%", String(id)); + server->sendContent(modalData); } @@ -283,9 +462,10 @@ void WebserverMemoryVariables::sendPrinterConfigFormAEModal(ESP8266WebServer *se * @param formId Form id/name * @param isChecked Checkbox checked * @param label Text for activated/deactivated + * @param inRow Extend the field with row div */ -void WebserverMemoryVariables::sendFormCheckbox(ESP8266WebServer *server, String formId, bool isChecked, String label) { - WebserverMemoryVariables::sendFormCheckboxEvent(server, formId, isChecked, label, ""); +void WebserverMemoryVariables::sendFormCheckbox(ESP8266WebServer *server, String formId, bool isChecked, String label, bool inRow, String uniqueId = "") { + WebserverMemoryVariables::sendFormCheckboxEvent(server, formId, isChecked, label, "", inRow, uniqueId); } /** @@ -295,9 +475,10 @@ void WebserverMemoryVariables::sendFormCheckbox(ESP8266WebServer *server, String * @param isChecked Checkbox checked * @param labelOn Text for activated * @param labelOff Text for deactivated + * @param inRow Extend the field with row div */ -void WebserverMemoryVariables::sendFormCheckbox(ESP8266WebServer *server, String formId, bool isChecked, String labelOn, String labelOff) { - WebserverMemoryVariables::sendFormCheckboxEvent(server, formId, isChecked, labelOn, labelOff, ""); +void WebserverMemoryVariables::sendFormCheckbox(ESP8266WebServer *server, String formId, bool isChecked, String labelOn, String labelOff, bool inRow, String uniqueId = "") { + WebserverMemoryVariables::sendFormCheckboxEvent(server, formId, isChecked, labelOn, labelOff, "", inRow, uniqueId); } /** @@ -307,14 +488,22 @@ void WebserverMemoryVariables::sendFormCheckbox(ESP8266WebServer *server, String * @param isChecked Checkbox checked * @param label Text for activated/deactivated * @param onChange Javascript function + * @param inRow Extend the field with row div */ -void WebserverMemoryVariables::sendFormCheckboxEvent(ESP8266WebServer *server, String formId, bool isChecked, String label, String onChange) { +void WebserverMemoryVariables::sendFormCheckboxEvent( + ESP8266WebServer *server, + String formId, + bool isChecked, + String label, + String onChange, + bool inRow, + String uniqueId = "" +) { String onAdd = FPSTR(FORM_ITEM_CHECKBOX_ON); String offAdd = FPSTR(FORM_ITEM_CHECKBOX_OFF); - WebserverMemoryVariables::sendFormCheckboxEvent(server, formId, isChecked, label + onAdd, label + offAdd, onChange); + WebserverMemoryVariables::sendFormCheckboxEvent(server, formId, isChecked, label + onAdd, label + offAdd, onChange, inRow, uniqueId); } - /** * @brief Send out an single checkbox form row with onChangeEvent * @param server Send out instancce @@ -323,8 +512,18 @@ void WebserverMemoryVariables::sendFormCheckboxEvent(ESP8266WebServer *server, S * @param labelOn Text for activated * @param labelOff Text for deactivated * @param onChange Javascript function + * @param inRow Extend the field with row div */ -void WebserverMemoryVariables::sendFormCheckboxEvent(ESP8266WebServer *server, String formId, bool isChecked, String labelOn, String labelOff, String onChange) { +void WebserverMemoryVariables::sendFormCheckboxEvent( + ESP8266WebServer *server, + String formId, + bool isChecked, + String labelOn, + String labelOff, + String onChange, + bool inRow, + String uniqueId = "" +) { String isCheckedText = ""; String onChangeText = ""; if (isChecked) { @@ -339,5 +538,177 @@ void WebserverMemoryVariables::sendFormCheckboxEvent(ESP8266WebServer *server, S form.replace("%LABELON%", labelOn); form.replace("%LABELOFF%", labelOff); form.replace("%ONCHANGE%", onChangeText); - server->sendContent(form); -} \ No newline at end of file + WebserverMemoryVariables::sendForm(server, formId, form, inRow, uniqueId); +} + + +/** + * @brief Send out an single input field form row + * @param server Send out instancce + * @param formId Form id/name + * @param label Text for label head + * @param value Value in field + * @param maxLen Max text len in input field + * @param events Extra events for input field + * @param isPassword True if password field + * @param inRow Extend the field with row div + * @param uniqueId Unique key for ids + */ +void WebserverMemoryVariables::sendFormInput( + ESP8266WebServer *server, + String formId, + String label, + String value, + int maxLen, + String events, + bool isPassword, + bool inRow, + String uniqueId = "" +) { + String form = FPSTR(FORM_ITEM_INPUT); + form.replace("%FORMID%", formId); + form.replace("%LABEL%", label); + form.replace("%VALUE%", value); + form.replace("%MAXLEN%", String(maxLen)); + form.replace("%EVENTS%", events); + + if (isPassword) { + form.replace("%FIELDTYPE%", "password"); + } + else { + form.replace("%FIELDTYPE%", "text"); + } + WebserverMemoryVariables::sendForm(server, formId, form, inRow, uniqueId); +} + +/** + * @brief Send out an single input field form row + * @param server Send out instancce + * @param formId Form id/name + * @param label Text for label head + * @param value Value in field + * @param events Extra events for input field + * @param inRow Extend the field with row div + * @param uniqueId Unique key for ids + */ +void WebserverMemoryVariables::sendFormSelect( + ESP8266WebServer *server, + String formId, + String label, + String value, + String events, + String options, + bool inRow, + String uniqueId = "" +) { + String form = FPSTR(FORM_ITEM_SELECT_START); + form.replace("%FORMID%", formId); + form.replace("%LABEL%", label); + form.replace("%EVENTS%", events); + + if (value.length() > 0) { + options.replace( + ">"+ value + "<", + " selected>" + String(value) + "<" + ); + } + + WebserverMemoryVariables::sendForm( + server, + formId, + form + options + FPSTR(FORM_ITEM_SELECT_END), + inRow, + uniqueId + ); +} + +/** + * @brief Send form out to client + * + * @param server Send out instancce + * @param formElement Form element + * @param inRow True if in row + */ +void WebserverMemoryVariables::sendFormSubmitButton( + ESP8266WebServer *server, + bool inRow +) { + WebserverMemoryVariables::sendForm( + server, + "", + FPSTR(FORM_ITEM_SUBMIT), + inRow, + "" + ); +} + +/** + * @brief Send form out to client + * + * @param server Send out instance + * @param formId Form id/name + * @param formElement Form element + * @param inRow True if in row + * @param uniqueId Unique key for ids + */ +void WebserverMemoryVariables::sendForm( + ESP8266WebServer *server, + String formId, + String formElement, + bool inRow, + String uniqueId +) { + if (uniqueId.length() > 0) { + formElement.replace("id='" + formId + "'", "id='" + formId + "-" + uniqueId + "'"); + formElement.replace("for='" + formId + "'", "for='" + 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%", ""); + } else { + formElement.replace("%ROWEXT%", ""); + formElement.replace("%DIVEXTRACLASS%", WebserverMemoryVariables::rowExtraClass); + } + server->sendContent(formElement); + if (inRow) { + server->sendContent(FPSTR(FORM_ITEM_ROW_END)); + } +} + +/** + * @brief Send danger modal out to client + * + * @param server Send out instancce + * @param formId ID of element + * @param label Label top + * @param title Dialog title + * @param content Dialog detailed content + * @param secActionTitle Title of secondary button + * @param primActionTitle Title of primary button + * @param primActionEvent Event of primary button + */ +void WebserverMemoryVariables::sendModalDanger( + ESP8266WebServer *server, + String formId, + String label, + String title, + String content, + String secActionTitle, + String primActionTitle, + String primActionEvent +) { + String modalDialog = FPSTR(MODAL_DANGER); + modalDialog.replace("%ID%", formId); + modalDialog.replace("%LABEL%", label); + modalDialog.replace("%HEADING%", title); + modalDialog.replace("%CONTENT%", content); + modalDialog.replace("%SECACTION%", secActionTitle); + modalDialog.replace("%MAINACTION%", primActionTitle); + modalDialog.replace("%MAINEVENT%", primActionEvent); + server->sendContent(modalDialog); +} + diff --git a/src/Network/WebserverMemoryVariables.h b/src/Network/WebserverMemoryVariables.h index a9042eb..4ae666a 100644 --- a/src/Network/WebserverMemoryVariables.h +++ b/src/Network/WebserverMemoryVariables.h @@ -7,8 +7,12 @@ /** * Webpage form items for reuse */ -static const char FORM_ITEM_CHECKBOX[] PROGMEM = "
" - "
" +static const char FORM_ITEM_ROW_START[] PROGMEM = "
"; +static const char FORM_ITEM_ROW_EXT[] PROGMEM = "bx--col bx--col--auto"; +static const char FORM_ITEM_ROW_END[] PROGMEM = "
"; + + +static const char FORM_ITEM_CHECKBOX[] PROGMEM = "
" "" "
"; + "
"; static const char FORM_ITEM_CHECKBOX_ON[] PROGMEM = " activated"; static const char FORM_ITEM_CHECKBOX_OFF[] PROGMEM = " deactivated"; +static const char FORM_ITEM_INPUT[] PROGMEM = "
" + "" + "" + "
"; + +static const char FORM_ITEM_SELECT_START[] PROGMEM = "
" + "" + "
" + "" + "" + "
" + "
"; + +static const char FORM_ITEM_SUBMIT[] PROGMEM = "
" + "" + "
"; + /** * Webpage side menu right for main items */ @@ -48,11 +69,11 @@ static const char MENUE_ITEMS[] PROGMEM = "Configure Sensor" "" "" - "
  • " + "
  • " "Reset Settings" "" "
  • " - "
  • " + "
  • " "Forget WiFi" "" "
  • " @@ -77,9 +98,12 @@ static const char HEADER_BLOCK2[] PROGMEM = "" "" "" - "" "" + "" "" + "" + "" + "" "
    " ""; @@ -129,6 +153,15 @@ static const char HEADER_BLOCK_ERROR[] PROGMEM = ""; + static const char FOOTER_BLOCK[] PROGMEM = "


    " "