diff --git a/NukiWrapper.cpp b/NukiWrapper.cpp index 9b6df33..502fea4 100644 --- a/NukiWrapper.cpp +++ b/NukiWrapper.cpp @@ -144,6 +144,12 @@ void NukiWrapper::setPin(const uint16_t pin) _nukiBle.saveSecurityPincode(pin); } +void NukiWrapper::unpair() +{ + _nukiBle.unPairNuki(); + _paired = false; +} + void NukiWrapper::updateKeyTurnerState() { _nukiBle.requestKeyTurnerState(&_keyTurnerState); diff --git a/NukiWrapper.h b/NukiWrapper.h index ec3bf86..a62376b 100644 --- a/NukiWrapper.h +++ b/NukiWrapper.h @@ -17,6 +17,8 @@ public: void setPin(const uint16_t pin); + void unpair(); + const Nuki::KeyTurnerState& keyTurnerState(); const bool isPaired(); diff --git a/Version.h b/Version.h index acf6488..3a47e2c 100644 --- a/Version.h +++ b/Version.h @@ -1,3 +1,3 @@ #pragma once -#define nuki_hub_version "2.4" \ No newline at end of file +#define nuki_hub_version "2.5" \ No newline at end of file diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index 55299fe..1d65999 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -10,6 +10,8 @@ WebCfgServer::WebCfgServer(NukiWrapper* nuki, Network* network, EthServer* ethSe _preferences(preferences), _allowRestartToPortal(allowRestartToPortal) { + _confirmCode = generateConfirmCode(); + String str = _preferences->getString(preference_cred_user); if(str.length() > 0) @@ -51,6 +53,13 @@ void WebCfgServer::initialize() buildConfigureWifiHtml(response); _server.send(200, "text/html", response); }); + _server.on("/unpair", [&]() { + if (_hasCredentials && !_server.authenticate(_credUser, _credPassword)) { + return _server.requestAuthentication(); + } + + processUnpair(); + }); _server.on("/wifimanager", [&]() { if (_hasCredentials && !_server.authenticate(_credUser, _credPassword)) { return _server.requestAuthentication(); @@ -320,6 +329,15 @@ void WebCfgServer::buildCredHtml(String &response) response.concat("
"); response.concat(""); + _confirmCode = generateConfirmCode(); + response.concat("

Unpair NUKI

"); + response.concat("
"); + String message = "Type "; + message.concat(_confirmCode); + message.concat(" to confirm unpair"); + printInputField(response, "CONFIRMTOKEN", message.c_str(), "", 10); + response.concat("

"); + response.concat("\n"); response.concat("\n"); } @@ -357,6 +375,42 @@ void WebCfgServer::buildConfigureWifiHtml(String &response) response.concat("\n"); } +//printInputField(response, "CONFIRMTOKEN", "Type confirm", "", 10); + + +//int count = _server.args(); +//for(int index = 0; index < count; index++) +//{ +//String key = _server.argName(index); +//String value = _server.arg(index); +void WebCfgServer::processUnpair() +{ + String response = ""; + if(_server.args() == 0) + { + buildConfirmHtml(response, "Confirm code is invalid.", 3); + _server.send(200, "text/html", response); + return; + } + else + { + String key = _server.argName(0); + String value = _server.arg(0); + + if(key != "CONFIRMTOKEN" || value != _confirmCode) + { + buildConfirmHtml(response, "Confirm code is invalid.", 3); + _server.send(200, "text/html", response); + return; + } + } + + buildConfirmHtml(response, "Unpairing NUKI and restarting.", 3); + _server.send(200, "text/html", response); + _nuki->unpair(); + waitAndProcess(false, 1000); + ESP.restart(); +} void WebCfgServer::buildHtmlHeader(String &response) { @@ -438,6 +492,13 @@ void WebCfgServer::printParameter(String& response, const char *description, con } + +String WebCfgServer::generateConfirmCode() +{ + int code = random(1000,9999); + return String(code); +} + void WebCfgServer::waitAndProcess(const bool blocking, const uint32_t duration) { unsigned long timeout = millis() + duration; @@ -454,3 +515,4 @@ void WebCfgServer::waitAndProcess(const bool blocking, const uint32_t duration) } } } + diff --git a/WebCfgServer.h b/WebCfgServer.h index 95fe4a0..7b6af4f 100644 --- a/WebCfgServer.h +++ b/WebCfgServer.h @@ -33,6 +33,7 @@ private: void buildCredHtml(String& response); void buildConfirmHtml(String& response, const String &message, uint32_t redirectDelay = 5); void buildConfigureWifiHtml(String& response); + void processUnpair(); void buildHtmlHeader(String& response); void printInputField(String& response, const char* token, const char* description, const char* value, const size_t maxLength, const bool isPassword = false); @@ -41,6 +42,7 @@ private: void printParameter(String& response, const char* description, const char* value); + String generateConfirmCode(); void waitAndProcess(const bool blocking, const uint32_t duration); WebServer _server; @@ -53,5 +55,7 @@ private: char _credPassword[20] = {0}; bool _allowRestartToPortal = false; + String _confirmCode = "----"; + bool _enabled = true; }; \ No newline at end of file diff --git a/webflash/nuki_hub.bin b/webflash/nuki_hub.bin index 31866b2..7f20226 100644 Binary files a/webflash/nuki_hub.bin and b/webflash/nuki_hub.bin differ