detect key turner state updates via scanning advertisements

This commit is contained in:
technyon
2022-03-31 17:41:25 +02:00
parent 9279257ce9
commit f3de3c75df
4 changed files with 22 additions and 6 deletions

View File

@@ -29,6 +29,7 @@ file(GLOB SRCFILES
lib/nuki_ble/src/NukiBle.cpp
lib/nuki_ble/src/NukiConstants.h
lib/nuki_ble/src/NukiUtills.h
lib/nuki_ble/src/BleScanner.cpp
include_directories(Lib/PubSubClient)
lib/pubsubclient/src/PubSubClient.cpp
)

View File

@@ -37,6 +37,8 @@ void Nuki::initialize()
_preferences->putInt(preference_query_interval_battery, _intervalBattery);
}
_nukiBle.setEventHandler(this);
Serial.print(F("Lock state interval: "));
Serial.print(_intervalLockstate);
Serial.print(F("| Battery interval: "));
@@ -63,8 +65,9 @@ void Nuki::update()
unsigned long ts = millis();
if(_nextLockStateUpdateTs == 0 || ts >= _nextLockStateUpdateTs)
if(_statusUpdated || _nextLockStateUpdateTs == 0 || ts >= _nextLockStateUpdateTs)
{
_statusUpdated = false;
_nextLockStateUpdateTs = ts + _intervalLockstate * 1000;
updateKeyTurnerState();
}
@@ -78,6 +81,7 @@ void Nuki::update()
_nukiBle.lockAction(_nextLockAction, 0, 0);
_nextLockAction = (LockAction)0xff;
}
_nukiBle.update();
}
void Nuki::updateKeyTurnerState()
@@ -94,9 +98,9 @@ void Nuki::updateKeyTurnerState()
if(_keyTurnerState.lockState != _lastKeyTurnerState.lockState)
{
_network->publishKeyTurnerState(lockStateStr, triggerStr, completionStatusStr);
Serial.print(F("Nuki lock state: "));
Serial.println(lockStateStr);
}
Serial.print(F("Nuki lock state: "));
Serial.println(lockStateStr);
memcpy(&_lastKeyTurnerState, &_keyTurnerState, sizeof(KeyTurnerState));
}
@@ -244,7 +248,6 @@ LockAction Nuki::lockActionToEnum(const char *str)
return (LockAction)0xff;
}
void Nuki::onLockActionReceived(const char *value)
{
nukiInst->_nextLockAction = nukiInst->lockActionToEnum(value);
@@ -256,3 +259,12 @@ const bool Nuki::isPaired()
{
return _paired;
}
void Nuki::notify(NukiEventType eventType)
{
if(eventType == NukiEventType::KeyTurnerStatusUpdated)
{
// Serial.println("SUP2");
_statusUpdated = true;
}
}

5
Nuki.h
View File

@@ -4,7 +4,7 @@
#include "NukiConstants.h"
#include "Network.h"
class Nuki
class Nuki : public NukiSmartlockEventHandler
{
public:
Nuki(const std::string& name, uint32_t id, Network* network, Preferences* preferences);
@@ -14,6 +14,8 @@ public:
const bool isPaired();
void notify(NukiEventType eventType) override;
private:
static void onLockActionReceived(const char* value);
@@ -39,6 +41,7 @@ private:
BatteryReport _lastBatteryReport;
bool _paired = false;
bool _statusUpdated = false;
unsigned long _nextLockStateUpdateTs = 0;
unsigned long _nextBatteryReportTs = 0;
LockAction _nextLockAction = (LockAction)0xff;