Exemple #1
1
void returnOK() {
  server.sendHeader("Connection", "close");
  server.sendHeader("Access-Control-Allow-Origin", "*");
  server.send(200, "text/plain", "");
}
Exemple #2
0
void MyFunctionRequestHandler::infoHandler(ESP8266WebServer& server) {

  String json = F("{");
  json += '"'; json += F("heap"); json += '"';
  json += ':';
  json += '"'; json +=  String(ESP.getFreeHeap()); json += '"';
  json += F(", "); 
  json += '"'; json += F("ssid"); json += '"';
  json += ':';
  json += '"'; json +=  String(WiFi.SSID()); json += '"';
  json += F(", "); 
  json += '"'; json += F("ipaddress"); json += '"';
  json += ':';
  json += '"'; json +=  WiFi.localIP().toString(); json += '"';
  json += F(", "); 
  json += '"'; json += F("ipgateway"); json += '"';
  json += ':';
  json += '"'; json +=  WiFi.gatewayIP().toString(); json += '"';
  json += F(", "); 
  json += '"'; json += F("ipdns"); json += '"';
  json += ':';
  json += '"'; json += WiFi.dnsIP().toString(); json += '"';
  json += F(", "); 
  json += '"'; json += F("ipsubnetmask"); json += '"';
  json += ':';
  json += '"'; json +=  WiFi.subnetMask().toString(); json += '"';
  json += F(", "); 
  json += '"'; json += F("macaddress"); json += '"';
  json += ':';
  json += '"'; json +=  WiFi.macAddress(); json += '"';
  json += F(", "); 
  json += '"'; json += F("hostname"); json += '"';
  json += ':';
  json += '"'; json +=  WiFi.hostname(); json += '"';
  json += F(", "); 
  json += '"'; json += F("apipaddress"); json += '"';
  json += ':';
  json += '"'; json +=  WiFi.softAPIP().toString(); json += '"';
  json += F(", "); 
  json += '"'; json += F("apconnectedstations"); json += '"';
  json += ':';
  json += '"'; json += String(WiFi.softAPgetStationNum()); json += '"';
  json += F(", "); 
  json += '"'; json += F("wifiautoconnect"); json += '"';
  json += ':';
  json += '"'; json += String(WiFi.getAutoConnect()); json += '"';
  json += F(", "); 
  json += '"'; json += F("firmwareversion"); json += '"';
  json += ':';
  json += '"'; json += String(FIRMWARE_VERSION); json += '"';
  json += '}';
  
  server.sendHeader(F("cache-control"), F("private, max-age=0, no-cache, no-store"));
  server.send(200, F("text/json"), json);
}
Exemple #3
0
bool  MyFunctionRequestHandler::handleStatic(ESP8266WebServer& server, String& actualUrl, const __FlashStringHelper* url, const __FlashStringHelper* staticFile, const __FlashStringHelper* contentType, bool mayCache){
  if (actualUrl.equals(String(url))){
    File file = SPIFFS.open(String(staticFile), String(F("r")).c_str());
    if (file){
      if (!mayCache){
        server.sendHeader(F("Cache-Control"), F("private, max-age=0, no-cache, no-store"));
      }
      server.streamFile(file, contentType);
      //server.send(200, String(F("text/html")).c_str());
      //server.client().write(file, 100);
      file.close();
      return true;
    }
  }

  return false;
}
Exemple #4
0
void MyFunctionRequestHandler::httpReboot(ESP8266WebServer& server, String message) {
  String response = F("<!DOCTYPE html>"
                    "<html>"
                    "<head>"
                    "<title>Dynatrace UFO configuration changed. Rebooting now... </title>"
                    "<META http-equiv='refresh' content='10;URL=/'>"
                    "</head>"
                    "<body>"
                    "<center>");
  response +=       message;
  response +=      F("Device is being rebooted. Redirecting to homepage in 10 seconds...</center>"
                   "</body>"
                   "</html>");

  server.sendHeader(F("cache-control"), F("private, max-age=0, no-cache, no-store"));
  server.send(200, F("text/html"), response);

  if (mDebug) Serial.println(F("ESP.restart()"));
  ESP.restart();
}
Exemple #5
0
void MyFunctionRequestHandler::dtIntegrationHandler(ESP8266WebServer& server) {

  String json = F("{");
  json += '"'; json += F("enabled"); json += '"';
  json += ':';
  json += mpConfig->enabled ? F("true") : F("false");
  json += F(", "); 
  json += '"'; json += F("environmentid"); json += '"';
  json += ':';
  json += '"'; json += mpConfig->dynatraceEnvironmentID; json += '"';
  json += F(", "); 
  json += '"'; json += F("apikey"); json += '"';
  json += ':';
  json += '"'; json += mpConfig->dynatraceApiKey; json += '"';
  json += F(", "); 
  json += '"'; json += F("interval"); json += '"';
  json += ':';
  json += '"'; json += mpConfig->pollingIntervalS; json += '"';
  json += '}';
  
  server.sendHeader(F("cache-control"), F("private, max-age=0, no-cache, no-store"));
  server.send(200, F("text/json"), json);
}
Exemple #6
0
void Threm::start() {
	_thremContext->isStarted = true;

#ifdef LOG
	LOG << "Threm::start" << endl;
	LOG << "Free heap " << ESP.getFreeHeap() << endl;
#endif

	spiffsOk = SPIFFS.begin();

	if (!spiffsOk) {
#ifdef LOG
		LOG << "SPIFFS failed" << endl;
#endif
	}

	ESP8266WebServer* server = _thremContext->getServer();

	IThremPlugin* plugin;
	PluginMeta* meta;

	for (int i = 0; i < _plugins->size(); i++) {
		plugin = _plugins->get(i);
		bool canEnable = true;
		int pluginId = plugin->getUniqueId();
		{
			String state = getJsonStateFor(pluginId);
			yield();

			StaticJsonBuffer<JSON_BUFFER_SIZE> jsonBuffer;
			JsonObject& root = jsonBuffer.parseObject(state);
			if (!root.success()) {
#ifdef LOG
				LOG << "Failed to parse config for " << String(plugin->getUniqueId()) << endl;
#endif
			}
			else {
				bool isDisabled = root["off"];
				if (isDisabled) {
					canEnable = false;
				}
			}

			if (canEnable) {
				canEnable = plugin->init(_thremContext, root);
			}
		}



		meta = _pluginMeta->get(i);
		meta->isEnabled = canEnable;
#ifdef DEBUG
		DEBUG << String(plugin->getName()) << " enabled=" << canEnable << endl;
#endif
		_pluginMeta->set(i, meta);

		yield();
	}
#ifdef DEBUG
	DEBUG << "Plugins declared: " << _plugins->size() << endl;
#endif

#ifdef LOG
	LOG << getJsonState() << endl;
#endif

#ifdef DEBUG
	DEBUG << "default callbacks setup." << endl;
#endif

	server->onNotFound([=](){
		IThremPlugin*plugin;
		PluginMeta* meta;

		String uri = server->uri();
		for (int i = 0; i < _plugins->size(); i++) {
			plugin = _plugins->get(i);
			meta = _pluginMeta->get(i);
			if (meta->isEnabled) {
				if (plugin->handleNotFound(_thremContext, uri)) {
#ifdef LOG
					LOG << "NotFound handled by: " << plugin->getName() << endl;
#endif
					return;
				}
			}
		}

		String message = "File Not Found\n\n";
		message += "URI: ";
		message += server->uri();
		message += "\nMethod: ";
		message += (server->method() == HTTP_GET) ? "GET" : "POST";
		message += "\nArguments: ";
		message += server->args();
		message += "\n";

		for (uint8_t i = 0; i < server->args(); i++) {
			message += " " + server->argName(i) + ": " + server->arg(i) + "\n";
		}
		server->sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
		server->sendHeader("Pragma", "no-cache");
		server->sendHeader("Expires", "-1");
		server->send(404, "text/plain", message);
	});
#ifdef DEBUG
	DEBUG << "Starting webserver." << endl;
#endif
	server->begin();
#ifdef DEBUG
	DEBUG << "Web server started." << endl;
#endif
	}
Exemple #7
0
void returnFail(String msg) {
  server.sendHeader("Connection", "close");
  server.sendHeader("Access-Control-Allow-Origin", "*");
  server.send(500, "text/plain", msg + "\r\n");
}
Exemple #8
0
void MyFunctionRequestHandler::apiHandler(ESP8266WebServer& server) {
  mpIpDisplay->StopShowingIp();
  //applicationProblems = -1;  //force repaint display in client mode

  // adjust logo brightness (on/off right now)
  if (server.hasArg(F("logo"))) {
    if (server.arg(F("logo")).equals(F("on"))) {
      mpLedstripLogo->setBrightness(255);
    } else if (server.arg(F("logo")).equals(F("default"))) {
       ledsSetLogoDefault();
    } else {
       mpLedstripLogo->setBrightness(0);
    }
  }

  // to quickly set the RGB colors of the logo remotely
  if (server.hasArg(F("logoled"))) {
    byte led = byte(server.arg(F("logoled")).toInt());
    byte r = byte(server.arg(F("r")).toInt());
    byte g = byte(server.arg(F("g")).toInt());
    byte b = byte(server.arg(F("b")).toInt());
    mpLedstripLogo->setPixelColor(led, r, g, b);
  }

  if (server.hasArg(F("top_init"))){
    mpDisplayUpperRing->Init();
  }
  if (server.hasArg(F("top"))){
    String argument = server.arg(F("top"));
    unsigned int i = 0;
    unsigned int ret = 0;
    
    while (i < argument.length())
      i = mpDisplayUpperRing->ParseLedArg(argument, i);
  }
  if (server.hasArg(F("top_bg"))){
    String argument = server.arg(F("top_bg"));
    if (argument.length() == 6)
      mpDisplayUpperRing->SetBackground(argument.substring(2, 4) + argument.substring(0, 2) + argument.substring(4, 6)); 
  }
  if (server.hasArg(F("top_whirl"))){
    String argument = server.arg(F("top_whirl"));
    mpDisplayUpperRing->ParseWhirlArg(argument);
  } 
  if (server.hasArg(F("top_morph"))){
    String argument = server.arg(F("top_morph"));
    mpDisplayUpperRing->ParseMorphArg(argument);
  } 
    
  if (server.hasArg(F("bottom_init"))){
    mpDisplayLowerRing->Init();
  }
  if (server.hasArg(F("bottom"))){
    String argument = server.arg(F("bottom"));
    unsigned int i = 0;
    unsigned int ret = 0;
    
    while (i < argument.length())
      i = mpDisplayLowerRing->ParseLedArg(argument, i);
  }
  if (server.hasArg(F("bottom_bg"))){
    String argument = server.arg(F("bottom_bg"));
    if (argument.length() == 6)
      mpDisplayLowerRing->SetBackground(argument.substring(2, 4) + argument.substring(0, 2) + argument.substring(4, 6)); 
  }
  if (server.hasArg(F("bottom_whirl"))){
    String argument = server.arg(F("bottom_whirl"));
    mpDisplayLowerRing->ParseWhirlArg(argument);
  } 
  if (server.hasArg(F("bottom_morph"))){
    String argument = server.arg(F("bottom_morph"));
    mpDisplayLowerRing->ParseMorphArg(argument);
  } 

  if (server.hasArg(F("dynatrace-environmentid")) && server.hasArg(F("dynatrace-apikey")) && server.hasArg(F("dynatrace-interval"))) {
    if (mDebug) Serial.println(F("Storing Dynatrace SaaS/Managed environment integration settings"));
    mpConfig->enabled = server.hasArg(F("dynatrace-on"));
    mpConfig->dynatraceEnvironmentID = server.arg(F("dynatrace-environmentid"));
    mpConfig->dynatraceApiKey = server.arg(F("dynatrace-apikey"));
    mpConfig->pollingIntervalS = server.arg(F("dynatrace-interval")).toInt();
    mpConfig->SignalChange();
    mpConfig->Write();
    server.sendHeader(F("location"), F("/"));
    server.send(302);
    return;
  }

  if (server.hasArg(F("hostname"))) {
    String newWifiHostname = server.arg(F("hostname"));
    eepromSet(newWifiHostname);
  }

  // note its required to provide both arguments SSID and PWD
  if (server.hasArg(F("ssid")) && server.hasArg(F("pwd"))) {
    String newWifiSSID = server.arg(F("ssid"));
    String newWifiPwd = server.arg(F("pwd"));

    // if SSID is given, also update wifi credentials
    if (newWifiSSID.length()) {
      WiFi.mode(WIFI_STA);
      WiFi.begin(newWifiSSID.c_str(), newWifiPwd.c_str() );
    }

    if (mDebug) {
      Serial.println(String(F("New Wifi settings: ")) + newWifiSSID + F(" / ") + newWifiPwd);
      Serial.println(String(F("Restarting....")));
      Serial.flush();
    }

    httpReboot(server, String(F("New WIFI settings accepted. Mac address: ")) + WiFi.macAddress() + String(F("<p/>")));

  }

 
  server.sendHeader(F("cache-control"), F("private, max-age=0, no-cache, no-store"));
  server.send(200);
}
Exemple #9
0
bool MyFunctionRequestHandler::handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri){
  
  server.client().setNoDelay(true);
  requestUri.toLowerCase();
  
  Serial.println(String(F("handle(")) + String(requestMethod) + String(F(", ")) + requestUri + String(F(")")));
   
  if (requestMethod == HTTP_GET){

    if (requestUri.equals(F("/")) || requestUri.equals(F("/index.html"))){
      server.sendHeader(F("cache-control"), F("private, max-age=0, no-cache, no-store"));
      server.sendHeader(F("Content-Encoding"), F("gzip"));
      server.send_P(200, String(F("text/html")).c_str(), index_html_gz, sizeof(index_html_gz));
      return true;
    }
    if (requestUri.equals(F("/font.woff"))){
      server.send_P(200, String(F("text/html")).c_str(), font_woff, sizeof(font_woff));
      return true;
    }
    /*if (handleStatic(server, requestUri, F("/"), F("/index_comb.html.gz"), F("text/html"), false)) return true;
    if (handleStatic(server, requestUri, F("/"), F("/index.html"), F("text/html"), false)) return true;
    if (handleStatic(server, requestUri, F("/index.html"), F("/index_comb.html.gz"), F("text/html"), false)) return true;
    if (handleStatic(server, requestUri, F("/index.html"), F("/index.html"), F("text/html"), false)) return true;
    if (handleStatic(server, requestUri, F("/phonon.min.css"), F("/phonon.min.css"), F("text/css"), true)) return true;
    if (handleStatic(server, requestUri, F("/phonon.min.js"), F("/phonon.min.js"), F("application/javascript"), true)) return true;
    if (handleStatic(server, requestUri, F("/forms.min.css"), F("/forms.min.css"), F("text/css"), true)) return true;
    if (handleStatic(server, requestUri, F("/forms.min.js"), F("/forms.min.js"), F("application/javascript"), true)) return true;
    if (handleStatic(server, requestUri, F("/icons.min.css"), F("/icons.min.css"), F("text/css"), true)) return true;
    if (handleStatic(server, requestUri, F("/lists.min.css"), F("/lists.min.css"), F("text/css"), true)) return true;
    if (handleStatic(server, requestUri, F("/phonon-base.min.css"), F("/phonon-base.min.css"), F("text/css"), true)) return true;
    if (handleStatic(server, requestUri, F("/phonon-core.min.js"), F("/phonon-core.min.js"), F("application/javascript"), true)) return true;
    if (handleStatic(server, requestUri, F("/font.woff"), F("/font.woff"), F("application/font.woff"), true)) return true;
    if (handleStatic(server, requestUri, F("/font.eot"), F("/font.eot"), F("application/font.eot"), true)) return true;
    if (handleStatic(server, requestUri, F("/font.svg"), F("/font.svg"), F("application/font.svg"), true)) return true;
    if (handleStatic(server, requestUri, F("/font.ttf"), F("/font.ttf"), F("application/font.ttf"), true)) return true;
    */
  
    if (requestUri.startsWith(F("/api"))){
      apiHandler(server);
      return true;
    }
    if (requestUri.startsWith(F("/info"))){
      infoHandler(server);
      return true;
    }
    if (requestUri.startsWith(F("/dtintinfo"))){
      dtIntegrationHandler(server);
      return true;
    }
  }
  else if (requestMethod == HTTP_POST){
    if (requestUri.startsWith(F("/update"))){

      StreamString error;
      if (Update.hasError()) {
        Update.printError(error);
      }
      if (mbRebootRequired)
        httpReboot(server, (Update.hasError()) ? error : String(F("Upload succeeded! ")) + String(miUploadSize) + String(F(" bytes transferred<p>")));
      else{
         server.sendHeader(F("location"), F("/"));
         server.send(302);
         return true;
      }
    }
    
    if (requestUri.equals(F("/dynatrace"))){
      dynatracePostHandler(server);
      return true;
    }
  }
  
  //Now we really need to respond with 404
  String message = F("File Not Found\n\n");
  message += F("URI: ");
  message += server.uri();
  message += F("\nMethod: ");
  message += (server.method() == HTTP_GET ) ? F("GET") : F("POST");
  message += F("\nArguments: ");
  message += server.args();
  message += F("\n");

  for ( uint8_t i = 0; i < server.args(); i++ ) {
    message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n";
  }
  server.sendHeader(F("cache-control"), F("private, max-age=0, no-cache, no-store"));
  server.send (404, F("text/plain"), message);
  
  return true;
}