Supports AutoConnectAux

pull/41/head
Hieromon Ikasamo 6 years ago
parent 8652dadfaf
commit 00471d3b83
  1. 14
      src/AutoConnect.h
  2. 186
      src/AutoConnectAux.cpp
  3. 18
      src/AutoConnectAux.h
  4. 14
      src/AutoConnectElementBasis.h
  5. 23
      src/AutoConnectElementJson.h
  6. 129
      src/AutoConnectElementJsonImpl.h
  7. 4
      src/AutoConnectPage.cpp

@ -39,10 +39,10 @@ typedef enum AC_SAVECREDENTIAL {
AC_SAVECREDENTIAL_AUTO AC_SAVECREDENTIAL_AUTO
} AC_SAVECREDENTIAL_t; } AC_SAVECREDENTIAL_t;
typedef enum AC_URIONBOOT { typedef enum AC_ONBOOTURI {
AC_URIONBOOT_ROOT, AC_ONBOOTURI_ROOT,
AC_URIONBOOT_HOME AC_ONBOOTURI_HOME
} AC_URIONBOOT_t; } AC_ONBOOTURI_t;
class AutoConnectConfig { class AutoConnectConfig {
public: public:
@ -60,7 +60,7 @@ class AutoConnectConfig {
channel(AUTOCONNECT_AP_CH), channel(AUTOCONNECT_AP_CH),
hidden(0), hidden(0),
autoSave(AC_SAVECREDENTIAL_AUTO), autoSave(AC_SAVECREDENTIAL_AUTO),
bootUri(AC_URIONBOOT_ROOT), bootUri(AC_ONBOOTURI_ROOT),
boundaryOffset(AC_IDENTIFIER_OFFSET), boundaryOffset(AC_IDENTIFIER_OFFSET),
uptime(AUTOCONNECT_STARTUPTIME), uptime(AUTOCONNECT_STARTUPTIME),
autoRise(true), autoRise(true),
@ -85,7 +85,7 @@ class AutoConnectConfig {
channel(channel), channel(channel),
hidden(0), hidden(0),
autoSave(AC_SAVECREDENTIAL_AUTO), autoSave(AC_SAVECREDENTIAL_AUTO),
bootUri(AC_URIONBOOT_ROOT), bootUri(AC_ONBOOTURI_ROOT),
boundaryOffset(AC_IDENTIFIER_OFFSET), boundaryOffset(AC_IDENTIFIER_OFFSET),
uptime(AUTOCONNECT_STARTUPTIME), uptime(AUTOCONNECT_STARTUPTIME),
autoRise(true), autoRise(true),
@ -134,7 +134,7 @@ class AutoConnectConfig {
uint8_t channel; /**< SoftAP used wifi channel */ uint8_t channel; /**< SoftAP used wifi channel */
uint8_t hidden; /**< SoftAP SSID hidden */ uint8_t hidden; /**< SoftAP SSID hidden */
AC_SAVECREDENTIAL_t autoSave; /**< Auto save credential */ 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 */ uint16_t boundaryOffset; /**< The save storage offset of EEPROM */
int uptime; /**< Length of start up time */ int uptime; /**< Length of start up time */
bool autoRise; /**< Automatic starting the captive portal */ bool autoRise; /**< Automatic starting the captive portal */

@ -97,6 +97,7 @@ AutoConnectElement* AutoConnectAux::getElement(const String name) {
for (std::size_t n = 0; n < _addonElm.size(); n++) for (std::size_t n = 0; n < _addonElm.size(); n++)
if (_addonElm[n].get().name == name) if (_addonElm[n].get().name == name)
return &(_addonElm[n].get()); return &(_addonElm[n].get());
AC_DBG("Element<%s> not registered\n", name.c_str());
return nullptr; return nullptr;
} }
@ -121,6 +122,71 @@ bool AutoConnectAux::release(const String name) {
return rc; 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<String> values) {
bool rc = false;
AutoConnectElement* elm = getElement(name);
if (elm) {
switch (elm->typeOf()) {
case AC_Radio: {
AutoConnectRadio* elmRadio = reinterpret_cast<AutoConnectRadio*>(elm);
elmRadio->empty();
for (String v : values)
elmRadio->add(v);
rc = true;
break;
}
case AC_Select: {
AutoConnectSelect* elmSelect = reinterpret_cast<AutoConnectSelect*>(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 * Concatenates subsequent AutoConnectAux pages starting from oneself
* to the chain list. * to the chain list.
@ -244,6 +310,16 @@ const String AutoConnectAux::_injectMenu(PageArgument& args) {
return menuItem; 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 #ifndef AUTOCONNECT_USE_JSON
/** /**
@ -252,11 +328,13 @@ const String AutoConnectAux::_injectMenu(PageArgument& args) {
* @return A reference of AutoConnectButton class. * @return A reference of AutoConnectButton class.
*/ */
template<> template<>
AutoConnectButtonBasis& AutoConnectAux::getElement(const String name) { AutoConnectButtonBasis& AutoConnectAux::getElement(const char* name) {
AutoConnectElement* elm = getElement(name); AutoConnectElement* elm = getElement(name);
if (elm) { if (elm) {
if (elm->typeOf() == AC_Button) if (elm->typeOf() == AC_Button)
return *(reinterpret_cast<AutoConnectButtonBasis*>(elm)); return *(reinterpret_cast<AutoConnectButtonBasis*>(elm));
else
AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf());
} }
return reinterpret_cast<AutoConnectButtonBasis&>(_nullElement()); return reinterpret_cast<AutoConnectButtonBasis&>(_nullElement());
} }
@ -267,11 +345,13 @@ AutoConnectButtonBasis& AutoConnectAux::getElement(const String name) {
* @return A reference of AutoConnectCheckbox class. * @return A reference of AutoConnectCheckbox class.
*/ */
template<> template<>
AutoConnectCheckboxBasis& AutoConnectAux::getElement(const String name) { AutoConnectCheckboxBasis& AutoConnectAux::getElement(const char* name) {
AutoConnectElement* elm = getElement(name); AutoConnectElement* elm = getElement(name);
if (elm) { if (elm) {
if (elm->typeOf() == AC_Checkbox) if (elm->typeOf() == AC_Checkbox)
return *(reinterpret_cast<AutoConnectCheckboxBasis*>(elm)); return *(reinterpret_cast<AutoConnectCheckboxBasis*>(elm));
else
AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf());
} }
return reinterpret_cast<AutoConnectCheckboxBasis&>(_nullElement()); return reinterpret_cast<AutoConnectCheckboxBasis&>(_nullElement());
} }
@ -282,11 +362,13 @@ AutoConnectCheckboxBasis& AutoConnectAux::getElement(const String name) {
* @return A reference of AutoConnectInput class. * @return A reference of AutoConnectInput class.
*/ */
template<> template<>
AutoConnectInputBasis& AutoConnectAux::getElement(const String name) { AutoConnectInputBasis& AutoConnectAux::getElement(const char* name) {
AutoConnectElement* elm = getElement(name); AutoConnectElement* elm = getElement(name);
if (elm) { if (elm) {
if (elm->typeOf() == AC_Input) if (elm->typeOf() == AC_Input)
return *(reinterpret_cast<AutoConnectInputBasis*>(elm)); return *(reinterpret_cast<AutoConnectInputBasis*>(elm));
else
AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf());
} }
return reinterpret_cast<AutoConnectInputBasis&>(_nullElement()); return reinterpret_cast<AutoConnectInputBasis&>(_nullElement());
} }
@ -297,11 +379,13 @@ AutoConnectInputBasis& AutoConnectAux::getElement(const String name) {
* @return A reference of AutoConnectRadio class. * @return A reference of AutoConnectRadio class.
*/ */
template<> template<>
AutoConnectRadioBasis& AutoConnectAux::getElement(const String name) { AutoConnectRadioBasis& AutoConnectAux::getElement(const char* name) {
AutoConnectElement* elm = getElement(name); AutoConnectElement* elm = getElement(name);
if (elm) { if (elm) {
if (elm->typeOf() == AC_Radio) if (elm->typeOf() == AC_Radio)
return *(reinterpret_cast<AutoConnectRadioBasis*>(elm)); return *(reinterpret_cast<AutoConnectRadioBasis*>(elm));
else
AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf());
} }
return reinterpret_cast<AutoConnectRadioBasis&>(_nullElement()); return reinterpret_cast<AutoConnectRadioBasis&>(_nullElement());
} }
@ -312,11 +396,13 @@ AutoConnectRadioBasis& AutoConnectAux::getElement(const String name) {
* @return A reference of AutoConnectSelect class. * @return A reference of AutoConnectSelect class.
*/ */
template<> template<>
AutoConnectSelectBasis& AutoConnectAux::getElement(const String name) { AutoConnectSelectBasis& AutoConnectAux::getElement(const char* name) {
AutoConnectElement* elm = getElement(name); AutoConnectElement* elm = getElement(name);
if (elm) { if (elm) {
if (elm->typeOf() == AC_Select) if (elm->typeOf() == AC_Select)
return *(reinterpret_cast<AutoConnectSelectBasis*>(elm)); return *(reinterpret_cast<AutoConnectSelectBasis*>(elm));
else
AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf());
} }
return reinterpret_cast<AutoConnectSelectBasis&>(_nullElement()); return reinterpret_cast<AutoConnectSelectBasis&>(_nullElement());
} }
@ -327,11 +413,13 @@ AutoConnectSelectBasis& AutoConnectAux::getElement(const String name) {
* @return A reference of AutoConnectSubmit class. * @return A reference of AutoConnectSubmit class.
*/ */
template<> template<>
AutoConnectSubmitBasis& AutoConnectAux::getElement(const String name) { AutoConnectSubmitBasis& AutoConnectAux::getElement(const char* name) {
AutoConnectElement* elm = getElement(name); AutoConnectElement* elm = getElement(name);
if (elm) { if (elm) {
if (elm->typeOf() == AC_Submit) if (elm->typeOf() == AC_Submit)
return *(reinterpret_cast<AutoConnectSubmitBasis*>(elm)); return *(reinterpret_cast<AutoConnectSubmitBasis*>(elm));
else
AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf());
} }
return reinterpret_cast<AutoConnectSubmitBasis&>(_nullElement()); return reinterpret_cast<AutoConnectSubmitBasis&>(_nullElement());
} }
@ -342,11 +430,13 @@ AutoConnectSubmitBasis& AutoConnectAux::getElement(const String name) {
* @return A reference of AutoConnectText class. * @return A reference of AutoConnectText class.
*/ */
template<> template<>
AutoConnectTextBasis& AutoConnectAux::getElement(const String name) { AutoConnectTextBasis& AutoConnectAux::getElement(const char* name) {
AutoConnectElement* elm = getElement(name); AutoConnectElement* elm = getElement(name);
if (elm) { if (elm) {
if (elm->typeOf() == AC_Text) if (elm->typeOf() == AC_Text)
return *(reinterpret_cast<AutoConnectTextBasis*>(elm)); return *(reinterpret_cast<AutoConnectTextBasis*>(elm));
else
AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf());
} }
return reinterpret_cast<AutoConnectTextBasis&>(_nullElement()); return reinterpret_cast<AutoConnectTextBasis&>(_nullElement());
} }
@ -374,11 +464,13 @@ bool AutoConnectAux::_jbLiteral; /**< JSON object lexical status */
* @return A reference of AutoConnectButton class. * @return A reference of AutoConnectButton class.
*/ */
template<> template<>
AutoConnectButtonJson& AutoConnectAux::getElement(const String name) { AutoConnectButtonJson& AutoConnectAux::getElement(const char* name) {
AutoConnectElement* elm = getElement(name); AutoConnectElement* elm = getElement(name);
if (elm) { if (elm) {
if (elm->typeOf() == AC_Button) if (elm->typeOf() == AC_Button)
return *(reinterpret_cast<AutoConnectButtonJson*>(elm)); return *(reinterpret_cast<AutoConnectButtonJson*>(elm));
else
AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf());
} }
return reinterpret_cast<AutoConnectButtonJson&>(_nullElement()); return reinterpret_cast<AutoConnectButtonJson&>(_nullElement());
} }
@ -389,11 +481,13 @@ AutoConnectButtonJson& AutoConnectAux::getElement(const String name) {
* @return A reference of AutoConnectCheckbox class. * @return A reference of AutoConnectCheckbox class.
*/ */
template<> template<>
AutoConnectCheckboxJson& AutoConnectAux::getElement(const String name) { AutoConnectCheckboxJson& AutoConnectAux::getElement(const char* name) {
AutoConnectElement* elm = getElement(name); AutoConnectElement* elm = getElement(name);
if (elm) { if (elm) {
if (elm->typeOf() == AC_Checkbox) if (elm->typeOf() == AC_Checkbox)
return *(reinterpret_cast<AutoConnectCheckboxJson*>(elm)); return *(reinterpret_cast<AutoConnectCheckboxJson*>(elm));
else
AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf());
} }
return reinterpret_cast<AutoConnectCheckboxJson&>(_nullElement()); return reinterpret_cast<AutoConnectCheckboxJson&>(_nullElement());
} }
@ -404,11 +498,13 @@ AutoConnectCheckboxJson& AutoConnectAux::getElement(const String name) {
* @return A reference of AutoConnectInput class. * @return A reference of AutoConnectInput class.
*/ */
template<> template<>
AutoConnectInputJson& AutoConnectAux::getElement(const String name) { AutoConnectInputJson& AutoConnectAux::getElement(const char* name) {
AutoConnectElement* elm = getElement(name); AutoConnectElement* elm = getElement(name);
if (elm) { if (elm) {
if (elm->typeOf() == AC_Input) if (elm->typeOf() == AC_Input)
return *(reinterpret_cast<AutoConnectInputJson*>(elm)); return *(reinterpret_cast<AutoConnectInputJson*>(elm));
else
AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf());
} }
return reinterpret_cast<AutoConnectInputJson&>(_nullElement()); return reinterpret_cast<AutoConnectInputJson&>(_nullElement());
} }
@ -419,11 +515,13 @@ AutoConnectInputJson& AutoConnectAux::getElement(const String name) {
* @return A reference of AutoConnectRadio class. * @return A reference of AutoConnectRadio class.
*/ */
template<> template<>
AutoConnectRadioJson& AutoConnectAux::getElement(const String name) { AutoConnectRadioJson& AutoConnectAux::getElement(const char* name) {
AutoConnectElement* elm = getElement(name); AutoConnectElement* elm = getElement(name);
if (elm) { if (elm) {
if (elm->typeOf() == AC_Radio) if (elm->typeOf() == AC_Radio)
return *(reinterpret_cast<AutoConnectRadioJson*>(elm)); return *(reinterpret_cast<AutoConnectRadioJson*>(elm));
else
AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf());
} }
return reinterpret_cast<AutoConnectRadioJson&>(_nullElement()); return reinterpret_cast<AutoConnectRadioJson&>(_nullElement());
} }
@ -434,11 +532,13 @@ AutoConnectRadioJson& AutoConnectAux::getElement(const String name) {
* @return A reference of AutoConnectSelect class. * @return A reference of AutoConnectSelect class.
*/ */
template<> template<>
AutoConnectSelectJson& AutoConnectAux::getElement(const String name) { AutoConnectSelectJson& AutoConnectAux::getElement(const char* name) {
AutoConnectElement* elm = getElement(name); AutoConnectElement* elm = getElement(name);
if (elm) { if (elm) {
if (elm->typeOf() == AC_Select) if (elm->typeOf() == AC_Select)
return *(reinterpret_cast<AutoConnectSelectJson*>(elm)); return *(reinterpret_cast<AutoConnectSelectJson*>(elm));
else
AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf());
} }
return reinterpret_cast<AutoConnectSelectJson&>(_nullElement()); return reinterpret_cast<AutoConnectSelectJson&>(_nullElement());
} }
@ -449,11 +549,13 @@ AutoConnectSelectJson& AutoConnectAux::getElement(const String name) {
* @return A reference of AutoConnectSubmit class. * @return A reference of AutoConnectSubmit class.
*/ */
template<> template<>
AutoConnectSubmitJson& AutoConnectAux::getElement(const String name) { AutoConnectSubmitJson& AutoConnectAux::getElement(const char* name) {
AutoConnectElement* elm = getElement(name); AutoConnectElement* elm = getElement(name);
if (elm) { if (elm) {
if (elm->typeOf() == AC_Submit) if (elm->typeOf() == AC_Submit)
return *(reinterpret_cast<AutoConnectSubmitJson*>(elm)); return *(reinterpret_cast<AutoConnectSubmitJson*>(elm));
else
AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf());
} }
return reinterpret_cast<AutoConnectSubmitJson&>(_nullElement()); return reinterpret_cast<AutoConnectSubmitJson&>(_nullElement());
} }
@ -464,11 +566,13 @@ AutoConnectSubmitJson& AutoConnectAux::getElement(const String name) {
* @return A reference of AutoConnectText class. * @return A reference of AutoConnectText class.
*/ */
template<> template<>
AutoConnectTextJson& AutoConnectAux::getElement(const String name) { AutoConnectTextJson& AutoConnectAux::getElement(const char* name) {
AutoConnectElement* elm = getElement(name); AutoConnectElement* elm = getElement(name);
if (elm) { if (elm) {
if (elm->typeOf() == AC_Text) if (elm->typeOf() == AC_Text)
return *(reinterpret_cast<AutoConnectTextJson*>(elm)); return *(reinterpret_cast<AutoConnectTextJson*>(elm));
else
AC_DBG("Element<%s> type mismatch<%d>\n", name, elm->typeOf());
} }
return reinterpret_cast<AutoConnectTextJson&>(_nullElement()); return reinterpret_cast<AutoConnectTextJson&>(_nullElement());
} }
@ -669,8 +773,8 @@ bool AutoConnectAux::_load(JsonObject& jb) {
* elements are to be loaded. * elements are to be loaded.
* @return A reference of loaded AutoConnectElement instance. * @return A reference of loaded AutoConnectElement instance.
*/ */
AutoConnectElement& AutoConnectAux::loadElement(const char* in, const String name) { AutoConnectElement& AutoConnectAux::loadElement(const String in, const String name) {
const size_t bufferSize = _calcJsonBufferSize(in); const size_t bufferSize = _calcJsonBufferSize(in.c_str());
DynamicJsonBuffer jsonBuffer(bufferSize); DynamicJsonBuffer jsonBuffer(bufferSize);
JsonObject& jb = jsonBuffer.parseObject(in); JsonObject& jb = jsonBuffer.parseObject(in);
return _loadElement(jb, name); 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. * Serialize a element to JSON and write it to the stream.
* @param out An output stream * @param out An output stream
* @param element A reference of the element to be output.
* @return Number of byte output * @return Number of byte output
*/ */
size_t AutoConnectAux::saveElement(Stream& out, const AutoConnectElement& element) { size_t AutoConnectAux::save(Stream& out) {
DynamicJsonBuffer jsonBuffer; size_t e = _addonElm.size();
JsonObject& jb = jsonBuffer.parseObject(out); if (e <= 0)
return e;
if (!jb.success())
return 0; DynamicJsonBuffer auxBuffer(3 + JSON_ARRAY_SIZE(e) + JSON_OBJECT_SIZE(5) * e);
JsonObject& json = auxBuffer.createObject();
JsonArray& aux = jb["aux"]; json[F(AUTOCONNECT_JSON_KEY_TITLE)] = _title;
if (!aux.success()) json[F(AUTOCONNECT_JSON_KEY_URI)] = _uriStr;
return 0; json[F(AUTOCONNECT_JSON_KEY_MENU)] = _menu;
JsonArray& elements = json.createNestedArray(F(AUTOCONNECT_JSON_KEY_ELEMENT));
for (JsonObject& page : aux) { for (size_t i = 0; i < e; i++) {
if (page["aux"].as<String>() == String(uri())) { JsonObject& element = elements.createNestedObject();
JsonArray& element_j = page[AUTOCONNECT_JSON_KEY_ELEMENT]; AutoConnectElement& elm = _addonElm[i];
for (JsonObject& elm : element_j) { elm.serialize(element);
if (elm[AUTOCONNECT_JSON_KEY_NAME].as<String>() == element.name) {
elm.set(F(AUTOCONNECT_JSON_KEY_VALUE), element.value);
return jb.prettyPrintTo(out);
}
}
}
} }
return 0;
return static_cast<size_t>(json.prettyPrintTo(out));
} }
/** /**
@ -788,16 +886,6 @@ const ACElement_t AutoConnectAux::_asElementType(const String type) {
return t; 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. * Calculate JSON dynamic buffer size.
* @param in JSON string * @param in JSON string

@ -55,22 +55,28 @@ class AutoConnectAux : public PageBuilder {
void add(AutoConnectElement& addon); /**< Add an element to the auxiliary page. */ void add(AutoConnectElement& addon); /**< Add an element to the auxiliary page. */
void add(AutoConnectElementVT addons); /**< Add the element set to the auxiliary page. */ void add(AutoConnectElementVT addons); /**< Add the element set to the auxiliary page. */
template<typename T> template<typename T>
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 */ 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 char* name) { return release(String(name)); } /**< Release an AutoConnectElement */
bool release(const 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. */ bool setElementValue(const char* name, const String value) { return setElementValue(String(name), value); }
void menu(const bool post) { _menu = post; } /**< Set or reset the display as menu item for this aux. */ bool setElementValue(const String name, const String value);
bool setElementValue(const char* name, std::vector<String> values) { return setElementValue(String(name), values); }
bool setElementValue(const String name, std::vector<String> 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 */ void on(const AuxHandlerFunctionT handler, const AutoConnectExitOrder_t order = AC_EXIT_AHEAD) { _handler = handler; _order = order; } /**< Set user handler */
#ifdef AUTOCONNECT_USE_JSON #ifdef AUTOCONNECT_USE_JSON
bool load(const char* in); /**< Load whole elements to AutoConnectAux Page */ bool load(const char* in); /**< Load whole elements to AutoConnectAux Page */
bool load(const __FlashStringHelper* 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 */ 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(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 */ 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 #endif // !AUTOCONNECT_USE_JSON
protected: 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 _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 _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 */ 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 #ifdef AUTOCONNECT_USE_JSON
bool _load(JsonObject& in); /**< Load all elements from JSON object */ 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& _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 */ 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 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 #endif // !AUTOCONNECT_USE_JSON
String _title; /**< A title of the page */ String _title; /**< A title of the page */

@ -65,7 +65,7 @@ class AutoConnectButtonBasis : virtual public AutoConnectElementBasis {
_type = AC_Button; _type = AC_Button;
} }
~AutoConnectButtonBasis() {} ~AutoConnectButtonBasis() {}
const String toHTML(void) const; const String toHTML(void) const override;
String action; String action;
}; };
@ -84,7 +84,7 @@ class AutoConnectCheckboxBasis : virtual public AutoConnectElementBasis {
_type = AC_Checkbox; _type = AC_Checkbox;
} }
virtual ~AutoConnectCheckboxBasis() {} virtual ~AutoConnectCheckboxBasis() {}
const String toHTML(void) const; const String toHTML(void) const override;
String label; /**< A label for a subsequent input box */ String label; /**< A label for a subsequent input box */
bool checked; /**< The element should be pre-selected */ bool checked; /**< The element should be pre-selected */
@ -104,7 +104,7 @@ class AutoConnectInputBasis : virtual public AutoConnectElementBasis {
_type = AC_Input; _type = AC_Input;
} }
virtual ~AutoConnectInputBasis() {} virtual ~AutoConnectInputBasis() {}
const String toHTML(void) const; const String toHTML(void) const override;
String placeholder; String placeholder;
String label; /**< A label for a subsequent input box */ String label; /**< A label for a subsequent input box */
@ -124,7 +124,7 @@ class AutoConnectRadioBasis : virtual public AutoConnectElementBasis {
_type = AC_Radio; _type = AC_Radio;
} }
virtual ~AutoConnectRadioBasis() {} virtual ~AutoConnectRadioBasis() {}
const String toHTML(void) const; const String toHTML(void) const override;
void add(const String value) { _values.push_back(value); } void add(const String value) { _values.push_back(value); }
void empty(void) { _values.clear(); } void empty(void) { _values.clear(); }
@ -150,7 +150,7 @@ class AutoConnectSelectBasis : virtual public AutoConnectElementBasis {
_type = AC_Select; _type = AC_Select;
} }
virtual ~AutoConnectSelectBasis() {} virtual ~AutoConnectSelectBasis() {}
const String toHTML(void) const; const String toHTML(void) const override;
void add(const String option) { _options.push_back(option); } void add(const String option) { _options.push_back(option); }
void empty(void) { _options.clear(); } void empty(void) { _options.clear(); }
@ -175,7 +175,7 @@ class AutoConnectSubmitBasis : virtual public AutoConnectElementBasis {
_type = AC_Submit; _type = AC_Submit;
} }
virtual ~AutoConnectSubmitBasis() {} virtual ~AutoConnectSubmitBasis() {}
const String toHTML(void) const; const String toHTML(void) const override;
String uri; /**< An url of submitting to */ String uri; /**< An url of submitting to */
}; };
@ -195,7 +195,7 @@ class AutoConnectTextBasis : virtual public AutoConnectElementBasis {
_type = AC_Text; _type = AC_Text;
} }
virtual ~AutoConnectTextBasis() {} virtual ~AutoConnectTextBasis() {}
const String toHTML(void) const; const String toHTML(void) const override;
String style; /**< CSS style modifier native code */ String style; /**< CSS style modifier native code */
}; };

@ -52,9 +52,11 @@ class AutoConnectElementJson : virtual public AutoConnectElementBasis {
} }
~AutoConnectElementJson() {} ~AutoConnectElementJson() {}
virtual bool loadElement(const JsonObject& json); virtual bool loadElement(const JsonObject& json);
virtual void serialize(JsonObject& json);
protected: protected:
void _setElement(const JsonObject& json); void _setElement(const JsonObject& json);
void _serialize(JsonObject& json);
}; };
/** /**
@ -73,7 +75,8 @@ class AutoConnectButtonJson : public AutoConnectElementJson, public AutoConnectB
AutoConnectButtonBasis::action = action; AutoConnectButtonBasis::action = action;
} }
~AutoConnectButtonJson() {} ~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; AutoConnectCheckboxBasis::checked = checked;
} }
~AutoConnectCheckboxJson() {} ~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; AutoConnectInputBasis::label = label;
} }
~AutoConnectInputJson() {} ~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; AutoConnectRadioBasis::checked = checked;
} }
~AutoConnectRadioJson() {} ~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; AutoConnectSelectBasis::label = label;
} }
~AutoConnectSelectJson() {} ~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; AutoConnectSubmitBasis::uri = uri;
} }
~AutoConnectSubmitJson() {} ~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; AutoConnectTextBasis::style = style;
} }
~AutoConnectTextJson() {} ~AutoConnectTextJson() {}
bool loadElement(const JsonObject& json); bool loadElement(const JsonObject& json) override;
void serialize(JsonObject& json) override;
}; };
#endif // _AUTOCONNECTELEMENTJSON_H_ #endif // _AUTOCONNECTELEMENTJSON_H_

@ -14,16 +14,25 @@
/** /**
* Set items common to any type of AutoConnectElement from JSON objects. * 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) { void AutoConnectElementJson::_setElement(const JsonObject& json) {
name = json.get<String>(F(AUTOCONNECT_JSON_KEY_NAME)); name = json.get<String>(F(AUTOCONNECT_JSON_KEY_NAME));
value = json.get<String>(F(AUTOCONNECT_JSON_KEY_VALUE)); value = json.get<String>(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. * 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 true AutoConnectElement loaded
* @return false Type of AutoConnectElement is mismatched. * @return false Type of AutoConnectElement is mismatched.
*/ */
@ -36,9 +45,18 @@ bool AutoConnectElementJson::loadElement(const JsonObject& json) {
return false; 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. * 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 true AutoConnectElement loaded
* @return false Type of AutoConnectElement is mismatched. * @return false Type of AutoConnectElement is mismatched.
*/ */
@ -52,9 +70,20 @@ bool AutoConnectButtonJson::loadElement(const JsonObject& json) {
return false; 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. * 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 true AutoConnectElement loaded
* @return false Type of AutoConnectElement is mismatched. * @return false Type of AutoConnectElement is mismatched.
*/ */
@ -69,9 +98,22 @@ bool AutoConnectCheckboxJson::loadElement(const JsonObject& json) {
return false; 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. * 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 true AutoConnectElement loaded
* @return false Type of AutoConnectElement is mismatched. * @return false Type of AutoConnectElement is mismatched.
*/ */
@ -86,9 +128,21 @@ bool AutoConnectInputJson::loadElement(const JsonObject& json) {
return false; 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. * 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 true AutoConnectElement loaded
* @return false Type of AutoConnectElement is mismatched. * @return false Type of AutoConnectElement is mismatched.
*/ */
@ -112,9 +166,31 @@ bool AutoConnectRadioJson::loadElement(const JsonObject& json) {
return false; 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. * 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 true AutoConnectElement loaded
* @return false Type of AutoConnectElement is mismatched. * @return false Type of AutoConnectElement is mismatched.
*/ */
@ -132,9 +208,22 @@ bool AutoConnectSelectJson::loadElement(const JsonObject& json) {
return false; 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. * 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 true AutoConnectElement loaded
* @return false Type of AutoConnectElement is mismatched. * @return false Type of AutoConnectElement is mismatched.
*/ */
@ -148,9 +237,20 @@ bool AutoConnectSubmitJson::loadElement(const JsonObject& json) {
return false; 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. * 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 true AutoConnectElement loaded
* @return false Type of AutoConnectElement is mismatched. * @return false Type of AutoConnectElement is mismatched.
*/ */
@ -164,4 +264,15 @@ bool AutoConnectTextJson::loadElement(const JsonObject& json) {
return false; 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_ #endif // _AUTOCONNECTELEMENTJSONIMPL_H_

@ -1083,9 +1083,9 @@ String AutoConnect::_token_UPTIME(PageArgument& args) {
String AutoConnect::_token_BOOTURI(PageArgument& args) { String AutoConnect::_token_BOOTURI(PageArgument& args) {
AC_UNUSED(args); AC_UNUSED(args);
if (_apConfig.bootUri == AC_URIONBOOT_ROOT) if (_apConfig.bootUri == AC_ONBOOTURI_ROOT)
return String(AUTOCONNECT_URI); 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("/"); return _apConfig.homeUri.length() > 0 ? _apConfig.homeUri : String("/");
else else
return ""; return "";

Loading…
Cancel
Save