Refactor webserver, changes to formulaCreation
This commit is contained in:
@ -28,34 +28,16 @@ SOFTWARE.
|
||||
#include "calc.h"
|
||||
#include "tempsensor.h"
|
||||
#include <ArduinoJson.h>
|
||||
#include <incbin.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <ESP8266mDNS.h>
|
||||
#include <LittleFS.h>
|
||||
|
||||
// Binary resouces
|
||||
#if defined( EMBED_HTML )
|
||||
INCBIN_EXTERN(IndexHtm);
|
||||
INCBIN_EXTERN(DeviceHtm);
|
||||
INCBIN_EXTERN(ConfigHtm);
|
||||
INCBIN_EXTERN(CalibrationHtm);
|
||||
INCBIN_EXTERN(AboutHtm);
|
||||
#else
|
||||
INCBIN_EXTERN(UploadHtm);
|
||||
#endif
|
||||
|
||||
WebServer myWebServer; // My wrapper class fr webserver functions
|
||||
ESP8266WebServer server(80);
|
||||
int lastFormulaCreateError = 0;
|
||||
|
||||
extern bool sleepModeActive;
|
||||
extern bool sleepModeAlwaysSkip;
|
||||
|
||||
//
|
||||
// Callback from webServer when / has been accessed.
|
||||
//
|
||||
void webHandleDevice() {
|
||||
void WebServer::webHandleDevice() {
|
||||
LOG_PERF_START("webserver-api-device");
|
||||
#if LOG_LEVEL==6
|
||||
Log.verbose(F("WEB : webServer callback for /api/config." CR));
|
||||
@ -71,14 +53,14 @@ void webHandleDevice() {
|
||||
#endif
|
||||
String out;
|
||||
serializeJson(doc, out);
|
||||
server.send(200, "application/json", out.c_str() );
|
||||
server->send(200, "application/json", out.c_str() );
|
||||
LOG_PERF_STOP("webserver-api-device");
|
||||
}
|
||||
|
||||
//
|
||||
// Callback from webServer when / has been accessed.
|
||||
//
|
||||
void webHandleConfig() {
|
||||
void WebServer::webHandleConfig() {
|
||||
LOG_PERF_START("webserver-api-config");
|
||||
#if LOG_LEVEL==6
|
||||
Log.verbose(F("WEB : webServer callback for /api/config." CR));
|
||||
@ -103,14 +85,14 @@ void webHandleConfig() {
|
||||
#endif
|
||||
String out;
|
||||
serializeJson(doc, out);
|
||||
server.send(200, "application/json", out.c_str() );
|
||||
server->send(200, "application/json", out.c_str() );
|
||||
LOG_PERF_STOP("webserver-api-config");
|
||||
}
|
||||
|
||||
//
|
||||
// Callback from webServer when / has been accessed.
|
||||
//
|
||||
void webHandleUpload() {
|
||||
void WebServer::webHandleUpload() {
|
||||
LOG_PERF_START("webserver-api-upload");
|
||||
#if LOG_LEVEL==6
|
||||
Log.verbose(F("WEB : webServer callback for /api/upload." CR));
|
||||
@ -130,21 +112,19 @@ void webHandleUpload() {
|
||||
#endif
|
||||
String out;
|
||||
serializeJson(doc, out);
|
||||
server.send(200, "application/json", out.c_str() );
|
||||
server->send(200, "application/json", out.c_str() );
|
||||
LOG_PERF_STOP("webserver-api-upload");
|
||||
}
|
||||
|
||||
//
|
||||
// Callback from webServer when / has been accessed.
|
||||
//
|
||||
File uploadFile;
|
||||
|
||||
void webHandleUploadFile() {
|
||||
void WebServer::webHandleUploadFile() {
|
||||
LOG_PERF_START("webserver-api-upload-file");
|
||||
#if LOG_LEVEL==6
|
||||
Log.verbose(F("WEB : webServer callback for /api/upload/file." CR));
|
||||
#endif
|
||||
HTTPUpload& upload = server.upload();
|
||||
HTTPUpload& upload = server->upload();
|
||||
String f = upload.filename;
|
||||
bool validFilename = false;
|
||||
|
||||
@ -169,10 +149,10 @@ void webHandleUploadFile() {
|
||||
uploadFile.close();
|
||||
Log.notice(F("WEB : File uploaded %d bytes." CR), upload.totalSize);
|
||||
}
|
||||
server.sendHeader("Location","/");
|
||||
server.send(303);
|
||||
server->sendHeader("Location","/");
|
||||
server->send(303);
|
||||
} else {
|
||||
server.send(500, "text/plain", "Couldn't create file.");
|
||||
server->send(500, "text/plain", "Couldn't create file.");
|
||||
}
|
||||
LOG_PERF_STOP("webserver-api-upload-file");
|
||||
}
|
||||
@ -180,46 +160,46 @@ void webHandleUploadFile() {
|
||||
//
|
||||
// Callback from webServer when / has been accessed.
|
||||
//
|
||||
void webHandleCalibrate() {
|
||||
void WebServer::webHandleCalibrate() {
|
||||
LOG_PERF_START("webserver-api-calibrate");
|
||||
String id = server.arg( CFG_PARAM_ID );
|
||||
String id = server->arg( CFG_PARAM_ID );
|
||||
#if LOG_LEVEL==6
|
||||
Log.verbose(F("WEB : webServer callback for /api/calibrate." CR));
|
||||
#endif
|
||||
if( !id.equalsIgnoreCase( myConfig.getID() ) ) {
|
||||
Log.error(F("WEB : Wrong ID received %s, expected %s" CR), id.c_str(), myConfig.getID());
|
||||
server.send(400, "text/plain", "Invalid ID.");
|
||||
server->send(400, "text/plain", "Invalid ID.");
|
||||
LOG_PERF_STOP("webserver-api-calibrate");
|
||||
return;
|
||||
}
|
||||
myGyro.calibrateSensor();
|
||||
server.send(200, "text/plain", "Device calibrated" );
|
||||
server->send(200, "text/plain", "Device calibrated" );
|
||||
LOG_PERF_STOP("webserver-api-calibrate");
|
||||
}
|
||||
|
||||
//
|
||||
// Callback from webServer when / has been accessed.
|
||||
//
|
||||
void webHandleFactoryReset() {
|
||||
String id = server.arg( CFG_PARAM_ID );
|
||||
void WebServer::webHandleFactoryReset() {
|
||||
String id = server->arg( CFG_PARAM_ID );
|
||||
#if LOG_LEVEL==6
|
||||
Log.verbose(F("WEB : webServer callback for /api/factory." CR));
|
||||
#endif
|
||||
if( !id.compareTo( myConfig.getID() ) ) {
|
||||
server.send(200, "text/plain", "Doing reset...");
|
||||
server->send(200, "text/plain", "Doing reset...");
|
||||
LittleFS.remove(CFG_FILENAME);
|
||||
LittleFS.end();
|
||||
delay(500);
|
||||
ESP.reset();
|
||||
} else {
|
||||
server.send(400, "text/plain", "Unknown ID.");
|
||||
server->send(400, "text/plain", "Unknown ID.");
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Callback from webServer when / has been accessed.
|
||||
//
|
||||
void webHandleStatus() {
|
||||
void WebServer::webHandleStatus() {
|
||||
LOG_PERF_START("webserver-api-status");
|
||||
#if LOG_LEVEL==6
|
||||
Log.verbose(F("WEB : webServer callback for /api/status." CR));
|
||||
@ -248,173 +228,173 @@ void webHandleStatus() {
|
||||
#endif
|
||||
String out;
|
||||
serializeJson(doc, out);
|
||||
server.send(200, "application/json", out.c_str() );
|
||||
server->send(200, "application/json", out.c_str() );
|
||||
LOG_PERF_STOP("webserver-api-status");
|
||||
}
|
||||
|
||||
//
|
||||
// Callback from webServer when / has been accessed.
|
||||
//
|
||||
void webHandleClearWIFI() {
|
||||
String id = server.arg( CFG_PARAM_ID );
|
||||
void WebServer::webHandleClearWIFI() {
|
||||
String id = server->arg( CFG_PARAM_ID );
|
||||
#if LOG_LEVEL==6
|
||||
Log.verbose(F("WEB : webServer callback for /api/clearwifi." CR));
|
||||
#endif
|
||||
if( !id.compareTo( myConfig.getID() ) ) {
|
||||
server.send(200, "text/plain", "Clearing WIFI credentials and doing reset...");
|
||||
server->send(200, "text/plain", "Clearing WIFI credentials and doing reset...");
|
||||
delay(1000);
|
||||
WiFi.disconnect(); // Clear credentials
|
||||
ESP.reset();
|
||||
} else {
|
||||
server.send(400, "text/plain", "Unknown ID.");
|
||||
server->send(400, "text/plain", "Unknown ID.");
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Used to force the device to never sleep.
|
||||
//
|
||||
void webHandleStatusSleepmode() {
|
||||
void WebServer::webHandleStatusSleepmode() {
|
||||
LOG_PERF_START("webserver-api-sleepmode");
|
||||
String id = server.arg( CFG_PARAM_ID );
|
||||
String id = server->arg( CFG_PARAM_ID );
|
||||
#if LOG_LEVEL==6
|
||||
Log.verbose(F("WEB : webServer callback for /api/status/sleepmode." CR) );
|
||||
#endif
|
||||
if( !id.equalsIgnoreCase( myConfig.getID() ) ) {
|
||||
Log.error(F("WEB : Wrong ID received %s, expected %s" CR), id.c_str(), myConfig.getID());
|
||||
server.send(400, "text/plain", "Invalid ID.");
|
||||
server->send(400, "text/plain", "Invalid ID.");
|
||||
LOG_PERF_STOP("webserver-api-sleepmode");
|
||||
return;
|
||||
}
|
||||
#if LOG_LEVEL==6
|
||||
Log.verbose(F("WEB : sleep-mode=%s." CR), server.arg( CFG_PARAM_SLEEP_MODE ).c_str() );
|
||||
Log.verbose(F("WEB : sleep-mode=%s." CR), server->arg( CFG_PARAM_SLEEP_MODE ).c_str() );
|
||||
#endif
|
||||
if( server.arg( CFG_PARAM_SLEEP_MODE ).equalsIgnoreCase( "true" ) )
|
||||
if( server->arg( CFG_PARAM_SLEEP_MODE ).equalsIgnoreCase( "true" ) )
|
||||
sleepModeAlwaysSkip = true;
|
||||
else
|
||||
sleepModeAlwaysSkip = false;
|
||||
server.send(200, "text/plain", "Sleep mode updated" );
|
||||
server->send(200, "text/plain", "Sleep mode updated" );
|
||||
LOG_PERF_STOP("webserver-api-sleepmode");
|
||||
}
|
||||
|
||||
//
|
||||
// Update device settings.
|
||||
//
|
||||
void webHandleConfigDevice() {
|
||||
void WebServer::webHandleConfigDevice() {
|
||||
LOG_PERF_START("webserver-api-config-device");
|
||||
String id = server.arg( CFG_PARAM_ID );
|
||||
String id = server->arg( CFG_PARAM_ID );
|
||||
#if LOG_LEVEL==6
|
||||
Log.verbose(F("WEB : webServer callback for /api/config/device." CR) );
|
||||
#endif
|
||||
if( !id.equalsIgnoreCase( myConfig.getID() ) ) {
|
||||
Log.error(F("WEB : Wrong ID received %s, expected %s" CR), id.c_str(), myConfig.getID());
|
||||
server.send(400, "text/plain", "Invalid ID.");
|
||||
server->send(400, "text/plain", "Invalid ID.");
|
||||
LOG_PERF_STOP("webserver-api-config-device");
|
||||
return;
|
||||
}
|
||||
#if LOG_LEVEL==6
|
||||
Log.verbose(F("WEB : mdns=%s, temp-format=%s." CR), server.arg( CFG_PARAM_MDNS ).c_str(), server.arg( CFG_PARAM_TEMPFORMAT ).c_str() );
|
||||
Log.verbose(F("WEB : mdns=%s, temp-format=%s." CR), server->arg( CFG_PARAM_MDNS ).c_str(), server->arg( CFG_PARAM_TEMPFORMAT ).c_str() );
|
||||
#endif
|
||||
myConfig.setMDNS( server.arg( CFG_PARAM_MDNS ).c_str() );
|
||||
myConfig.setTempFormat( server.arg( CFG_PARAM_TEMPFORMAT ).charAt(0) );
|
||||
myConfig.setSleepInterval( server.arg( CFG_PARAM_SLEEP_INTERVAL ).c_str() );
|
||||
myConfig.setMDNS( server->arg( CFG_PARAM_MDNS ).c_str() );
|
||||
myConfig.setTempFormat( server->arg( CFG_PARAM_TEMPFORMAT ).charAt(0) );
|
||||
myConfig.setSleepInterval( server->arg( CFG_PARAM_SLEEP_INTERVAL ).c_str() );
|
||||
myConfig.saveFile();
|
||||
server.sendHeader("Location", "/config.htm#collapseOne", true);
|
||||
server.send(302, "text/plain", "Device config updated" );
|
||||
server->sendHeader("Location", "/config.htm#collapseOne", true);
|
||||
server->send(302, "text/plain", "Device config updated" );
|
||||
LOG_PERF_STOP("webserver-api-config-device");
|
||||
}
|
||||
|
||||
//
|
||||
// Update push settings.
|
||||
//
|
||||
void webHandleConfigPush() {
|
||||
void WebServer::webHandleConfigPush() {
|
||||
LOG_PERF_START("webserver-api-config-push");
|
||||
String id = server.arg( CFG_PARAM_ID );
|
||||
String id = server->arg( CFG_PARAM_ID );
|
||||
#if LOG_LEVEL==6
|
||||
Log.verbose(F("WEB : webServer callback for /api/config/push." CR) );
|
||||
#endif
|
||||
if( !id.equalsIgnoreCase( myConfig.getID() ) ) {
|
||||
Log.error(F("WEB : Wrong ID received %s, expected %s" CR), id.c_str(), myConfig.getID());
|
||||
server.send(400, "text/plain", "Invalid ID.");
|
||||
server->send(400, "text/plain", "Invalid ID.");
|
||||
LOG_PERF_STOP("webserver-api-config-push");
|
||||
return;
|
||||
}
|
||||
#if LOG_LEVEL==6
|
||||
Log.verbose(F("WEB : http=%s,%s, bf=%s influx2=%s, %s, %s, %s." CR), server.arg( CFG_PARAM_PUSH_HTTP ).c_str(),
|
||||
server.arg( CFG_PARAM_PUSH_HTTP2 ).c_str(), server.arg( CFG_PARAM_PUSH_BREWFATHER ).c_str(),
|
||||
server.arg( CFG_PARAM_PUSH_INFLUXDB2 ).c_str(), server.arg( CFG_PARAM_PUSH_INFLUXDB2_ORG ).c_str(),
|
||||
server.arg( CFG_PARAM_PUSH_INFLUXDB2_BUCKET ).c_str(), server.arg( CFG_PARAM_PUSH_INFLUXDB2_AUTH ).c_str()
|
||||
Log.verbose(F("WEB : http=%s,%s, bf=%s influx2=%s, %s, %s, %s." CR), server->arg( CFG_PARAM_PUSH_HTTP ).c_str(),
|
||||
server->arg( CFG_PARAM_PUSH_HTTP2 ).c_str(), server->arg( CFG_PARAM_PUSH_BREWFATHER ).c_str(),
|
||||
server->arg( CFG_PARAM_PUSH_INFLUXDB2 ).c_str(), server->arg( CFG_PARAM_PUSH_INFLUXDB2_ORG ).c_str(),
|
||||
server->arg( CFG_PARAM_PUSH_INFLUXDB2_BUCKET ).c_str(), server->arg( CFG_PARAM_PUSH_INFLUXDB2_AUTH ).c_str()
|
||||
);
|
||||
#endif
|
||||
myConfig.setHttpPushUrl( server.arg( CFG_PARAM_PUSH_HTTP ).c_str() );
|
||||
myConfig.setHttpPushUrl2( server.arg( CFG_PARAM_PUSH_HTTP2 ).c_str() );
|
||||
myConfig.setBrewfatherPushUrl( server.arg( CFG_PARAM_PUSH_BREWFATHER ).c_str() );
|
||||
myConfig.setInfluxDb2PushUrl( server.arg( CFG_PARAM_PUSH_INFLUXDB2 ).c_str() );
|
||||
myConfig.setInfluxDb2PushOrg( server.arg( CFG_PARAM_PUSH_INFLUXDB2_ORG ).c_str() );
|
||||
myConfig.setInfluxDb2PushBucket( server.arg( CFG_PARAM_PUSH_INFLUXDB2_BUCKET ).c_str() );
|
||||
myConfig.setInfluxDb2PushToken( server.arg( CFG_PARAM_PUSH_INFLUXDB2_AUTH ).c_str() );
|
||||
myConfig.setHttpPushUrl( server->arg( CFG_PARAM_PUSH_HTTP ).c_str() );
|
||||
myConfig.setHttpPushUrl2( server->arg( CFG_PARAM_PUSH_HTTP2 ).c_str() );
|
||||
myConfig.setBrewfatherPushUrl( server->arg( CFG_PARAM_PUSH_BREWFATHER ).c_str() );
|
||||
myConfig.setInfluxDb2PushUrl( server->arg( CFG_PARAM_PUSH_INFLUXDB2 ).c_str() );
|
||||
myConfig.setInfluxDb2PushOrg( server->arg( CFG_PARAM_PUSH_INFLUXDB2_ORG ).c_str() );
|
||||
myConfig.setInfluxDb2PushBucket( server->arg( CFG_PARAM_PUSH_INFLUXDB2_BUCKET ).c_str() );
|
||||
myConfig.setInfluxDb2PushToken( server->arg( CFG_PARAM_PUSH_INFLUXDB2_AUTH ).c_str() );
|
||||
myConfig.saveFile();
|
||||
server.sendHeader("Location", "/config.htm#collapseTwo", true);
|
||||
server.send(302, "text/plain", "Push config updated" );
|
||||
server->sendHeader("Location", "/config.htm#collapseTwo", true);
|
||||
server->send(302, "text/plain", "Push config updated" );
|
||||
LOG_PERF_STOP("webserver-api-config-push");
|
||||
}
|
||||
|
||||
//
|
||||
// Update gravity settings.
|
||||
//
|
||||
void webHandleConfigGravity() {
|
||||
void WebServer::webHandleConfigGravity() {
|
||||
LOG_PERF_START("webserver-api-config-gravity");
|
||||
String id = server.arg( CFG_PARAM_ID );
|
||||
String id = server->arg( CFG_PARAM_ID );
|
||||
#if LOG_LEVEL==6
|
||||
Log.verbose(F("WEB : webServer callback for /api/config/gravity." CR) );
|
||||
#endif
|
||||
if( !id.equalsIgnoreCase( myConfig.getID() ) ) {
|
||||
Log.error(F("WEB : Wrong ID received %s, expected %s" CR), id.c_str(), myConfig.getID());
|
||||
server.send(400, "text/plain", "Invalid ID.");
|
||||
server->send(400, "text/plain", "Invalid ID.");
|
||||
LOG_PERF_STOP("webserver-api-config-gravity");
|
||||
return;
|
||||
}
|
||||
#if LOG_LEVEL==6
|
||||
Log.verbose(F("WEB : formula=%s, temp-corr=%s." CR), server.arg( CFG_PARAM_GRAVITY_FORMULA ).c_str(), server.arg( CFG_PARAM_GRAVITY_TEMP_ADJ ).c_str() );
|
||||
Log.verbose(F("WEB : formula=%s, temp-corr=%s." CR), server->arg( CFG_PARAM_GRAVITY_FORMULA ).c_str(), server->arg( CFG_PARAM_GRAVITY_TEMP_ADJ ).c_str() );
|
||||
#endif
|
||||
myConfig.setGravityFormula( server.arg( CFG_PARAM_GRAVITY_FORMULA ).c_str() );
|
||||
myConfig.setGravityTempAdj( server.arg( CFG_PARAM_GRAVITY_TEMP_ADJ ).equalsIgnoreCase( "on" ) ? true:false);
|
||||
myConfig.setGravityFormula( server->arg( CFG_PARAM_GRAVITY_FORMULA ).c_str() );
|
||||
myConfig.setGravityTempAdj( server->arg( CFG_PARAM_GRAVITY_TEMP_ADJ ).equalsIgnoreCase( "on" ) ? true:false);
|
||||
myConfig.saveFile();
|
||||
server.sendHeader("Location", "/config.htm#collapseThree", true);
|
||||
server.send(302, "text/plain", "Gravity config updated" );
|
||||
server->sendHeader("Location", "/config.htm#collapseThree", true);
|
||||
server->send(302, "text/plain", "Gravity config updated" );
|
||||
LOG_PERF_STOP("webserver-api-config-gravity");
|
||||
}
|
||||
|
||||
//
|
||||
// Update hardware settings.
|
||||
//
|
||||
void webHandleConfigHardware() {
|
||||
void WebServer::webHandleConfigHardware() {
|
||||
LOG_PERF_START("webserver-api-config-hardware");
|
||||
String id = server.arg( CFG_PARAM_ID );
|
||||
String id = server->arg( CFG_PARAM_ID );
|
||||
#if LOG_LEVEL==6
|
||||
Log.verbose(F("WEB : webServer callback for /api/config/hardware." CR) );
|
||||
#endif
|
||||
if( !id.equalsIgnoreCase( myConfig.getID() ) ) {
|
||||
Log.error(F("WEB : Wrong ID received %s, expected %s" CR), id.c_str(), myConfig.getID());
|
||||
server.send(400, "text/plain", "Invalid ID.");
|
||||
server->send(400, "text/plain", "Invalid ID.");
|
||||
LOG_PERF_STOP("webserver-api-config-hardware");
|
||||
return;
|
||||
}
|
||||
#if LOG_LEVEL==6
|
||||
Log.verbose(F("WEB : vf=%s, tempadj=%s, ota=%s." CR), server.arg( CFG_PARAM_VOLTAGEFACTOR ).c_str(), server.arg( CFG_PARAM_TEMP_ADJ ).c_str(), server.arg( CFG_PARAM_OTA ).c_str() );
|
||||
Log.verbose(F("WEB : vf=%s, tempadj=%s, ota=%s." CR), server->arg( CFG_PARAM_VOLTAGEFACTOR ).c_str(), server->arg( CFG_PARAM_TEMP_ADJ ).c_str(), server->arg( CFG_PARAM_OTA ).c_str() );
|
||||
#endif
|
||||
myConfig.setVoltageFactor( server.arg( CFG_PARAM_VOLTAGEFACTOR ).toFloat() );
|
||||
myConfig.setTempSensorAdj( server.arg( CFG_PARAM_TEMP_ADJ ).toFloat() );
|
||||
myConfig.setOtaURL( server.arg( CFG_PARAM_OTA ).c_str() );
|
||||
myConfig.setVoltageFactor( server->arg( CFG_PARAM_VOLTAGEFACTOR ).toFloat() );
|
||||
myConfig.setTempSensorAdj( server->arg( CFG_PARAM_TEMP_ADJ ).toFloat() );
|
||||
myConfig.setOtaURL( server->arg( CFG_PARAM_OTA ).c_str() );
|
||||
myConfig.saveFile();
|
||||
server.sendHeader("Location", "/config.htm#collapseFour", true);
|
||||
server.send(302, "text/plain", "Hardware config updated" );
|
||||
server->sendHeader("Location", "/config.htm#collapseFour", true);
|
||||
server->send(302, "text/plain", "Hardware config updated" );
|
||||
LOG_PERF_STOP("webserver-api-config-hardware");
|
||||
}
|
||||
|
||||
//
|
||||
// Callback from webServer when / has been accessed.
|
||||
//
|
||||
void webHandleFormulaRead() {
|
||||
void WebServer::webHandleFormulaRead() {
|
||||
LOG_PERF_START("webserver-api-formula-read");
|
||||
#if LOG_LEVEL==6
|
||||
Log.verbose(F("WEB : webServer callback for /api/formula/get." CR));
|
||||
@ -463,61 +443,61 @@ void webHandleFormulaRead() {
|
||||
#endif
|
||||
String out;
|
||||
serializeJson(doc, out);
|
||||
server.send(200, "application/json", out.c_str() );
|
||||
server->send(200, "application/json", out.c_str() );
|
||||
LOG_PERF_STOP("webserver-api-formula-read");
|
||||
}
|
||||
|
||||
//
|
||||
// Update hardware settings.
|
||||
//
|
||||
void webHandleFormulaWrite() {
|
||||
void WebServer::webHandleFormulaWrite() {
|
||||
LOG_PERF_START("webserver-api-formula-write");
|
||||
String id = server.arg( CFG_PARAM_ID );
|
||||
String id = server->arg( CFG_PARAM_ID );
|
||||
#if LOG_LEVEL==6
|
||||
Log.verbose(F("WEB : webServer callback for /api/formula/post." CR) );
|
||||
#endif
|
||||
if( !id.equalsIgnoreCase( myConfig.getID() ) ) {
|
||||
Log.error(F("WEB : Wrong ID received %s, expected %s" CR), id.c_str(), myConfig.getID());
|
||||
server.send(400, "text/plain", "Invalid ID.");
|
||||
server->send(400, "text/plain", "Invalid ID.");
|
||||
LOG_PERF_STOP("webserver-api-formula-write");
|
||||
return;
|
||||
}
|
||||
#if LOG_LEVEL==6
|
||||
Log.verbose(F("WEB : angles=%F,%F,%F,%F,%F." CR), server.arg( "a1" ).toFloat(), server.arg( "a2" ).toFloat(), server.arg( "a3" ).toFloat(), server.arg( "a4" ).toFloat(), server.arg( "a5" ).toFloat() );
|
||||
Log.verbose(F("WEB : gravity=%F,%F,%F,%F,%F." CR), server.arg( "g1" ).toFloat(), server.arg( "g2" ).toFloat(), server.arg( "g3" ).toFloat(), server.arg( "g4" ).toFloat(), server.arg( "g5" ).toFloat() );
|
||||
Log.verbose(F("WEB : angles=%F,%F,%F,%F,%F." CR), server->arg( "a1" ).toFloat(), server->arg( "a2" ).toFloat(), server->arg( "a3" ).toFloat(), server->arg( "a4" ).toFloat(), server->arg( "a5" ).toFloat() );
|
||||
Log.verbose(F("WEB : gravity=%F,%F,%F,%F,%F." CR), server->arg( "g1" ).toFloat(), server->arg( "g2" ).toFloat(), server->arg( "g3" ).toFloat(), server->arg( "g4" ).toFloat(), server->arg( "g5" ).toFloat() );
|
||||
#endif
|
||||
RawFormulaData fd;
|
||||
fd.a[0] = server.arg( "a1" ).toDouble();
|
||||
fd.a[1] = server.arg( "a2" ).toDouble();
|
||||
fd.a[2] = server.arg( "a3" ).toDouble();
|
||||
fd.a[3] = server.arg( "a4" ).toDouble();
|
||||
fd.a[4] = server.arg( "a5" ).toDouble();
|
||||
fd.g[0] = server.arg( "g1" ).toDouble();
|
||||
fd.g[1] = server.arg( "g2" ).toDouble();
|
||||
fd.g[2] = server.arg( "g3" ).toDouble();
|
||||
fd.g[3] = server.arg( "g4" ).toDouble();
|
||||
fd.g[4] = server.arg( "g5" ).toDouble();
|
||||
fd.a[0] = server->arg( "a1" ).toDouble();
|
||||
fd.a[1] = server->arg( "a2" ).toDouble();
|
||||
fd.a[2] = server->arg( "a3" ).toDouble();
|
||||
fd.a[3] = server->arg( "a4" ).toDouble();
|
||||
fd.a[4] = server->arg( "a5" ).toDouble();
|
||||
fd.g[0] = server->arg( "g1" ).toDouble();
|
||||
fd.g[1] = server->arg( "g2" ).toDouble();
|
||||
fd.g[2] = server->arg( "g3" ).toDouble();
|
||||
fd.g[3] = server->arg( "g4" ).toDouble();
|
||||
fd.g[4] = server->arg( "g5" ).toDouble();
|
||||
myConfig.setFormulaData( fd );
|
||||
|
||||
int e;
|
||||
char buf[100];
|
||||
|
||||
e = createFormula( fd, &buf[0], 4 );
|
||||
e = createFormula( fd, &buf[0], 2 );
|
||||
|
||||
if( e ) {
|
||||
// If we fail with order=4 try with 3
|
||||
Log.warning(F("WEB : Failed to find formula with order 4." CR), e );
|
||||
// If we fail with order=2 try with 3
|
||||
Log.warning(F("WEB : Failed to find formula with order 3." CR), e );
|
||||
e = createFormula( fd, &buf[0], 3 );
|
||||
}
|
||||
|
||||
if( e ) {
|
||||
// If we fail with order=3 try with 2
|
||||
Log.warning(F("WEB : Failed to find formula with order 3." CR), e );
|
||||
e = createFormula( fd, &buf[0], 2 );
|
||||
// If we fail with order=3 try with 4
|
||||
Log.warning(F("WEB : Failed to find formula with order 4." CR), e );
|
||||
e = createFormula( fd, &buf[0], 4 );
|
||||
}
|
||||
|
||||
if( e ) {
|
||||
// If we fail with order=2 then we mark it as failed
|
||||
// If we fail with order=4 then we mark it as failed
|
||||
Log.error(F("WEB : Unable to find formula based on provided values err=%d." CR), e );
|
||||
lastFormulaCreateError = e;
|
||||
} else {
|
||||
@ -528,8 +508,8 @@ void webHandleFormulaWrite() {
|
||||
}
|
||||
|
||||
myConfig.saveFile();
|
||||
server.sendHeader("Location", "/calibration.htm", true);
|
||||
server.send(302, "text/plain", "Formula updated" );
|
||||
server->sendHeader("Location", "/calibration.htm", true);
|
||||
server->send(302, "text/plain", "Formula updated" );
|
||||
LOG_PERF_STOP("webserver-api-formula-write");
|
||||
}
|
||||
//
|
||||
@ -570,35 +550,34 @@ bool WebServer::checkHtmlFile( HtmlFile item ) {
|
||||
return LittleFS.exists( fn );
|
||||
}
|
||||
|
||||
//
|
||||
// Handler for page not found
|
||||
//
|
||||
void WebServer::webHandlePageNotFound()
|
||||
{
|
||||
Log.error(F("WEB : URL not found %s received." CR), server->uri().c_str());
|
||||
server->send(404, "text/plain", F("URL not found") );
|
||||
}
|
||||
|
||||
//
|
||||
// Setup the Web Server callbacks and start it
|
||||
//
|
||||
bool WebServer::setupWebServer() {
|
||||
Log.notice(F("WEB : Configuring web server." CR));
|
||||
|
||||
server = new ESP8266WebServer();
|
||||
|
||||
MDNS.begin( myConfig.getMDNS() );
|
||||
MDNS.addService("http", "tcp", 80);
|
||||
|
||||
// Static content
|
||||
#if defined( EMBED_HTML )
|
||||
server.on("/",[]() {
|
||||
server.send_P(200, "text/html", (const char*) gIndexHtmData, gIndexHtmSize );
|
||||
} );
|
||||
server.on("/index.htm",[]() {
|
||||
server.send_P(200, "text/html", (const char*) gIndexHtmData, gIndexHtmSize );
|
||||
} );
|
||||
server.on("/device.htm",[]() {
|
||||
server.send_P(200, "text/html", (const char*) gDeviceHtmData, gDeviceHtmSize );
|
||||
} );
|
||||
server.on("/config.htm",[]() {
|
||||
server.send_P(200, "text/html", (const char*) gConfigHtmData, gConfigHtmSize );
|
||||
} );
|
||||
server.on("/calibration.htm",[]() {
|
||||
server.send_P(200, "text/html", (const char*) gCalibrationHtmData, gCalibrationHtmSize );
|
||||
} );
|
||||
server.on("/about.htm",[]() {
|
||||
server.send_P(200, "text/html", (const char*) gAboutHtmData, gAboutHtmSize );
|
||||
} );
|
||||
server->on("/", std::bind(&WebServer::webReturnIndexHtm, this) );
|
||||
server->on("/index.htm", std::bind(&WebServer::webReturnIndexHtm, this) );
|
||||
server->on("/device.htm", std::bind(&WebServer::webReturnDeviceHtm, this) );
|
||||
server->on("/config.htm", std::bind(&WebServer::webReturnConfigHtm, this) );
|
||||
server->on("/calibration.htm", std::bind(&WebServer::webReturnCalibrationHtm, this) );
|
||||
server->on("/about.htm", std::bind(&WebServer::webReturnAboutHtm, this) );
|
||||
#else
|
||||
// Show files in the filessytem at startup
|
||||
|
||||
@ -614,50 +593,41 @@ bool WebServer::setupWebServer() {
|
||||
if( checkHtmlFile( HTML_INDEX ) && checkHtmlFile( HTML_DEVICE ) && checkHtmlFile( HTML_CONFIG ) && checkHtmlFile( HTML_ABOUT ) ) {
|
||||
Log.notice(F("WEB : All html files exist, starting in normal mode." CR));
|
||||
|
||||
server.serveStatic("/", LittleFS, "/index.min.htm" );
|
||||
server.serveStatic("/index.htm", LittleFS, "/index.min.htm" );
|
||||
server.serveStatic("/device.htm", LittleFS, "/device.min.htm" );
|
||||
server.serveStatic("/config.htm", LittleFS, "/config.min.htm" );
|
||||
server.serveStatic("/about.htm", LittleFS, "/about.min.htm" );
|
||||
server.serveStatic("/calibration.htm", LittleFS, "/calibration.min.htm" );
|
||||
server->serveStatic("/", LittleFS, "/index.min.htm" );
|
||||
server->serveStatic("/index.htm", LittleFS, "/index.min.htm" );
|
||||
server->serveStatic("/device.htm", LittleFS, "/device.min.htm" );
|
||||
server->serveStatic("/config.htm", LittleFS, "/config.min.htm" );
|
||||
server->serveStatic("/about.htm", LittleFS, "/about.min.htm" );
|
||||
server->serveStatic("/calibration.htm", LittleFS, "/calibration.min.htm" );
|
||||
|
||||
// Also add the static upload view in case we we have issues that needs to be fixed.
|
||||
server.on("/upload.htm",[]() {
|
||||
server.send_P(200, "text/html", (const char*) gUploadHtmData, gUploadHtmSize );
|
||||
} );
|
||||
server->on("/upload.htm", std::bind(&WebServer::webReturnUploadHtm, this) );
|
||||
} else {
|
||||
Log.error(F("WEB : Missing html files, starting with upload UI." CR));
|
||||
|
||||
server.on("/",[]() {
|
||||
server.send_P(200, "text/html", (const char*) gUploadHtmData, gUploadHtmSize );
|
||||
} );
|
||||
server->on("/", std::bind(&WebServer::webReturnUploadHtm, this) );
|
||||
}
|
||||
#endif
|
||||
|
||||
// Dynamic content
|
||||
server.on("/api/config", HTTP_GET, webHandleConfig); // Get config.json
|
||||
server.on("/api/device", HTTP_GET, webHandleDevice); // Get device.json
|
||||
server.on("/api/formula", HTTP_GET, webHandleFormulaRead); // Get formula.json (calibration page)
|
||||
server.on("/api/formula", HTTP_POST, webHandleFormulaWrite); // Get formula.json (calibration page)
|
||||
server.on("/api/calibrate", HTTP_POST, webHandleCalibrate); // Run calibration routine (param id)
|
||||
server.on("/api/factory", HTTP_GET, webHandleFactoryReset); // Reset the device
|
||||
server.on("/api/status", HTTP_GET, webHandleStatus); // Get the status.json
|
||||
server.on("/api/clearwifi", HTTP_GET, webHandleClearWIFI); // Clear wifi settings
|
||||
server.on("/api/upload", HTTP_GET, webHandleUpload); // Get upload.json
|
||||
server->on("/api/config", HTTP_GET, std::bind(&WebServer::webHandleConfig, this) ); // Get config.json
|
||||
server->on("/api/device", HTTP_GET, std::bind(&WebServer::webHandleDevice, this)); // Get device.json
|
||||
server->on("/api/formula", HTTP_GET, std::bind(&WebServer::webHandleFormulaRead, this)); // Get formula.json (calibration page)
|
||||
server->on("/api/formula", HTTP_POST, std::bind(&WebServer::webHandleFormulaWrite, this)); // Get formula.json (calibration page)
|
||||
server->on("/api/calibrate", HTTP_POST, std::bind(&WebServer::webHandleCalibrate, this)); // Run calibration routine (param id)
|
||||
server->on("/api/factory", HTTP_GET, std::bind(&WebServer::webHandleFactoryReset, this)); // Reset the device
|
||||
server->on("/api/status", HTTP_GET, std::bind(&WebServer::webHandleStatus, this)); // Get the status.json
|
||||
server->on("/api/clearwifi", HTTP_GET, std::bind(&WebServer::webHandleClearWIFI, this)); // Clear wifi settings
|
||||
server->on("/api/upload", HTTP_GET, std::bind(&WebServer::webHandleUpload, this)); // Get upload.json
|
||||
|
||||
server.on("/api/upload", HTTP_POST, [](){ server.send(200); }, webHandleUploadFile); // File upload data
|
||||
server.on("/api/status/sleepmode", HTTP_POST, webHandleStatusSleepmode); // Change sleep mode
|
||||
server.on("/api/config/device", HTTP_POST, webHandleConfigDevice); // Change device settings
|
||||
server.on("/api/config/push", HTTP_POST, webHandleConfigPush); // Change push settings
|
||||
server.on("/api/config/gravity", HTTP_POST, webHandleConfigGravity); // Change gravity settings
|
||||
server.on("/api/config/hardware", HTTP_POST, webHandleConfigHardware); // Change hardware settings
|
||||
server->on("/api/upload", HTTP_POST, std::bind(&WebServer::webReturnOK, this), std::bind(&WebServer::webHandleUploadFile, this)); // File upload data
|
||||
server->on("/api/status/sleepmode", HTTP_POST, std::bind(&WebServer::webHandleStatusSleepmode, this)); // Change sleep mode
|
||||
server->on("/api/config/device", HTTP_POST, std::bind(&WebServer::webHandleConfigDevice, this)); // Change device settings
|
||||
server->on("/api/config/push", HTTP_POST, std::bind(&WebServer::webHandleConfigPush, this)); // Change push settings
|
||||
server->on("/api/config/gravity", HTTP_POST, std::bind(&WebServer::webHandleConfigGravity, this)); // Change gravity settings
|
||||
server->on("/api/config/hardware", HTTP_POST, std::bind(&WebServer::webHandleConfigHardware, this)); // Change hardware settings
|
||||
|
||||
server.onNotFound( []() {
|
||||
Log.error(F("WEB : URL not found %s received." CR), server.uri().c_str());
|
||||
server.send(404, "text/plain", F("URL not found") );
|
||||
} );
|
||||
|
||||
server.begin();
|
||||
server->onNotFound( std::bind(&WebServer::webHandlePageNotFound, this));
|
||||
server->begin();
|
||||
Log.notice(F("WEB : Web server started." CR));
|
||||
return true;
|
||||
}
|
||||
@ -667,7 +637,7 @@ bool WebServer::setupWebServer() {
|
||||
//
|
||||
void WebServer::loop() {
|
||||
// Dont put serial debug output in this call
|
||||
server.handleClient();
|
||||
server->handleClient();
|
||||
MDNS.update();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user