// // FILE: WebServer.cpp // PURPOSE: Web Server offering online configuration changes and editing // // #include "WebServer.h" #include #include #include #include #include #include #include #ifdef ESP8266 extern "C" { #include "user_interface.h" } #endif const char * const PROGMEM webConfig[] = {"showConfig:boolean:true", "xxx:int:4", "yyy:int:64"}; static Battery battery; static size_t lastHeap = 0; void WebServer::handleBattery() { int value = battery.getPercentage(); String output = String(F("{\"battery\":")) + value + F("}"); server.send(200, contentType_textJson, output); logHeap(); } void WebServer::handleGetClockStatus() { String output = String(F("{\"result\":\"ok\",")); output += "\"clockname\":\"" + clockServer.getName() + "\", \"time\":\"" + clockServer.getClockString() + "\""; output += "}"; server.send(200, contentType_textJson, output); logHeap(); } void WebServer::handleClockActivate() { clockServer.activateClock(); String output = String(F("{\"result\":\"ok\"}")); server.send(200, contentType_textJson, output); logHeap(); } void WebServer::handleClockDeactivate() { clockServer.deactivateClock(); String output = String(F("{\"result\":\"ok\"}")); server.send(200, contentType_textJson, output); logHeap(); } void WebServer::handleClockStatus() { String json = "{"; json += "\"heap\":" + String(ESP.getFreeHeap()); json += ",\"battery\":" + String(battery.getPercentage()); json += ",\"clockserver_name\":\"" + clockServer.getName() + "\""; json += ",\"clockserver_clock\":\"" + clockServer.getClockString() + "\""; json += ",\"clockserver_hours\":\"" + String(clockServer.getHours()) + "\""; json += ",\"clockserver_minutes\":\"" + String(clockServer.getMinutes()) + "\""; json += ",\"clockserver_seconds\":\"" + String(clockServer.getSeconds()) + "\""; json += ",\"clockserver_speed\":\"" + String(clockServer.getSpeed(),1) + "\""; json += ",\"clockserver_text\":\"" + clockServer.getText() + "\""; json += ",\"clockserver_active\":\"" + (clockServer.isActive()? String("true"):String("false")) + "\""; json += "}"; server.send(200, contentType_textJson, json); json = String(); } void WebServer::begin() { GenericWebServer::begin(); config.begin("web.cfg", webConfig, sizeof(webConfig)/sizeof(webConfig[0])); logHeap(); server.on("/all", HTTP_GET, [this](){ String json = "{"; json += "\"heap\":" + String(ESP.getFreeHeap()); json += ",\"battery\":" + String(battery.getPercentage()); json += ",\"clockserver_name\":\"" + clockServer.getName() + "\""; json += ",\"clockserver_clock\":\"" + clockServer.getClockString() + "\""; json += ",\"clockserver_hours\":\"" + String(clockServer.getHours()) + "\""; json += ",\"clockserver_minutes\":\"" + String(clockServer.getMinutes()) + "\""; json += ",\"clockserver_seconds\":\"" + String(clockServer.getSeconds()) + "\""; json += ",\"clockserver_speed\":\"" + String(clockServer.getSpeed(),1) + "\""; json += ",\"clockserver_text\":\"" + clockServer.getText() + "\""; json += ",\"clockserver_active\":\"" + (clockServer.isActive()? String("true"):String("false")) + "\""; json += "}"; debug.outln(json); server.send(200, contentType_textJson, json); json = String(); }); server.on("/clock/start", HTTP_GET, [this](){ handleClockActivate(); }); server.on("/clock/stop", HTTP_GET, [this](){ handleClockDeactivate(); }); server.on("/clock/status", HTTP_GET, [this](){ handleClockStatus(); }); server.on("/battery", HTTP_GET, [this](){ handleBattery();}); server.on("/inline", HTTP_GET, []() { server.send(200, contentType_textPlain, F("this works as well")); } ); server.begin(); debug.outln(F("HTTP server started"), DEBUG_MIN_INFO); logHeap(); } void WebServer::loop() { GenericWebServer::loop(); if (lastHeap > 0) { debug.out(lastHeap); debug.outln(F("< heap on recent call")); lastHeap = 0; } }