/* ================================================================== * * Function: web_index * Description: Static callback function to display the homepage of the web server * Parameters: See Webduino documentation * obj is a pointer to the instance of Server that added the callback * ================================================================== */ void web_index(WebServer &server, WebServer::ConnectionType type, char * c, bool b, void * obj) { server.httpSuccess(); if (type != WebServer::HEAD) { server.printP(control_panel); } }
void CheckLogin(WebServer &rServer, WebServer::ConnectionType Type, char *pchUrlTail, bool bTailComplete) { rServer.httpSuccess(); switch (Type) { case WebServer::GET: SendErrorPage(rServer); break; case WebServer::POST: if (IsValidLogin(rServer)) { ActivateGarageDoor(); SendAccessGrantedPage(rServer); } else SendAccessDeniedPage(rServer); break; // None of these are expected, so we don't respond. case WebServer::INVALID: case WebServer::HEAD: case WebServer::PUT: case WebServer::DELETE: case WebServer::PATCH: default: break; } }
// --------------- Page request handlers void ShowWebRoot(WebServer &rServer, WebServer::ConnectionType Type, char *pchUrlTail, bool bTailComplete) { // Expire grace login period when the home page is shown. PasswordManager.ClearGracePassword(); // We show the password entry page as the web root. Presents a form that asks for a password. rServer.httpSuccess(); switch (Type) { case WebServer::GET: SendLoginPage(rServer); break; case WebServer::POST: SendErrorPage(rServer); break; // None of these are expected, so we don't respond. case WebServer::INVALID: case WebServer::HEAD: case WebServer::PUT: case WebServer::DELETE: case WebServer::PATCH: default: break; } }
/* ================================================================== * * Function: web_input * Description: Static callback function to handle input to the server * Parameters: See Webduino documentation * obj is a pointer to the instance of Server that added the callback * ================================================================== */ void web_input(WebServer &server, WebServer::ConnectionType type, char * c, bool b, void * obj) { if (type == WebServer::POST) { Server * s = (Server *) obj; bool repeat; char name[16], value[16]; do { // Read all POST params, returns false when no more params repeat = server.readPOSTparam(name, 16, value, 16); if (strcmp(name, "visualizer") == 0) { int type = strtol(value, NULL, 10); // Ensure type is valid, default to VISUALIZER_BARS switch (type) { case VISUALIZER_BARS: case VISUALIZER_BARS_MIDDLE: case VISUALIZER_PULSE: case VISUALIZER_PLASMA: case VISUALIZER_RAINBOW: case VISUALIZER_WHEEL: s->set_visualizer(type); break; default: s->set_visualizer(VISUALIZER_BARS); break; } } else if (strcmp(name, "other") == 0) { int type = strtol(value, NULL, 10); // Ensure type is valid, default to BOUNCING_LINES switch (type) { case BOUNCING_LINES: case BAR_TEST: case PIXEL_TEST: case AMBIENT_LIGHTING: s->set_visualizer(type); break; default: s->set_visualizer(BOUNCING_LINES); break; } } else if (strcmp(name, "power") == 0) { s->set_power(strtol(value, NULL, 10)); } } while (repeat); // after procesing the POST data, tell the web browser to reload // the page using a GET method. server.httpSeeOther("/web_input"); return; } /* for a GET or HEAD, send the standard "it's all OK headers" */ server.httpSuccess(); /* we don't output the body for a HEAD request */ if (type == WebServer::GET) { server.printP(control_panel); } }
void RestDhtApi::dht(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) { URLPARAM_RESULT rc; char name[32]; char value[32]; //server.httpSuccess("application/json"); server.httpSuccess(); if (type != WebServer::GET) return; if (strlen(url_tail)) { DHT dht; while (strlen(url_tail)) { rc = server.nextURLparam(&url_tail, name, 32, value, 32); String param = String(name); if (param == "pin") { String vl = value; int v = atoi(vl.c_str()); dht.setup(v); dht.getMinimumSamplingPeriod(); double hum = dht.getHumidity(); double tempC = dht.getTemperature(); double tempF = dht.toFahrenheit(tempC); Serial.println(v); Serial.print(dht.getStatusString()); Serial.print(" - "); Serial.print(hum, 1); Serial.print("% - "); Serial.print(tempC, 1); Serial.print("C - "); Serial.print(tempF, 1); Serial.println("F"); } } } }
void WebManager::pushButtonCmd(WebServer& server, WebServer::ConnectionType type, char* url_tail, bool tail_complete) { #ifdef PRINT_DEBUG_MSGS Serial.println("ajax_action"); #endif if (getInstance()->m_webserver->checkCredentials(IOManager::getInstance()->m_credentialsFile->m_authCredentials)) { if (type == WebServer::POST) { IOManager::getInstance()->m_actionDoorCmd->toggleFor(); } /* for a GET or HEAD, send the standard "it's all OK headers" */ server.httpSuccess(); } else { /* send a 401 error back causing the web browser to prompt the user for credentials */ server.httpUnauthorized(); } }
//---------------------------------------------------------------------------------------------------- // This function is called by webserver (by pointer on function). It manage http request void WebManager::webPageCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) { #ifdef PRINT_DEBUG_MSGS Serial.println("Index"); #endif if (getInstance()->m_webserver->checkCredentials(IOManager::getInstance()->m_credentialsFile->m_authCredentials)) { /* for a GET or HEAD, send the standard "it's all OK headers" */ server.httpSuccess(); /* we don't output the body for a HEAD request */ if (type != WebServer::HEAD) { getInstance()->m_mainPage->print(); } } else { /* send a 401 error back causing the web browser to prompt the user for credentials */ server.httpUnauthorized(); } }
void RestApi::put(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) { URLPARAM_RESULT rc; char name[32]; char value[32]; //server.httpSuccess("application/json"); server.httpSuccess(); if (type != WebServer::PUT) return; if (strlen(url_tail)) { while (strlen(url_tail)) { rc = server.nextURLparam(&url_tail, name, 32, value, 32); String param = String(name); String vl = value; int v = atoi(vl.c_str()); if (v >= 0) { String t = name; char tp = t.charAt(0); String p = t.substring(1, 32); int pin = atoi(p.c_str()); if (tp == 'd') { digitalWrite(pin, v); } else { analogWrite(pin, v); } Serial.print(pin + ":"); Serial.println(v); } } } }
void WebManager::getStateCmd(WebServer& server, WebServer::ConnectionType type, char* url_tail, bool tail_complete) { #ifdef PRINT_DEBUG_MSGS Serial.println("ajax_refresh"); #endif if (getInstance()->m_webserver->checkCredentials(IOManager::getInstance()->m_credentialsFile->m_authCredentials)) { /* for a GET or HEAD, send the standard "it's all OK headers" */ server.httpSuccess(); server.print("{'state': "); server.print(DoorStateManager::getInstance()->m_currentState->getId()); server.print(", 'obstacle': "); server.print(DoorStateManager::getInstance()->isObstacleDetected() ? "true" : "false"); server.print(", 'forgottenOpened': "); server.print(DoorStateManager::getInstance()->isForgottenOpenedDoor() ? "true" : "false"); server.print('}'); } else { /* send a 401 error back causing the web browser to prompt the user for credentials */ server.httpUnauthorized(); } }
/* commands are functions that get called by the webserver framework * they can read any posted data from client, and they output to the * server to send data back to the web browser. */ void helloCmd(WebServer &server, WebServer::ConnectionType type, char *, bool) { /* this line sends the standard "we're all OK" headers back to the browser */ server.httpSuccess(); /* if we're handling a GET or POST, we can output our data here. For a HEAD request, we just stop after outputting headers. */ if (type != WebServer::HEAD) { /* this defines some HTML text in read-only memory aka PROGMEM. * This is needed to avoid having the string copied to our limited * amount of RAM. */ const char *helloMsg = "<html><body><h1>Hello, Costanza!</h1></body></html>"; /* this is a special form of print that outputs from PROGMEM */ server.print(helloMsg); } }
void RestApi::get(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) { URLPARAM_RESULT rc; char name[32]; char value[32]; //server.httpSuccess("application/json"); server.httpSuccess(); if (type != WebServer::GET) return; if (strlen(url_tail)) { while (strlen(url_tail)) { rc = server.nextURLparam(&url_tail, name, 32, value, 32); String param = String(name); if (param == "pin") { String t = value; char tp = t.charAt(0); String p = t.substring(1, 32); int pin = atoi(p.c_str()); if (tp == 'd') { Serial.println(digitalRead(pin)); } else { Serial.println(analogRead(pin)); } } } } }