diff --git a/README.md b/README.md index ee792ea..5040e86 100644 --- a/README.md +++ b/README.md @@ -96,17 +96,21 @@ Full documentation is available on https://Hieromon.github.io/AutoConnect, some ## Change log -### [0.9.8] Apr. 25, 2019 +### [0.9.8] May 3, 2019 - Supports ArduinoJson 6.9.1 or later. - Supports allocating JsonDocument buffer to PSRAM on ESP32 with ArduinoJson 6.10.0 or later. - Supports **operator`[]`** as a shortcut for AutoConnectAux::getElement function. - Supports **AutoConnectElement::as** function to easily coding for conversion from an AutoConnectElement to an actual type. - Supports new element type **AutoConnectFile** and built-in file uploader. -- Supports a **format attribute** with the AutoConnectText element. -- Supports a **AutoConnectSelect::selected** attribute. -- Fixed blank page responds with Configure new. -- Fixed the value of AutoConnectElements not reflected. +- Supports a **format** attribute with the AutoConnectText element. +- Supports a **selected** attribute with the AutoConnectSelect element. +- Supports multiple element loading with AutoConnectAux::loadElement. - Changed menu labels placement in source files structure. +- Changed API interface of AutoConnect::where function. +- Fixed blank page responds with Configure new. +- Fixed loading elements value missing. +- Fixed losing elements in saveElement with ArduinoJson V6. +- Fixed compile error with older than ESP8266 core 2.5.0. ### [0.9.7] Feb. 25, 2019 - Fixed crash in some environments. Thank you @ageurtse diff --git a/docs/acelements.html b/docs/acelements.html index cb81c92..14e7a74 100644 --- a/docs/acelements.html +++ b/docs/acelements.html @@ -732,6 +732,13 @@ label + + +
  • + + selected + +
  • @@ -1508,6 +1515,13 @@ label + + +
  • + + selected + +
  • @@ -1859,7 +1873,7 @@ Only will be displayed if a label is not specified AutoConnectSelect select("select", { String("Europe/London"), String("Europe/Berlin"), String("Europe/Helsinki"), String("Europe/Moscow"), String("Asia/Dubai") }, "Select TZ name");

    On the page:

    Constructor

    -
    AutoConnectSelect(const char* name, std::vector<String> const& options, const char* label)
    +
    AutoConnectSelect(const char* name, std::vector<String> const& options, const char* label, const uint8_t selected)
     

    name

    @@ -1868,6 +1882,8 @@ Only will be displayed if a label is not specified

    An options is an array of String type for the options which as actually std::vector for an HTML <option> tag. It is an initialization list can be used. The option tags will be generated from each entry in the options, the amount of which is the same as the number of items in an options.

    label

    A label is an optional string. A label is always arranged on the left side of the drop-down list. Specification of a label will generate an HTML <label> tag with an id attribute. The select tag and the label are connected by the id attribute.

    +

    selected

    +

    A selected is an optional value. Specifies that an option should be pre-selected when the page loads.

    AutoConnectSubmit

    AutoConnectSubmit generates an HTML <input type="button"> tag attached onclick attribute. The native code of the onclick attribute is the submission of the form with the POST method.

    Sample
    diff --git a/docs/achandling.html b/docs/achandling.html index c5b4082..e65e3b5 100644 --- a/docs/achandling.html +++ b/docs/achandling.html @@ -1148,7 +1148,7 @@ AutoConnectElements contained in AutoConnectAux object are uniquely identified b

    Loading & saving AutoConnectElements with JSON

    -

    AutoConnect supports reading the custom Web page definitions written in JSON and also supports loading and saving of AutoConnectElements. In both cases, the target object is a JSON document for AutoConnect. However, it can not save all AutoConnectElements contained in the page as a custom Web page. (ie. AutoConnectAux)

    +

    AutoConnect supports reading the custom Web page definitions written in JSON and also supports loading and saving of AutoConnectAux or AutoConnectElements. In both cases, the target object is a JSON document for AutoConnect. However, it can not save all AutoConnectElements contained in the page as a custom Web page. (ie. AutoConnectAux)

    Loading AutoConnectAux & AutoConnectElements with JSON

    To load a JSON document as AutoConnectAux use the AutoConnect::load function and load the JSON document of each AutoConnectElement using the AutoConnectAux::loadElement function. Although the functions of both are similar, the structure of the target JSON document is different.

    @@ -1216,7 +1216,7 @@ AutoConnectElements contained in AutoConnectAux object are uniquely identified b

    Saving AutoConnectElements with JSON

    -

    To save the AutoConnectElement as a JSON document, use the AutoConnectAux::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.

    +

    To save the AutoConnectAux or the AutoConnectElement as a JSON document, use the AutoConnectAux::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.

    // Open a parameter file on the SPIFFS.
     SPIFFS.begin();
     FILE param = SPIFFS.open("/param", "w");
    @@ -1252,6 +1252,7 @@ AutoConnectElements contained in AutoConnectAux object are uniquely identified b
     ]
     
    +

    Above JSON document can be loaded as it is into a custom Web page using the loadElement function. The loadElement function also loads the value of the element, so the saved value can be restored on the custom Web page.

    Custom field data 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.

    @@ -1361,12 +1362,13 @@ AutoConnectElements contained in AutoConnectAux object are uniquely identified b String feelsOn(AutoConnectAux& aux, PageArgument& args) { // Get the AutoConnectInput named "feels". - // The where() function returns the AutoConnectAux of the page that triggered this handler. - AutoConnectInput& feels = portal.where()->getElement<AutoConnectInput>("feels"); - - // Get the AutoConnectText named "echo". - AutoConnectText& echo = aux.getElement<AutoConnectText>("echo"); - + // The where() function returns an uri string of the AutoConnectAux that triggered this handler. + AutoConnectAux* hello = portal.aux(portal.where()); + AutoConnectInput& feels = hello->getElement<AutoConnectInput>("feels"); + + // Get the AutoConnectText named "echo". + AutoConnectText& echo = aux.getElement<AutoConnectText>("echo"); + // Echo back from input-box to /feels page. echo.value = feels.value + String(" and a bold world!"); return String(""); @@ -1385,7 +1387,7 @@ AutoConnectElements contained in AutoConnectAux object are uniquely identified b

    The above example handles in the handler for the values of a custom Web page. An AutoConnect::on function registers a handler for the AutoConnectAux page of the specified uri. The argument of the custom Web page handler is an AutoConnectAux of the page itself and the PageArgument object.

    -

    To retrieve the values entered in a custom Web page you need to access the AutoConnectElement of the page that caused the request to this page and to do this, you use the AutoConnect::where function. The AutoConnect::where function returns a pointer to the AutoConnectAux object of the custom Web page that caused the HTTP request.

    +

    To retrieve the values entered in a custom Web page you need to access the AutoConnectElement of the page that caused the request to this page and to do this, you use the AutoConnect::where function. The AutoConnect::where function returns an uri string of the AutoConnectAux object of the custom Web page that caused the HTTP request.

    The where() function is available for only AutoConnectAux.

    The AutoConnect::where function is available only for the AutoConnectAux object. It is invalid for HTTP requests from individual pages registered with the on handler of ESP8266WebServer/WebServer for ESP32. In other words, the AutoConnect::where function only returns the last AutoConnecAux page called.

    diff --git a/docs/api.html b/docs/api.html index 1023246..24808b0 100644 --- a/docs/api.html +++ b/docs/api.html @@ -1287,14 +1287,14 @@ Register the handler function for undefined URL request detected.
    fnA function of the "not found" handler.

    where

    -

    AutoConenctAux& where(void)
    +

    String where(void)
     
    -Returns a pointer to the AutoConnectAux object of the custom Web page that caused the request to the page.
    +Returns an uri string of the AutoConnectAux uri object of the custom Web page that caused the request to the page.
    AutoConnect identifies the URI (ie. the referrer URI) that caused the request each time from the client occurs and will save the URI If the request source is a custom Web page of AutoConnectAux. The where function returns a pointer of AutoConnectAux which is a URI of a least recent request from the custom Web page.
    This function is provided to access the fields (ie. the AutoConnectElements) with a custom Web page handler of a page and is available only for request source that is the custom Web pages. It is invalid for HTTP requests from individual pages registered with the on handler of ESP8266WebServer/WebServer for ESP32. In other words, this function only returns the AutoConnecAux page which is a least recently displayed.
    Return value
    -
    A pointer to the AutoConnectAux that caused the request the page.
    +
    An uri string of the AutoConnectAux that caused the request the page.

    The where function usage is described in the section Where to pick up the values.

    diff --git a/docs/apiaux.html b/docs/apiaux.html index 45f414e..ee60963 100644 --- a/docs/apiaux.html +++ b/docs/apiaux.html @@ -996,7 +996,7 @@ Add an element to the AutoConnectAux. An added element is displayed on the custo
    Parameters
    addonReference of AutoConnectElements. Specifies one of the AutoConnectElements classes.
    -
    addonsAn array list of reference of AutoConnectElements. An list initialization of the std::vector can be used for the addons parameter cause the actual definition of type AutoConnectElementVT is std::vector<std::reference_wrapper<AutoConnectElement>>.
    +
    addonsAn array list of reference of AutoConnectElements. The list initialization with braced-init-list of the std::vector can be used for the addons parameter cause the actual definition of type AutoConnectElementVT is std::vector<std::reference_wrapper<AutoConnectElement>>.

    getElement

    T& getElement<T>(const String& name)
    @@ -1056,10 +1056,16 @@ Load all AutoConnectElements elements from JSON document into AutoConnectAux as
     

    loadElement

    bool loadElement(const String& in, const String& name = String(""))
     
    +
    bool loadElement(const String& in, std::vector<String> const& names)
    +
    bool loadElement(const __FlashStringHelper* in, const String& name = String(""))
     
    +
    bool loadElement(const __FlashStringHelper* in, std::vector<String> const& names)
    +
    bool loadElement(Stream& in, const String& name = String(""))
     
    +
    bool loadElement(Stream& in, std::vector<String> const& names)
    +
    Load specified element from JSON document into AutoConnectAux. The JSON document specified by the loadElement function must be the AutoConnectElement document structure. When loading from a JSON document that describes multiple elements, its description must be an array syntax.
    Parameters
    @@ -1070,6 +1076,7 @@ Load specified element from JSON document into AutoConnectAux. The JSON document
  • Stream : An entity that inherits stream class, generally SPIFFS or SD.
    nameSpecifies the name to be load. If the name is not specified, the loadElement function will load all elements contained in the JSON document.
    +
    names Spefifies an array list of String indicating the name of the element to be loaded. The list initialization with braced-init-list of the std::vector can be used.
    Return value
    trueSpecified AutoConnectElements successfully loaded.
    falseJSON document loading failed.
    @@ -1180,6 +1187,10 @@ Write elements of AutoConnectAux to the stream. The saveElement function outputs

    The output format is pretty

    The saveElement function outputs a prettified JSON document.

  • +
    +

    It is not complementary with loadElement

    +

    The saveElement function which missing the names parameter without name list to be saved that saves an entire AutoConnectAux element, not just AutoConnectElements. Its saved JSON document is not a complementary input to the loadElement function. The JSON document describing AutoConnectAux saved without the names parameter must be loaded by the AutoConnectAux::load function or AutoConnect::load function.

    +

    setElementValue

    bool setElementValue(const String& name, const String value)
     
    diff --git a/docs/apielements.html b/docs/apielements.html index 74134a6..80f2128 100644 --- a/docs/apielements.html +++ b/docs/apielements.html @@ -1076,6 +1076,13 @@ label + + +
  • + + selected + +
  • @@ -1117,6 +1124,13 @@ operator [ ] + + +
  • + + select + +
  • @@ -1124,6 +1138,13 @@ size +
  • + +
  • + + value + +
  • @@ -1167,7 +1188,7 @@
  • - + value @@ -1241,7 +1262,7 @@
  • - + value @@ -2096,6 +2117,13 @@ label +
  • + +
  • + + selected + +
  • @@ -2137,6 +2165,13 @@ operator [ ] + + +
  • + + select + +
  • @@ -2144,6 +2179,13 @@ size +
  • + +
  • + + value + +
  • @@ -2187,7 +2229,7 @@
  • - + value @@ -2261,7 +2303,7 @@
  • - + value @@ -2586,7 +2628,7 @@ Evaluate the pattern as a regexp and return whether value matches. Always return
    Parameters
    nameThe element name.
    -
    valuesAn array of values of the radio buttons. Specifies an std::vector object.
    +
    valuesAn array of values of the radio buttons. Specifies a std::vector object.
    labelLabel string.
    orderThe direction to arrange the radio buttons.
    checkedAn index to be checked in the radio buttons.
    @@ -2688,13 +2730,14 @@ Returns current checked option of the radio buttons.

    AutoConnectSelect

    Constructor

    -

    AutoConnectSelect(const char* name = "", std::vector<String> const& options = {}, const char* label = "")
    +

    AutoConnectSelect(const char* name = "", std::vector<String> const& options = {}, const char* label = "", const uint8_t selected = 0)
     
    Parameters
    nameThe element name.
    -
    optionsAn array of options of the select element. Specifies an std::vector object.
    +
    optionsAn array of options of the select element. Specifies a std::vector object.
    labelLabel string.
    +
    selectedAn option should be pre-selected when the page loads.

    Public member variables

    name

    @@ -2715,6 +2758,12 @@ Returns current checked option of the radio buttons.
    Type
    String

    +

    selected

    +

    A selected is an optional value. Specifies 1-based index value of an options array that an option should be pre-selected when the page loads. +

    +
    Type
    +
    uint8_t
    +

    Public member functions

    typeOf

    ACElement_t typeOf(void)
    @@ -2749,7 +2798,14 @@ Returns an option string of the index specified by n.
         
    nIndex of options array to return. Its base number is 0.
    Return value
    A reference of a option string indexed by the specified the n.
    -
    +

    +

    select

    +

    void  select(const String& value);
    +
    +Selects an option with the value. +
    +
    Parameter
    +
    valueString value that option should be selected in an option array.

    size

    size_t size(void)
    @@ -2759,6 +2815,14 @@ Returns number of options which contained.
         
    Return value
    Number of options which contained.

    +

    value

    +

    const String& value(void) const;
    +
    +Returns current selected option of the select list. +
    +
    Return value
    +
    A String of an option current selected. If there is no select option, a null string returned.
    +

    AutoConnectSubmit

    Constructor

    AutoConnectSubmit(const char* name = "", const char* value ="", char* uri = "")
    @@ -2776,7 +2840,7 @@ Returns number of options which contained.
         
    Type
    String

    -

    value

    +

    value

    The name of the submit button. It will also be the label of the button.

    Type
    @@ -2815,7 +2879,7 @@ Returns type of AutoConnectElement.
    Type
    String

    -

    value

    +

    value

    A content string of the text element.

    Type
    diff --git a/docs/changelog.html b/docs/changelog.html index a2c184e..2052a59 100644 --- a/docs/changelog.html +++ b/docs/changelog.html @@ -139,7 +139,7 @@ - + Skip to content @@ -657,8 +657,8 @@
    • - - [0.9.8] Apr. 25, 2019 + + [0.9.8] May 3, 2019
    • @@ -754,8 +754,8 @@
      • - - [0.9.8] Apr. 25, 2019 + + [0.9.8] May 3, 2019
      • @@ -828,16 +828,22 @@

        Change log

        -

        [0.9.8] Apr. 25, 2019

        +

        [0.9.8] May 3, 2019

        • Supports ArduinoJson 6.9.1 or later.
        • Supports allocating JsonDocument buffer to PSRAM on ESP32 with ArduinoJson 6.10.0 or later.
        • Supports operator[] as a shortcut for AutoConnectAux::getElement function.
        • Supports AutoConnectElement::as<T> function to easily coding for conversion from an AutoConnectElement to an actual type.
        • Supports new element type AutoConnectFile and built-in file uploader.
        • -
        • Supports a format attribute with the AutoConnectText element.
        • -
        • Fixed blank page responds with Configure new.
        • +
        • Supports a format attribute with the AutoConnectText element.
        • +
        • Supports a selected attribute with the AutoConnectSelect element.
        • +
        • Supports multiple element loading with AutoConnectAux::loadElement.
        • Changed menu labels placement in source files structure.
        • +
        • Changed API interface of **AutoConnect::where function.
        • +
        • Fixed blank page responds with Configure new.
        • +
        • Fixed loading elements value missing.
        • +
        • Fixed losing elements in saveElement with ArduinoJson V6.
        • +
        • Fixed compile error with older than ESP8266 core 2.5.0.

        [0.9.7] Jan. 25, 2019

          diff --git a/docs/faq.html b/docs/faq.html index cd0754a..b805d98 100644 --- a/docs/faq.html +++ b/docs/faq.html @@ -1095,7 +1095,7 @@ For AutoConnect menus to work properly, call To avoid this problem, try changing the channel.

          ESP32 hardware equips only one RF circuitry for WiFi signal. At the AP_STA mode, ESP32 as an AP attempts connect to another AP on another channel while keeping the connection with the station then the channel switching will occur causes the station may be disconnected. But it may not be just a matter of channel switching causes ESP8266 has the same constraints too. It may be a problem with AutoConnect or the arduino core or SDK issue. This problem will persist until a specific solution.

          Does not appear esp8266ap in smartphone.

          -

          Maybe it is successfully connected at the first WiFi.begin. ESP8266 remembers the last SSID successfully connected and will use at the next. It means SoftAP will only start up when the first WiFi.begin() fails.

          +

          Maybe it is successfully connected at the first WiFi.begin. ESP8266 remembers the last SSID successfully connected and will use at the next. It means SoftAP will only start up when the first WiFi.begin() fails.

          The saved SSID would be cleared by WiFi.disconnect() with WIFI_STA mode. If you do not want automatic reconnection, you can erase the memorized SSID with the following simple sketch.

          #include <ESP8266WiFi.h>
           
          diff --git a/docs/images/ac_load_save.svg b/docs/images/ac_load_save.svg
          index c5ef733..bf73a2f 100644
          --- a/docs/images/ac_load_save.svg
          +++ b/docs/images/ac_load_save.svg
          @@ -9,15 +9,61 @@
              xmlns="http://www.w3.org/2000/svg"
              xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
              xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
          -   width="134.10013mm"
          -   height="86.830833mm"
          -   viewBox="0 0 134.10013 86.830834"
          +   width="135.15846mm"
          +   height="81.855286mm"
          +   viewBox="0 0 135.15846 81.855286"
              version="1.1"
              id="svg8"
          -   inkscape:version="0.92.2 (5c3e80d, 2017-08-06)"
          +   inkscape:version="0.92.4 (5da689c313, 2019-01-14)"
              sodipodi:docname="ac_load_save.svg">
             
          +     id="defs2">
          +    
          +      
          +    
          +    
          +      
          +    
          +    
          +      
          +    
          +  
             
          +     transform="translate(-53.740477,-29.775057)">
               
                 
          -    
          -      
          -      
          -      
          -    
          +       width="1.699528"
          +       height="2.319947"
          +       x="119.41328"
          +       y="91.30558"
          +       rx="0.053364325"
          +       ry="0.46776518" />
               
          @@ -595,7 +609,7 @@
               
               
                 
               
          +    AutoConnect::load
          +    AutoConnectAux::loadElement
          +    AutoConnectAux::saveElement
          +    
               
          +       id="g1498"
          +       transform="translate(-0.16225749)">
                 
               
          +    AutoConnectAux::saveElement
               
          +       transform="translate(-0.16225749,36.803633)"
          +       id="g1498-6">
          +      
                 
          +         id="rect1046-3-2-3"
          +         style="opacity:1;vector-effect:none;fill:#3366cc;fill-opacity:1;stroke:#3366cc;stroke-width:1.32291663;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;paint-order:stroke markers fill" />
                 
          +         id="rect1046-3-6-21-0"
          +         style="opacity:1;vector-effect:none;fill:#3366cc;fill-opacity:1;stroke:#3366cc;stroke-width:1.32291663;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;paint-order:stroke markers fill" />
               
          -    AutoConnect::load
          -    AutoConnectAux::loadElement
          -    AutoConnectAux::saveElement
             
           
          diff --git a/docs/search/search_index.json b/docs/search/search_index.json
          index 115389f..a5435df 100644
          --- a/docs/search/search_index.json
          +++ b/docs/search/search_index.json
          @@ -1 +1 @@
          -{"config":{"lang":["en"],"prebuild_index":false,"separator":"[\\s\\-]+"},"docs":[{"location":"index.html","text":"AutoConnect for ESP8266/ESP32 \u00b6 An Arduino library for ESP8266/ESP32 WLAN configuration at run time with web interface. Overview \u00b6 To the dynamic configuration for joining to WLAN with SSID and PSK accordingly. It an Arduino library united with ESP8266WebServer class for ESP8266 or WebServer class for ESP32. Easy implementing the Web interface constituting the WLAN for ESP8266/ESP32 WiFi connection. With this library to make a sketch easily which connects from ESP8266/ESP32 to the access point at runtime by the web interface without hard-coded SSID and password. No need pre-coded SSID & password \u00b6 It is no needed hard-coding in advance the SSID and Password into the sketch to connect between ESP8266/ESP32 and WLAN. You can input SSID & Password from a smartphone via the web interface at runtime. Simple usage \u00b6 AutoConnect control screen will be displayed automatically for establishing new connections. It aids by the captive portal when vested the connection cannot be detected. By using the AutoConnect menu , to manage the connections convenient. Store the established connection \u00b6 The connection authentication data as credentials are saved automatically in EEPROM of ESP8266/ESP32 and You can select the past SSID from the AutoConnect menu . Easy to embed in \u00b6 AutoConnect can be placed easily in your sketch. It's \" begin \" and \" handleClient \" only. Lives with your sketches \u00b6 The sketches which provide the web page using ESP8266WebServer there is, AutoConnect will not disturb it. AutoConnect can use an already instantiated ESP8266WebServer object, or itself can assign it. This effect also applies to ESP32. The corresponding class for ESP32 will be the WebServer. Easy to add the custom Web pages ENHANCED w/v0.9.7 \u00b6 You can easily add your owned web pages that can consist of representative HTML elements and invoke them from the menu. Further it possible importing the custom Web pages declarations described with JSON which stored in PROGMEM, SPIFFS, or SD. Installation \u00b6 Requirements \u00b6 Supported hardware \u00b6 Generic ESP8266 modules (applying the ESP8266 Community's Arduino core) Adafruit HUZZAH ESP8266 (ESP-12) ESP-WROOM-02 Heltec WiFi Kit 8 NodeMCU 0.9 (ESP-12) / NodeMCU 1.0 (ESP-12E) Olimex MOD-WIFI-ESP8266 SparkFun Thing SweetPea ESP-210 ESP32Dev Board (applying the Espressif's arduino-esp32 core) SparkFun ESP32 Thing WEMOS LOLIN D32 Ai-Thinker NodeMCU-32S Heltec WiFi Kit 32 M5Stack And other ESP8266/ESP32 modules supported by the Additional Board Manager URLs of the Arduino-IDE. About flash size on the module The AutoConnect sketch size is relatively large. Large flash capacity is necessary. 512Kbyte (4Mbits) flash inclusion module such as ESP-01 is not recommended. Required libraries \u00b6 AutoConnect requires the following environment and libraries. Arduino IDE The current upstream at the 1.8 level or later is needed. Please install from the official Arduino IDE download page . This step is not required if you already have a modern version. ESP8266 Arduino core AutoConnect targets sketches made on the assumption of ESP8266 Community's Arduino core . Stable 2.4.0 or higher required and the latest release is recommended. Install third-party platform using the Boards Manager of Arduino IDE. Package URL is http://arduino.esp8266.com/stable/package_esp8266com_index.json ESP32 Arduino core Also, to apply AutoConnect to ESP32, the arduino-esp32 core provided by Espressif is needed. Stable 1.0.1 or required and the latest release is recommended. Install third-party platform using the Boards Manager of Arduino IDE. You can add multiple URLs into Additional Board Manager URLs field, separating them with commas. Package URL is https://dl.espressif.com/dl/package_esp32_index.json for ESP32. Additional library (Required) The PageBuilder library to build HTML for ESP8266WebServer is needed. To install the PageBuilder library into your Arduino IDE, you can use the Library Manager . Select the board of ESP8266 series in the Arduino IDE, open the library manager and search keyword ' PageBuilder ' with the topic ' Communication ', then you can see the PageBuilder . The latest version is required 1.3.3 later . 1 Additional library (Optional) By adding the ArduinoJson library, AutoConnect will be able to handle the custom Web pages described with JSON. Since AutoConnect v0.9.7 you can insert user-owned web pages that can consist of representative HTML elements as styled TEXT, INPUT, BUTTON, CHECKBOX, SELECT, SUBMIT and invoke them from the AutoConnect menu. These HTML elements can be added by sketches using the AutoConnect API. Further it possible importing the custom Web pages declarations described with JSON which stored in PROGMEM, SPIFFS, or SD. ArduinoJson is required to use this feature. 2 AutoConnect can work with ArduinoJson both version 5 and version 6 . Install the AutoConnect \u00b6 Clone or download from the AutoConnect GitHub repository . When you select Download, you can import it to Arduino IDE immediately. After downloaded, the AutoConnect-master.zip file will be saved in your download folder. Then in the Arduino IDE, navigate to \"Sketch > Include Library\" . At the top of the drop down list, select the option to \"Add .ZIP Library...\" . Details for Arduino official page . Supported by Library manager. AutoConnect was added to the Arduino IDE library manager. It can be used with the PlatformIO library also. window.onload = function() { Gifffer(); }; Since AutoConnect v0.9.8, PageBuilder v1.3.3 later is required. \u21a9 Using the AutoConnect API natively allows you to sketch custom Web pages without JSON. \u21a9","title":"Overview"},{"location":"index.html#autoconnect-for-esp8266esp32","text":"An Arduino library for ESP8266/ESP32 WLAN configuration at run time with web interface.","title":"AutoConnect for ESP8266/ESP32"},{"location":"index.html#overview","text":"To the dynamic configuration for joining to WLAN with SSID and PSK accordingly. It an Arduino library united with ESP8266WebServer class for ESP8266 or WebServer class for ESP32. Easy implementing the Web interface constituting the WLAN for ESP8266/ESP32 WiFi connection. With this library to make a sketch easily which connects from ESP8266/ESP32 to the access point at runtime by the web interface without hard-coded SSID and password.","title":"Overview"},{"location":"index.html#no-need-pre-coded-ssid-password","text":"It is no needed hard-coding in advance the SSID and Password into the sketch to connect between ESP8266/ESP32 and WLAN. You can input SSID & Password from a smartphone via the web interface at runtime.","title":" No need pre-coded SSID & password"},{"location":"index.html#simple-usage","text":"AutoConnect control screen will be displayed automatically for establishing new connections. It aids by the captive portal when vested the connection cannot be detected. By using the AutoConnect menu , to manage the connections convenient.","title":" Simple usage"},{"location":"index.html#store-the-established-connection","text":"The connection authentication data as credentials are saved automatically in EEPROM of ESP8266/ESP32 and You can select the past SSID from the AutoConnect menu .","title":" Store the established connection"},{"location":"index.html#easy-to-embed-in","text":"AutoConnect can be placed easily in your sketch. It's \" begin \" and \" handleClient \" only.","title":" Easy to embed in"},{"location":"index.html#lives-with-your-sketches","text":"The sketches which provide the web page using ESP8266WebServer there is, AutoConnect will not disturb it. AutoConnect can use an already instantiated ESP8266WebServer object, or itself can assign it. This effect also applies to ESP32. The corresponding class for ESP32 will be the WebServer.","title":" Lives with your sketches"},{"location":"index.html#easy-to-add-the-custom-web-pages-enhanced-wv097","text":"You can easily add your owned web pages that can consist of representative HTML elements and invoke them from the menu. Further it possible importing the custom Web pages declarations described with JSON which stored in PROGMEM, SPIFFS, or SD.","title":" Easy to add the custom Web pages ENHANCED w/v0.9.7"},{"location":"index.html#installation","text":"","title":"Installation"},{"location":"index.html#requirements","text":"","title":"Requirements"},{"location":"index.html#supported-hardware","text":"Generic ESP8266 modules (applying the ESP8266 Community's Arduino core) Adafruit HUZZAH ESP8266 (ESP-12) ESP-WROOM-02 Heltec WiFi Kit 8 NodeMCU 0.9 (ESP-12) / NodeMCU 1.0 (ESP-12E) Olimex MOD-WIFI-ESP8266 SparkFun Thing SweetPea ESP-210 ESP32Dev Board (applying the Espressif's arduino-esp32 core) SparkFun ESP32 Thing WEMOS LOLIN D32 Ai-Thinker NodeMCU-32S Heltec WiFi Kit 32 M5Stack And other ESP8266/ESP32 modules supported by the Additional Board Manager URLs of the Arduino-IDE. About flash size on the module The AutoConnect sketch size is relatively large. Large flash capacity is necessary. 512Kbyte (4Mbits) flash inclusion module such as ESP-01 is not recommended.","title":"Supported hardware"},{"location":"index.html#required-libraries","text":"AutoConnect requires the following environment and libraries. Arduino IDE The current upstream at the 1.8 level or later is needed. Please install from the official Arduino IDE download page . This step is not required if you already have a modern version. ESP8266 Arduino core AutoConnect targets sketches made on the assumption of ESP8266 Community's Arduino core . Stable 2.4.0 or higher required and the latest release is recommended. Install third-party platform using the Boards Manager of Arduino IDE. Package URL is http://arduino.esp8266.com/stable/package_esp8266com_index.json ESP32 Arduino core Also, to apply AutoConnect to ESP32, the arduino-esp32 core provided by Espressif is needed. Stable 1.0.1 or required and the latest release is recommended. Install third-party platform using the Boards Manager of Arduino IDE. You can add multiple URLs into Additional Board Manager URLs field, separating them with commas. Package URL is https://dl.espressif.com/dl/package_esp32_index.json for ESP32. Additional library (Required) The PageBuilder library to build HTML for ESP8266WebServer is needed. To install the PageBuilder library into your Arduino IDE, you can use the Library Manager . Select the board of ESP8266 series in the Arduino IDE, open the library manager and search keyword ' PageBuilder ' with the topic ' Communication ', then you can see the PageBuilder . The latest version is required 1.3.3 later . 1 Additional library (Optional) By adding the ArduinoJson library, AutoConnect will be able to handle the custom Web pages described with JSON. Since AutoConnect v0.9.7 you can insert user-owned web pages that can consist of representative HTML elements as styled TEXT, INPUT, BUTTON, CHECKBOX, SELECT, SUBMIT and invoke them from the AutoConnect menu. These HTML elements can be added by sketches using the AutoConnect API. Further it possible importing the custom Web pages declarations described with JSON which stored in PROGMEM, SPIFFS, or SD. ArduinoJson is required to use this feature. 2 AutoConnect can work with ArduinoJson both version 5 and version 6 .","title":"Required libraries"},{"location":"index.html#install-the-autoconnect","text":"Clone or download from the AutoConnect GitHub repository . When you select Download, you can import it to Arduino IDE immediately. After downloaded, the AutoConnect-master.zip file will be saved in your download folder. Then in the Arduino IDE, navigate to \"Sketch > Include Library\" . At the top of the drop down list, select the option to \"Add .ZIP Library...\" . Details for Arduino official page . Supported by Library manager. AutoConnect was added to the Arduino IDE library manager. It can be used with the PlatformIO library also. window.onload = function() { Gifffer(); }; Since AutoConnect v0.9.8, PageBuilder v1.3.3 later is required. \u21a9 Using the AutoConnect API natively allows you to sketch custom Web pages without JSON. \u21a9","title":"Install the AutoConnect"},{"location":"acelements.html","text":"The elements for the custom Web pages \u00b6 Representative HTML elements for making the custom Web page are provided as AutoConnectElements. AutoConnectButton : Labeled action button AutoConnectCheckbox : Labeled checkbox AutoConnectElement : General tag AutoConnectFile : File uploader AutoConnectInput : Labeled text input box AutoConnectRadio : Labeled radio button AutoConnectSelect : Selection list AutoConnectSubmit : Submit button AutoConnectText : Style attributed text Layout on a custom Web page \u00b6 The elements of the page created by AutoConnectElements are aligned vertically exclude the AutoConnectRadio . You can specify the direction to arrange the radio buttons as AutoConnectRadio vertically or horizontally. This basic layout depends on the CSS of the AutoConnect menu so you can not change drastically. Form and AutoConnectElements \u00b6 All AutoConnectElements placed on custom web pages will be contained into one form. Its form is fixed and created by AutoConnect. The form value (usually the text or checkbox you entered) is sent by AutoConnectSubmit using the POST method with HTTP. The post method sends the actual form data which is a query string whose contents are the name and value of AutoConnectElements. You can retrieve the value for the parameter with the sketch from the query string with ESP8266WebServer::arg function or PageArgument class of the AutoConnect::on handler when the form is submitted. AutoConnectElement - A basic class of elements \u00b6 AutoConnectElement is a base class for other element classes and has common attributes for all elements. It can also be used as a variant of each element. The following items are attributes that AutoConnectElement has and are common to other elements. Sample AutoConnectElement element(\"element\", \"
          \"); On the page: Constructor \u00b6 AutoConnectElement( const char * name, const char * value) name \u00b6 Each element has a name. The name is the String data type. You can identify each element by the name to access it with sketches. value \u00b6 The value is the string which is a source to generate an HTML code. Characteristics of Value vary depending on the element. The value of AutoConnectElement is native HTML code. A string of value is output as HTML as it is. type \u00b6 The type indicates the type of the element and represented as the ACElement_t enumeration type in the sketch. Since AutoConnectElement also acts as a variant of other elements, it can be applied to handle elements collectively. At that time, the type can be referred to by the typeOf() function. The following example changes the font color of all AutoConnectText elements of a custom Web page to gray. AutoConnectAux customPage; AutoConnectElementVT & elements = customPage.getElements(); for (AutoConnectElement & elm : elements) { if (elm.typeOf() == AC_Text) { AutoConnectText & text = reinterpret_cast < AutoConnectText &> (elm); text.style = \"color:gray;\" ; } } The enumerators for ACElement_t are as follows: AutoConnectButton: AC_Button AutoConnectCheckbox: AC_Checkbox AutoConnectElement: AC_Element AutoConnectFile: AC_File AutoConnectInput: AC_Input AutoConnectRadio: AC_Radio AutoConnectSelect: AC_Select AutoConnectSubmit: AC_Submit AutoConnectText: AC_Text Uninitialized element: AC_Unknown Furthermore, to convert an entity that is not an AutoConnectElement to its native type, you must re-interpret that type with c++. Or, you can be coding the sketch more easily with using the as function. AutoConnectAux customPage; AutoConnectElementVT & elements = customPage.getElements(); for (AutoConnectElement & elm : elements) { if (elm.type() == AC_Text) { AutoConnectText & text = customPage[elm.name].as < AutoConnectText > (); text.style = \"color:gray;\" ; // Or, it is also possible to write the code further reduced as follows. // customPage[elm.name].as().style = \"color:gray;\"; } } AutoConnectButton \u00b6 AutoConnectButton generates an HTML < button type = \"button\" > tag and locates a clickable button to a custom Web page. Currently AutoConnectButton corresponds only to name, value, an onclick attribute of HTML button tag. An onclick attribute is generated from an action member variable of the AutoConnectButton, which is mostly used with a JavaScript to activate a script. Sample AutoConnectButton button(\"button\", \"OK\", \"myFunction()\"); On the page: Constructor \u00b6 AutoConnectButton( const char * name, const char * value, const String & action) name \u00b6 It is the name of the AutoConnectButton element and matches the name attribute of the button tag. It also becomes the parameter name of the query string when submitted. value \u00b6 It becomes a value of the value attribute of an HTML button tag. action \u00b6 action is String data type and is an onclick attribute fire on a mouse click on the element. It is mostly used with a JavaScript to activate a script. 1 For example, the following code defines a custom Web page that copies a content of Text1 to Text2 by clicking Button . const char * scCopyText = R\"( )\" ; ACInput(Text1, \"Text1\" ); ACInput(Text2, \"Text2\" ); ACButton(Button, \"COPY\" , \"CopyText()\" ); ACElement(TextCopy, scCopyText); AutoConnectCheckbox \u00b6 AutoConnectCheckbox generates an HTML < input type = \"checkbox\" > tag and a < label > tag. It places horizontally on a custom Web page by default. Sample AutoConnectCheckbox checkbox(\"checkbox\", \"uniqueapid\", \"Use APID unique\", false); On the page: Constructor \u00b6 AutoConnectCheckbox( const char * name, const char * value, const char * label, const bool checked) name \u00b6 It is the name of the AutoConnectCheckbox element and matches the name attribute of the input tag. It also becomes the parameter name of the query string when submitted. value \u00b6 It becomes a value of the value attribute of an HTML < input type = \"checkbox\" > tag. label \u00b6 A label is an optional string. A label is always arranged on the right side of the checkbox. Specification of a label will generate an HTML
    nameSpecifies the name to be load. If the name is not specified, the loadElement function will load all elements contained in the JSON document.
    +
    names Spefifies an array list of String indicating the name of the element to be loaded. The [list initialization](https://en.cppreference.com/w/cpp/language/list_initialization) with braced-init-list of the [std::vector](https://en.cppreference.com/w/cpp/container/vector) can be used.
    **Return value**
    trueSpecified AutoConnectElements successfully loaded.
    falseJSON document loading failed.
    @@ -243,6 +253,9 @@ Write elements of AutoConnectAux to the stream. The saveElement function outputs !!! note "The output format is pretty" The saveElement function outputs a prettified JSON document. +!!! Info "It is not complementary with loadElement" + The saveElement function which missing the names parameter without name list to be saved that saves an entire AutoConnectAux element, not just AutoConnectElements. Its saved JSON document is not a complementary input to the loadElement function. The JSON document describing AutoConnectAux saved without the names parameter must be loaded by the [AutoConnectAux::load](apiaux.md#load) function or [AutoConnect::load](api.md#load) function. + ### setElementValue ```cpp diff --git a/mkdocs/apielements.md b/mkdocs/apielements.md index 004d360..235ec67 100644 --- a/mkdocs/apielements.md +++ b/mkdocs/apielements.md @@ -349,7 +349,7 @@ AutoConnectRadio(const char* name = "", std::vector const& values = {},
    **Parameters**
    nameThe element name.
    -
    valuesAn array of values of the radio buttons. Specifies an [std::vector](https://en.cppreference.com/w/cpp/container/vector) object.
    +
    valuesAn array of values of the radio buttons. Specifies a [std::vector](https://en.cppreference.com/w/cpp/container/vector) object.
    labelLabel string.
    orderThe direction to arrange the radio buttons.
    checkedAn index to be checked in the radio buttons.
    @@ -487,13 +487,14 @@ Returns current checked option of the radio buttons. ### Constructor ```cpp -AutoConnectSelect(const char* name = "", std::vector const& options = {}, const char* label = "") +AutoConnectSelect(const char* name = "", std::vector const& options = {}, const char* label = "", const uint8_t selected = 0) ```
    **Parameters**
    nameThe element name.
    -
    optionsAn array of options of the select element. Specifies an [std::vector](https://en.cppreference.com/w/cpp/container/vector) object.
    +
    optionsAn array of options of the select element. Specifies a [std::vector](https://en.cppreference.com/w/cpp/container/vector) object.
    labelLabel string.
    +
    selectedAn option should be pre-selected when the page loads.
    ### Public member variables @@ -522,6 +523,14 @@ A label is an optional string. A label will be arranged in the top of the select
    String
    +#### selected + +A `selected` is an optional value. Specifies 1-based index value of an options array that an option should be pre-selected when the page loads. +
    +
    **Type**
    +
    uint8_t
    +
    + ### Public member functions #### typeOf @@ -568,7 +577,17 @@ Returns an option string of the index specified by **_n_**.
    nIndex of options array to return. Its base number is 0.
    **Return value**
    A reference of a option string indexed by the specified the **n**.
    -
    + + +#### select + +```cpp +void select(const String& value); +``` +Selects an option with the value. +
    +
    **Parameter**
    +
    valueString value that option should be selected in an option array.
    #### size @@ -582,6 +601,17 @@ Returns number of options which contained.
    Number of options which contained.
    +#### value + +```cpp +const String& value(void) const; +``` +Returns current selected option of the select list. +
    +
    **Return value**
    +
    A String of an option current selected. If there is no select option, a null string returned.
    +
    + ## AutoConnectSubmit ### Constructor diff --git a/mkdocs/changelog.md b/mkdocs/changelog.md index 17c1009..cd6af8b 100644 --- a/mkdocs/changelog.md +++ b/mkdocs/changelog.md @@ -1,12 +1,18 @@ -#### [0.9.8] Apr. 25, 2019 +#### [0.9.8] May 3, 2019 - Supports ArduinoJson 6.9.1 or later. - Supports allocating JsonDocument buffer to PSRAM on ESP32 with ArduinoJson 6.10.0 or later. - Supports [**operator`[]`**](apiaux.md#operator) as a shortcut for AutoConnectAux::getElement function. - Supports [**AutoConnectElement::as**](apielements.md#ast62) function to easily coding for conversion from an AutoConnectElement to an actual type. - Supports new element type [**AutoConnectFile**](acelements.md#autoconnectfile) and built-in file uploader. -- Supports a [**format attribute**](acelements.md#format) with the AutoConnectText element. -- Fixed blank page responds with Configure new. +- Supports a [**format**](acelements.md#format) attribute with the AutoConnectText element. +- Supports a [**selected**](acelements.md#selected) attribute with the AutoConnectSelect element. +- Supports multiple element loading with [AutoConnectAux::loadElement](apiaux.md#loadelement). - Changed menu labels placement in source files structure. +- Changed API interface of [**AutoConnect::where](api.md#where) function. +- Fixed blank page responds with Configure new. +- Fixed loading elements value missing. +- Fixed losing elements in saveElement with ArduinoJson V6. +- Fixed compile error with older than ESP8266 core 2.5.0. #### [0.9.7] Jan. 25, 2019 diff --git a/mkdocs/images/ac_load_save.svg b/mkdocs/images/ac_load_save.svg index c5ef733..bf73a2f 100644 --- a/mkdocs/images/ac_load_save.svg +++ b/mkdocs/images/ac_load_save.svg @@ -9,15 +9,61 @@ xmlns="http://www.w3.org/2000/svg" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" - width="134.10013mm" - height="86.830833mm" - viewBox="0 0 134.10013 86.830834" + width="135.15846mm" + height="81.855286mm" + viewBox="0 0 135.15846 81.855286" version="1.1" id="svg8" - inkscape:version="0.92.2 (5c3e80d, 2017-08-06)" + inkscape:version="0.92.4 (5da689c313, 2019-01-14)" sodipodi:docname="ac_load_save.svg"> + id="defs2"> + + + + + + + + + + + transform="translate(-53.740477,-29.775057)"> - - - - - + width="1.699528" + height="2.319947" + x="119.41328" + y="91.30558" + rx="0.053364325" + ry="0.46776518" /> @@ -595,7 +609,7 @@ + AutoConnect::load + AutoConnectAux::loadElement + AutoConnectAux::saveElement + + id="g1498" + transform="translate(-0.16225749)"> + AutoConnectAux::saveElement + transform="translate(-0.16225749,36.803633)" + id="g1498-6"> + + id="rect1046-3-2-3" + style="opacity:1;vector-effect:none;fill:#3366cc;fill-opacity:1;stroke:#3366cc;stroke-width:1.32291663;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;paint-order:stroke markers fill" /> + id="rect1046-3-6-21-0" + style="opacity:1;vector-effect:none;fill:#3366cc;fill-opacity:1;stroke:#3366cc;stroke-width:1.32291663;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;paint-order:stroke markers fill" /> - AutoConnect::load - AutoConnectAux::loadElement - AutoConnectAux::saveElement diff --git a/src/AutoConnect.h b/src/AutoConnect.h index 8519342..b1e98b4 100644 --- a/src/AutoConnect.h +++ b/src/AutoConnect.h @@ -185,7 +185,7 @@ class AutoConnect { void join(AutoConnectAux& aux); void join(AutoConnectAuxVT auxVector); bool on(const String& uri, const AuxHandlerFunctionT handler, AutoConnectExitOrder_t order = AC_EXIT_AHEAD); - AutoConnectAux& where(void) const { return *aux(_auxUri); } + String where(void) const { return _auxUri; } /** For AutoConnectAux described in JSON */ #ifdef AUTOCONNECT_USE_JSON diff --git a/src/AutoConnectAux.cpp b/src/AutoConnectAux.cpp index bbdb500..c84371d 100644 --- a/src/AutoConnectAux.cpp +++ b/src/AutoConnectAux.cpp @@ -673,34 +673,58 @@ bool AutoConnectAux::_load(JsonObject& jb) { * @return A reference of loaded AutoConnectElement instance. */ bool AutoConnectAux::loadElement(const String& in, const String& name) { - return _parseElement(in, name); + return _parseElement(in, name); } bool AutoConnectAux::loadElement(const __FlashStringHelper* in, const String& name) { - return _parseElement(in, name); + return _parseElement(in, name); } bool AutoConnectAux::loadElement(Stream& in, const String& name) { - return _parseElement(in, name); + return _parseElement(in, name); } -bool AutoConnectAux::_loadElement(JsonVariant& jb, const String& name) { +bool AutoConnectAux::loadElement(const String& in, std::vector const& names) { + return _parseElement const&>(in, names); +} + +bool AutoConnectAux::loadElement(const __FlashStringHelper* in, std::vector const& names) { + return _parseElement const&>(in, names); +} + +bool AutoConnectAux::loadElement(Stream& in, std::vector const& names) { + return _parseElement const&>(in, names); +} + +bool AutoConnectAux::_loadElement(JsonVariant& jb, std::vector const& names) { bool rc = true; + for (const String& name : names) + rc &= _loadElement(jb, name); + return rc; +} + +bool AutoConnectAux::_loadElement(JsonVariant& jb, const String& name) { + bool rc = false; if (jb.is()) { ArduinoJsonArray elements = jb.as(); for (ArduinoJsonObject element : elements) { + if (name.length()) { + //Finds an element with the specified name in the JSON array and loads it. + if (!name.equalsIgnoreCase(element[F(AUTOCONNECT_JSON_KEY_NAME)].as())) + continue; + } AutoConnectElement& elm = _loadElement(element, name); - if (!elm.name.length()) { - rc = false; + if (elm.name.length()) + rc = true; + if (name.length()) break; - } } } else { ArduinoJsonObject element = jb.as(); AutoConnectElement& elm = _loadElement(element, name); - if (!elm.name.length()) - rc = false; + if (elm.name.length()) + rc = true; } return rc; } @@ -750,19 +774,26 @@ size_t AutoConnectAux::saveElement(Stream& out, std::vector const& names size_t size_n = 0; // Calculate JSON buffer size - if (amount == 0) + if (amount == 0) { bufferSize += JSON_OBJECT_SIZE(4); + bufferSize += sizeof(AUTOCONNECT_JSON_KEY_TITLE) + _title.length() + sizeof(AUTOCONNECT_JSON_KEY_URI) + _uriStr.length() + sizeof(AUTOCONNECT_JSON_KEY_MENU) + sizeof("false") + sizeof(AUTOCONNECT_JSON_KEY_ELEMENT); + } if (amount != 1) bufferSize += JSON_ARRAY_SIZE(amount); - for (String name : names) - for (AutoConnectElement& elm : _addonElm) - if (elm.name.equalsIgnoreCase(name)) { - bufferSize += elm.getObjectSize(); - break; - } + for (AutoConnectElement& elmEach : _addonElm) { + AutoConnectElement* elm = &elmEach; + if (amount > 0) { + String& elmName = elm->name; + auto aim = std::find_if(names.begin(), names.end(), [&](const String& n) { return n.equalsIgnoreCase(elmName); }); + if (aim == names.end()) + continue; + } + bufferSize += elm->getObjectSize(); + } // Round up to 16 boundary - bufferSize = bufferSize > 0 ? ((bufferSize + 16) & (~0xf)) : bufferSize; + bufferSize = bufferSize > 0 ? ((bufferSize + 128) & (~0xf)) : bufferSize; + AC_DBG("JSON buffer size:%d\n", bufferSize); // Serialization if (bufferSize > 0) { diff --git a/src/AutoConnectAux.h b/src/AutoConnectAux.h index 3bd7db1..7bd2b02 100644 --- a/src/AutoConnectAux.h +++ b/src/AutoConnectAux.h @@ -75,9 +75,12 @@ class AutoConnectAux : public PageBuilder { bool load(const __FlashStringHelper* in); /**< Load whole elements to AutoConnectAux Page */ bool load(Stream& in); /**< Load whole elements to AutoConnectAux Page */ bool loadElement(const String& in, const String& name = String("")); /**< Load specified element */ - bool loadElement(const __FlashStringHelper* in, const String& name = String("")); /**< Load specified element */ + bool loadElement(const String& in, std::vector const& names);/**< Load any specified elements */ + bool loadElement(const __FlashStringHelper* in, const String& name = String("")); /**< Load specified element */ + bool loadElement(const __FlashStringHelper* in, std::vector const& names); /**< Load any specified elements */ bool loadElement(Stream& in, const String& name = String("")); /**< Load specified element */ - size_t saveElement(Stream& out, std::vector const& names = {}); /**< Write elements of AutoConnectAux to the stream */ + bool loadElement(Stream& in, std::vector const& names); /**< Load any specified elements */ + size_t saveElement(Stream& out, std::vector const& names = {}); /**< Write elements of AutoConnectAux to the stream */ #endif // !AUTOCONNECT_USE_JSON protected: @@ -98,11 +101,38 @@ class AutoConnectAux : public PageBuilder { bool _parseJson(T in); bool _load(JsonObject& in); /**< Load all elements from JSON object */ bool _loadElement(JsonVariant& in, const String& name); /**< Load an element as specified name from JSON object */ - template - bool _parseElement(T in, const String& name); + bool _loadElement(JsonVariant& in, std::vector const& names); /**< Load any elements as specified name from JSON object */ AutoConnectElement& _loadElement(JsonObject& in, const String& name); /**< Load an element as specified name from JSON object */ AutoConnectElement* _createElement(const JsonObject& json); /**< Create an AutoConnectElement instance from JSON object */ static ACElement_t _asElementType(const String& type); /**< Convert a string of element type to the enumeration value */ + /** + * Parse and load a JSON document which declares one of the AutoConnectElement. + * The compiler instantiates this template according to the stored data type that contains the JSON document. + * This template also generates different parsing function calls depending on the ArduinoJson version. + * @param T An object type of the JSON document which must be a passable object to ArduinoJson. + * @param U An instance of a souce name to load. + */ + template::value || std::is_same const&>::value>::type* = nullptr> + bool _parseElement(T in, U name) { + ArduinoJsonBuffer jsonBuffer(AUTOCONNECT_JSONBUFFER_PRIMITIVE_SIZE); + JsonVariant jb; + #if ARDUINOJSON_VERSION_MAJOR<=5 + jb = jsonBuffer.parse(in); + if (!jb.success()) { + AC_DBG("JSON parse error\n"); + return false; + } + #else + DeserializationError err = deserializeJson(jsonBuffer, in); + if (err) { + AC_DBG("Deserialize:%s\n", err.c_str()); + return false; + } + jb = jsonBuffer.as(); + #endif + return _loadElement(jb, name); + } #endif // !AUTOCONNECT_USE_JSON String _title; /**< A title of the page */ diff --git a/src/AutoConnectAuxImpl.h b/src/AutoConnectAuxImpl.h index 66d01d7..6ee9cc3 100644 --- a/src/AutoConnectAuxImpl.h +++ b/src/AutoConnectAuxImpl.h @@ -230,37 +230,6 @@ bool AutoConnectAux::_parseJson(T in) { return _load(jb); } -/** - * Parse and load a JSON document which declares one of the AutoConnectElement. - * The compiler instantiates this template according to the stored data - * type that contains the JSON document. - * This template also generates different parsing function calls - * depending on the ArduinoJson version. - * @param T An object type of the JSON document which must be a - * passable object to ArduinoJson. - * @param in An instance of a source JSON document to load. - */ -template -bool AutoConnectAux::_parseElement(T in, const String& name) { - ArduinoJsonBuffer jsonBuffer(AUTOCONNECT_JSONBUFFER_PRIMITIVE_SIZE); - JsonVariant jb; -#if ARDUINOJSON_VERSION_MAJOR<=5 - jb = jsonBuffer.parse(in); - if (!jb.success()) { - AC_DBG("JSON parse error\n"); - return false; - } -#else - DeserializationError err = deserializeJson(jsonBuffer, in); - if (err) { - AC_DBG("Deserialize:%s\n", err.c_str()); - return false; - } - jb = jsonBuffer.as(); -#endif - return _loadElement(jb, name); -} - /** * Get AutoConnectElementJson element. * @param name an element name. diff --git a/src/AutoConnectElementJsonImpl.h b/src/AutoConnectElementJsonImpl.h index fd03913..65f260f 100644 --- a/src/AutoConnectElementJsonImpl.h +++ b/src/AutoConnectElementJsonImpl.h @@ -257,9 +257,9 @@ void AutoConnectInputJson::serialize(JsonObject& json) { * @return An object size for JsonBuffer. */ size_t AutoConnectRadioJson::getObjectSize() const { - size_t size =AutoConnectElementJson::getObjectSize() + JSON_OBJECT_SIZE(3) + _values.size() * JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(1); - size += sizeof(AUTOCONNECT_JSON_KEY_LABEL) + label.length() + sizeof(AUTOCONNECT_JSON_KEY_ARRANGE) + sizeof(AUTOCONNECT_JSON_VALUE_HORIZONTAL) + sizeof(AUTOCONNECT_JSON_KEY_CHECKED); - for (String _value : _values) + size_t size = AutoConnectElementJson::getObjectSize() + JSON_OBJECT_SIZE(2) + JSON_ARRAY_SIZE(_values.size()); + size += sizeof(AUTOCONNECT_JSON_KEY_LABEL) + label.length() + sizeof(AUTOCONNECT_JSON_KEY_ARRANGE) + sizeof(AUTOCONNECT_JSON_VALUE_HORIZONTAL) + sizeof(AUTOCONNECT_JSON_KEY_CHECKED) + 2; + for (const String& _value : _values) size += _value.length(); return size; } @@ -310,7 +310,7 @@ void AutoConnectRadioJson::serialize(JsonObject& json) { json[F(AUTOCONNECT_JSON_KEY_TYPE)] = String(F(AUTOCONNECT_JSON_TYPE_ACRADIO)); json[F(AUTOCONNECT_JSON_KEY_LABEL)] = label; ArduinoJsonArray values = json.createNestedArray(F(AUTOCONNECT_JSON_KEY_VALUE)); - for (String v : _values) + for (const String& v : _values) values.add(v); switch (order) { case AC_Horizontal: @@ -329,9 +329,9 @@ void AutoConnectRadioJson::serialize(JsonObject& json) { * @return An object size for JsonBuffer. */ size_t AutoConnectSelectJson::getObjectSize() const { - size_t size = AutoConnectElementJson::getObjectSize() + JSON_OBJECT_SIZE(4) + _options.size() * JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(1); - size += sizeof(AUTOCONNECT_JSON_KEY_LABEL) + label.length(); - for (String _option : _options) + size_t size = AutoConnectElementJson::getObjectSize() + JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(_options.size()); + size += sizeof(AUTOCONNECT_JSON_KEY_LABEL) + label.length() + sizeof(AUTOCONNECT_JSON_KEY_SELECTED) + 2; + for (const String& _option : _options) size += _option.length(); return size; }