// 
// EnergyWebServer.cpp
// 

#include "EnergyWebServer.h"
#include "Relais.h"
#include "EnergySensor.h"

void EnergyWebServerClass::begin(EMSettingsClass& settings, String authenticationUsername, String authenticationPassword,
                                 String authenticationRealm) {
  rewrite("/", "/index.html");          // root auf 'index.html' umleiten
  rewrite("/index.htm", "/index.html"); // 'index.htm' auf 'index.html' umleiten

  // Handler registrieren für ...
  on("/data", HTTP_ANY, [this](AsyncWebServerRequest *request) { handleData(request); }); // ... HTTP-Interface '/data'
  on("/index.html", HTTP_ANY, [this](AsyncWebServerRequest *request) { handleIndexHTML(request); }); // ... 'index.html'

  // Die inkludierten Dateien mit Lebensdauer versehen:
  serveStatic("/ein.png", SPIFFS, "/ein.png").setCacheControl("max-age=3600");
  serveStatic("/aus.png", SPIFFS, "/aus.png").setCacheControl("max-age=3611");
  serveStatic("/broken.png", SPIFFS, "/broken.png").setCacheControl("max-age=3622");
  serveStatic("/leftHandle.png", SPIFFS, "/leftHandle.png").setCacheControl("max-age=3633");
  serveStatic("/rightHandle.png", SPIFFS, "/rightHandle.png").setCacheControl("max-age=3644");
  serveStatic("/reset.png", SPIFFS, "/reset.png").setCacheControl("max-age=3655");
  serveStatic("/em-min.js", SPIFFS, "/em-min.js").setCacheControl("max-age=3666");

  serveStatic("/", SPIFFS, "/");   // Alle sonstigen Anfagen aus dem SPIFFS bedienen

  // Webserver starten
  UrsAsyncWebServerClass::begin(&settings, authenticationUsername, authenticationPassword, authenticationRealm);
  EnergyMeterLog->println(F("Web-Server gestartet"));
} // begin

EnergyWebServerClass EnergyWebServer;

