diff --git a/src/AutoConnect.h b/src/AutoConnect.h index a758c6b..fca34c8 100644 --- a/src/AutoConnect.h +++ b/src/AutoConnect.h @@ -39,10 +39,10 @@ typedef enum AC_SAVECREDENTIAL { AC_SAVECREDENTIAL_AUTO } AC_SAVECREDENTIAL_t; -typedef enum AC_URIONBOOT { - AC_URIONBOOT_ROOT, - AC_URIONBOOT_HOME -} AC_URIONBOOT_t; +typedef enum AC_ONBOOTURI { + AC_ONBOOTURI_ROOT, + AC_ONBOOTURI_HOME +} AC_ONBOOTURI_t; class AutoConnectConfig { public: @@ -60,7 +60,7 @@ class AutoConnectConfig { channel(AUTOCONNECT_AP_CH), hidden(0), autoSave(AC_SAVECREDENTIAL_AUTO), - bootUri(AC_URIONBOOT_ROOT), + bootUri(AC_ONBOOTURI_ROOT), boundaryOffset(AC_IDENTIFIER_OFFSET), uptime(AUTOCONNECT_STARTUPTIME), autoRise(true), @@ -85,7 +85,7 @@ class AutoConnectConfig { channel(channel), hidden(0), autoSave(AC_SAVECREDENTIAL_AUTO), - bootUri(AC_URIONBOOT_ROOT), + bootUri(AC_ONBOOTURI_ROOT), boundaryOffset(AC_IDENTIFIER_OFFSET), uptime(AUTOCONNECT_STARTUPTIME), autoRise(true), @@ -134,7 +134,7 @@ class AutoConnectConfig { uint8_t channel; /**< SoftAP used wifi channel */ uint8_t hidden; /**< SoftAP SSID hidden */ AC_SAVECREDENTIAL_t autoSave; /**< Auto save credential */ - AC_URIONBOOT_t bootUri; /**< An uri invoking after reset */ + AC_ONBOOTURI_t bootUri; /**< An uri invoking after reset */ uint16_t boundaryOffset; /**< The save storage offset of EEPROM */ int uptime; /**< Length of start up time */ bool autoRise; /**< Automatic starting the captive portal */ diff --git a/src/AutoConnectAux.cpp b/src/AutoConnectAux.cpp index b70163e..f35d3bb 100644 --- a/src/AutoConnectAux.cpp +++ b/src/AutoConnectAux.cpp @@ -97,6 +97,7 @@ AutoConnectElement* AutoConnectAux::getElement(const String name) { for (std::size_t n = 0; n < _addonElm.size(); n++) if (_addonElm[n].get().name == name) return &(_addonElm[n].get()); + AC_DBG("Element<%s> not registered\n", name.c_str()); return nullptr; } @@ -121,6 +122,71 @@ bool AutoConnectAux::release(const String name) { return rc; } +/** + * Set the value to specified element. + * @param name A string of element name to set the value. + * @param value Setting value. (String) + * @return true The value was set. + * @return false An element specified name is not registered, + * or its element value does not match storage type. + */ +bool AutoConnectAux::setElementValue(const String name, const String value) { + AutoConnectElement* elm = getElement(name); + if (elm) { + if (elm->typeOf() != AC_Select && elm->typeOf() != AC_Radio) { + elm->value = value; + return true; + } + else + AC_DBG("Element<%s> value type mismatch\n", name.c_str()); + } + else + AC_DBG("Element<%s> not registered\n", name.c_str()); + return false; +} + +/** + * Set the value to specified element. + * @param name A string of element name to set the value. + * @param value Setting value. (String) + * @return true The value was set. + * @return false An element specified name is not registered, + * or its element value must be array. + */ +bool AutoConnectAux::setElementValue(const String name, std::vector values) { + bool rc = false; + + AutoConnectElement* elm = getElement(name); + if (elm) { + switch (elm->typeOf()) { + case AC_Radio: { + AutoConnectRadio* elmRadio = reinterpret_cast(elm); + elmRadio->empty(); + for (String v : values) + elmRadio->add(v); + rc = true; + break; + } + case AC_Select: { + AutoConnectSelect* elmSelect = reinterpret_cast(elm); + elmSelect->empty(); + for (String o : values) + elmSelect->add(o); + rc = true; + break; + } + default: { + AC_DBG("Element<%s> value type mismatch\n", name.c_str()); + break; + } + } + } + else + AC_DBG("Element<%s> not registered\n", name.c_str()); + + return rc; +} + /** * Concatenates subsequent AutoConnectAux pages starting from oneself * to the chain list. @@ -244,6 +310,16 @@ const String AutoConnectAux::_injectMenu(PageArgument& args) { return menuItem; } +/** + * Returns a null element as static storage. + * This static element is referred by invalid JSON data. + * @return A reference of a static element defined by name as null. + */ +AutoConnectElement& AutoConnectAux::_nullElement() { + static AutoConnectElement nullElement("", ""); + return nullElement; +} + #ifndef AUTOCONNECT_USE_JSON /** @@ -252,11 +328,13 @@ const String AutoConnectAux::_injectMenu(PageArgument& args) { * @return A reference of AutoConnectButton class. */ template<> -AutoConnectButtonBasis& AutoConnectAux::getElement(const String name) { +AutoConnectButtonBasis& AutoConnectAux::getElement(const char* name) { AutoConnectElement* elm = getElement(name); if (elm) { if (elm->typeOf() == AC_Button) return *(reinterpret_cast(elm)); + else + AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf()); } return reinterpret_cast(_nullElement()); } @@ -267,11 +345,13 @@ AutoConnectButtonBasis& AutoConnectAux::getElement(const String name) { * @return A reference of AutoConnectCheckbox class. */ template<> -AutoConnectCheckboxBasis& AutoConnectAux::getElement(const String name) { +AutoConnectCheckboxBasis& AutoConnectAux::getElement(const char* name) { AutoConnectElement* elm = getElement(name); if (elm) { if (elm->typeOf() == AC_Checkbox) return *(reinterpret_cast(elm)); + else + AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf()); } return reinterpret_cast(_nullElement()); } @@ -282,11 +362,13 @@ AutoConnectCheckboxBasis& AutoConnectAux::getElement(const String name) { * @return A reference of AutoConnectInput class. */ template<> -AutoConnectInputBasis& AutoConnectAux::getElement(const String name) { +AutoConnectInputBasis& AutoConnectAux::getElement(const char* name) { AutoConnectElement* elm = getElement(name); if (elm) { if (elm->typeOf() == AC_Input) return *(reinterpret_cast(elm)); + else + AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf()); } return reinterpret_cast(_nullElement()); } @@ -297,11 +379,13 @@ AutoConnectInputBasis& AutoConnectAux::getElement(const String name) { * @return A reference of AutoConnectRadio class. */ template<> -AutoConnectRadioBasis& AutoConnectAux::getElement(const String name) { +AutoConnectRadioBasis& AutoConnectAux::getElement(const char* name) { AutoConnectElement* elm = getElement(name); if (elm) { if (elm->typeOf() == AC_Radio) return *(reinterpret_cast(elm)); + else + AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf()); } return reinterpret_cast(_nullElement()); } @@ -312,11 +396,13 @@ AutoConnectRadioBasis& AutoConnectAux::getElement(const String name) { * @return A reference of AutoConnectSelect class. */ template<> -AutoConnectSelectBasis& AutoConnectAux::getElement(const String name) { +AutoConnectSelectBasis& AutoConnectAux::getElement(const char* name) { AutoConnectElement* elm = getElement(name); if (elm) { if (elm->typeOf() == AC_Select) return *(reinterpret_cast(elm)); + else + AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf()); } return reinterpret_cast(_nullElement()); } @@ -327,11 +413,13 @@ AutoConnectSelectBasis& AutoConnectAux::getElement(const String name) { * @return A reference of AutoConnectSubmit class. */ template<> -AutoConnectSubmitBasis& AutoConnectAux::getElement(const String name) { +AutoConnectSubmitBasis& AutoConnectAux::getElement(const char* name) { AutoConnectElement* elm = getElement(name); if (elm) { if (elm->typeOf() == AC_Submit) return *(reinterpret_cast(elm)); + else + AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf()); } return reinterpret_cast(_nullElement()); } @@ -342,11 +430,13 @@ AutoConnectSubmitBasis& AutoConnectAux::getElement(const String name) { * @return A reference of AutoConnectText class. */ template<> -AutoConnectTextBasis& AutoConnectAux::getElement(const String name) { +AutoConnectTextBasis& AutoConnectAux::getElement(const char* name) { AutoConnectElement* elm = getElement(name); if (elm) { if (elm->typeOf() == AC_Text) return *(reinterpret_cast(elm)); + else + AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf()); } return reinterpret_cast(_nullElement()); } @@ -374,11 +464,13 @@ bool AutoConnectAux::_jbLiteral; /**< JSON object lexical status */ * @return A reference of AutoConnectButton class. */ template<> -AutoConnectButtonJson& AutoConnectAux::getElement(const String name) { +AutoConnectButtonJson& AutoConnectAux::getElement(const char* name) { AutoConnectElement* elm = getElement(name); if (elm) { if (elm->typeOf() == AC_Button) return *(reinterpret_cast(elm)); + else + AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf()); } return reinterpret_cast(_nullElement()); } @@ -389,11 +481,13 @@ AutoConnectButtonJson& AutoConnectAux::getElement(const String name) { * @return A reference of AutoConnectCheckbox class. */ template<> -AutoConnectCheckboxJson& AutoConnectAux::getElement(const String name) { +AutoConnectCheckboxJson& AutoConnectAux::getElement(const char* name) { AutoConnectElement* elm = getElement(name); if (elm) { if (elm->typeOf() == AC_Checkbox) return *(reinterpret_cast(elm)); + else + AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf()); } return reinterpret_cast(_nullElement()); } @@ -404,11 +498,13 @@ AutoConnectCheckboxJson& AutoConnectAux::getElement(const String name) { * @return A reference of AutoConnectInput class. */ template<> -AutoConnectInputJson& AutoConnectAux::getElement(const String name) { +AutoConnectInputJson& AutoConnectAux::getElement(const char* name) { AutoConnectElement* elm = getElement(name); if (elm) { if (elm->typeOf() == AC_Input) return *(reinterpret_cast(elm)); + else + AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf()); } return reinterpret_cast(_nullElement()); } @@ -419,11 +515,13 @@ AutoConnectInputJson& AutoConnectAux::getElement(const String name) { * @return A reference of AutoConnectRadio class. */ template<> -AutoConnectRadioJson& AutoConnectAux::getElement(const String name) { +AutoConnectRadioJson& AutoConnectAux::getElement(const char* name) { AutoConnectElement* elm = getElement(name); if (elm) { if (elm->typeOf() == AC_Radio) return *(reinterpret_cast(elm)); + else + AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf()); } return reinterpret_cast(_nullElement()); } @@ -434,11 +532,13 @@ AutoConnectRadioJson& AutoConnectAux::getElement(const String name) { * @return A reference of AutoConnectSelect class. */ template<> -AutoConnectSelectJson& AutoConnectAux::getElement(const String name) { +AutoConnectSelectJson& AutoConnectAux::getElement(const char* name) { AutoConnectElement* elm = getElement(name); if (elm) { if (elm->typeOf() == AC_Select) return *(reinterpret_cast(elm)); + else + AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf()); } return reinterpret_cast(_nullElement()); } @@ -449,11 +549,13 @@ AutoConnectSelectJson& AutoConnectAux::getElement(const String name) { * @return A reference of AutoConnectSubmit class. */ template<> -AutoConnectSubmitJson& AutoConnectAux::getElement(const String name) { +AutoConnectSubmitJson& AutoConnectAux::getElement(const char* name) { AutoConnectElement* elm = getElement(name); if (elm) { if (elm->typeOf() == AC_Submit) return *(reinterpret_cast(elm)); + else + AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf()); } return reinterpret_cast(_nullElement()); } @@ -464,11 +566,13 @@ AutoConnectSubmitJson& AutoConnectAux::getElement(const String name) { * @return A reference of AutoConnectText class. */ template<> -AutoConnectTextJson& AutoConnectAux::getElement(const String name) { +AutoConnectTextJson& AutoConnectAux::getElement(const char* name) { AutoConnectElement* elm = getElement(name); if (elm) { if (elm->typeOf() == AC_Text) return *(reinterpret_cast(elm)); + else + AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf()); } return reinterpret_cast(_nullElement()); } @@ -669,8 +773,8 @@ bool AutoConnectAux::_load(JsonObject& jb) { * elements are to be loaded. * @return A reference of loaded AutoConnectElement instance. */ -AutoConnectElement& AutoConnectAux::loadElement(const char* in, const String name) { - const size_t bufferSize = _calcJsonBufferSize(in); +AutoConnectElement& AutoConnectAux::loadElement(const String in, const String name) { + const size_t bufferSize = _calcJsonBufferSize(in.c_str()); DynamicJsonBuffer jsonBuffer(bufferSize); JsonObject& jb = jsonBuffer.parseObject(in); return _loadElement(jb, name); @@ -731,32 +835,26 @@ AutoConnectElement& AutoConnectAux::_loadElement(JsonObject& jb, const String na /** * Serialize a element to JSON and write it to the stream. * @param out An output stream - * @param element A reference of the element to be output. * @return Number of byte output */ -size_t AutoConnectAux::saveElement(Stream& out, const AutoConnectElement& element) { - DynamicJsonBuffer jsonBuffer; - JsonObject& jb = jsonBuffer.parseObject(out); - - if (!jb.success()) - return 0; - - JsonArray& aux = jb["aux"]; - if (!aux.success()) - return 0; - - for (JsonObject& page : aux) { - if (page["aux"].as() == String(uri())) { - JsonArray& element_j = page[AUTOCONNECT_JSON_KEY_ELEMENT]; - for (JsonObject& elm : element_j) { - if (elm[AUTOCONNECT_JSON_KEY_NAME].as() == element.name) { - elm.set(F(AUTOCONNECT_JSON_KEY_VALUE), element.value); - return jb.prettyPrintTo(out); - } - } - } +size_t AutoConnectAux::save(Stream& out) { + size_t e = _addonElm.size(); + if (e <= 0) + return e; + + DynamicJsonBuffer auxBuffer(3 + JSON_ARRAY_SIZE(e) + JSON_OBJECT_SIZE(5) * e); + JsonObject& json = auxBuffer.createObject(); + json[F(AUTOCONNECT_JSON_KEY_TITLE)] = _title; + json[F(AUTOCONNECT_JSON_KEY_URI)] = _uriStr; + json[F(AUTOCONNECT_JSON_KEY_MENU)] = _menu; + JsonArray& elements = json.createNestedArray(F(AUTOCONNECT_JSON_KEY_ELEMENT)); + for (size_t i = 0; i < e; i++) { + JsonObject& element = elements.createNestedObject(); + AutoConnectElement& elm = _addonElm[i]; + elm.serialize(element); } - return 0; + + return static_cast(json.prettyPrintTo(out)); } /** @@ -788,16 +886,6 @@ const ACElement_t AutoConnectAux::_asElementType(const String type) { return t; } -/** - * Returns a null element as static storage. - * This static element is referred by invalid JSON data. - * @return A reference of a static element defined by name as null. - */ -AutoConnectElement& AutoConnectAux::_nullElement() { - static AutoConnectElement nullElement("", ""); - return nullElement; -} - /** * Calculate JSON dynamic buffer size. * @param in JSON string diff --git a/src/AutoConnectAux.h b/src/AutoConnectAux.h index d5715eb..295da87 100644 --- a/src/AutoConnectAux.h +++ b/src/AutoConnectAux.h @@ -55,22 +55,28 @@ class AutoConnectAux : public PageBuilder { void add(AutoConnectElement& addon); /**< Add an element to the auxiliary page. */ void add(AutoConnectElementVT addons); /**< Add the element set to the auxiliary page. */ template - T& getElement(const String name); /**< Get AutoConnect element */ + T& getElement(const char* name); /**< Get AutoConnect element */ + AutoConnectElement* getElement(const char* name) { return getElement(String(name)); } /**< Get registered AutoConnectElement as specified name */ AutoConnectElement* getElement(const String name); /**< Get registered AutoConnectElement as specified name */ + void menu(const bool post) { _menu = post; } /**< Set or reset the display as menu item for this aux. */ bool release(const char* name) { return release(String(name)); } /**< Release an AutoConnectElement */ bool release(const String name); /**< Release an AutoConnectElement */ - void setTitle(const char* title) { _title = String(title); } /**< Set a title of the auxiliary page. */ - void menu(const bool post) { _menu = post; } /**< Set or reset the display as menu item for this aux. */ + bool setElementValue(const char* name, const String value) { return setElementValue(String(name), value); } + bool setElementValue(const String name, const String value); + bool setElementValue(const char* name, std::vector values) { return setElementValue(String(name), values); } + bool setElementValue(const String name, std::vector values); + void setTitle(const char* title) { setTitle(String(title)); } /**< Set a title of the auxiliary page. */ + void setTitle(const String title) { _title = title; } /**< Set a title of the auxiliary page. */ void on(const AuxHandlerFunctionT handler, const AutoConnectExitOrder_t order = AC_EXIT_AHEAD) { _handler = handler; _order = order; } /**< Set user handler */ #ifdef AUTOCONNECT_USE_JSON bool load(const char* in); /**< Load whole elements to AutoConnectAux Page */ bool load(const __FlashStringHelper* in); /**< Load whole elements to AutoConnectAux Page */ bool load(Stream& in, const size_t bufferSize = AUTOCONNECT_JSON_BUFFER_SIZE); /**< Load whole elements to AutoConnectAux Page */ - AutoConnectElement& loadElement(const char* in, const String name = "*"); /**< Load specified element */ + AutoConnectElement& loadElement(const String in, const String name = "*"); /**< Load specified element */ AutoConnectElement& loadElement(const __FlashStringHelper* in, const String name = "*"); /**< Load specified element */ AutoConnectElement& loadElement(Stream& in, const String name = "*", const size_t bufferSize = AUTOCONNECT_JSON_BUFFER_SIZE); /**< Load specified element */ - size_t saveElement(Stream& out, const AutoConnectElement& element); /**< Load specified element */ + size_t save(Stream& out); /**< Save specified element */ #endif // !AUTOCONNECT_USE_JSON protected: @@ -80,13 +86,13 @@ class AutoConnectAux : public PageBuilder { const String _insertElement(PageArgument& args); /**< Insert a generated HTML to the page built by PageBuilder */ const String _injectTitle(PageArgument& args) { return _title; } /**< Returns title of this page to PageBuilder */ const String _injectMenu(PageArgument& args); /**< Inject menu title of this page to PageBuilder */ + static AutoConnectElement& _nullElement(void); /**< A static returning value as invalid */ #ifdef AUTOCONNECT_USE_JSON bool _load(JsonObject& in); /**< Load all elements 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 const ACElement_t _asElementType(const String type); /**< Convert a string of element type to the enumeration value */ - static AutoConnectElement& _nullElement(void); /**< A static returning value as invalid */ #endif // !AUTOCONNECT_USE_JSON String _title; /**< A title of the page */ diff --git a/src/AutoConnectElementBasis.h b/src/AutoConnectElementBasis.h index bd886ac..35fa7f3 100644 --- a/src/AutoConnectElementBasis.h +++ b/src/AutoConnectElementBasis.h @@ -65,7 +65,7 @@ class AutoConnectButtonBasis : virtual public AutoConnectElementBasis { _type = AC_Button; } ~AutoConnectButtonBasis() {} - const String toHTML(void) const; + const String toHTML(void) const override; String action; }; @@ -84,7 +84,7 @@ class AutoConnectCheckboxBasis : virtual public AutoConnectElementBasis { _type = AC_Checkbox; } virtual ~AutoConnectCheckboxBasis() {} - const String toHTML(void) const; + const String toHTML(void) const override; String label; /**< A label for a subsequent input box */ bool checked; /**< The element should be pre-selected */ @@ -104,7 +104,7 @@ class AutoConnectInputBasis : virtual public AutoConnectElementBasis { _type = AC_Input; } virtual ~AutoConnectInputBasis() {} - const String toHTML(void) const; + const String toHTML(void) const override; String placeholder; String label; /**< A label for a subsequent input box */ @@ -124,7 +124,7 @@ class AutoConnectRadioBasis : virtual public AutoConnectElementBasis { _type = AC_Radio; } virtual ~AutoConnectRadioBasis() {} - const String toHTML(void) const; + const String toHTML(void) const override; void add(const String value) { _values.push_back(value); } void empty(void) { _values.clear(); } @@ -150,7 +150,7 @@ class AutoConnectSelectBasis : virtual public AutoConnectElementBasis { _type = AC_Select; } virtual ~AutoConnectSelectBasis() {} - const String toHTML(void) const; + const String toHTML(void) const override; void add(const String option) { _options.push_back(option); } void empty(void) { _options.clear(); } @@ -175,7 +175,7 @@ class AutoConnectSubmitBasis : virtual public AutoConnectElementBasis { _type = AC_Submit; } virtual ~AutoConnectSubmitBasis() {} - const String toHTML(void) const; + const String toHTML(void) const override; String uri; /**< An url of submitting to */ }; @@ -195,7 +195,7 @@ class AutoConnectTextBasis : virtual public AutoConnectElementBasis { _type = AC_Text; } virtual ~AutoConnectTextBasis() {} - const String toHTML(void) const; + const String toHTML(void) const override; String style; /**< CSS style modifier native code */ }; diff --git a/src/AutoConnectElementJson.h b/src/AutoConnectElementJson.h index 4bce015..b4fea4e 100644 --- a/src/AutoConnectElementJson.h +++ b/src/AutoConnectElementJson.h @@ -52,9 +52,11 @@ class AutoConnectElementJson : virtual public AutoConnectElementBasis { } ~AutoConnectElementJson() {} virtual bool loadElement(const JsonObject& json); + virtual void serialize(JsonObject& json); protected: void _setElement(const JsonObject& json); + void _serialize(JsonObject& json); }; /** @@ -73,7 +75,8 @@ class AutoConnectButtonJson : public AutoConnectElementJson, public AutoConnectB AutoConnectButtonBasis::action = action; } ~AutoConnectButtonJson() {} - bool loadElement(const JsonObject& json); + bool loadElement(const JsonObject& json) override; + void serialize(JsonObject& json) override; }; /** @@ -94,7 +97,8 @@ class AutoConnectCheckboxJson : public AutoConnectElementJson, public AutoConnec AutoConnectCheckboxBasis::checked = checked; } ~AutoConnectCheckboxJson() {} - bool loadElement(const JsonObject& json); + bool loadElement(const JsonObject& json) override; + void serialize(JsonObject& json) override; }; /** @@ -115,7 +119,8 @@ class AutoConnectInputJson : public AutoConnectElementJson, public AutoConnectIn AutoConnectInputBasis::label = label; } ~AutoConnectInputJson() {} - bool loadElement(const JsonObject& json); + bool loadElement(const JsonObject& json) override; + void serialize(JsonObject& json) override; }; /** @@ -136,7 +141,8 @@ class AutoConnectRadioJson : public AutoConnectElementJson, public AutoConnectRa AutoConnectRadioBasis::checked = checked; } ~AutoConnectRadioJson() {} - bool loadElement(const JsonObject& json); + bool loadElement(const JsonObject& json) override; + void serialize(JsonObject& json) override; }; /** @@ -155,7 +161,8 @@ class AutoConnectSelectJson : public AutoConnectElementJson, public AutoConnectS AutoConnectSelectBasis::label = label; } ~AutoConnectSelectJson() {} - bool loadElement(const JsonObject& json); + bool loadElement(const JsonObject& json) override; + void serialize(JsonObject& json) override; }; /** @@ -175,7 +182,8 @@ class AutoConnectSubmitJson : public AutoConnectElementJson, public AutoConnectS AutoConnectSubmitBasis::uri = uri; } ~AutoConnectSubmitJson() {} - bool loadElement(const JsonObject& json); + bool loadElement(const JsonObject& json) override; + void serialize(JsonObject& json) override; }; /** @@ -195,7 +203,8 @@ class AutoConnectTextJson : public AutoConnectElementJson, public AutoConnectTex AutoConnectTextBasis::style = style; } ~AutoConnectTextJson() {} - bool loadElement(const JsonObject& json); + bool loadElement(const JsonObject& json) override; + void serialize(JsonObject& json) override; }; #endif // _AUTOCONNECTELEMENTJSON_H_ diff --git a/src/AutoConnectElementJsonImpl.h b/src/AutoConnectElementJsonImpl.h index 0116615..66e2913 100644 --- a/src/AutoConnectElementJsonImpl.h +++ b/src/AutoConnectElementJsonImpl.h @@ -14,16 +14,25 @@ /** * Set items common to any type of AutoConnectElement from JSON objects. - * @param json A JSON object with the definition of AutoConnectElement. + * @param json JSON object with the definition of AutoConnectElement. */ void AutoConnectElementJson::_setElement(const JsonObject& json) { name = json.get(F(AUTOCONNECT_JSON_KEY_NAME)); value = json.get(F(AUTOCONNECT_JSON_KEY_VALUE)); } +/** + * Serialize AutoConnectElement to JSON. + * This function is base for each element. + * @param json JSON object to be serialized. + */ +void AutoConnectElementJson::_serialize(JsonObject& json) { + json.set(F(AUTOCONNECT_JSON_KEY_NAME), name); +} + /** * Load an element member value from the JSON object. - * @param json A JSON object with the definition of AutoConnectElement. + * @param json JSON object with the definition of AutoConnectElement. * @return true AutoConnectElement loaded * @return false Type of AutoConnectElement is mismatched. */ @@ -36,9 +45,18 @@ bool AutoConnectElementJson::loadElement(const JsonObject& json) { return false; } +/** + * Serialize AutoConnectElement to JSON. + * @param json JSON object to be serialized. + */ +void AutoConnectElementJson::serialize(JsonObject& json) { + json.set(F(AUTOCONNECT_JSON_KEY_TYPE), F(AUTOCONNECT_JSON_TYPE_ACELEMENT)); + json.set(F(AUTOCONNECT_JSON_KEY_VALUE), value); +} + /** * Load a button element attribute member from the JSON object. - * @param json A JSON object with the definition of AutoConnectElement. + * @param json JSON object with the definition of AutoConnectElement. * @return true AutoConnectElement loaded * @return false Type of AutoConnectElement is mismatched. */ @@ -52,9 +70,20 @@ bool AutoConnectButtonJson::loadElement(const JsonObject& json) { return false; } +/** + * Serialize AutoConnectButton to JSON. + * @param json JSON object to be serialized. + */ +void AutoConnectButtonJson::serialize(JsonObject& json) { + _serialize(json); + json.set(F(AUTOCONNECT_JSON_KEY_TYPE), F(AUTOCONNECT_JSON_TYPE_ACBUTTON)); + json.set(F(AUTOCONNECT_JSON_KEY_VALUE), value); + json.set(F(AUTOCONNECT_JSON_KEY_ACTION), action); +} + /** * Load a checkbox element attribute member from the JSON object. - * @param json A JSON object with the definition of AutoConnectElement. + * @param json JSON object with the definition of AutoConnectElement. * @return true AutoConnectElement loaded * @return false Type of AutoConnectElement is mismatched. */ @@ -69,9 +98,22 @@ bool AutoConnectCheckboxJson::loadElement(const JsonObject& json) { return false; } +/** + * Serialize AutoConnectCheckbox to JSON. + * @param json JSON object to be serialized. + */ +void AutoConnectCheckboxJson::serialize(JsonObject& json) { + _serialize(json); + json.set(F(AUTOCONNECT_JSON_KEY_TYPE), F(AUTOCONNECT_JSON_TYPE_ACCHECKBOX)); + json.set(F(AUTOCONNECT_JSON_KEY_NAME), name); + json.set(F(AUTOCONNECT_JSON_KEY_VALUE), value); + json.set(F(AUTOCONNECT_JSON_KEY_LABEL), label); + json.set(F(AUTOCONNECT_JSON_KEY_CHECKED), checked); +} + /** * Load a input-box element attribute member from the JSON object. - * @param json A JSON object with the definition of AutoConnectElement. + * @param json JSON object with the definition of AutoConnectElement. * @return true AutoConnectElement loaded * @return false Type of AutoConnectElement is mismatched. */ @@ -86,9 +128,21 @@ bool AutoConnectInputJson::loadElement(const JsonObject& json) { return false; } +/** + * Serialize AutoConnectInput to JSON. + * @param json JSON object to be serialized. + */ +void AutoConnectInputJson::serialize(JsonObject& json) { + _serialize(json); + json.set(F(AUTOCONNECT_JSON_KEY_TYPE), F(AUTOCONNECT_JSON_TYPE_ACINPUT)); + json.set(F(AUTOCONNECT_JSON_KEY_VALUE), value); + json.set(F(AUTOCONNECT_JSON_KEY_PLACEHOLDER), placeholder); + json.set(F(AUTOCONNECT_JSON_KEY_LABEL), label); +} + /** * Load a radio-button element attribute member from the JSON object. -* @param json A JSON object with the definition of AutoConnectElement. +* @param json JSON object with the definition of AutoConnectElement. * @return true AutoConnectElement loaded * @return false Type of AutoConnectElement is mismatched. */ @@ -112,9 +166,31 @@ bool AutoConnectRadioJson::loadElement(const JsonObject& json) { return false; } +/** + * Serialize AutoConnectRadio to JSON. + * @param json JSON object to be serialized. + */ +void AutoConnectRadioJson::serialize(JsonObject& json) { + _serialize(json); + json.set(F(AUTOCONNECT_JSON_KEY_TYPE), F(AUTOCONNECT_JSON_TYPE_ACRADIO)); + json.set(F(AUTOCONNECT_JSON_KEY_LABEL), label); + JsonArray& values = json.createNestedArray(F(AUTOCONNECT_JSON_KEY_VALUE)); + for (String v : _values) + values.add(v); + switch (order) { + case AC_Horizontal: + json.set(F(AUTOCONNECT_JSON_KEY_ARRANGE), AUTOCONNECT_JSON_KEY_HORIZONTAL); + break; + case AC_Vertical: + json.set(F(AUTOCONNECT_JSON_KEY_ARRANGE), AUTOCONNECT_JSON_KEY_VERTICAL); + break; + } + json.set(F(AUTOCONNECT_JSON_KEY_CHECKED), checked); +} + /** * Load a select element attribute member from the JSON object. - * @param json A JSON object with the definition of AutoConnectElement. + * @param json JSON object with the definition of AutoConnectElement. * @return true AutoConnectElement loaded * @return false Type of AutoConnectElement is mismatched. */ @@ -132,9 +208,22 @@ bool AutoConnectSelectJson::loadElement(const JsonObject& json) { return false; } +/** + * Serialize AutoConnectSelect to JSON. + * @param json JSON object to be serialized. + */ +void AutoConnectSelectJson::serialize(JsonObject& json) { + _serialize(json); + json.set(F(AUTOCONNECT_JSON_KEY_TYPE), F(AUTOCONNECT_JSON_TYPE_ACSELECT)); + JsonArray& options = json.createNestedArray(F(AUTOCONNECT_JSON_KEY_OPTION)); + for (String o : _options) + options.add(o); + json.set(F(AUTOCONNECT_JSON_KEY_LABEL), label); +} + /** * Load a submit element attribute member from the JSON object. - * @param json A JSON object with the definition of AutoConnectElement. + * @param json JSON object with the definition of AutoConnectElement. * @return true AutoConnectElement loaded * @return false Type of AutoConnectElement is mismatched. */ @@ -148,9 +237,20 @@ bool AutoConnectSubmitJson::loadElement(const JsonObject& json) { return false; } +/** + * Serialize AutoConnectSubmit to JSON. + * @param json JSON object to be serialized. + */ +void AutoConnectSubmitJson::serialize(JsonObject& json) { + _serialize(json); + json.set(F(AUTOCONNECT_JSON_KEY_TYPE), F(AUTOCONNECT_JSON_TYPE_ACSUBMIT)); + json.set(F(AUTOCONNECT_JSON_KEY_VALUE), value); + json.set(F(AUTOCONNECT_JSON_KEY_URI), uri); +} + /** * Load a text element attribute member from the JSON object. - * @param json A JSON object with the definition of AutoConnectElement. + * @param json JSON object with the definition of AutoConnectElement. * @return true AutoConnectElement loaded * @return false Type of AutoConnectElement is mismatched. */ @@ -164,4 +264,15 @@ bool AutoConnectTextJson::loadElement(const JsonObject& json) { return false; } +/** + * Serialize AutoConnectText to JSON. + * @param json JSON object to be serialized. + */ +void AutoConnectTextJson::serialize(JsonObject& json) { + _serialize(json); + json.set(F(AUTOCONNECT_JSON_KEY_TYPE), F(AUTOCONNECT_JSON_TYPE_ACTEXT)); + json.set(F(AUTOCONNECT_JSON_KEY_VALUE), value); + json.set(F(AUTOCONNECT_JSON_KEY_STYLE), style); +} + #endif // _AUTOCONNECTELEMENTJSONIMPL_H_ diff --git a/src/AutoConnectPage.cpp b/src/AutoConnectPage.cpp index 6d16108..fe4fe0d 100644 --- a/src/AutoConnectPage.cpp +++ b/src/AutoConnectPage.cpp @@ -1083,9 +1083,9 @@ String AutoConnect::_token_UPTIME(PageArgument& args) { String AutoConnect::_token_BOOTURI(PageArgument& args) { AC_UNUSED(args); - if (_apConfig.bootUri == AC_URIONBOOT_ROOT) + if (_apConfig.bootUri == AC_ONBOOTURI_ROOT) return String(AUTOCONNECT_URI); - else if (_apConfig.bootUri == AC_URIONBOOT_HOME) + else if (_apConfig.bootUri == AC_ONBOOTURI_HOME) return _apConfig.homeUri.length() > 0 ? _apConfig.homeUri : String("/"); else return "";