Changed JSON parameter loading to the function

pull/41/head
Hieromon Ikasamo 5 years ago
parent fea9616f63
commit be451c4725
  1. 2
      examples/HelloWorld/Data/style.json
  2. 25
      examples/HelloWorld/HelloWorld.ino

@ -1,5 +1,5 @@
{ {
"name" : "Caption", "name" : "Caption",
"type" : "ACText", "type" : "ACText",
"style": "text-align:center;font-family:'Avenir','Corbel','Osaka';color:Green;" "style": "text-align:center;font-size:24px;font-family:'Impact','Futura',sans-serif;color:tomato;"
} }

@ -28,11 +28,17 @@ typedef WebServer WEBServer;
#define HELLO_URI "/hello" #define HELLO_URI "/hello"
#define PARAM_STYLE "/style.json" #define PARAM_STYLE "/style.json"
// Declare AutoConnectElements and AutoConnectAux for the custom Web page. // Declare AutoConnectText with only a value.
// Qualify the Caption by reading style attributes from the SPIFFS style.json file.
ACText(Caption, "Hello, world"); ACText(Caption, "Hello, world");
//AutoConnectAux for the custom Web page.
AutoConnectAux helloPage(HELLO_URI, "Hello", true, { Caption }); AutoConnectAux helloPage(HELLO_URI, "Hello", true, { Caption });
AutoConnect portal; AutoConnect portal;
// JSON document loading buffer
String ElementJson;
// Redirects from root to the hello page. // Redirects from root to the hello page.
void onRoot() { void onRoot() {
WEBServer& webServer = portal.host(); WEBServer& webServer = portal.host();
@ -44,17 +50,26 @@ void onRoot() {
// Load the attribute of the element to modify at runtime from external. // Load the attribute of the element to modify at runtime from external.
String onHello(AutoConnectAux& aux, PageArgument& args) { String onHello(AutoConnectAux& aux, PageArgument& args) {
File param = SPIFFS.open(PARAM_STYLE, "r"); aux.loadElement(ElementJson);
aux.loadElement(param);
param.close();
return String(); return String();
} }
// Load the element from specified file in SPIFFS.
void loadParam(const char* fileName) {
SPIFFS.begin();
File param = SPIFFS.open(fileName, "r");
if (param) {
ElementJson = param.readString();
param.close();
}
SPIFFS.end();
}
void setup() { void setup() {
delay(1000); delay(1000);
Serial.begin(115200); Serial.begin(115200);
SPIFFS.begin(); // Prepare SPIFFS
loadParam(PARAM_STYLE); // Pre-load the element from JSON.
helloPage.on(onHello); // Register the attribute overwrite handler. helloPage.on(onHello); // Register the attribute overwrite handler.
portal.join(helloPage); // Join the hello page. portal.join(helloPage); // Join the hello page.
portal.begin(); portal.begin();

Loading…
Cancel
Save