diff --git a/src/Network/WebServer.cpp b/src/Network/WebServer.cpp index 8985be5..9aecd47 100644 --- a/src/Network/WebServer.cpp +++ b/src/Network/WebServer.cpp @@ -14,7 +14,7 @@ void WebServer::setup() { this->server = new ESP8266WebServer(this->globalDataController->getSystemSettings()->webserverPort); this->serverUpdater = new ESP8266HTTPUpdateServer(); - this->server->on("/", []() { obj->displayPrinterStatus(); }); + this->server->on("/", []() { obj->handleMainPage(); }); this->server->on("/systemreset", []() { obj->handleSystemReset(); }); this->server->on("/forgetwifi", []() { obj->handleWifiReset(); }); this->server->on("/configurestation/show", []() { obj->handleConfigureStation(); }); @@ -28,8 +28,6 @@ void WebServer::setup() { this->server->on("/configuresensor/update", []() { obj->handleConfigureSensor(); }); this->server->on("/update", HTTP_GET, []() { obj->handleUpdatePage(); }); - this->server->on("/updateconfig", []() { obj->handleUpdateConfig(); }); - this->server->onNotFound([]() { obj->redirectHome(); }); this->serverUpdater->setup( this->server, @@ -64,33 +62,13 @@ void WebServer::redirectHome() { } void WebServer::displayPrinterStatus() { - WebserverMemoryVariables::sendHeader(this->server, this->globalDataController, "Status", "Monitor", true); - + /*BasePrinterClient *printerClient = this->globalDataController->getPrinterClient(); String html = ""; - String displayTime = - this->globalDataController->getTimeClient()->getAmPmHours() + ":" + - this->globalDataController->getTimeClient()->getMinutes() + ":" + - this->globalDataController->getTimeClient()->getSeconds() + " " + - this->globalDataController->getTimeClient()->getAmPm(); - if (this->globalDataController->getClockSettings()->is24h) { - displayTime = - this->globalDataController->getTimeClient()->getHours() + ":" + - this->globalDataController->getTimeClient()->getMinutes() + ":" + - this->globalDataController->getTimeClient()->getSeconds(); - } - - html += "

" + printerClient->getPrinterType() + " Monitor

"; - html += "

"; - if ((printerClient->getPrinterType() == "Repetier") || (printerClient->getPrinterType() == "Klipper")) { - html += "Printer Name: " + printerClient->getPrinterName() + "
"; - } - else { - html += "Host Name: " + this->globalDataController->getPrinterHostName() + "
"; - } + if (printerClient->getError() != "") { html += "Status: Offline
"; @@ -104,42 +82,9 @@ void WebServer::displayPrinterStatus() { } if (printerClient->isPrinting()) { - html += "File: " + printerClient->getFileName() + "
"; - float fileSize = printerClient->getFileSize().toFloat(); - if (fileSize > 0) { - fileSize = fileSize / 1024; - html += "File Size: " + String(fileSize) + "KB
"; - } - int filamentLength = printerClient->getFilamentLength().toInt(); - if (filamentLength > 0) { - float fLength = float(filamentLength) / 1000; - html += "Filament: " + String(fLength) + "m
"; - } - - html += "Tool Temperature: " + printerClient->getTempToolActual() + "° C
"; - if (printerClient->getTempBedActual() != 0 ) { - html += "Bed Temperature: " + printerClient->getTempBedActual() + "° C
"; - } - int val = printerClient->getProgressPrintTimeLeft().toInt(); - int hours = this->globalDataController->numberOfHours(val); - int minutes = this->globalDataController->numberOfMinutes(val); - int seconds = this->globalDataController->numberOfSeconds(val); - html += "Est. Print Time Left: " - + this->globalDataController->zeroPad(hours) + ":" - + this->globalDataController->zeroPad(minutes) + ":" - + this->globalDataController->zeroPad(seconds) + "
"; - val = printerClient->getProgressPrintTime().toInt(); - hours = this->globalDataController->numberOfHours(val); - minutes = this->globalDataController->numberOfMinutes(val); - seconds = this->globalDataController->numberOfSeconds(val); - html += "Printing Time: " + this->globalDataController->zeroPad(hours) + ":" + this->globalDataController->zeroPad(minutes) + ":" + this->globalDataController->zeroPad(seconds) + "
"; - html += ""; - html += "

" + printerClient->getProgressCompletion() + "%
"; - } else { - html += "
"; - } + html += "

"; @@ -147,79 +92,13 @@ void WebServer::displayPrinterStatus() { this->server->sendContent(html); // spit out what we got html = ""; - - if (this->globalDataController->getWeatherSettings()->show) { - OpenWeatherMapClient *weatherClient = this->globalDataController->getWeatherClient(); - if (weatherClient->getCity(0) == "") { - html += "

Please Configure Weather API

"; - if (weatherClient->getError() != "") { - html += "

Weather Error: " + weatherClient->getError() + "

"; - } - } else { - html += "

" + weatherClient->getCity(0) + ", " + weatherClient->getCountry(0) + "

"; - html += "
"; - html += "" + weatherClient->getDescription(0) + "
"; - html += weatherClient->getHumidity(0) + "% Humidity
"; - html += weatherClient->getWind(0) + " " + weatherClient->getSpeedSymbol() + " Wind
"; - html += "
"; - html += "

"; - html += weatherClient->getCondition(0) + " (" + weatherClient->getDescription(0) + ")
"; - html += weatherClient->getTempRounded(0) + weatherClient->getTempSymbol(true) + "
"; - html += " Map It!
"; - html += "

"; - } - - this->server->sendContent(html); // spit out what we got - html = ""; // fresh start - }*/ + */ - WebserverMemoryVariables::sendFooter(this->server, this->globalDataController); + } -void WebServer::handleUpdateConfig() { - /*boolean flipOld = this->globalDataController->isDisplayInverted(); - if (!this->authentication()) { - return this->server->requestAuthentication(); - } - if (this->server->hasArg("printer")) { - this->globalDataController->getPrinterClient()->setPrinterName( - this->server->arg("printer") - ); - } - - this->globalDataController->setPrinterApiKey(this->server->arg("PrinterApiKey")); - this->globalDataController->setPrinterHostName(this->server->arg("PrinterHostName")); - this->globalDataController->setPrinterServer(this->server->arg("PrinterAddress")); - this->globalDataController->setPrinterPort(this->server->arg("PrinterPort").toInt()); - this->globalDataController->setPrinterAuthUser(this->server->arg("octoUser")); - this->globalDataController->setPrinterAuthPass(this->server->arg("octoPass")); - this->globalDataController->setHasPrinterPsu(this->server->hasArg("hasPSU")); - this->globalDataController->setDisplayClock(this->server->hasArg("isClockEnabled")); - this->globalDataController->setIsDisplayInverted(this->server->hasArg("invDisp")); - this->globalDataController->setClockIs24h(this->server->hasArg("is24hour")); - this->globalDataController->setClockResyncMinutes(this->server->arg("refresh").toInt()); - this->globalDataController->setWebserverTheme(this->server->arg("theme")); - this->globalDataController->setClockUtcOffset(this->server->arg("utcoffset").toFloat()); - String temp = this->server->arg("userid"); - this->globalDataController->setWebserverUsername(temp); - temp = this->server->arg("stationpassword"); - this->globalDataController->setWebserverPassword(temp); - //this->globalDataController->setUseLedFlash(this->server->hasArg("useFlash")); - - this->globalDataController->writeSettings(); - this->findMDNS(); - - this->globalDataController->getPrinterClient()->getPrinterJobResults(); - this->globalDataController->getPrinterClient()->getPrinterPsuState(); - if (this->globalDataController->isDisplayInverted() != flipOld) { - this->globalDataController->getDisplayClient()->flipDisplayUpdate(); - } - this->globalDataController->getDisplayClient()->handleUpdate(); - this->globalDataController->getTimeClient()->resetLastEpoch();*/ - this->redirectHome(); -} void WebServer::findMDNS() { return; // nothing to do here @@ -254,151 +133,6 @@ void WebServer::findMDNS() { }*/ } -/* -void WebServer::handleConfigure() { - if (!this->authentication()) { - return this->server->requestAuthentication(); - } - BasePrinterClient *printerClient = this->globalDataController->getPrinterClient(); - String html = ""; - - WebserverMemoryVariables::sendHeader(this->server, this->globalDataController, "Configure", "Station"); - - - // send javascript functions for repetier server test - html = ""; - this->server->sendContent(html); - - // send javascript functions for klipper test - html = ""; - this->server->sendContent(html); - - // send javascript functions for octoprint test - html = ""; - this->server->sendContent(html); - - // Let us create a form for all printers - html = "

Station Config:

"; - html += "
"; - this->server->sendContent(html); - - - - String form = ""; - if (printerClient->getPrinterType() != "Klipper") { - form += "

" - "

"; - } - if ((printerClient->getPrinterType() == "OctoPrint") || (printerClient->getPrinterType() == "Klipper")) { - form += "

"; - } - form += "

" - "

" - "

" - "

"; - if ((printerClient->getPrinterType() == "Repetier") || (printerClient->getPrinterType() == "Klipper")) { - form += "" - "

" - ""; - } - else { - form += "

"; - } - form += "

" - "

"; - - form.replace("%PRINTERAPIKEY%", this->globalDataController->getPrinterApiKey()); - form.replace("%PRINTERHOST%", this->globalDataController->getPrinterHostName()); - form.replace("%PRINTERADDRESS%", this->globalDataController->getPrinterServer()); - form.replace("%PRINTERPORT%", String(this->globalDataController->getPrinterPort())); - form.replace("%PRINTERUSER%", this->globalDataController->getPrinterAuthUser()); - form.replace("%PRINTERPASS%", this->globalDataController->getPrinterAuthPass()); - this->server->sendContent(form); - - - this->server->sendContent(form); - - WebserverMemoryVariables::sendFooter(this->server, this->globalDataController); -}*/ - - - - - - - - - - - - - - -void WebServer::handleConfigureSensor() { - if (!this->authentication()) { - return this->server->requestAuthentication(); - } - WebserverMemoryVariables::sendHeader(this->server, this->globalDataController, "Configure", "Sensor"); - - WebserverMemoryVariables::sendFooter(this->server, this->globalDataController); -} - - - - - - - - - - - - - /** * @brief Handle authentification on all subsites * @return boolean @@ -412,6 +146,27 @@ boolean WebServer::authentication() { return true; // Authentication not required } +/** + * @brief Send main page to client + */ +void WebServer::handleMainPage() { + WebserverMemoryVariables::sendHeader(this->server, this->globalDataController, "Status", "Monitor", true); + WebserverMemoryVariables::sendMainPage(this->server, this->globalDataController); + WebserverMemoryVariables::sendFooter(this->server, this->globalDataController); +} + +/** + * @brief Send sensor configuration page to client + */ +void WebServer::handleConfigureSensor() { + if (!this->authentication()) { + return this->server->requestAuthentication(); + } + WebserverMemoryVariables::sendHeader(this->server, this->globalDataController, "Configure", "Sensor"); + + WebserverMemoryVariables::sendFooter(this->server, this->globalDataController); +} + /** * @brief Send printer configuration page to client */ diff --git a/src/Network/WebServer.h b/src/Network/WebServer.h index bdf4625..eab5e22 100644 --- a/src/Network/WebServer.h +++ b/src/Network/WebServer.h @@ -26,10 +26,9 @@ public: void redirectTarget(String targetUri); void displayPrinterStatus(); void handleSystemReset(); - void handleWifiReset(); - void handleUpdateConfig(); - + void handleWifiReset(); + void handleMainPage(); void handleConfigurePrinter(); void handleUpdatePrinter(); void handleDeletePrinter(); diff --git a/src/Network/WebserverMemoryVariables.cpp b/src/Network/WebserverMemoryVariables.cpp index 368f7a0..342e9d0 100644 --- a/src/Network/WebserverMemoryVariables.cpp +++ b/src/Network/WebserverMemoryVariables.cpp @@ -58,7 +58,14 @@ void WebserverMemoryVariables::sendHeader( server->sendContent(pageLabel); server->sendContent("

"); server->sendContent(pageTitle); - server->sendContent("

"); + server->sendContent(""); + + TimeClient *timeCli = globalDataController->getTimeClient(); + String displayTime = timeCli->getAmPmHours() + ":" + timeCli->getMinutes() + ":" + timeCli->getSeconds() + " " + timeCli->getAmPm(); + if (globalDataController->getClockSettings()->is24h) { + displayTime = timeCli->getHours() + ":" + timeCli->getMinutes() + ":" + timeCli->getSeconds(); + } + server->sendContent("
Current time
" + displayTime + "
"); if (globalDataController->getSystemSettings()->lastError.length() > 0) { String errorBlock = FPSTR(HEADER_BLOCK_ERROR); @@ -105,6 +112,191 @@ void WebserverMemoryVariables::sendFooter(ESP8266WebServer *server, GlobalDataCo globalDataController->ledOnOff(false); } +/** + * @brief Send out main page + * @param server Send out instancce + * @param globalDataController Access to global data + */ +void WebserverMemoryVariables::sendMainPage(ESP8266WebServer *server, GlobalDataController *globalDataController) { + String rowStart = FPSTR(FORM_ITEM_ROW_START); + rowStart.replace("%ROWEXTRACLASS%", ""); + + // Show weather and sensordata + OpenWeatherMapClient *weatherClient = globalDataController->getWeatherClient(); + server->sendContent(rowStart); + String startBlock = FPSTR(MAINPAGE_ROW_WEATHER_AND_SENSOR_START); + String displayBlock = FPSTR(MAINPAGE_ROW_WEATHER_AND_SENSOR_BLOCK); + if (!globalDataController->getWeatherSettings()->show || weatherClient->getCity(0) == "") { + startBlock.replace("%ICON%", ""); + server->sendContent(startBlock); + if (globalDataController->getWeatherSettings()->show) { + displayBlock = FPSTR(MAINPAGE_ROW_WEATHER_ERROR_BLOCK); + if (weatherClient->getError() != "") { + displayBlock.replace("%ERRORMSG%", "Weather Error: " + weatherClient->getError()); + } else { + displayBlock.replace("%ERRORMSG%", ""); + } + } else { + displayBlock = ""; + } + } else { + startBlock.replace("%ICON%", weatherClient->getIcon(0)); + server->sendContent(startBlock); + displayBlock.replace("%BTITLE%", weatherClient->getCity(0) + ", " + weatherClient->getCountry(0)); + displayBlock.replace("%BLABEL%", "Lat: " + weatherClient->getLat(0) + ", Lon: " + weatherClient->getLon(0)); + displayBlock.replace("%TEMPICON%", FPSTR(ICON32_TEMP)); + displayBlock.replace("%TEMPERATURE%", weatherClient->getTempRounded(0) + weatherClient->getTempSymbol(true)); + displayBlock.replace("%ICONA%", FPSTR(ICON16_WIND)); + displayBlock.replace("%ICONB%", FPSTR(ICON16_HUMIDITY)); + displayBlock.replace("%TEXTA%", weatherClient->getWind(0) + " " + weatherClient->getSpeedSymbol() + " Winds"); + displayBlock.replace("%TEXTB%", weatherClient->getHumidity(0) + "% Humidity"); + displayBlock.replace("%EXTRABLOCK%", "Condition: " + weatherClient->getDescription(0)); + } + server->sendContent(displayBlock); + + // TODO: SensorData + /*displayBlock = FPSTR(MAINPAGE_ROW_WEATHER_AND_SENSOR_BLOCK); + displayBlock.replace("%TEMPICON%", FPSTR(ICON32_TEMP)); + displayBlock.replace("%BTITLE%", "Sensor"); + displayBlock.replace("%BLABEL%", "BME280"); + displayBlock.replace("%TEMPERATURE%", "24°C"); + displayBlock.replace("%ICONA%", FPSTR(ICON16_HUMIDITY)); + displayBlock.replace("%ICONB%", FPSTR(ICON16_PRESSURE)); + displayBlock.replace("%TEXTA%", "30% Humidity"); + displayBlock.replace("%TEXTB%", "1000 hPa"); + displayBlock.replace("%EXTRABLOCK%", ""); + server->sendContent(displayBlock);*/ + + server->sendContent(FPSTR(MAINPAGE_ROW_WEATHER_AND_SENSOR_END)); + server->sendContent(FPSTR(FORM_ITEM_ROW_END)); + + // Show all printer states + int totalPrinters = globalDataController->getNumPrinters(); + PrinterDataStruct *printerConfigs = globalDataController->getPrinterSettings(); + String lineData = ""; + int colCnt = 0; + server->sendContent(rowStart); + + // Show all errors if printers have one + for(int i=0; i= 3) { + server->sendContent(FPSTR(FORM_ITEM_ROW_END)); + server->sendContent(rowStart); + colCnt = 0; + } + + if ((printerConfigs[i].state == PRINTER_STATE_ERROR) || (printerConfigs[i].state == PRINTER_STATE_OFFLINE)) { + server->sendContent(FPSTR(MAINPAGE_ROW_PRINTER_BLOCK_S_ERROROFFLINE)); + } + else if (printerConfigs[i].state == PRINTER_STATE_STANDBY) { + server->sendContent(FPSTR(MAINPAGE_ROW_PRINTER_BLOCK_S_STANDBY)); + } + else { + server->sendContent(FPSTR(MAINPAGE_ROW_PRINTER_BLOCK_S_PRINTING)); + } + + lineData = FPSTR(MAINPAGE_ROW_PRINTER_BLOCK_TITLE); + lineData.replace("%NAME%", String(printerConfigs[i].customName)); + lineData.replace("%API%", globalDataController->getPrinterClientType(&printerConfigs[i])); + server->sendContent(lineData); + + lineData = FPSTR(MAINPAGE_ROW_PRINTER_BLOCK_LINE); + lineData.replace("%T%", "Host"); + lineData.replace("%V%", String(printerConfigs[i].remoteAddress) + ":" + String(printerConfigs[i].remotePort)); + server->sendContent(lineData); + + lineData = FPSTR(MAINPAGE_ROW_PRINTER_BLOCK_LINE); + lineData.replace("%T%", "State"); + lineData.replace("%V%", globalDataController->getPrinterStateAsText(&printerConfigs[i])); + server->sendContent(lineData); + + if (printerConfigs[i].state != PRINTER_STATE_STANDBY) { + if (printerConfigs[i].state == PRINTER_STATE_ERROR) { + lineData = FPSTR(MAINPAGE_ROW_PRINTER_BLOCK_LINE); + lineData.replace("%T%", "Reason"); + lineData.replace("%V%", String(printerConfigs[i].error)); + server->sendContent(lineData); + } + else if (printerConfigs[i].state == PRINTER_STATE_OFFLINE) { + lineData = FPSTR(MAINPAGE_ROW_PRINTER_BLOCK_LINE); + lineData.replace("%T%", "Reason"); + lineData.replace("%V%", "Not reachable"); + server->sendContent(lineData); + } else { + if ((printerConfigs[i].state == PRINTER_STATE_PRINTING) || (printerConfigs[i].state == PRINTER_STATE_PAUSED)) { + lineData = FPSTR(MAINPAGE_ROW_PRINTER_BLOCK_PROG); + lineData.replace("%P%", String(printerConfigs[i].progressCompletion) + "%"); + server->sendContent(lineData); + server->sendContent(FPSTR(MAINPAGE_ROW_PRINTER_BLOCK_HR)); + + int val = printerConfigs[i].progressPrintTime; + int hours = globalDataController->numberOfHours(val); + int minutes = globalDataController->numberOfMinutes(val); + int seconds = globalDataController->numberOfSeconds(val); + lineData = FPSTR(MAINPAGE_ROW_PRINTER_BLOCK_LINE); + lineData.replace("%T%", "Printing Time"); + lineData.replace("%V%", globalDataController->zeroPad(hours) + ":" + globalDataController->zeroPad(minutes) + ":" + globalDataController->zeroPad(seconds)); + server->sendContent(lineData); + + val = printerConfigs[i].progressPrintTimeLeft; + hours = globalDataController->numberOfHours(val); + minutes = globalDataController->numberOfMinutes(val); + seconds = globalDataController->numberOfSeconds(val); + lineData = FPSTR(MAINPAGE_ROW_PRINTER_BLOCK_LINE); + lineData.replace("%T%", "Est. Print Time Left"); + lineData.replace("%V%", globalDataController->zeroPad(hours) + ":" + globalDataController->zeroPad(minutes) + ":" + globalDataController->zeroPad(seconds)); + server->sendContent(lineData); + + server->sendContent(FPSTR(MAINPAGE_ROW_PRINTER_BLOCK_HR)); + + lineData = FPSTR(MAINPAGE_ROW_PRINTER_BLOCK_LINE); + lineData.replace("%T%", "File"); + lineData.replace("%V%", String(printerConfigs[i].fileName)); + server->sendContent(lineData); + + float fileSize = printerConfigs[i].fileSize; + if (fileSize > 0) { + lineData = FPSTR(MAINPAGE_ROW_PRINTER_BLOCK_LINE); + lineData.replace("%T%", "Filesize"); + lineData.replace("%V%", String(fileSize) + " KB"); + server->sendContent(lineData); + } + + int filamentLength = printerConfigs[i].filamentLength; + if (filamentLength > 0) { + float fLength = float(filamentLength) / 1000; + lineData = FPSTR(MAINPAGE_ROW_PRINTER_BLOCK_LINE); + lineData.replace("%T%", "Filament"); + lineData.replace("%V%", String(fLength) + " m"); + server->sendContent(lineData); + } + } + server->sendContent(FPSTR(MAINPAGE_ROW_PRINTER_BLOCK_HR)); + + lineData = FPSTR(MAINPAGE_ROW_PRINTER_BLOCK_LINE); + lineData.replace("%T%", "Tool Temperature"); + lineData.replace("%V%", String(printerConfigs[i].toolTemp) + "° C [" + String(printerConfigs[i].toolTargetTemp) + "]"); + server->sendContent(lineData); + + if (printerConfigs[i].bedTemp > 0 ) { + lineData = FPSTR(MAINPAGE_ROW_PRINTER_BLOCK_LINE); + lineData.replace("%T%", "Bed Temperature"); + lineData.replace("%V%", String(printerConfigs[i].bedTemp) + "° C [" + String(printerConfigs[i].bedTargetTemp) + "]"); + server->sendContent(lineData); + } + } + } + + server->sendContent(FPSTR(MAINPAGE_ROW_PRINTER_BLOCK_E)); + colCnt++; + } + while(colCnt < 3) { + server->sendContent("
"); + colCnt++; + } + server->sendContent(FPSTR(FORM_ITEM_ROW_END)); +} + /** * @brief Send out upload form for updates * @param server Send out instancce diff --git a/src/Network/WebserverMemoryVariables.h b/src/Network/WebserverMemoryVariables.h index a4f0486..593c894 100644 --- a/src/Network/WebserverMemoryVariables.h +++ b/src/Network/WebserverMemoryVariables.h @@ -11,7 +11,6 @@ static const char FORM_ITEM_ROW_START[] PROGMEM = "
" "
" "" "" "
" - "