Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tasks #371

Merged
merged 6 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ In a browser navigate to the IP address assigned to the ESP32.
- lock/trigger: The trigger of the last action: autoLock, automatic, button, manual, system.
- lock/lastLockAction: Reports the last lock action as a string. Possible values are: Unlock, Lock, Unlatch, LockNgo, LockNgoUnlatch, FullLock, FobAction1, FobAction2, FobAction3, Unknown.
- lock/log: If "Publish auth data" is enabled in the web interface, this topic will be filled with the log of authorization data.
- lock/shortLog: If "Publish auth data" is enabled in the web interface, this topic will be filled with the 3 most recent entries in the log of authorization data, updates faster than lock/log.
- lock/completionStatus: Status of the last action as reported by Nuki Lock: success, motorBlocked, canceled, tooRecent, busy, lowMotorVoltage, clutchFailure, motorPowerFailure, incompleteFailure, invalidCode, otherError, unknown.
- lock/authorizationId: If enabled in the web interface, this node returns the authorization id of the last lock action.
- lock/authorizationName: If enabled in the web interface, this node returns the authorization name of the last lock action.
Expand Down
4 changes: 2 additions & 2 deletions src/CharBuffer.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "CharBuffer.h"

void CharBuffer::initialize()
void CharBuffer::initialize(char16_t buffer_size)
{
_buffer = new char[CHAR_BUFFER_SIZE];
_buffer = new char[buffer_size];
}

char *CharBuffer::get()
Expand Down
4 changes: 1 addition & 3 deletions src/CharBuffer.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#pragma once

#define CHAR_BUFFER_SIZE 4096

class CharBuffer
{
public:
static void initialize();
static void initialize(char16_t buffer_size);
static char* get();

private:
Expand Down
9 changes: 8 additions & 1 deletion src/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "sdkconfig.h"

#define NUKI_HUB_VERSION "8.34"
#define NUKI_HUB_BUILD "unknownbuildnr"

#define GITHUB_LATEST_RELEASE_URL "https://github.com/technyon/nuki_hub/releases/latest"
#define GITHUB_LATEST_RELEASE_API_URL "https://api.github.com/repos/technyon/nuki_hub/releases/latest"
Expand All @@ -24,4 +25,10 @@

#define GPIO_DEBOUNCE_TIME 200

#define NUKI_HUB_BUILD "unknownbuildnr"
#define CHAR_BUFFER_SIZE 4096
#define NETWORK_TASK_SIZE 12288
#define NUKI_TASK_SIZE 8192
#define PD_TASK_SIZE 1024
#define MAX_AUTHLOG 5
#define MAX_KEYPAD 10
#define MAX_TIMECONTROL 10
3 changes: 3 additions & 0 deletions src/MqttTopics.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#define mqtt_topic_lock_trigger "/lock/trigger"
#define mqtt_topic_lock_last_lock_action "/lock/lastLockAction"
#define mqtt_topic_lock_log "/lock/log"
#define mqtt_topic_lock_log_latest "/lock/shortLog"
#define mqtt_topic_lock_auth_id "/lock/authorizationId"
#define mqtt_topic_lock_auth_name "/lock/authorizationName"
#define mqtt_topic_lock_completionStatus "/lock/completionStatus"
Expand Down Expand Up @@ -71,6 +72,8 @@
#define mqtt_topic_info_nuki_hub_ip "/info/nukiHubIp"

#define mqtt_topic_reset "/maintenance/reset"
#define mqtt_topic_webserver_state "/maintenance/webserver/state"
#define mqtt_topic_webserver_action "/maintenance/webserver/enable"
#define mqtt_topic_uptime "/maintenance/uptime"
#define mqtt_topic_wifi_rssi "/maintenance/wifiRssi"
#define mqtt_topic_log "/maintenance/log"
Expand Down
8 changes: 7 additions & 1 deletion src/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,13 @@ void Network::initialize()
bool Network::update()
{
unsigned long ts = millis();


if(ts > 120000 && ts < 125000 && _preferences->getInt(preference_bootloop_counter, 0) > 0)
{
_preferences->putInt(preference_bootloop_counter, 0);
Log->println(F("Bootloop counter reset"));
}

_device->update();

if(!_mqttEnabled)
Expand Down
44 changes: 35 additions & 9 deletions src/NetworkLock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ void NetworkLock::initialize()
_network->subscribe(_mqttPath, mqtt_topic_reset);
_network->initTopic(_mqttPath, mqtt_topic_reset, "0");

_network->subscribe(_mqttPath, mqtt_topic_webserver_action);
_network->initTopic(_mqttPath, mqtt_topic_webserver_action, "--");
_network->initTopic(_mqttPath, mqtt_topic_webserver_state, (_preferences->getBool(preference_webserver_enabled, true) ? "1" : "0"));

_network->initTopic(_mqttPath, mqtt_topic_query_config, "0");
_network->initTopic(_mqttPath, mqtt_topic_query_lockstate, "0");
_network->initTopic(_mqttPath, mqtt_topic_query_battery, "0");
Expand Down Expand Up @@ -101,6 +105,31 @@ void NetworkLock::onMqttDataReceived(const char* topic, byte* payload, const uns
restartEsp(RestartReason::RequestedViaMqtt);
}

if(comparePrefixedPath(topic, mqtt_topic_webserver_action))
{
if(strcmp(value, "") == 0 ||
strcmp(value, "--") == 0) return;

if(strcmp(value, "1") == 0)
{
if(_preferences->getBool(preference_webserver_enabled, true)) return;
Log->println(F("Webserver enabled, restarting."));
_preferences->putBool(preference_webserver_enabled, true);

}
else if (strcmp(value, "0") == 0)
{
if(!_preferences->getBool(preference_webserver_enabled, true)) return;
Log->println(F("Webserver disabled, restarting."));
_preferences->putBool(preference_webserver_enabled, false);
}

publishString(mqtt_topic_webserver_action, "--");
_network->clearWifiFallback();
delay(200);
restartEsp(RestartReason::RequestedViaMqtt);
}

if(comparePrefixedPath(topic, mqtt_topic_lock_action))
{
if(strcmp(value, "") == 0 ||
Expand Down Expand Up @@ -371,23 +400,16 @@ void NetworkLock::publishState(NukiLock::LockState lockState)
}
}

void NetworkLock::publishAuthorizationInfo(const std::list<NukiLock::LogEntry>& logEntries)
void NetworkLock::publishAuthorizationInfo(const std::list<NukiLock::LogEntry>& logEntries, bool latest)
{
char str[50];
char authName[33];
bool authFound = false;

JsonDocument json;

int i = 5;
for(const auto& log : logEntries)
{
if(i <= 0)
{
break;
}
--i;

memset(authName, 0, sizeof(authName));
authName[0] = '\0';

Expand Down Expand Up @@ -437,6 +459,7 @@ void NetworkLock::publishAuthorizationInfo(const std::list<NukiLock::LogEntry>&
memset(str, 0, sizeof(str));
NukiLock::completionStatusToString((NukiLock::CompletionStatus)log.data[3], str);
entry["completionStatus"] = str;
entry["completionStatusVal"] = log.data[3];
break;
case NukiLock::LoggingType::KeypadAction:
memset(str, 0, sizeof(str));
Expand All @@ -446,6 +469,7 @@ void NetworkLock::publishAuthorizationInfo(const std::list<NukiLock::LogEntry>&
memset(str, 0, sizeof(str));
NukiLock::completionStatusToString((NukiLock::CompletionStatus)log.data[2], str);
entry["completionStatus"] = str;
entry["completionStatusVal"] = log.data[2];
break;
case NukiLock::LoggingType::DoorSensor:
memset(str, 0, sizeof(str));
Expand Down Expand Up @@ -475,7 +499,9 @@ void NetworkLock::publishAuthorizationInfo(const std::list<NukiLock::LogEntry>&
}

serializeJson(json, _buffer, _bufferSize);
publishString(mqtt_topic_lock_log, _buffer);

if(latest) publishString(mqtt_topic_lock_log_latest, _buffer);
else publishString(mqtt_topic_lock_log, _buffer);

if(authFound)
{
Expand Down
2 changes: 1 addition & 1 deletion src/NetworkLock.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class NetworkLock : public MqttReceiver

void publishKeyTurnerState(const NukiLock::KeyTurnerState& keyTurnerState, const NukiLock::KeyTurnerState& lastKeyTurnerState);
void publishState(NukiLock::LockState lockState);
void publishAuthorizationInfo(const std::list<NukiLock::LogEntry>& logEntries);
void publishAuthorizationInfo(const std::list<NukiLock::LogEntry>& logEntries, bool latest);
void clearAuthorizationInfo();
void publishCommandResult(const char* resultStr);
void publishLockstateCommandResult(const char* resultStr);
Expand Down
44 changes: 4 additions & 40 deletions src/NetworkOpener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,23 +369,16 @@ void NetworkOpener::publishState(NukiOpener::OpenerState lockState)
}
}

void NetworkOpener::publishAuthorizationInfo(const std::list<NukiOpener::LogEntry>& logEntries)
void NetworkOpener::publishAuthorizationInfo(const std::list<NukiOpener::LogEntry>& logEntries, bool latest)
{
char str[50];
char authName[33];
bool authFound = false;

JsonDocument json;

int i = 5;
for(const auto& log : logEntries)
{
if(i <= 0)
{
break;
}
--i;

memset(authName, 0, sizeof(authName));
authName[0] = '\0';

Expand Down Expand Up @@ -498,46 +491,17 @@ void NetworkOpener::publishAuthorizationInfo(const std::list<NukiOpener::LogEntr
}

serializeJson(json, _buffer, _bufferSize);
publishString(mqtt_topic_lock_log, _buffer);

if(latest) publishString(mqtt_topic_lock_log_latest, _buffer);
else publishString(mqtt_topic_lock_log, _buffer);

if(authFound)
{
publishUInt(mqtt_topic_lock_auth_id, _authId);
publishString(mqtt_topic_lock_auth_name, _authName);
}
}

void NetworkOpener::logactionCompletionStatusToString(uint8_t value, char* out)
{
switch (value)
{
case 0x00:
strcpy(out, "success");
break;
case 0x02:
strcpy(out, "cancelled");
break;
case 0x03:
strcpy(out, "tooRecent");
break;
case 0x04:
strcpy(out, "busy");
break;
case 0x08:
strcpy(out, "incomplete");
break;
case 0xfe:
strcpy(out, "otherError");
break;
case 0xff:
strcpy(out, "unknown");
break;
default:
strcpy(out, "undefined");
break;
}
}

void NetworkOpener::clearAuthorizationInfo()
{
publishString(mqtt_topic_lock_log, "--");
Expand Down
3 changes: 1 addition & 2 deletions src/NetworkOpener.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class NetworkOpener : public MqttReceiver
void publishKeyTurnerState(const NukiOpener::OpenerState& keyTurnerState, const NukiOpener::OpenerState& lastKeyTurnerState);
void publishRing(const bool locked);
void publishState(NukiOpener::OpenerState lockState);
void publishAuthorizationInfo(const std::list<NukiOpener::LogEntry>& logEntries);
void publishAuthorizationInfo(const std::list<NukiOpener::LogEntry>& logEntries, bool latest);
void clearAuthorizationInfo();
void publishCommandResult(const char* resultStr);
void publishLockstateCommandResult(const char* resultStr);
Expand Down Expand Up @@ -65,7 +65,6 @@ class NetworkOpener : public MqttReceiver

void buildMqttPath(const char* path, char* outPath);
void subscribe(const char* path);
void logactionCompletionStatusToString(uint8_t value, char* out);
void buttonPressActionToString(const NukiOpener::ButtonPressAction btnPressAction, char* str);
void fobActionToString(const int fobact, char* str);
void operatingModeToString(const int opmode, char* str);
Expand Down
Loading