2022-01-10 00:55:04 +01:00
|
|
|
#include "webui.h"
|
2022-01-07 21:02:27 +01:00
|
|
|
|
2022-02-10 22:33:52 +01:00
|
|
|
AsyncWebServer webServer(80);
|
2022-01-09 20:51:16 +01:00
|
|
|
|
2022-02-10 22:33:52 +01:00
|
|
|
const char *PARAM_MESSAGE = "message";
|
2022-01-07 21:02:27 +01:00
|
|
|
|
2022-02-10 22:33:52 +01:00
|
|
|
String processor(const String &var);
|
2022-02-15 20:58:32 +01:00
|
|
|
void WebserverPOST_Callback(AsyncWebServerRequest *request);
|
2022-02-10 22:33:52 +01:00
|
|
|
void WebserverNotFound_Callback(AsyncWebServerRequest *request);
|
2022-02-15 20:58:32 +01:00
|
|
|
void Webserver_Callback(AsyncWebServerRequest *request);
|
2022-01-19 22:23:36 +01:00
|
|
|
|
2022-02-10 22:33:52 +01:00
|
|
|
void initWebUI()
|
2022-01-08 03:14:26 +01:00
|
|
|
{
|
2022-02-10 22:33:52 +01:00
|
|
|
if (!LittleFS.begin())
|
2022-01-08 03:14:26 +01:00
|
|
|
{
|
2022-02-10 22:33:52 +01:00
|
|
|
Serial.println("An Error has occurred while mounting LittleFS");
|
|
|
|
return;
|
2022-01-09 20:51:16 +01:00
|
|
|
}
|
|
|
|
|
2022-02-15 20:58:32 +01:00
|
|
|
webServer.serveStatic("/static/", LittleFS, "/static/");
|
2022-02-27 22:23:58 +01:00
|
|
|
webServer.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
|
|
|
|
{ request->redirect("/index.htm"); });
|
2022-02-10 22:33:52 +01:00
|
|
|
webServer.onNotFound(WebserverNotFound_Callback);
|
2022-02-15 20:58:32 +01:00
|
|
|
webServer.on("/index.htm", HTTP_GET, Webserver_Callback);
|
2022-02-27 22:23:58 +01:00
|
|
|
webServer.on("/post.htm", HTTP_POST, WebserverPOST_Callback);
|
2022-01-09 20:51:16 +01:00
|
|
|
|
2022-02-10 22:33:52 +01:00
|
|
|
webServer.begin();
|
2022-01-07 21:02:27 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 22:33:52 +01:00
|
|
|
void UpdateWebUI()
|
2022-01-19 22:23:36 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-02-10 22:33:52 +01:00
|
|
|
String processor(const String &var)
|
2022-01-08 03:14:26 +01:00
|
|
|
{
|
2022-02-15 20:58:32 +01:00
|
|
|
if (var == "TANK_REMAIN_CAPACITY")
|
|
|
|
return String((PersistenceData.tankRemain_µl / 10) / LubeConfig.tankCapacity_ml);
|
|
|
|
if (var == "LUBE_DISTANCE_NORMAL")
|
|
|
|
return String(LubeConfig.DistancePerLube_Default);
|
|
|
|
if (var == "LUBE_DISTANCE_RAIN")
|
|
|
|
return String(LubeConfig.DistancePerLube_Rain);
|
|
|
|
if (var == "TANK_CAPACITY")
|
|
|
|
return String(LubeConfig.tankCapacity_ml);
|
|
|
|
if (var == "AMOUNT_PER_DOSE")
|
|
|
|
return String(LubeConfig.amountPerDose_µl);
|
|
|
|
if (var == "TANK_REMIND")
|
|
|
|
return String(LubeConfig.TankRemindAtPercentage);
|
|
|
|
if (var == "PULSE_PER_REV")
|
|
|
|
return String(LubeConfig.PulsePerRevolution);
|
|
|
|
if (var == "TIRE_WIDTH_MM")
|
|
|
|
return String(LubeConfig.TireWidth_mm);
|
|
|
|
if (var == "TIRE_RATIO")
|
|
|
|
return String(LubeConfig.TireWidthHeight_Ratio);
|
|
|
|
if (var == "RIM_DIAMETER")
|
|
|
|
return String(LubeConfig.RimDiameter_Inch);
|
|
|
|
if (var == "DISTANCE_PER_REV")
|
|
|
|
return String(LubeConfig.DistancePerRevolution_mm);
|
|
|
|
if (var == "BLEEDING_PULSES")
|
|
|
|
return String(LubeConfig.BleedingPulses);
|
|
|
|
if (var == "SPEED_SOURCE")
|
|
|
|
return String(SpeedSourceString[LubeConfig.SpeedSource]);
|
|
|
|
if (var == "GPS_BAUD")
|
|
|
|
return String(GPSBaudRateString[LubeConfig.GPSBaudRate]);
|
|
|
|
#if PCB_REVISION == 13
|
|
|
|
if (var == "CAN_SOURCE")
|
|
|
|
return String(CANSourceString[LubeConfig.CANSource]);
|
|
|
|
#endif
|
|
|
|
if (var == "CONFIG_CHECKSUM")
|
2022-03-08 21:23:52 +01:00
|
|
|
{
|
|
|
|
char buffer[7];
|
|
|
|
sprintf(buffer, "0x%04X", LubeConfig.checksum);
|
|
|
|
return String(buffer);
|
|
|
|
}
|
2022-02-15 20:58:32 +01:00
|
|
|
if (var == "WRITE_CYCLE_COUNT")
|
|
|
|
return String(PersistenceData.writeCycleCounter);
|
|
|
|
if (var == "TANK_REMAIN_UL")
|
|
|
|
return String(PersistenceData.tankRemain_µl);
|
|
|
|
if (var == "TRAVEL_DISTANCE_HIGHRES")
|
|
|
|
return String(PersistenceData.TravelDistance_highRes);
|
|
|
|
if (var == "PERSISTANCE_CHECKSUM")
|
2022-03-08 21:23:52 +01:00
|
|
|
{
|
|
|
|
char buffer[7];
|
|
|
|
sprintf(buffer, "0x%04X", PersistenceData.checksum);
|
|
|
|
return String(buffer);
|
|
|
|
}
|
2022-02-15 20:58:32 +01:00
|
|
|
if (var == "SHOW_IMPULSE_SETTINGS")
|
|
|
|
return LubeConfig.SpeedSource == SOURCE_IMPULSE ? "" : "hidden";
|
|
|
|
if (var == "SHOW_CAN_SETTINGS")
|
|
|
|
return LubeConfig.SpeedSource == SOURCE_CAN ? "" : "hidden";
|
|
|
|
if (var == "SHOW_GPS_SETTINGS")
|
|
|
|
return LubeConfig.SpeedSource == SOURCE_GPS ? "" : "hidden";
|
|
|
|
|
|
|
|
if (var == "SOURCE_SELECT_OPTIONS")
|
|
|
|
{
|
|
|
|
String temp;
|
|
|
|
for (uint32_t i = 0; i < SpeedSourceString_Elements; i++)
|
2022-02-27 22:23:58 +01:00
|
|
|
{
|
|
|
|
String selected = LubeConfig.SpeedSource == i ? " selected " : "";
|
|
|
|
temp = temp + "<option value=\"" + i + "\"" + selected + ">" + SpeedSourceString[i] + "</option>";
|
|
|
|
}
|
2022-02-15 20:58:32 +01:00
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (var == "CANSOURCE_SELECT_OPTIONS")
|
|
|
|
{
|
|
|
|
String temp;
|
|
|
|
for (uint32_t i = 0; i < CANSourceString_Elements; i++)
|
2022-02-27 22:23:58 +01:00
|
|
|
{
|
|
|
|
String selected = LubeConfig.CANSource == i ? " selected " : "";
|
|
|
|
temp = temp + "<option value=\"" + i + "\"" + selected + ">" + CANSourceString[i] + "</option>";
|
|
|
|
}
|
2022-02-15 20:58:32 +01:00
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
if (var == "GPSBAUD_SELECT_OPTIONS")
|
|
|
|
{
|
|
|
|
String temp;
|
|
|
|
for (uint32_t i = 0; i < GPSBaudRateString_Elements; i++)
|
2022-02-27 22:23:58 +01:00
|
|
|
{
|
|
|
|
String selected = LubeConfig.GPSBaudRate == i ? " selected " : "";
|
|
|
|
temp = temp + "<option value=\"" + i + "\"" + selected + ">" + GPSBaudRateString[i] + "</option>";
|
|
|
|
}
|
2022-02-15 20:58:32 +01:00
|
|
|
return temp;
|
|
|
|
}
|
2022-01-09 20:51:16 +01:00
|
|
|
|
2022-02-17 09:17:16 +01:00
|
|
|
if (var == "SYSTEM_STATUS")
|
|
|
|
return String(globals.systemStatustxt);
|
|
|
|
|
2022-02-10 22:33:52 +01:00
|
|
|
if (var == "PLACEHOLDER")
|
|
|
|
return "placeholder";
|
2022-01-09 20:51:16 +01:00
|
|
|
|
2022-02-10 22:33:52 +01:00
|
|
|
return String();
|
|
|
|
}
|
2022-01-19 22:23:36 +01:00
|
|
|
|
2022-02-15 20:58:32 +01:00
|
|
|
void Webserver_Callback(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
request->send(LittleFS, "/index.htm", String(), false, processor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebserverPOST_Callback(AsyncWebServerRequest *request)
|
2022-02-10 22:33:52 +01:00
|
|
|
{
|
2022-02-27 22:23:58 +01:00
|
|
|
Serial.print("POST:\n");
|
|
|
|
int paramsNr = request->params();
|
|
|
|
for (int i = 0; i < paramsNr; i++)
|
2022-01-19 22:23:36 +01:00
|
|
|
{
|
2022-02-27 22:23:58 +01:00
|
|
|
AsyncWebParameter *p = request->getParam(i);
|
|
|
|
Serial.printf("%s : %s\n", p->name().c_str(), p->value().c_str());
|
|
|
|
|
2022-03-08 21:23:52 +01:00
|
|
|
// begin: POST Form Source Changed
|
|
|
|
if (p->name() == "sourceselect")
|
|
|
|
{
|
|
|
|
SpeedSource_t temp = (SpeedSource_t)p->value().toInt();
|
|
|
|
Serial.printf("temp: %d", temp);
|
|
|
|
Serial.printf("SpeedSource: %d", LubeConfig.SpeedSource);
|
|
|
|
if (LubeConfig.SpeedSource != temp)
|
|
|
|
{
|
|
|
|
LubeConfig.SpeedSource = temp;
|
|
|
|
globals.systemStatus = sysStat_Shutdown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// end: POST Form Source Changed
|
|
|
|
// begin: POST Form Source Pulse Settings
|
|
|
|
if (p->name() == "tirewidth")
|
|
|
|
LubeConfig.TireWidth_mm = p->value().toInt();
|
|
|
|
if (p->name() == "tireratio")
|
|
|
|
LubeConfig.TireWidthHeight_Ratio = p->value().toInt();
|
|
|
|
if (p->name() == "tiredia")
|
|
|
|
LubeConfig.RimDiameter_Inch = p->value().toInt();
|
|
|
|
if (p->name() == "pulserev")
|
|
|
|
LubeConfig.PulsePerRevolution = p->value().toInt();
|
|
|
|
if (p->name() == "pulsesave")
|
|
|
|
globals.requestEEAction = EE_CFG_SAVE;
|
|
|
|
// end: POST Form Source Pulse Settings
|
|
|
|
// begin: POST Form Source GPS Settings
|
|
|
|
if (p->name() == "gpsbaud")
|
|
|
|
LubeConfig.GPSBaudRate = (GPSBaudRate_t)p->value().toInt();
|
|
|
|
if (p->name() == "gpssave")
|
|
|
|
globals.requestEEAction = EE_CFG_SAVE;
|
|
|
|
// end: POST Form Source GPS Settings
|
|
|
|
// begin: POST Form Source CAN Settings
|
|
|
|
if (p->name() == "cansource")
|
|
|
|
LubeConfig.CANSource = (CANSource_t)p->value().toInt();
|
|
|
|
if (p->name() == "cansave")
|
|
|
|
globals.requestEEAction = EE_CFG_SAVE;
|
|
|
|
// end: POST Form Source CAN Settings
|
|
|
|
}
|
2022-02-27 22:23:58 +01:00
|
|
|
request->send(LittleFS, "/post.htm", String(), false, processor);
|
2022-01-08 03:14:26 +01:00
|
|
|
}
|
2022-01-12 01:10:21 +01:00
|
|
|
|
2022-02-10 22:33:52 +01:00
|
|
|
void WebserverNotFound_Callback(AsyncWebServerRequest *request)
|
2022-01-12 01:10:21 +01:00
|
|
|
{
|
2022-02-10 22:33:52 +01:00
|
|
|
request->send(404, "text/plain", "Not found");
|
2022-01-12 01:10:21 +01:00
|
|
|
}
|