Suports AutoConnectAux

pull/41/head
Hieromon Ikasamo 6 years ago
parent d41ae5d08b
commit 45dd91903a
  1. 1
      src/AutoConnect.cpp
  2. 100
      src/AutoConnectAux.cpp
  3. 5
      src/AutoConnectAux.h
  4. 2
      src/AutoConnectPage.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) {

@ -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<String> values) {
bool AutoConnectAux::setElementValue(const String& name, std::vector<String> 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<size_t>(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<String> 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<size_t>(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;
}
/**

@ -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<String> values); /**< Set values collection to specified element */
bool setElementValue(const String& name, std::vector<String> 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<String> const& names = {}); /**< Write elements of AutoConnectAux to the stream */
#endif // !AUTOCONNECT_USE_JSON
protected:

@ -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) {

Loading…
Cancel
Save