From 45dd91903a726efdc6e7746156856906495d2198 Mon Sep 17 00:00:00 2001 From: Hieromon Ikasamo Date: Thu, 20 Dec 2018 21:38:36 +0900 Subject: [PATCH] Suports AutoConnectAux --- src/AutoConnect.cpp | 1 + src/AutoConnectAux.cpp | 100 ++++++++++++++++++++++++---------------- src/AutoConnectAux.h | 5 +- src/AutoConnectPage.cpp | 2 + 4 files changed, 66 insertions(+), 42 deletions(-) diff --git a/src/AutoConnect.cpp b/src/AutoConnect.cpp index 9f095b2..e65b850 100644 --- a/src/AutoConnect.cpp +++ b/src/AutoConnect.cpp @@ -496,6 +496,7 @@ bool AutoConnect::_loadAvailCredential() { if (credential.entries() > 0) { // Scan the vicinity only when the saved credentials are existing. + WiFi.scanDelete(); int8_t nn = WiFi.scanNetworks(false, true); AC_DBG("%d network(s) found\n", (int)nn); if (nn > 0) { diff --git a/src/AutoConnectAux.cpp b/src/AutoConnectAux.cpp index 2545f28..96ca874 100644 --- a/src/AutoConnectAux.cpp +++ b/src/AutoConnectAux.cpp @@ -151,7 +151,7 @@ bool AutoConnectAux::setElementValue(const String& name, const String value) { * @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 AutoConnectAux::setElementValue(const String& name, std::vector const& values) { bool rc = false; AutoConnectElement* elm = getElement(name); @@ -841,52 +841,74 @@ AutoConnectElement& AutoConnectAux::_loadElement(JsonObject& element, const Stri return auxElm ? *auxElm : _nullElement(); } -/** - * Serialize whole elements owned by an AutoConnectAux into the stream. - * @param out An output stream - * @return Number of bytes output - */ -size_t AutoConnectAux::save(Stream& out) { - size_t bs = 0; - size_t e = _addonElm.size(); - - for (size_t n = 0; n < e; e++) { - AutoConnectElement& elm = _addonElm[n]; - bs += elm.getObjectSize(); - } - if (bs <= 0) - return 0; - - DynamicJsonBuffer jb(bs + JSON_OBJECT_SIZE(4)+ JSON_ARRAY_SIZE(1)); - JsonObject& json = jb.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 (std::size_t n = 0; n < e; n++) { - JsonObject& element = elements.createNestedObject(); - AutoConnectElement& elm = _addonElm[n]; - elm.serialize(element); - } - return static_cast(json.prettyPrintTo(out)); -} - /** * Serialize an element specified the name into the stream. * @param name An element name to be output. * @return Number of bytes output */ -size_t AutoConnectAux::saveElement(const String& name, Stream& out) { - for (std::size_t n = 0; n < _addonElm.size(); n++) { - AutoConnectElement& elm = _addonElm[n]; - if (elm.name == name) { - DynamicJsonBuffer jb(elm.getObjectSize()); +size_t AutoConnectAux::saveElement(Stream& out, std::vector const& names) { + size_t bufferSize = 0; + size_t stores = _addonElm.size(); + size_t amount = names.size(); + size_t size_n = 0; + + // Calculate JSON buffer size + if (amount == 0) + bufferSize += JSON_OBJECT_SIZE(4); + if (amount != 1) + bufferSize += JSON_ARRAY_SIZE(amount); + for (size_t n = 0; n < amount; n++) { + for (size_t e = 0; e < stores; e++) { + AutoConnectElement& elm = _addonElm[e]; + if (elm.name.equalsIgnoreCase(names[n])) { + bufferSize += elm.getObjectSize(); + break; + } + } + } + + if (bufferSize > 0) { + DynamicJsonBuffer jb(bufferSize); + if (amount == 1) { JsonObject& element = jb.createObject(); - elm.serialize(element); - return static_cast(element.prettyPrintTo(out)); + for (size_t e = 0; e < stores; e++) { + AutoConnectElement& elm = _addonElm[e]; + if (elm.name.equalsIgnoreCase(names[0])) { + elm.serialize(element); + break; + } + } + size_n = element.printTo(out); + } + else if (amount == 0) { + JsonObject& json = jb.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 e = 0; e < stores; e++) { + JsonObject& element = elements.createNestedObject(); + AutoConnectElement& elm = _addonElm[e]; + elm.serialize(element); + } + size_n = json.prettyPrintTo(out); + } + else if (amount >= 2) { + JsonArray& elements = jb.createArray(); + for (size_t n = 0; n < amount; n++) { + for (size_t e = 0; e < stores; e++) { + AutoConnectElement& elm = _addonElm[e]; + if (elm.name.equalsIgnoreCase(names[n])) { + JsonObject& element = elements.createNestedObject(); + elm.serialize(element); + break; + } + } + } + size_n = elements.prettyPrintTo(out); } } - return 0; + return size_n; } /** diff --git a/src/AutoConnectAux.h b/src/AutoConnectAux.h index 50b8507..e69e827 100644 --- a/src/AutoConnectAux.h +++ b/src/AutoConnectAux.h @@ -59,7 +59,7 @@ class AutoConnectAux : public PageBuilder { void menu(const bool post) { _menu = post; } /**< Set or reset the display as menu item for this aux */ bool release(const String& name); /**< Release an AutoConnectElement */ bool setElementValue(const String& name, const String value); /**< Set value to specified element */ - bool setElementValue(const String& name, std::vector values); /**< Set values collection to specified element */ + bool setElementValue(const String& name, std::vector const& values); /**< Set values collection to specified element */ 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 */ @@ -70,8 +70,7 @@ class AutoConnectAux : public PageBuilder { 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(Stream& in, const String& name = String(""), const size_t bufferSize = AUTOCONNECT_JSON_BUFFER_SIZE); /**< Load specified element */ - size_t save(Stream& out); /**< Write AutoConnectAux elements to the stream */ - size_t saveElement(const String& name, Stream& out); /**< Write an element of AutoConnectAux to the stream */ + size_t saveElement(Stream& out, std::vector const& names = {}); /**< Write elements of AutoConnectAux to the stream */ #endif // !AUTOCONNECT_USE_JSON protected: diff --git a/src/AutoConnectPage.cpp b/src/AutoConnectPage.cpp index 25b696b..e8e0cd6 100644 --- a/src/AutoConnectPage.cpp +++ b/src/AutoConnectPage.cpp @@ -1021,7 +1021,9 @@ String AutoConnect::_token_LIST_SSID(PageArgument& args) { AC_UNUSED(args); String ssidList = ""; _hiddenSSIDCount = 0; + WiFi.scanDelete(); int8_t nn = WiFi.scanNetworks(false, true); + AC_DBG("%d network(s) found\n", (int)nn); for (uint8_t i = 0; i < nn; i++) { String ssid = WiFi.SSID(i); if (ssid.length() > 0) {