#include "WebServer.h" static const char WEB_ACTIONS[] PROGMEM = " Home" " Configure" " Weather" " Reset Settings" " Forget WiFi" " Firmware Update" " About"; String CHANGE_FORM = ""; // moved to config to make it dynamic 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 = "" ""; static const char LANG_OPTIONS[] PROGMEM = "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""; static const char COLOR_THEMES[] PROGMEM = "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""; WebServer::WebServer(GlobalDataController *globalDataController, DebugController *debugController) { this->globalDataController = globalDataController; this->debugController = debugController; } void WebServer::setup() { static WebServer* obj = this; this->server = new ESP8266WebServer(this->globalDataController->getWebserverPort()); this->serverUpdater = new ESP8266HTTPUpdateServer(); this->server->on("/", []() { obj->displayPrinterStatus(); }); this->server->on("/systemreset", []() { obj->handleSystemReset(); }); this->server->on("/forgetwifi", []() { obj->handleWifiReset(); }); this->server->on("/updateconfig", []() { obj->handleUpdateConfig(); }); this->server->on("/updateweatherconfig", []() { obj->handleUpdateWeather(); }); this->server->on("/configure", []() { obj->handleConfigure(); }); this->server->on("/configureweather", []() { obj->handleWeatherConfigure(); }); this->server->onNotFound([]() { obj->redirectHome(); }); this->serverUpdater->setup(this->server, "/update", this->globalDataController->getWebserverUsername(), this->globalDataController->getWebserverPassword()); // Start the server this->server->begin(); this->debugController->printLn("Server started"); } void WebServer::handleClient() { this->server->handleClient(); } boolean WebServer::authentication() { if (this->globalDataController->getWebserverIsBasicAuth() && (this->globalDataController->getWebserverUsername().length() >= 1 && this->globalDataController->getWebserverPassword().length() >= 1) ) { return this->server->authenticate( this->globalDataController->getWebserverUsername().c_str(), this->globalDataController->getWebserverPassword().c_str() ); } return true; // Authentication not required } void WebServer::redirectHome() { // Send them back to the Root Directory this->server->sendHeader("Location", String("/"), true); this->server->sendHeader("Cache-Control", "no-cache, no-store"); this->server->sendHeader("Pragma", "no-cache"); this->server->sendHeader("Expires", "-1"); this->server->send(302, "text/plain", ""); this->server->client().stop(); } void WebServer::displayPrinterStatus() { this->globalDataController->ledOnOff(true); BasePrinterClient *printerClient = this->globalDataController->getPrinterClient(); String html = ""; this->server->sendHeader("Cache-Control", "no-cache, no-store"); this->server->sendHeader("Pragma", "no-cache"); this->server->sendHeader("Expires", "-1"); this->server->setContentLength(CONTENT_LENGTH_UNKNOWN); this->server->send(200, "text/html", ""); this->server->sendContent(String(getHeader(true))); String displayTime = this->globalDataController->getTimeClient()->getAmPmHours() + ":" + this->globalDataController->getTimeClient()->getMinutes() + ":" + this->globalDataController->getTimeClient()->getSeconds() + " " + this->globalDataController->getTimeClient()->getAmPm(); if (this->globalDataController->getClockIs24h()) { displayTime = this->globalDataController->getTimeClient()->getHours() + ":" + this->globalDataController->getTimeClient()->getMinutes() + ":" + this->globalDataController->getTimeClient()->getSeconds(); } html += "";
if (printerClient->getPrinterType() == "Repetier") {
html += "Printer Name: " + printerClient->getPrinterName() + "
";
} else {
html += "Host Name: " + this->globalDataController->getPrinterHostName() + "
";
}
if (printerClient->getError() != "") {
html += "Status: Offline
";
html += "Reason: " + printerClient->getError() + "
";
} else {
html += "Status: " + printerClient->getState();
if (printerClient->isPSUoff() && this->globalDataController->hasPrinterPsu()) {
html += ", PSU off";
}
html += "
";
}
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 += "
Please Configure Weather API
"; if (weatherClient.getError() != "") { html += "Weather Error: " + weatherClient.getError() + "
"; } } else { html += "";
html += weatherClient.getCondition(0) + " (" + weatherClient.getDescription(0) + ")
";
html += weatherClient.getTempRounded(0) + getTempSymbol(true) + "
";
html += " Map It!
";
html += "