Supports AutoConnectAux

pull/41/head
Hieromon Ikasamo 6 years ago
parent 45dd91903a
commit 258fb32722
  1. 1
      src/AutoConnect.cpp
  2. 1
      src/AutoConnect.h
  3. 97
      src/AutoConnectAux.cpp
  4. 3
      src/AutoConnectAux.h
  5. 2
      src/AutoConnectPage.cpp

@ -602,7 +602,6 @@ String AutoConnect::_induceConnect(PageArgument& args) {
// Read from EEPROM
AutoConnectCredential credential(_apConfig.boundaryOffset);
struct station_config entry;
// AC_DBG("Load credential:%s\n", args.arg(AUTOCONNECT_PARAMID_CRED).c_str());
credential.load(args.arg(AUTOCONNECT_PARAMID_CRED).c_str(), &entry);
strncpy(reinterpret_cast<char*>(_credential.ssid), reinterpret_cast<const char*>(entry.ssid), sizeof(_credential.ssid));
strncpy(reinterpret_cast<char*>(_credential.password), reinterpret_cast<const char*>(entry.password), sizeof(_credential.password));

@ -231,6 +231,7 @@ class AutoConnect {
/** Extended pages made up with AutoConnectAux */
std::unique_ptr<AutoConnectAux> _aux;
String _auxLastUri;
/** Saved configurations */
AutoConnectConfig _apConfig;

@ -51,6 +51,11 @@ const char AutoConnectAux::_PAGE_AUX[] PROGMEM = {
"</div>"
"<script>"
"function _sa(url) {"
"var uri=document.createElement('input');"
"uri.setAttribute('type','hidden');"
"uri.setAttribute('name','uri');"
"uri.setAttribute('value','{{AUX_URI}}');"
"document.getElementById('_aux').appendChild(uri);"
"document.getElementById('_aux').action=url;"
"document.getElementById('_aux').submit();"
"}"
@ -122,6 +127,28 @@ bool AutoConnectAux::release(const String& name) {
return rc;
}
/**
* Set the value to AutoConnectRadio element.
* @param name A string of AutoConnectRadio element name to set the value.
* @param checked A checked value.
* @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 uint8_t checked) {
// AutoConnectElement* elm = getElement(name);
// if (elm) {
// if (elm->typeOf() == AC_Radio) {
// AutoConnectRadio* elmRadio = reinterpret_cast<AutoConnectRadio*>(elm);
// elmRadio->checked = checked;
// return true;
// }
// else
// AC_DBG("Element<%s> value type mismatch\n", name.c_str());
// }
// return false;
//}
/**
* Set the value to specified element.
* @param name A string of element name to set the value.
@ -133,8 +160,19 @@ bool AutoConnectAux::release(const String& name) {
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;
if (elm->typeOf() != AC_Select) {
if (elm->typeOf() == AC_Checkbox) {
if (value == "checked") {
AutoConnectCheckbox* elmCheckbox = reinterpret_cast<AutoConnectCheckbox*>(elm);
elmCheckbox->checked = true;
}
}
else if (elm->typeOf() == AC_Radio) {
AutoConnectRadio* elmRadio = reinterpret_cast<AutoConnectRadio*>(elm);
elmRadio->checked = static_cast<uint8_t>(value.toInt());
}
else
elm->value = value;
return true;
}
else
@ -213,6 +251,16 @@ void AutoConnectAux::_join(AutoConnect& ac) {
_next->_join(ac);
}
/**
* Insert the uri that caused the request to the aux.
*/
const String AutoConnectAux::_indicateUri(PageArgument& args) {
AC_UNUSED(args);
String lastUri = _ac->_auxLastUri;
lastUri.replace(String("/"), String("&#47;"));
return lastUri;
}
/**
* Insert the token handler of PageBuilder. This handler inserts HTML
* elements generated by the whole AutoConnectElements to the auxiliary page.
@ -220,12 +268,38 @@ void AutoConnectAux::_join(AutoConnect& ac) {
* @return HTML string that should be inserted.
*/
const String AutoConnectAux::_insertElement(PageArgument& args) {
AC_UNUSED(args);
String body = String();
// Save elements owned by AutoConnectAux that caused the request.
String auxUri = args.arg("uri");
if (auxUri.length()) {
auxUri.replace(String("&#47;"), String("/"));
AutoConnectAux* auxPage = _ac->_aux.get();
while (auxPage) {
if (auxPage->_uriStr == auxUri)
break;
auxPage = auxPage->_next.get();
}
// Caused page is exist, restore elements value.
if (auxPage) {
for (size_t n = 0; n < args.size(); n++) {
String elmName = args.argName(n);
String elmValue = args.arg(n);
AutoConnectElement* elm = auxPage->getElement(elmName);
if (elm) {
if (elm->typeOf() == AC_Checkbox)
elmValue = "checked";
auxPage->setElementValue(elmName, elmValue);
}
}
}
}
// Save invoked Aux.
_ac->_auxLastUri = _uriStr;
// Build the current Aux page.
String body = String();
if (_handler) {
if (_order & AC_EXIT_AHEAD) {
AC_DBG("CB %s\n", uri());
AC_DBG("CB in AHEAD %s\n", uri());
body += _handler(*this, args);
}
}
@ -237,11 +311,10 @@ const String AutoConnectAux::_insertElement(PageArgument& args) {
if (_handler) {
if (_order & AC_EXIT_LATER) {
AC_DBG("CB %s\n", uri());
AC_DBG("CB in LATER %s\n", uri());
body += _handler(*this, args);
}
}
return body;
}
@ -281,6 +354,7 @@ PageElement* AutoConnectAux::_setupPage(String uri) {
elm->addToken(String(FPSTR("MENU_PRE")), std::bind(&AutoConnect::_token_MENU_PRE, mother, std::placeholders::_1));
elm->addToken(String(FPSTR("MENU_AUX")), std::bind(&AutoConnect::_token_MENU_AUX, mother, std::placeholders::_1));
elm->addToken(String(FPSTR("MENU_POST")), std::bind(&AutoConnect::_token_MENU_POST, mother, std::placeholders::_1));
elm->addToken(String(FPSTR("AUX_URI")), std::bind(&AutoConnectAux::_indicateUri, this, std::placeholders::_1));
elm->addToken(String(FPSTR("AUX_ELEMENT")), std::bind(&AutoConnectAux::_insertElement, this, std::placeholders::_1));
}
}
@ -879,6 +953,9 @@ size_t AutoConnectAux::saveElement(Stream& out, std::vector<String> const& names
}
}
size_n = element.printTo(out);
AC_DBG("");
element.printTo(Serial);
AC_DBG_DUMB("\n");
}
else if (amount == 0) {
JsonObject& json = jb.createObject();
@ -892,6 +969,9 @@ size_t AutoConnectAux::saveElement(Stream& out, std::vector<String> const& names
elm.serialize(element);
}
size_n = json.prettyPrintTo(out);
AC_DBG("");
json.printTo(Serial);
AC_DBG_DUMB("\n");
}
else if (amount >= 2) {
JsonArray& elements = jb.createArray();
@ -906,6 +986,9 @@ size_t AutoConnectAux::saveElement(Stream& out, std::vector<String> const& names
}
}
size_n = elements.prettyPrintTo(out);
AC_DBG("");
elements.printTo(Serial);
AC_DBG_DUMB("\n");
}
}
return size_n;

@ -58,6 +58,8 @@ class AutoConnectAux : public PageBuilder {
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 String& name); /**< Release an AutoConnectElement */
// bool setElementValue(const String& name, const bool checked); /**< Set checked value to specified AutoConnectChechbox element */
// bool setElementValue(const String& name, const uint8_t checked); /**< Set checked value to specified AutoConnectRadio element */
bool setElementValue(const String& name, const String value); /**< Set value 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 */
@ -80,6 +82,7 @@ class AutoConnectAux : public PageBuilder {
const String _insertElement(PageArgument& args); /**< Insert a generated HTML to the page built by PageBuilder */
const String _injectTitle(PageArgument& args) const { (void)(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 _indicateUri(PageArgument& args); /**< Inject the uri that caused the request */
static AutoConnectElement& _nullElement(void); /**< A static returning value as invalid */
#ifdef AUTOCONNECT_USE_JSON

@ -464,6 +464,8 @@ const char AutoConnect::_ELM_HTML_HEAD[] PROGMEM = {
"<!DOCTYPE html>"
"<html>"
"<head>"
"<meta http-equiv=\"Pragma\" content=\"no-cache\">"
"<meta http-equiv=\"Cache-Control\" content=\"no-cache\">"
"<meta charset=\"UTF-8\" name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
};

Loading…
Cancel
Save