The add function adds specified AutoConnectElement to the AutoConnectAux. If speficied the collection of AutoConnectElements as a `std::vector` of the references to each element, these elements added in bulk.
The add function adds specified AutoConnectElement to the AutoConnectAux. If specified the collection of AutoConnectElements as a `std::vector` of the references to each element, these elements added in bulk.
The AutoConnectElements contained in the AutoConnectAux object are uniquely identified by the name. When adding an AutoConnectElement, if an element with the same name already exists in the AutoConnectAux, checking the type, and if it is the same, the value will be replaced. If another type of AutoConnectElement exists with the same name, that add operation will be invalid.[^1] In the following example, an AutoConnectButton as `button` addition is invalid.
The AutoConnectElements contained in the AutoConnectAux object are uniquely identified by the name. When adding an AutoConnectElement, if an element with the same name already exists in the AutoConnectAux, checking the type, and if it is the same, the value will be replaced. If another type of AutoConnectElement exists with the same name, that add operation will be invalid.[^1] In the following example, an AutoConnectButton as `button` addition is invalid.
*AutoConnectElementVT* is a predefined type for it and can use methods of [std::vector](https://en.cppreference.com/w/cpp/container/vector)<[std::reference_wrapper](https://en.cppreference.com/w/cpp/utility/functional/reference_wrapper)>.
*AutoConnectElementVT* is a predefined type for it and can use methods of [std::vector](https://en.cppreference.com/w/cpp/container/vector)<[std::reference_wrapper](https://en.cppreference.com/w/cpp/utility/functional/reference_wrapper)>.
## Loading & saving AutoConnectElements
## Loading & saving AutoConnectElements with JSON
## Saving AutoConnectElements
AutoConnect supports that loading the AutoConnectAux as a set of elements, and Loading and saving individual elements. It is not possible to save all AutoConnectElements at once as AutoConnectAux. In both cases the target object is a JSON document.
## Page transition
<imgsrc="../images/ac_load_save.svg">
### <iclass="fa fa-upload"></i> Loading AutoConnectAux & AutoConnectElements with JSON
To load a JSON document as AutoConnectAux use the [**AutoConnect::load**](api.md#load) function and load the JSON document of each AutoConnectElement using the [**AutoConnectAux::loadElement**](apiaux.md#loadelement) function. Although the functions of both are similar, the structure of the target JSON document is different.
The [AutoConnect::load](apiaux.md#load) function loads the entire AutoConnectAux and creates both the AutoConnectAux instance and each AutoConnectElement instance. If the JSON document is described as an array of multiple custom Web pages, all pages contained in the array will be generated. Therefore, it is necessary to supply the JSON document of AutoConnectAux as an input of the load function and must contain the elements described [**here**](acjson.md#json-document-structure-for-autoconnectaux).
The [AutoConnectAux::loadElement](apiaux.md#loadelement) function loads the elements individually into an AutoConnectAux object. The structure of its supplying JSON document is not AutoConnectAux. It must be a [JSON structure for AutoConnectElement](acjson.md#json-object-for-autoconnectelements), but you can specify an array.
```cpp
// AutoConnectAux as a custom Web page.
const char page[] PROGMEM = R"raw(
{
"title": "Settings",
"uri": "/settings",
"menu": true,
"element": [
{
"name": "server",
"type": "ACInput",
"label": "Server"
},
{
"name": "set",
"type": "ACSubmit",
"value": "SET",
"uri" : "/set"
}
]
}
)raw";
// Additional AutoConnectElements.
const char addons[] PROGMEM = R"raw(
[
{
"name": "notes",
"type": "ACText",
"value": "An update period as the below optionally."
},
{
"name": "period",
"type": "ACRadio",
"value": [
"30 sec.",
"60 sec.",
"180 sec."
],
"arrange": "vertical",
"checked": 1
}
]
)raw";
AutoConnect portal;
AutoConnectAux* auxPage;
// Load a custom Web page.
portal.load(page);
// Get a '/settings' page
auxPage = portal.aux("/settings");
// Also, load only AutoConnectRadio named the period.
auxPage->loadElement(addons, "period");
// Retrieve a server name from an AutoConnectText value.
### <iclass="fa fa-download"></i> Saving AutoConnectElements with JSON
To save the AutoConnectElement as a JSON document, use the [AutoConnectAux::saveElement](apiaux.md#saveelement) function. It serializes the contents of the object based on the type of the AutoConnectElement. You can persist a serialized AutoConnectElements as a JSON document to a stream.
The example above saves `server` and `period` elements from the AutoConnectAux object as mentioned above to the `/param` file on SPIFFS. Its JSON document of AutoConnectElements saved by its code looks like this:
```json
[
{
"name": "server",
"type": "ACInput",
"value": "An inputted server name",
"label": "Server",
"placeholder": ""
},
{
"name": "period",
"type": "ACRadio",
"value": [
"30 sec.",
"60 sec.",
"180 sec."
],
"arrange": "vertical",
"checked": 2
}
]
```
## Parameter handling
## Parameter handling
A sketch can access variables of AutoConnectElements in the custom Web page. The value entered into the AutoConnectElements on the page is stored in the member variable of each element by AutoConnect whenever GET/POST transmission occurs.
The following diagram shows the flow of the input values of a custom Web page into a sketch and is the basis for actions to manipulate the values of custom Web pages using sketches.
<imgsrc="./images/ac_param_flow.svg">
### <iclass="fa fa-desktop"></i> When to pick up the values
### <iclass="fa fa-desktop"></i> When setting the initial values
### <iclass="fa fa-wrench"></i> How you can reach the valuess
WebServer.args, PageArgument
WebServer.args, PageArgument
Handling in 'on' handler
Handling in 'on' handler
A sketch can access variables of AutoConnectElements in the custom Web page. The value entered into the AutoConnectElements on the page is stored in the member variable of the element by AutoConnect whenever GET/POST transmission occurs.
d="M 236.936,0 H 91.465 C 87.459,0 83.693,1.56 80.86,4.392 80.859,4.393 80.859,4.393 80.859,4.393 L 33.647,51.604 c -2.833,2.833 -4.393,6.6 -4.393,10.606 v 203.98 c 0,8.271 6.728,15 15,15 7.091,0 185.609,0 192.682,0 8.271,0 15,-6.729 15,-15 V 15 c 0,-8.271 -6.729,-15 -15,-15 z m -25,271.19 H 69.254 v -82.595 c 0,-2.757 2.243,-5 5,-5 h 132.682 c 2.757,0 5,2.243 5,5 z m 25,0 h -15 v -82.595 c 0,-8.271 -6.729,-15 -15,-15 H 74.254 c -8.271,0 -15,6.729 -15,15 v 82.595 h -15 c -2.757,0 -5,-2.243 -5,-5 V 62.211 c 0,-1.335 0.52,-2.591 1.464,-3.536 l 47.21,-47.21 C 88.873,10.521 90.13,10 91.465,10 h 145.471 c 2.757,0 5,2.243 5,5 v 251.19 0 c 0,2.757 -2.243,5 -5,5 z"
inkscape:connector-curvature="0"
style="stroke-width:4.67014837" />
<path
id="path817-0"
d="m 129.96,29.929 h -25.524 c -2.762,0 -5,2.239 -5,5 v 49.333 c 0,2.761 2.238,5 5,5 h 25.524 c 2.762,0 5,-2.239 5,-5 V 34.929 c 0,-2.762 -2.238,-5 -5,-5 z m -5,49.333 H 109.436 V 39.929 h 15.524 z"
inkscape:connector-curvature="0"
style="stroke-width:4.67014837" />
<path
id="path819-6"
d="m 173.448,29.929 h -25.524 c -2.762,0 -5,2.239 -5,5 v 49.333 c 0,2.761 2.238,5 5,5 h 25.524 c 2.762,0 5,-2.239 5,-5 V 34.929 c 0,-2.762 -2.238,-5 -5,-5 z m -5,49.333 H 152.924 V 39.929 h 15.524 z"
inkscape:connector-curvature="0"
style="stroke-width:4.67014837" />
<path
id="path821-1"
d="m 216.936,29.929 h -25.525 c -2.762,0 -5,2.239 -5,5 v 49.333 c 0,2.761 2.238,5 5,5 h 25.525 c 2.762,0 5,-2.239 5,-5 V 34.929 c 0,-2.762 -2.238,-5 -5,-5 z m -5,49.333 H 196.411 V 39.929 h 15.525 z"
d="M 236.936,0 H 91.465 C 87.459,0 83.693,1.56 80.86,4.392 80.859,4.393 80.859,4.393 80.859,4.393 L 33.647,51.604 c -2.833,2.833 -4.393,6.6 -4.393,10.606 v 203.98 c 0,8.271 6.728,15 15,15 7.091,0 185.609,0 192.682,0 8.271,0 15,-6.729 15,-15 V 15 c 0,-8.271 -6.729,-15 -15,-15 z m -25,271.19 H 69.254 v -82.595 c 0,-2.757 2.243,-5 5,-5 h 132.682 c 2.757,0 5,2.243 5,5 z m 25,0 h -15 v -82.595 c 0,-8.271 -6.729,-15 -15,-15 H 74.254 c -8.271,0 -15,6.729 -15,15 v 82.595 h -15 c -2.757,0 -5,-2.243 -5,-5 V 62.211 c 0,-1.335 0.52,-2.591 1.464,-3.536 l 47.21,-47.21 C 88.873,10.521 90.13,10 91.465,10 h 145.471 c 2.757,0 5,2.243 5,5 v 251.19 0 c 0,2.757 -2.243,5 -5,5 z"
inkscape:connector-curvature="0"
style="stroke-width:4.67014837" />
<path
id="path817-0-7"
d="m 129.96,29.929 h -25.524 c -2.762,0 -5,2.239 -5,5 v 49.333 c 0,2.761 2.238,5 5,5 h 25.524 c 2.762,0 5,-2.239 5,-5 V 34.929 c 0,-2.762 -2.238,-5 -5,-5 z m -5,49.333 H 109.436 V 39.929 h 15.524 z"
inkscape:connector-curvature="0"
style="stroke-width:4.67014837" />
<path
id="path819-6-3"
d="m 173.448,29.929 h -25.524 c -2.762,0 -5,2.239 -5,5 v 49.333 c 0,2.761 2.238,5 5,5 h 25.524 c 2.762,0 5,-2.239 5,-5 V 34.929 c 0,-2.762 -2.238,-5 -5,-5 z m -5,49.333 H 152.924 V 39.929 h 15.524 z"
inkscape:connector-curvature="0"
style="stroke-width:4.67014837" />
<path
id="path821-1-2"
d="m 216.936,29.929 h -25.525 c -2.762,0 -5,2.239 -5,5 v 49.333 c 0,2.761 2.238,5 5,5 h 25.525 c 2.762,0 5,-2.239 5,-5 V 34.929 c 0,-2.762 -2.238,-5 -5,-5 z m -5,49.333 H 196.411 V 39.929 h 15.525 z"
d="M 236.936,0 H 91.465 C 87.459,0 83.693,1.56 80.86,4.392 80.859,4.393 80.859,4.393 80.859,4.393 L 33.647,51.604 c -2.833,2.833 -4.393,6.6 -4.393,10.606 v 203.98 c 0,8.271 6.728,15 15,15 7.091,0 185.609,0 192.682,0 8.271,0 15,-6.729 15,-15 V 15 c 0,-8.271 -6.729,-15 -15,-15 z m -25,271.19 H 69.254 v -82.595 c 0,-2.757 2.243,-5 5,-5 h 132.682 c 2.757,0 5,2.243 5,5 z m 25,0 h -15 v -82.595 c 0,-8.271 -6.729,-15 -15,-15 H 74.254 c -8.271,0 -15,6.729 -15,15 v 82.595 h -15 c -2.757,0 -5,-2.243 -5,-5 V 62.211 c 0,-1.335 0.52,-2.591 1.464,-3.536 l 47.21,-47.21 C 88.873,10.521 90.13,10 91.465,10 h 145.471 c 2.757,0 5,2.243 5,5 v 251.19 0 c 0,2.757 -2.243,5 -5,5 z"
inkscape:connector-curvature="0"
style="stroke-width:4.67014837" />
<path
id="path817-6"
d="m 129.96,29.929 h -25.524 c -2.762,0 -5,2.239 -5,5 v 49.333 c 0,2.761 2.238,5 5,5 h 25.524 c 2.762,0 5,-2.239 5,-5 V 34.929 c 0,-2.762 -2.238,-5 -5,-5 z m -5,49.333 H 109.436 V 39.929 h 15.524 z"
inkscape:connector-curvature="0"
style="stroke-width:4.67014837" />
<path
id="path819-1"
d="m 173.448,29.929 h -25.524 c -2.762,0 -5,2.239 -5,5 v 49.333 c 0,2.761 2.238,5 5,5 h 25.524 c 2.762,0 5,-2.239 5,-5 V 34.929 c 0,-2.762 -2.238,-5 -5,-5 z m -5,49.333 H 152.924 V 39.929 h 15.524 z"
inkscape:connector-curvature="0"
style="stroke-width:4.67014837" />
<path
id="path821-5"
d="m 216.936,29.929 h -25.525 c -2.762,0 -5,2.239 -5,5 v 49.333 c 0,2.761 2.238,5 5,5 h 25.525 c 2.762,0 5,-2.239 5,-5 V 34.929 c 0,-2.762 -2.238,-5 -5,-5 z m -5,49.333 H 196.411 V 39.929 h 15.525 z"