#include "WebServer.h" static const char WEB_ACTIONS[] PROGMEM = " Home" " Configure" " Weather" " Reset Settings" " Forget WiFi" " Firmware Update" " About"; static const char CLOCK_FORM[] PROGMEM = "

Display Clock when printer is off

" "

Use 24 Hour Clock (military time)

" "

Flip display orientation

" "

Flash System LED on Service Calls

" "

Use OctoPrint PSU control plugin for clock/blank

" "

Clock Sync / Weather Refresh (minutes)

"; static const char THEME_FORM[] PROGMEM = "

Theme Color

" "


" "

Use Security Credentials for Configuration Changes

" "

" "

" ""; static const char WEATHER_FORM[] PROGMEM = "

Weather Config:

" "

Display Weather when printer is off

" "" "" "

" "

Use Metric (Celsius)

" "

Weather Language

" "
" ""; static const char LANG_OPTIONS[] PROGMEM = "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""; static const char COLOR_THEMES[] PROGMEM = "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""; WebServer::WebServer(ESP8266WebServer *serverHandle, Debug *debugHandle, PrinterClientInterface *printerClient, TimeClient *timeClient, String themeColor, String swVersion, boolean hasPsu) { this->serverHandle = serverHandle; this->themeColor = themeColor; this->debugHandle = debugHandle; this->swVersion = swVersion; this->printerClient = printerClient; this->timeClient = timeClient; this->hostName = ""; this->hasPsu = hasPsu; this->isBasicAuth = false; this->wwwUsername = ""; this->wwwPassword = ""; } void WebServer::setHostname(String hostName) { this->hostName = hostName; } void WebServer::redirectHome() { // Send them back to the Root Directory this->serverHandle->sendHeader("Location", String("/"), true); this->serverHandle->sendHeader("Cache-Control", "no-cache, no-store"); this->serverHandle->sendHeader("Pragma", "no-cache"); this->serverHandle->sendHeader("Expires", "-1"); this->serverHandle->send(302, "text/plain", ""); this->serverHandle->client().stop(); } String WebServer::getHeader() { return this->getHeader(false); } String WebServer::getHeader(boolean refresh) { String menu = FPSTR(WEB_ACTIONS); String html = ""; html += "Printer Monitor"; html += ""; html += ""; if (refresh) { html += ""; } html += ""; html += ""; html += ""; html += ""; html += ""; html += "

Printer Monitor

"; html += ""; html += "
"; return html; } String WebServer::getFooter() { int8_t rssi = this->getWifiQuality(); this->debugHandle->print("Signal Strength (RSSI): "); this->debugHandle->print(rssi); this->debugHandle->printLn("%"); String html = "


"; html += "
"; html += ""; html += ""; return html; } void WebServer::displayPrinterStatus() { String html = ""; this->serverHandle->sendHeader("Cache-Control", "no-cache, no-store"); this->serverHandle->sendHeader("Pragma", "no-cache"); this->serverHandle->sendHeader("Expires", "-1"); this->serverHandle->setContentLength(CONTENT_LENGTH_UNKNOWN); this->serverHandle->send(200, "text/html", ""); this->serverHandle->sendContent(String(this->getHeader(true))); String displayTime = this->timeClient->getAmPmHours() + ":" + this->timeClient->getMinutes() + ":" + this->timeClient->getSeconds() + " " + this->timeClient->getAmPm(); if (this->timeClient->isHour24()) { displayTime = this->timeClient->getHours() + ":" + this->timeClient->getMinutes() + ":" + this->timeClient->getSeconds(); } html += "

" + this->printerClient->getPrinterType() + " Monitor

"; html += "

"; if (this->printerClient->getPrinterType() == "Repetier") { html += "Printer Name: " + this->printerClient->getPrinterName() + "
"; } else { html += "Host Name: " + this->hostName + "
"; } if (this->printerClient->getError() != "") { html += "Status: Offline
"; html += "Reason: " + this->printerClient->getError() + "
"; } else { html += "Status: " + this->printerClient->getState(); if (this->printerClient->isPSUoff() && this->hasPsu) { html += ", PSU off"; } html += "
"; } if (this->printerClient->isPrinting()) { html += "File: " + this->printerClient->getFileName() + "
"; float fileSize = this->printerClient->getFileSize().toFloat(); if (fileSize > 0) { fileSize = fileSize / 1024; html += "File Size: " + String(fileSize) + "KB
"; } int filamentLength = this->printerClient->getFilamentLength().toInt(); if (filamentLength > 0) { float fLength = float(filamentLength) / 1000; html += "Filament: " + String(fLength) + "m
"; } html += "Tool Temperature: " + this->printerClient->getTempToolActual() + "° C
"; if ( this->printerClient->getTempBedActual() != 0 ) { html += "Bed Temperature: " + this->printerClient->getTempBedActual() + "° C
"; } int val = this->printerClient->getProgressPrintTimeLeft().toInt(); int hours = numberOfHours(val); int minutes = numberOfMinutes(val); int seconds = numberOfSeconds(val); html += "Est. Print Time Left: " + zeroPad(hours) + ":" + zeroPad(minutes) + ":" + zeroPad(seconds) + "
"; val = this->printerClient->getProgressPrintTime().toInt(); hours = numberOfHours(val); minutes = numberOfMinutes(val); seconds = numberOfSeconds(val); html += "Printing Time: " + zeroPad(hours) + ":" + zeroPad(minutes) + ":" + zeroPad(seconds) + "
"; html += ""; html += "

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

"; html += "

Time: " + displayTime + "

"; this->serverHandle->sendContent(html); // spit out what we got html = ""; /* if (DISPLAYWEATHER) { 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) + " " + getSpeedSymbol() + " Wind
"; html += "
"; html += "

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

"; } this->serverHandle->sendContent(html); // spit out what we got html = ""; // fresh start }*/ this->serverHandle->sendContent(String(getFooter())); this->serverHandle->sendContent(""); this->serverHandle->client().stop(); } void WebServer::displayMessage(String message) { this->serverHandle->sendHeader("Cache-Control", "no-cache, no-store"); this->serverHandle->sendHeader("Pragma", "no-cache"); this->serverHandle->sendHeader("Expires", "-1"); this->serverHandle->setContentLength(CONTENT_LENGTH_UNKNOWN); this->serverHandle->send(200, "text/html", ""); String html = this->getHeader(); this->serverHandle->sendContent(String(html)); this->serverHandle->sendContent(String(message)); html = this->getFooter(); this->serverHandle->sendContent(String(html)); this->serverHandle->sendContent(""); this->serverHandle->client().stop(); } void WebServer::setAuthentication(boolean enableBasicAuth, char *username, char *password) { this->isBasicAuth = enableBasicAuth; this->wwwUsername = username; this->wwwPassword = password; } boolean WebServer::authentication() { if (this->isBasicAuth && (strlen(this->wwwUsername) >= 1 && strlen(this->wwwPassword) >= 1)) { return this->serverHandle->authenticate(this->wwwUsername, this->wwwPassword); } return true; // Authentication not required } void WebServer::handleWifiReset() { if (!this->authentication()) { return this->serverHandle->requestAuthentication(); } //WiFiManager //Local intialization. Once its business is done, there is no need to keep it around this->redirectHome(); WiFiManager wifiManager; wifiManager.resetSettings(); ESP.restart(); } void WebServer::handleSystemReset() { if (!this->authentication()) { return this->serverHandle->requestAuthentication(); } this->debugHandle->printLn("Reset System Configuration"); //if (SPIFFS.remove(CONFIG)) { this->redirectHome(); ESP.restart(); //} } int8_t WebServer::getWifiQuality() { int32_t dbm = WiFi.RSSI(); if(dbm <= -100) { return 0; } else if(dbm >= -50) { return 100; } else { return 2 * (dbm + 100); } } String WebServer::zeroPad(int value) { String rtnValue = String(value); if (value < 10) { rtnValue = "0" + rtnValue; } return rtnValue; }