Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add state callback #228

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/IotWebConf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ void IotWebConf::setConfigSavedCallback(std::function<void()> func)
this->_configSavedCallback = func;
}

void IotWebConf::setStateChangedCallback(std::function<void(
NetworkState oldState, NetworkState newState)> func)
{
this->_stateChangedCallback = func;
}

void IotWebConf::setFormValidator(
std::function<bool(WebRequestWrapper* webRequestWrapper)> func)
{
Expand Down Expand Up @@ -629,6 +635,10 @@ void IotWebConf::changeState(NetworkState newState)
NetworkState oldState = this->_state;
this->_state = newState;
this->stateChanged(oldState, newState);
if (this->_stateChangedCallback != nullptr)
{
this->_stateChangedCallback(oldState, newState);
}
#ifdef IOTWEBCONF_DEBUG_TO_SERIAL
Serial.print("State changed from: ");
Serial.print(oldState);
Expand Down
9 changes: 9 additions & 0 deletions src/IotWebConf.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,13 @@ class IotWebConf
*/
void setConfigSavedCallback(std::function<void()> func);

/**
* Specify a callback method, that will be called when ever the state is changed.
* See NetworkState enum for possible values
*/
void setStateChangedCallback(std::function<void(
NetworkState oldState, NetworkState newState)> func);

/**
* Specify a callback method, that will be called when form validation is required.
* If the method will return false, the configuration will not be saved.
Expand Down Expand Up @@ -600,6 +607,8 @@ class IotWebConf
std::function<void()> _wifiConnectionCallback = nullptr;
std::function<void(int)> _configSavingCallback = nullptr;
std::function<void()> _configSavedCallback = nullptr;
std::function<void(NetworkState oldState, NetworkState newState)>
_stateChangedCallback = nullptr;
std::function<bool(WebRequestWrapper* webRequestWrapper)> _formValidator = nullptr;
std::function<void(const char*, const char*)> _apConnectionHandler =
&(IotWebConf::connectAp);
Expand Down