From 1264c29caad9ff71a69b666eb54ecec39e4bfe10 Mon Sep 17 00:00:00 2001
From: Eric Duminil <eric.duminil@gmail.com>
Date: Mon, 7 Mar 2022 10:15:38 +0100
Subject: [PATCH] Trying StateChangedCallback

---
 ampel-firmware/src/lib/IotWebConf/src/IotWebConf.cpp | 12 +++++++++++-
 ampel-firmware/src/lib/IotWebConf/src/IotWebConf.h   |  9 +++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/ampel-firmware/src/lib/IotWebConf/src/IotWebConf.cpp b/ampel-firmware/src/lib/IotWebConf/src/IotWebConf.cpp
index b0fca3c..5ad5427 100644
--- a/ampel-firmware/src/lib/IotWebConf/src/IotWebConf.cpp
+++ b/ampel-firmware/src/lib/IotWebConf/src/IotWebConf.cpp
@@ -246,6 +246,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)
 {
@@ -617,6 +623,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);
@@ -983,4 +993,4 @@ WifiAuthInfo* IotWebConf::handleConnectWifiFailure()
   return nullptr;
 }
 
-} // end namespace
\ No newline at end of file
+} // end namespace
diff --git a/ampel-firmware/src/lib/IotWebConf/src/IotWebConf.h b/ampel-firmware/src/lib/IotWebConf/src/IotWebConf.h
index ddf708b..4fa4bda 100644
--- a/ampel-firmware/src/lib/IotWebConf/src/IotWebConf.h
+++ b/ampel-firmware/src/lib/IotWebConf/src/IotWebConf.h
@@ -293,6 +293,13 @@ public:
    */
   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.
@@ -600,6 +607,8 @@ private:
   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);
-- 
GitLab