Skip to content

Commit

Permalink
show success page after firmware update & reboot after initial wifi s…
Browse files Browse the repository at this point in the history
…etup
  • Loading branch information
zivillian committed Jul 30, 2023
1 parent 9578903 commit 2a13553
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 30 deletions.
3 changes: 2 additions & 1 deletion include/pages.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "debug.h"

void setupPages(AsyncWebServer* server, PhaseSwitch *phaseSwitch, Config *config, WiFiManager *wm);
void sendResponseHeader(AsyncResponseStream *response, const char *title);
void sendResponseHeader(AsyncResponseStream *response, const char *title, bool inlineStyle = false);
void sendResponseTrailer(AsyncResponseStream *response);
void sendButton(AsyncResponseStream *response, const char *title, const char *action, const char *css = "");
void sendPostButton(AsyncResponseStream *response, const char *title, const char *action);
Expand All @@ -22,6 +22,7 @@
void sendTableRow(AsyncResponseStream *response, const char *name, String value);
void sendTableRow(AsyncResponseStream *response, const char *name, const char *value);
void sendDebugForm(AsyncResponseStream *response, String slaveId, String reg, String function, String count);
void sendMinCss(AsyncResponseStream *response);
const String ErrorName(Modbus::Error code);
const String WiFiQuality(int rssiValue);
const String ChargingState(uint16_t state);
Expand Down
5 changes: 5 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ void setup() {
digitalWrite(PIN_FACTORY_LED, LOW);

wm.setClass("invert");
auto reboot = false;
wm.setAPCallback([&reboot](WiFiManager *wifiManager){reboot = true;});
wm.autoConnect();
if (reboot){
ESP.restart();
}
MBUlogLvl = LOG_LEVEL_WARNING;
LOGDEVICE = &debugOut;
dbgln("[wifi] finished");
Expand Down
79 changes: 50 additions & 29 deletions src/pages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,24 @@ void setupPages(AsyncWebServer *server, PhaseSwitch *phaseSwitch, Config *config
request->send(response);
});
server->on("/update", HTTP_POST, [](AsyncWebServerRequest *request){
request->onDisconnect([](){
ESP.restart();
});
dbgln("[webserver] OTA finished");
if (Update.hasError()){
auto *response = request->beginResponse(500, "text/plain", "Ota failed");
response->addHeader("Connection", "close");
request->send(response);
}
else{
request->redirect("/");
auto *response = request->beginResponseStream("text/html");
response->addHeader("Connection", "close");
sendResponseHeader(response, "Firmware Update", true);
response->print("<p>Update successful.</p>");
sendButton(response, "Back", "/");
sendResponseTrailer(response);
request->send(response);
}
ESP.restart();
}, [&](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){
dbg("[webserver] OTA progress ");dbgln(index);
if (!index) {
Expand Down Expand Up @@ -286,31 +294,9 @@ void setupPages(AsyncWebServer *server, PhaseSwitch *phaseSwitch, Config *config
}
}
dbgln("[webserver] GET /style.css");
auto *response = request->beginResponse(200, "text/css",
"body{"
"font-family:sans-serif;"
"text-align: center;"
"background: #252525;"
"color: #faffff;"
"}"
"#content{"
"display: inline-block;"
"min-width: 340px;"
"}"
"button{"
"width: 100%;"
"line-height: 2.4rem;"
"background: #1fa3ec;"
"border: 0;"
"border-radius: 0.3rem;"
"font-size: 1.2rem;"
"-webkit-transition-duration: 0.4s;"
"transition-duration: 0.4s;"
"color: #faffff;"
"}"
"button:hover{"
"background: #0e70a4;"
"}"
auto *response = request->beginResponseStream("text/css");
sendMinCss(response);
response->print(
"button.r{"
"background: #d43535;"
"}"
Expand Down Expand Up @@ -340,14 +326,49 @@ void setupPages(AsyncWebServer *server, PhaseSwitch *phaseSwitch, Config *config
});
}

void sendResponseHeader(AsyncResponseStream *response, const char *title){
void sendMinCss(AsyncResponseStream *response){
response->print("body{"
"font-family:sans-serif;"
"text-align: center;"
"background: #252525;"
"color: #faffff;"
"}"
"#content{"
"display: inline-block;"
"min-width: 340px;"
"}"
"button{"
"width: 100%;"
"line-height: 2.4rem;"
"background: #1fa3ec;"
"border: 0;"
"border-radius: 0.3rem;"
"font-size: 1.2rem;"
"-webkit-transition-duration: 0.4s;"
"transition-duration: 0.4s;"
"color: #faffff;"
"}"
"button:hover{"
"background: #0e70a4;"
"}");
}

void sendResponseHeader(AsyncResponseStream *response, const char *title, bool inlineStyle){
response->print("<!DOCTYPE html>"
"<html lang=\"en\" class=\"\">"
"<head>"
"<meta charset='utf-8'>"
"<meta name=\"viewport\" content=\"width=device-width,initial-scale=1,user-scalable=no\"/>");
response->printf("<title>Heidelberg Phase Switch - %s</title>", title);
response->print("<link rel=\"stylesheet\" href=\"style.css\">"
if (inlineStyle){
response->print("<style>");
sendMinCss(response);
response->print("</style>");
}
else{
response->print("<link rel=\"stylesheet\" href=\"style.css\">");
}
response->print(
"</head>"
"<body>"
"<h2>Heidelberg Phase Switch</h2>");
Expand Down

0 comments on commit 2a13553

Please sign in to comment.