From 8a1d7e752392baac3e68c33eead1ef687fa0ae5b Mon Sep 17 00:00:00 2001 From: technyon Date: Fri, 6 May 2022 22:40:09 +0200 Subject: [PATCH] allow to enable publishing auth data in web interface --- NukiWrapper.cpp | 46 ++++++++++++++++++++++++++-------------------- NukiWrapper.h | 5 +++-- PreferencesKeys.h | 1 + WebCfgServer.cpp | 33 ++++++++++++++++++++++++++++++++- WebCfgServer.h | 1 + 5 files changed, 63 insertions(+), 23 deletions(-) diff --git a/NukiWrapper.cpp b/NukiWrapper.cpp index b9edca5..d5b421b 100644 --- a/NukiWrapper.cpp +++ b/NukiWrapper.cpp @@ -42,6 +42,7 @@ void NukiWrapper::initialize() _intervalLockstate = _preferences->getInt(preference_query_interval_lockstate); _intervalBattery = _preferences->getInt(preference_query_interval_battery); + _publishAuthData = _preferences->getBool(preference_publish_authdata); if(_intervalLockstate == 0) { @@ -59,7 +60,14 @@ void NukiWrapper::initialize() Serial.print(F("Lock state interval: ")); Serial.print(_intervalLockstate); Serial.print(F(" | Battery interval: ")); - Serial.println(_intervalBattery); + Serial.print(_intervalBattery); + Serial.print(F(" | Publish auth data: ")); + Serial.println(_publishAuthData ? "yes" : "no"); + + if(!_publishAuthData) + { + _clearAuthData = true; + } } void NukiWrapper::update() @@ -101,11 +109,6 @@ void NukiWrapper::update() _nextConfigUpdateTs = ts + _intervalConfig * 1000; updateConfig(); } - if(_nextLogUpdateTs == 0 || ts > _nextLogUpdateTs) - { - _nextLogUpdateTs = ts + 10 * 1000; - updateAuthInfo(); - } if(_nextLockAction != (Nuki::LockAction)0xff) { @@ -126,6 +129,12 @@ void NukiWrapper::update() } } + if(_clearAuthData) + { + _network->publishAuthorizationInfo(0, ""); + _clearAuthData = false; + } + memcpy(&_lastKeyTurnerState, &_keyTurnerState, sizeof(Nuki::KeyTurnerState)); } @@ -147,22 +156,15 @@ void NukiWrapper::updateKeyTurnerState() Serial.println(lockStateStr); } - updateAuthInfo(); + if(_publishAuthData) + { + updateAuthData(); + } } void NukiWrapper::updateBatteryState() { _nukiBle.requestBatteryReport(&_batteryReport); - - /* - Serial.print(F("Voltage: ")); Serial.println(_batteryReport.batteryVoltage); - Serial.print(F("Drain: ")); Serial.println(_batteryReport.batteryDrain); - Serial.print(F("Resistance: ")); Serial.println(_batteryReport.batteryResistance); - Serial.print(F("Max Current: ")); Serial.println(_batteryReport.maxTurnCurrent); - Serial.print(F("Crit. State: ")); Serial.println(_batteryReport.criticalBatteryState); - Serial.print(F("Lock Dist: ")); Serial.println(_batteryReport.lockDistance); - */ - _network->publishBatteryReport(_batteryReport); } @@ -174,13 +176,12 @@ void NukiWrapper::updateConfig() _network->publishAdvancedConfig(_nukiAdvancedConfig); } -void NukiWrapper::updateAuthInfo() +void NukiWrapper::updateAuthData() { - return; - Nuki::CmdResult result = _nukiBle.retrieveLogEntries(0, 0, 0, true); if(result != Nuki::CmdResult::Success) { + _network->publishAuthorizationInfo(0, ""); return; } vTaskDelay( 100 / portTICK_PERIOD_MS); @@ -188,6 +189,7 @@ void NukiWrapper::updateAuthInfo() result = _nukiBle.retrieveLogEntries(_nukiBle.getLogEntryCount() - 2, 1, 0, false); if(result != Nuki::CmdResult::Success) { + _network->publishAuthorizationInfo(0, ""); return; } vTaskDelay( 200 / portTICK_PERIOD_MS); @@ -206,6 +208,10 @@ void NukiWrapper::updateAuthInfo() _lastAuthId = entry.authId; } } + else + { + _network->publishAuthorizationInfo(0, ""); + } } //struct __attribute__((packed)) LogEntry { diff --git a/NukiWrapper.h b/NukiWrapper.h index 93846b7..054c8d8 100644 --- a/NukiWrapper.h +++ b/NukiWrapper.h @@ -32,7 +32,7 @@ private: void updateKeyTurnerState(); void updateBatteryState(); void updateConfig(); - void updateAuthInfo(); + void updateAuthData(); void readConfig(); void readAdvancedConfig(); @@ -47,6 +47,8 @@ private: int _intervalLockstate = 0; // seconds int _intervalBattery = 0; // seconds int _intervalConfig = 60 * 60; // seconds + bool _publishAuthData = false; + bool _clearAuthData = false; Nuki::KeyTurnerState _lastKeyTurnerState; Nuki::KeyTurnerState _keyTurnerState; @@ -66,7 +68,6 @@ private: unsigned long _nextLockStateUpdateTs = 0; unsigned long _nextBatteryReportTs = 0; unsigned long _nextConfigUpdateTs = 0; - unsigned long _nextLogUpdateTs = 0; unsigned long _nextPairTs = 0; Nuki::LockAction _nextLockAction = (Nuki::LockAction)0xff; }; diff --git a/PreferencesKeys.h b/PreferencesKeys.h index 1fe4595..01ef08e 100644 --- a/PreferencesKeys.h +++ b/PreferencesKeys.h @@ -11,6 +11,7 @@ #define preference_query_interval_battery "batInterval" #define preference_cred_user "crdusr" #define preference_cred_password "crdpass" +#define preference_publish_authdata "pubauth" #define preference_presence_detection_timeout "prdtimeout" #define preference_has_mac_saved "hasmac" #define preference_has_mac_byte_0 "macb0" diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index ed6483e..fdddd62 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -98,6 +98,8 @@ bool WebCfgServer::processArgs(String& message) bool clearMqttCredentials = false; bool clearCredentials = false; + bool publishAuthData = false; + int count = _server.args(); for(int index = 0; index < count; index++) { @@ -159,6 +161,10 @@ bool WebCfgServer::processArgs(String& message) _preferences->putInt(preference_presence_detection_timeout, value.toInt()); configChanged = true; } + else if(key == "PUBAUTH") + { + publishAuthData = true; + } else if(key == "CREDUSER") { if(value == "#") @@ -191,6 +197,12 @@ bool WebCfgServer::processArgs(String& message) } } + if(_preferences->getBool(preference_publish_authdata) != publishAuthData) + { + _preferences->putBool(preference_publish_authdata, publishAuthData); + configChanged = true; + } + if(clearMqttCredentials) { _preferences->putString(preference_mqtt_user, ""); @@ -255,6 +267,7 @@ void WebCfgServer::buildHtml(String& response) printInputField(response, "HOSTNAME", "Host name", _preferences->getString(preference_hostname).c_str(), 100); printInputField(response, "LSTINT", "Query interval lock state (seconds)", _preferences->getInt(preference_query_interval_lockstate), 10); printInputField(response, "BATINT", "Query interval battery (seconds)", _preferences->getInt(preference_query_interval_battery), 10); + printCheckBox(response, "PUBAUTH", "Publish auth data", _preferences->getBool(preference_publish_authdata)); printInputField(response, "PRDTMO", "Presence detection timeout (seconds, -1 to disable)", _preferences->getInt(preference_presence_detection_timeout), 10); response.concat(""); @@ -372,7 +385,7 @@ void WebCfgServer::printInputField(String& response, response.concat(token); response.concat("\" SIZE=\"25\" MAXLENGTH=\""); response.concat(maxLengthStr); - response.concat("\\\">"); + response.concat("\\\"/>"); response.concat(""); response.concat(""); } @@ -388,6 +401,24 @@ void WebCfgServer::printInputField(String& response, printInputField(response, token, description, valueStr, maxLength); } +void WebCfgServer::printCheckBox(String &response, const char *token, const char *description, const bool value) +{ + response.concat(""); + response.concat(""); + response.concat(description); + response.concat(""); + response.concat(""); + response.concat(" "); + response.concat(""); + response.concat(""); +} + void WebCfgServer::printParameter(String& response, const char *description, const char *value) { response.concat(""); diff --git a/WebCfgServer.h b/WebCfgServer.h index dd477a6..95fe4a0 100644 --- a/WebCfgServer.h +++ b/WebCfgServer.h @@ -37,6 +37,7 @@ private: 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); void printInputField(String& response, const char* token, const char* description, const int value, size_t maxLength); + void printCheckBox(String& response, const char* token, const char* description, const bool value); void printParameter(String& response, const char* description, const char* value);