From a202deef23781cf67859806ad820b8fb11eaf305 Mon Sep 17 00:00:00 2001 From: technyon Date: Fri, 25 Mar 2022 21:39:47 +0100 Subject: [PATCH] implement send lock action via mqtt --- MqttTopics.h | 3 ++- Network.cpp | 17 +++++++++++++++-- Network.h | 4 +++- Nuki.cpp | 37 +++++++++++++++++++++++++++++++++++-- Nuki.h | 4 +++- 5 files changed, 58 insertions(+), 7 deletions(-) diff --git a/MqttTopics.h b/MqttTopics.h index 050a581..8a60a4a 100644 --- a/MqttTopics.h +++ b/MqttTopics.h @@ -1,3 +1,4 @@ #pragma once -#define mqtt_topc_lockstate "nuki/lockstate" \ No newline at end of file +#define mqtt_topc_lockstate "nuki/lockState" +#define mqtt_topc_lockstate_setpoint "nuki/lockActions" \ No newline at end of file diff --git a/Network.cpp b/Network.cpp index fcb5227..645caa2 100644 --- a/Network.cpp +++ b/Network.cpp @@ -54,7 +54,7 @@ bool Network::reconnect() Serial.println("connected"); // ... and resubscribe - _mqttClient.subscribe("nuki/cmd"); + _mqttClient.subscribe(mqtt_topc_lockstate_setpoint); } else { Serial.print("failed, rc="); Serial.print(_mqttClient.state()); @@ -118,9 +118,17 @@ void Network::onMqttDataReceived(char *&topic, byte *&payload, unsigned int &len value[l] = 0; - if(strcmp(topic, "nuki/cmd") == 0) + if(strcmp(topic, mqtt_topc_lockstate_setpoint) == 0) { + if(strcmp(value, "") == 0) return; + + Serial.print("lockstate setpoint received: "); Serial.println(value); + if(_lockActionReceivedCallback != NULL) + { + _lockActionReceivedCallback(value); + } + _mqttClient.publish(mqtt_topc_lockstate_setpoint, ""); } } @@ -128,3 +136,8 @@ void Network::publishKeyTurnerState(const char* state) { _mqttClient.publish(mqtt_topc_lockstate, state); } + +void Network::setLockActionReceived(void (*lockActionReceivedCallback)(const char *)) +{ + _lockActionReceivedCallback = lockActionReceivedCallback; +} diff --git a/Network.h b/Network.h index 9f25377..bc70b83 100644 --- a/Network.h +++ b/Network.h @@ -14,6 +14,8 @@ public: void publishKeyTurnerState(const char* state); + void setLockActionReceived(void (*lockActionReceivedCallback)(const char* value)); + private: static void onMqttDataReceivedCallback(char* topic, byte* payload, unsigned int length); void onMqttDataReceived(char*& topic, byte*& payload, unsigned int& length); @@ -23,5 +25,5 @@ private: PubSubClient _mqttClient; WiFiClient _wifiClient; - unsigned long _publishTs = 0; + void (*_lockActionReceivedCallback)(const char* value) = NULL; }; diff --git a/Nuki.cpp b/Nuki.cpp index d2682e3..9039229 100644 --- a/Nuki.cpp +++ b/Nuki.cpp @@ -1,12 +1,18 @@ #include "Nuki.h" #include +Nuki* nukiInst; + Nuki::Nuki(const std::string& name, uint32_t id, Network* network) : _nukiBle(name, id), _network(network) { + nukiInst = this; + memset(&_lastKeyTurnerState, sizeof(KeyTurnerState), 0); memset(&_keyTurnerState, sizeof(KeyTurnerState), 0); + + network->setLockActionReceived(nukiInst->onLockActionReceived); } void Nuki::initialize() @@ -34,7 +40,7 @@ void Nuki::update() _nukiBle.requestKeyTurnerState(&_keyTurnerState); char str[20]; - stateToString(_keyTurnerState.lockState, str); + lockstateToString(_keyTurnerState.lockState, str); Serial.print(F("Nuki lock state: ")); Serial.println(str); @@ -48,7 +54,7 @@ void Nuki::update() vTaskDelay( 20000 / portTICK_PERIOD_MS); } -void Nuki::stateToString(LockState state, char* str) +void Nuki::lockstateToString(const LockState state, char* str) { switch(state) { @@ -87,3 +93,30 @@ void Nuki::stateToString(LockState state, char* str) break; } } + +LockAction Nuki::lockActionToEnum(const char *str) +{ + if(strcmp(str, "unlock") == 0) return LockAction::unlock; + else if(strcmp(str, "lock") == 0) return LockAction::lock; + else if(strcmp(str, "unlatch") == 0) return LockAction::unlatch; + else if(strcmp(str, "lockNgo") == 0) return LockAction::lockNgo; + else if(strcmp(str, "lockNgoUnlatch") == 0) return LockAction::lockNgoUnlatch; + else if(strcmp(str, "fullLock") == 0) return LockAction::fullLock; + else if(strcmp(str, "fobAction2") == 0) return LockAction::fobAction2; + else if(strcmp(str, "fobAction1") == 0) return LockAction::fobAction1; + else if(strcmp(str, "fobAction3") == 0) return LockAction::fobAction3; + return (LockAction)0xff; +} + + +void Nuki::onLockActionReceived(const char *value) +{ + LockAction action = nukiInst->lockActionToEnum(value); + Serial.print("Action: "); + Serial.println((int)action); + if(action != (LockAction)0xff) + { +// nukiInst->_nukiBle.lockAction(action, 0, 0); + vTaskDelay( 5000 / portTICK_PERIOD_MS); + } +} diff --git a/Nuki.h b/Nuki.h index 4074782..9e13b9d 100644 --- a/Nuki.h +++ b/Nuki.h @@ -13,7 +13,9 @@ public: void update(); private: - void stateToString(LockState state, char* str); // char array at least 14 characters + static void onLockActionReceived(const char* value); + void lockstateToString(const LockState state, char* str); // char array at least 14 characters + LockAction lockActionToEnum(const char* str); // char array at least 14 characters NukiBle _nukiBle; Network* _network;