RRFs::RRFs(ESP8266WebServer* webserver){ DEBUGPRINT.println("Setting up RRFs"); server=webserver; if(SPIFFS.begin()){ DEBUGPRINT.println("Filesystem started"); }else{ DEBUGPRINT.println("Filesystem failed"); } //Setup pages: //SERVER INIT //list directory server->on("/fs/list/", HTTP_GET, std::bind(&RRFs::handleFileList,this)); //load editor server->on("/fs/edit/", HTTP_GET, [&](){ if(!handleFileRead("/edit.htm")) server->send(404, "text/plain", "FileNotFound"); }); //create file server->on("/fs/edit/", HTTP_PUT, std::bind(&RRFs::handleFileCreate,this)); //delete file server->on("/fs/edit/", HTTP_DELETE, std::bind(&RRFs::handleFileDelete,this)); //first callback is called after the request has ended with all parsed arguments //second callback handles file uploads at that location server->on("/fs/edit/", HTTP_POST, [&](){ server->send(200, "text/plain", ""); },std::bind(&RRFs::handleFileUpload,this)); //called when the url is not defined here //use it to load content from SPIFFS server->onNotFound([&](){ if(!handleFileRead(server->uri())) this->server->send(404, "text/plain", "FileNotFound"); }); }
/* ====================================================================== Function: handleNotFound Purpose : default WEB routing when URI is not found Input : - Output : - Comments: - ====================================================================== */ void handleNotFound(void) { String response = ""; boolean found = false; // Led on LedBluON(); // try to return SPIFFS file found = handleFileRead(server.uri()); // Try Teleinfo ETIQUETTE if (!found) { // We check for an known label ValueList * me = tinfo.getList(); const char * uri; // convert uri to char * for compare uri = server.uri().c_str(); Debugf("handleNotFound(%s)\r\n", uri); // Got at least one and consistent URI ? if (me && uri && *uri=='/' && *++uri ) { // Loop thru the linked list of values while (me->next && !found) { // go to next node me = me->next; //Debugf("compare to '%s' ", me->name); // Do we have this one ? if (stricmp (me->name, uri) == 0 ) { // no need to continue found = true; // Add to respone response += F("{\"") ; response += me->name ; response += F("\":") ; formatNumberJSON(response, me->value); response += F("}\r\n"); } } } // Got it, send json if (found) server.send ( 200, "text/json", response ); } // All trys failed if (!found) { // send error message in plain text String message = F("File Not Found\n\n"); message += F("URI: "); message += server.uri(); message += F("\nMethod: "); message += ( server.method() == HTTP_GET ) ? "GET" : "POST"; message += F("\nArguments: "); message += server.args(); message += FPSTR(FP_NL); for ( uint8_t i = 0; i < server.args(); i++ ) { message += " " + server.argName ( i ) + ": " + server.arg ( i ) + FPSTR(FP_NL); } server.send ( 404, "text/plain", message ); } // Led off LedBluOFF(); }
/* ====================================================================== Function: handleRoot Purpose : handle main page / Input : - Output : - Comments: - ====================================================================== */ void handleRoot(void) { LedBluON(); handleFileRead("/"); LedBluOFF(); }