diff --git a/PreferencesKeys.h b/PreferencesKeys.h index ed0ed72..d7eaf5a 100644 --- a/PreferencesKeys.h +++ b/PreferencesKeys.h @@ -10,6 +10,7 @@ #define preference_query_interval_battery "batInterval" #define preference_cred_user "crdusr" #define preference_cred_password "crdpass" +#define preference_presence_detection_timeout "prdtimeout" diff --git a/PresenceDetection.cpp b/PresenceDetection.cpp index 7a0a4c2..fe41bfd 100644 --- a/PresenceDetection.cpp +++ b/PresenceDetection.cpp @@ -1,10 +1,22 @@ #include "PresenceDetection.h" +#include "PreferencesKeys.h" -PresenceDetection::PresenceDetection(BleScanner *bleScanner, Network* network) -: _bleScanner(bleScanner), +PresenceDetection::PresenceDetection(Preferences* preferences, BleScanner *bleScanner, Network* network) +: _preferences(preferences), + _bleScanner(bleScanner), _network(network) { _csv = new char[presence_detection_buffer_size]; + + _timeout = _preferences->getInt(preference_presence_detection_timeout) * 1000; + if(_timeout == 0) + { + _timeout = 60000; + _preferences->putInt(preference_presence_detection_timeout, 60); + } + + Serial.print(F("Presence detection timeout (ms): ")); + Serial.println(_timeout); } PresenceDetection::~PresenceDetection() @@ -25,8 +37,9 @@ void PresenceDetection::initialize() void PresenceDetection::update() { - vTaskDelay( 5000 / portTICK_PERIOD_MS); + vTaskDelay( 10000 / portTICK_PERIOD_MS); + if(_timeout < 0) return; if(_devices.size() == 0) return; memset(_csv, 0, presence_detection_buffer_size); @@ -34,7 +47,7 @@ void PresenceDetection::update() long ts = millis(); for(auto it : _devices) { - if(ts - 20000 < it.second.timestamp) + if(ts - _timeout < it.second.timestamp) { buildCsv(it.second); } diff --git a/PresenceDetection.h b/PresenceDetection.h index a7a1b6c..b0419be 100644 --- a/PresenceDetection.h +++ b/PresenceDetection.h @@ -16,7 +16,7 @@ struct PdDevice class PresenceDetection : public BLEScannerSubscriber { public: - PresenceDetection(BleScanner* bleScanner, Network* network); + PresenceDetection(Preferences* preferences, BleScanner* bleScanner, Network* network); virtual ~PresenceDetection(); void initialize(); @@ -27,12 +27,11 @@ public: private: void buildCsv(const PdDevice& device); + Preferences* _preferences; BleScanner* _bleScanner; Network* _network; char* _csv = {0}; std::map _devices; - uint _timeout = 20000; + int _timeout = 20000; int _csvIndex = 0; - - }; diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index 76420df..177ec4a 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -129,6 +129,11 @@ bool WebCfgServer::processArgs() _preferences->putInt(preference_query_interval_battery, value.toInt()); configChanged = true; } + else if(key == "PRDTMO") + { + _preferences->putInt(preference_presence_detection_timeout, value.toInt()); + configChanged = true; + } else if(key == "CREDUSER") { if(value == "#") @@ -205,6 +210,7 @@ void WebCfgServer::buildHtml(String& response) printInputField(response, "MQTTPATH", "MQTT Path", _preferences->getString(preference_mqtt_path).c_str(), 180); 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); + printInputField(response, "PRDTMO", "Presence detection timeout (seconds, -1 to disable)", _preferences->getInt(preference_presence_detection_timeout), 10); response.concat(""); response.concat("
"); @@ -218,9 +224,6 @@ void WebCfgServer::buildHtml(String& response) response.concat(""); response.concat(""); - - - // response.concat("\n"); response.concat("\n"); } diff --git a/main.cpp b/main.cpp index de79591..f839118 100644 --- a/main.cpp +++ b/main.cpp @@ -79,7 +79,7 @@ void setup() webCfgServer->initialize(); nuki->initialize(); - presenceDetection = new PresenceDetection(nuki->bleScanner(), network); + presenceDetection = new PresenceDetection(preferences, nuki->bleScanner(), network); presenceDetection->initialize(); setupTasks();