|
|
|
@ -77,7 +77,7 @@ AutoConnectAux::~AutoConnectAux() { |
|
|
|
|
*/ |
|
|
|
|
void AutoConnectAux::add(AutoConnectElement& addon) { |
|
|
|
|
_addonElm.push_back(addon); |
|
|
|
|
AC_DBG("%s placed on %s\n", addon.name.length() ? addon.name.c_str() : "*nonamed*", uri()); |
|
|
|
|
AC_DBG("%s placed on %s\n", addon.name.length() ? addon.name.c_str() : "*noname", uri()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -258,6 +258,10 @@ AutoConnectElement* AutoConnectAux::_createElement(const JsonObject& json) { |
|
|
|
|
AutoConnectInput* cert_elm = new AutoConnectInput; |
|
|
|
|
return reinterpret_cast<AutoConnectElement*>(cert_elm); |
|
|
|
|
} |
|
|
|
|
case AC_Radio: { |
|
|
|
|
AutoConnectRadio* cert_elm = new AutoConnectRadio; |
|
|
|
|
return reinterpret_cast<AutoConnectElement*>(cert_elm); |
|
|
|
|
} |
|
|
|
|
case AC_Select: { |
|
|
|
|
AutoConnectSelect* cert_elm = new AutoConnectSelect; |
|
|
|
|
return reinterpret_cast<AutoConnectElement*>(cert_elm); |
|
|
|
@ -274,59 +278,112 @@ AutoConnectElement* AutoConnectAux::_createElement(const JsonObject& json) { |
|
|
|
|
return elm; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs an AutoConnectAux instance by reading all the |
|
|
|
|
* AutoConnectElements of the specified URI from the elements defined JSON. |
|
|
|
|
* @param in AutoConnectAux element data which is described by JSON. |
|
|
|
|
* @return true The element collection successfully loaded. |
|
|
|
|
* @return false Invalid JSON data occurred.
|
|
|
|
|
*/ |
|
|
|
|
bool AutoConnectAux::load(const char* in) { |
|
|
|
|
// DynamicJsonBuffer jsonBuffer();
|
|
|
|
|
const size_t bufferSize = 2*JSON_ARRAY_SIZE(2) + 3*JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(9) + JSON_OBJECT_SIZE(1) + 2*JSON_OBJECT_SIZE(3) + 9*JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(6) + 1080; |
|
|
|
|
DynamicJsonBuffer jsonBuffer(bufferSize); |
|
|
|
|
JsonObject& jb = jsonBuffer.parseObject(in); |
|
|
|
|
return _load(jb); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool AutoConnectAux::load(const __FlashStringHelper* in) { |
|
|
|
|
// DynamicJsonBuffer jsonBuffer();
|
|
|
|
|
const size_t bufferSize = 2*JSON_ARRAY_SIZE(2) + 3*JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(9) + JSON_OBJECT_SIZE(1) + 2*JSON_OBJECT_SIZE(3) + 9*JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(6) + 1080; |
|
|
|
|
DynamicJsonBuffer jsonBuffer(bufferSize); |
|
|
|
|
JsonObject& jb = jsonBuffer.parseObject(in); |
|
|
|
|
return _load(jb); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool AutoConnectAux::load(Stream& in) { |
|
|
|
|
// DynamicJsonBuffer jsonBuffer();
|
|
|
|
|
const size_t bufferSize = 2*JSON_ARRAY_SIZE(2) + 3*JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(9) + JSON_OBJECT_SIZE(1) + 2*JSON_OBJECT_SIZE(3) + 9*JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(6) + 1080; |
|
|
|
|
DynamicJsonBuffer jsonBuffer(bufferSize); |
|
|
|
|
JsonObject& jb = jsonBuffer.parseObject(in); |
|
|
|
|
return _load(jb); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool AutoConnectAux::_load(JsonObject& jb) { |
|
|
|
|
if (!jb.success()) |
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
_title = jb.get<String>(F(AUTOCONNECT_JSON_KEY_TITLE)); |
|
|
|
|
_uriStr = jb.get<String>(F(AUTOCONNECT_JSON_KEY_URI)); |
|
|
|
|
_uri = _uriStr.c_str(); |
|
|
|
|
_menu = jb.get<bool>(F(AUTOCONNECT_JSON_KEY_MENU)); |
|
|
|
|
(void)_loadElement(jb, "*"); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Load element specified by the name parameter from the stream |
|
|
|
|
* described by JSON. Usually, the Stream is specified a storm file of |
|
|
|
|
* SD or SPIFFS. The Stream must be opened before invoking the function. |
|
|
|
|
* @param in Reference of the Stream which contains the parameter |
|
|
|
|
* file described by JSON. |
|
|
|
|
* @param name The element name to be loaded. |
|
|
|
|
* data described by JSON. |
|
|
|
|
* @param name The element name to be loaded. '*'specifies that all |
|
|
|
|
* elements are to be loaded. |
|
|
|
|
* @return A reference of loaded AutoConnectElement instance. |
|
|
|
|
*/ |
|
|
|
|
AutoConnectElement& AutoConnectAux::loadElement(const char* in, const String name) { |
|
|
|
|
DynamicJsonBuffer jsonBuffer; |
|
|
|
|
JsonObject& jb = jsonBuffer.parseObject(in); |
|
|
|
|
return _loadElement(jb, name); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
AutoConnectElement& AutoConnectAux::loadElement(const __FlashStringHelper* in, const String name) { |
|
|
|
|
DynamicJsonBuffer jsonBuffer; |
|
|
|
|
JsonObject& jb = jsonBuffer.parseObject(in); |
|
|
|
|
return _loadElement(jb, name); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
AutoConnectElement& AutoConnectAux::loadElement(Stream& in, const String name) { |
|
|
|
|
DynamicJsonBuffer jsonBuffer; |
|
|
|
|
JsonObject& jb = jsonBuffer.parseObject(in); |
|
|
|
|
return _loadElement(jb, name); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!jb.success()) |
|
|
|
|
return _nullElement(); |
|
|
|
|
AutoConnectElement& AutoConnectAux::_loadElement(JsonObject& jb, const String name) { |
|
|
|
|
AutoConnectElement* auxElm = nullptr; |
|
|
|
|
bool wc = name == "*"; |
|
|
|
|
|
|
|
|
|
JsonArray& aux = jb[AUTOCONNECT_JSON_KEY_AUX]; |
|
|
|
|
if (!aux.success()) |
|
|
|
|
if (!jb.success()) |
|
|
|
|
return _nullElement(); |
|
|
|
|
|
|
|
|
|
for (JsonObject& page : aux) { |
|
|
|
|
if (page["uri"].as<String>() == String(uri())) { |
|
|
|
|
JsonArray& element = page[AUTOCONNECT_JSON_KEY_ELEMENT]; |
|
|
|
|
for (JsonObject& elm : element) { |
|
|
|
|
if (name.equalsIgnoreCase(elm.get<String>(F(AUTOCONNECT_JSON_KEY_NAME)))) { |
|
|
|
|
// The specified element is defined in the JSON stream.
|
|
|
|
|
// Loads from JSON object.
|
|
|
|
|
const String inType = elm[AUTOCONNECT_JSON_KEY_TYPE].as<String>(); |
|
|
|
|
AutoConnectElement* auxElm = _getElement(name); |
|
|
|
|
// The element is not created yet, create new one.
|
|
|
|
|
if (!auxElm) { |
|
|
|
|
if ((auxElm = _createElement(elm))) { |
|
|
|
|
AC_DBG("%s<%d> of %s created\n", name.c_str(), (int)(auxElm->typeOf()), uri()); |
|
|
|
|
add(*auxElm); // Insert to AutoConnect
|
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
AC_DBG("%s unknown element type\n", name.c_str()); |
|
|
|
|
return _nullElement(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (auxElm->loadElement(elm)) { |
|
|
|
|
AC_DBG("%s<%d> of %s loaded\n", name.c_str(), (int)auxElm->typeOf(), uri()); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
// Element type mismatch
|
|
|
|
|
AC_DBG("Type of %s element mismatched\n", name.c_str()); |
|
|
|
|
return _nullElement(); |
|
|
|
|
} |
|
|
|
|
JsonArray& elements = jb[AUTOCONNECT_JSON_KEY_ELEMENT]; |
|
|
|
|
for (JsonObject& element : elements) { |
|
|
|
|
String elmName = element.get<String>(F(AUTOCONNECT_JSON_KEY_NAME)); |
|
|
|
|
if (wc || name.equalsIgnoreCase(elmName)) { |
|
|
|
|
// The specified element is defined in the JSON stream.
|
|
|
|
|
// Loads from JSON object.
|
|
|
|
|
auxElm = _getElement(elmName); |
|
|
|
|
// The element is not created yet, create new one.
|
|
|
|
|
if (!auxElm) { |
|
|
|
|
if ((auxElm = _createElement(element))) { |
|
|
|
|
AC_DBG("%s<%d> of %s created\n", elmName.c_str(), (int)(auxElm->typeOf()), uri()); |
|
|
|
|
add(*auxElm); // Insert to AutoConnect
|
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
AC_DBG("%s unknown element type\n", elmName.c_str()); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (auxElm->loadElement(element)) |
|
|
|
|
AC_DBG("%s<%d> of %s loaded\n", auxElm->name.c_str(), (int)auxElm->typeOf(), uri()); |
|
|
|
|
else { |
|
|
|
|
// Element type mismatch
|
|
|
|
|
AC_DBG("Type of %s element mismatched\n", elmName.c_str()); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return _nullElement(); |
|
|
|
|
return auxElm ? *auxElm : _nullElement(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -342,12 +399,12 @@ size_t AutoConnectAux::saveElement(Stream& out, const AutoConnectElement& elemen |
|
|
|
|
if (!jb.success()) |
|
|
|
|
return 0; |
|
|
|
|
|
|
|
|
|
JsonArray& aux = jb[AUTOCONNECT_JSON_KEY_AUX]; |
|
|
|
|
JsonArray& aux = jb["aux"]; |
|
|
|
|
if (!aux.success()) |
|
|
|
|
return 0; |
|
|
|
|
|
|
|
|
|
for (JsonObject& page : aux) { |
|
|
|
|
if (page["uri"].as<String>() == String(uri())) { |
|
|
|
|
if (page["aux"].as<String>() == String(uri())) { |
|
|
|
|
JsonArray& element_j = page[AUTOCONNECT_JSON_KEY_ELEMENT]; |
|
|
|
|
for (JsonObject& elm : element_j) { |
|
|
|
|
if (elm[AUTOCONNECT_JSON_KEY_NAME].as<String>() == element.name) { |
|
|
|
@ -387,6 +444,7 @@ const ACElement_t AutoConnectAux::_asElementType(const String type) { |
|
|
|
|
{ AUTOCONNECT_JSON_TYPE_ACCHECKBOX, AC_Checkbox }, |
|
|
|
|
{ AUTOCONNECT_JSON_TYPE_ACELEMENT, AC_Element }, |
|
|
|
|
{ AUTOCONNECT_JSON_TYPE_ACINPUT, AC_Input }, |
|
|
|
|
{ AUTOCONNECT_JSON_TYPE_ACRADIO, AC_Radio }, |
|
|
|
|
{ AUTOCONNECT_JSON_TYPE_ACSELECT, AC_Select }, |
|
|
|
|
{ AUTOCONNECT_JSON_TYPE_ACSUBMIT, AC_Submit }, |
|
|
|
|
{ AUTOCONNECT_JSON_TYPE_ACTEXT, AC_Text } |
|
|
|
|