implement send lock action via mqtt

This commit is contained in:
technyon
2022-03-25 21:39:47 +01:00
parent 66e0239589
commit a202deef23
5 changed files with 58 additions and 7 deletions

View File

@@ -1,3 +1,4 @@
#pragma once
#define mqtt_topc_lockstate "nuki/lockstate"
#define mqtt_topc_lockstate "nuki/lockState"
#define mqtt_topc_lockstate_setpoint "nuki/lockActions"

View File

@@ -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;
}

View File

@@ -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;
};

View File

@@ -1,12 +1,18 @@
#include "Nuki.h"
#include <FreeRTOS.h>
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);
}
}

4
Nuki.h
View File

@@ -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;