Beispiel #1
0
/**
 * Initialize web server and add requests
 */
void webserver_start()
{
  // not found
  g_server.onNotFound(handleNotFound);

#ifdef CAPTIVE_PORTAL
  // handle captive requests
  g_server.addHandler(new CaptiveRequestHandler()).setFilter(ON_AP_FILTER);
#endif

  // CDN
  g_server.on("/js/jquery-2.1.4.min.js", HTTP_GET, [](AsyncWebServerRequest *request) {
    request->redirect(F("http://code.jquery.com/jquery-2.1.4.min.js"));
  }).setFilter(ON_STA_FILTER);
  g_server.on("/css/foundation.min.css", HTTP_GET, [](AsyncWebServerRequest *request) {
    request->redirect(F("http://cdnjs.cloudflare.com/ajax/libs/foundation/6.2.3/foundation.min.css"));
  }).setFilter(ON_STA_FILTER);

  // GET
  g_server.on("/api/status", HTTP_GET, handleGetStatus);
  g_server.on("/api/plugins", HTTP_GET, handleGetPlugins);
  g_server.on("/api/scan", HTTP_GET, handleWifiScan);

  // POST
  g_server.on("/settings", HTTP_POST, handleSettings);
  g_server.on("/restart", HTTP_POST, [](AsyncWebServerRequest *request) {
    // AsyncWebServerResponse *response = request->beginResponse(302);
    // response->addHeader("Location", net_hostname + ".local");
    // request->send(response);

    request->send(200, F(CONTENT_TYPE_HTML), F("<!DOCTYPE html><html><head><meta http-equiv=\"refresh\" content=\"5; url=/\"></head><body>Restarting...<br/><img src=\"/img/loading.gif\"></body></html>"));
    requestRestart();
  });

  // make sure config.json is not served!
  g_server.on("/config.json", HTTP_GET, [](AsyncWebServerRequest *request) {
    request->send(400);
  });

  // catch-all
  g_server.serveStatic("/", SPIFFS, "/", CACHE_HEADER).setDefaultFile("index.html");

  // sensor api
  registerPlugins();

#ifdef SPIFFS_EDITOR
  g_server.addHandler(new SPIFFSEditor());
#endif

#ifdef BROWSER_EVENTS
  g_server.addHandler(&g_events);
#endif

  // start server
  g_server.begin();
}
//------------------------------------------
void WebDS18B20Buses::InitWebServer(AsyncWebServer &server) {

  server.on("/getList", HTTP_GET, [this](AsyncWebServerRequest * request) {

    //check DS18B20Buses is initialized
    if (!_initialized) {
      request->send(400, F("text/html"), F("Buses not Initialized"));
      return;
    }

    //check bus param is there
    if (!request->hasParam(F("bus"))) {
      request->send(400, F("text/html"), F("Missing parameter"));
      return;
    }
    //convert busNumber
    int busNumber = request->getParam(F("bus"))->value().toInt();
    //check value found
    if ((busNumber == 0 && request->getParam(F("bus"))->value() != "0") || busNumber >= _nbOfBuses) {
      request->send(400, F("text/html"), F("Incorrect bus number"));
      return;
    }

#if ESP01_PLATFORM
    Serial.flush();
    delay(5);
    Serial.end();
#endif

    //list OneWire Temperature sensors
    request->send(200, F("text/json"), DS18B20Bus(_owBusesPins[busNumber][0], _owBusesPins[busNumber][1]).GetRomCodeListJSON());

#if ESP01_PLATFORM
    Serial.begin(SERIAL_SPEED);
#endif
  });

  server.on("/getTemp", HTTP_GET, [this](AsyncWebServerRequest * request) {

    //check DS18B20Buses is initialized
    if (!_initialized) {
      request->send(400, F("text/html"), F("Buses not Initialized"));
      return;
    }

    //check bus param
    if (!request->hasParam(F("bus"))) {
      request->send(400, F("text/html"), F("Missing parameter"));
      return;
    }
    //convert busNumber
    int busNumber = request->getParam(F("bus"))->value().toInt();
    //check value found
    if ((busNumber == 0 && request->getParam(F("bus"))->value() != "0") || busNumber >= _nbOfBuses) {
      request->send(400, F("text/html"), F("Incorrect bus number"));
      return;
    }


    //check ROMCode param
    if (!request->hasParam(F("ROMCode"))) {
      request->send(400, F("text/html"), F("Missing ROMCode"));
      return;
    }

    const char* ROMCodeA = request->getParam(F("ROMCode"))->value().c_str();

    if (!isROMCodeString(ROMCodeA)) {
      request->send(400, F("text/html"), F("Incorrect ROMCode"));
      return;
    }

    //Parse ROMCode
    byte romCode[8];
    for (byte i = 0; i < 8; i++) {
      romCode[i] = (AsciiToHex(ROMCodeA[i * 2]) * 0x10) + AsciiToHex(ROMCodeA[(i * 2) + 1]);
    }

#if ESP01_PLATFORM
    Serial.flush();
    delay(5);
    Serial.end();
#endif

    //Read Temperature
    String temperatureJSON = DS18B20Bus(_owBusesPins[busNumber][0], _owBusesPins[busNumber][1]).GetTempJSON(romCode);

    if (temperatureJSON.length() > 0) request->send(200, F("text/json"), temperatureJSON);
    else request->send(500, F("text/html"), F("Read sensor failed"));

#if ESP01_PLATFORM
    Serial.begin(SERIAL_SPEED);
#endif
  });

  server.on("/gs1", HTTP_GET, [this](AsyncWebServerRequest * request) {
    request->send(200, F("text/json"), GetStatus());
  });
}