Skip to content

Commit

Permalink
Fixes (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
iranl authored Aug 21, 2024
1 parent 618591e commit 22348d7
Show file tree
Hide file tree
Showing 18 changed files with 1,468 additions and 1,430 deletions.
69 changes: 39 additions & 30 deletions src/NukiNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ NukiNetwork::NukiNetwork(Preferences *preferences)
}

_inst = this;
_hostname = _preferences->getString(preference_hostname);
_webEnabled = _preferences->getBool(preference_webserver_enabled, true);
_updateFromMQTT = _preferences->getBool(preference_update_from_mqtt, false);

#ifndef NUKI_HUB_UPDATER
memset(_maintenancePathPrefix, 0, sizeof(_maintenancePathPrefix));
Expand Down Expand Up @@ -197,24 +197,22 @@ bool NukiNetwork::update()
#else
void NukiNetwork::initialize()
{
_restartOnDisconnect = _preferences->getBool(preference_restart_on_disconnect, false);
_checkUpdates = _preferences->getBool(preference_check_updates, false);
_reconnectNetworkOnMqttDisconnect = _preferences->getBool(preference_recon_netw_on_mqtt_discon, false);
_rssiPublishInterval = _preferences->getInt(preference_rssi_publish_interval, 0) * 1000;
_hostname = _preferences->getString(preference_hostname, "");
_discoveryTopic = _preferences->getString(preference_mqtt_hass_discovery, "");
_mqttPort = _preferences->getInt(preference_mqtt_broker_port, 1883);

if(_hostname == "")
{
_hostname = "nukihub";
_preferences->putString(preference_hostname, _hostname);
}
if(_rssiPublishInterval == 0)

_mqttPort = _preferences->getInt(preference_mqtt_broker_port, 0);

if(_mqttPort == 0)
{
_rssiPublishInterval = 60000;
_preferences->putInt(preference_rssi_publish_interval, 60);
_mqttPort = 1883;
_preferences->putInt(preference_mqtt_broker_port, _mqttPort);
}

strcpy(_hostnameArr, _hostname.c_str());
_device->initialize();

Expand All @@ -224,13 +222,6 @@ void NukiNetwork::initialize()
String brokerAddr = _preferences->getString(preference_mqtt_broker);
strcpy(_mqttBrokerAddr, brokerAddr.c_str());

int port = _preferences->getInt(preference_mqtt_broker_port, 0);
if(port == 0)
{
port = 1883;
_preferences->putInt(preference_mqtt_broker_port, port);
}

String mqttUser = _preferences->getString(preference_mqtt_user);
if(mqttUser.length() > 0)
{
Expand All @@ -254,21 +245,12 @@ void NukiNetwork::initialize()
Log->print(F("MQTT Broker: "));
Log->print(_mqttBrokerAddr);
Log->print(F(":"));
Log->println(port);
Log->println(_mqttPort);

_device->mqttSetClientId(_hostnameArr);
_device->mqttSetCleanSession(MQTT_CLEAN_SESSIONS);
_device->mqttSetKeepAlive(MQTT_KEEP_ALIVE);

_networkTimeout = _preferences->getInt(preference_network_timeout, 0);
if(_networkTimeout == 0)
{
_networkTimeout = -1;
_preferences->putInt(preference_network_timeout, _networkTimeout);
}

_publishDebugInfo = _preferences->getBool(preference_publish_debug_info, false);

char gpioPath[250];
bool rebGpio = rebuildGpio();

Expand Down Expand Up @@ -309,6 +291,33 @@ void NukiNetwork::initialize()
{
gpioActionCallback(action, pin);
});

_discoveryTopic = _preferences->getString(preference_mqtt_hass_discovery, "");
_offEnabled = _preferences->getBool(preference_official_hybrid, false);
readSettings();
}

void NukiNetwork::readSettings()
{
_restartOnDisconnect = _preferences->getBool(preference_restart_on_disconnect, false);
_checkUpdates = _preferences->getBool(preference_check_updates, false);
_reconnectNetworkOnMqttDisconnect = _preferences->getBool(preference_recon_netw_on_mqtt_discon, false);
_rssiPublishInterval = _preferences->getInt(preference_rssi_publish_interval, 0) * 1000;

if(_rssiPublishInterval == 0)
{
_rssiPublishInterval = 60000;
_preferences->putInt(preference_rssi_publish_interval, 60);
}

_networkTimeout = _preferences->getInt(preference_network_timeout, 0);
if(_networkTimeout == 0)
{
_networkTimeout = -1;
_preferences->putInt(preference_network_timeout, _networkTimeout);
}

_publishDebugInfo = _preferences->getBool(preference_publish_debug_info, false);
}

bool NukiNetwork::update()
Expand Down Expand Up @@ -1055,7 +1064,7 @@ void NukiNetwork::publishHASSConfig(char* deviceType, const char* baseTopic, cha
removeHassTopic((char*)"sensor", (char*)"mqtt_log", uidString);
}

if(_preferences->getBool(preference_official_hybrid, false))
if(_offEnabled)
{
// Hybrid connected
publishHassTopic("binary_sensor",
Expand Down Expand Up @@ -1180,7 +1189,7 @@ void NukiNetwork::publishHASSConfig(char* deviceType, const char* baseTopic, cha
"",
{ { (char*)"en", (char*)"true" }});

if(_preferences->getBool(preference_check_updates))
if(_checkUpdates)
{
// NUKI Hub latest
publishHassTopic("sensor",
Expand All @@ -1204,7 +1213,7 @@ void NukiNetwork::publishHASSConfig(char* deviceType, const char* baseTopic, cha
_lockPath.toCharArray(latest_version_topic,_lockPath.length() + 1);
strcat(latest_version_topic, mqtt_topic_info_nuki_hub_latest);

if(!_preferences->getBool(preference_update_from_mqtt, false))
if(!_updateFromMQTT)
{
publishHassTopic("update",
"nuki_hub_update",
Expand Down
3 changes: 3 additions & 0 deletions src/NukiNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class NukiNetwork
{
public:
void initialize();
void readSettings();
bool update();
void reconfigureDevice();
void clearWifiFallback();
Expand Down Expand Up @@ -112,6 +113,8 @@ class NukiNetwork
NetworkDeviceType _networkDeviceType = (NetworkDeviceType)-1;
bool _firstBootAfterDeviceChange = false;
bool _webEnabled = true;
bool _updateFromMQTT = false;
bool _offEnabled = false;

#ifndef NUKI_HUB_UPDATER
static void onMqttDataReceivedCallback(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total);
Expand Down
21 changes: 17 additions & 4 deletions src/NukiNetworkLock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ void NukiNetworkLock::onMqttDataReceived(const char* topic, byte* payload, const
else if(comparePrefixedPath(topic, mqtt_topic_update) && strcmp(value, "1") == 0 && _preferences->getBool(preference_update_from_mqtt, false))
{
Log->println(F("Update requested via MQTT."));

bool otaManifestSuccess = false;
JsonDocument doc;

Expand Down Expand Up @@ -235,7 +235,7 @@ void NukiNetworkLock::onMqttDataReceived(const char* topic, byte* payload, const
{
String currentVersion = NUKI_HUB_VERSION;

if(atof(doc["release"]["version"]) >= atof(currentVersion.c_str()))
if(atof(doc["release"]["version"]) >= atof(currentVersion.c_str()))
{
if(strcmp(NUKI_HUB_VERSION, doc["release"]["fullversion"].as<const char*>()) == 0 && strcmp(NUKI_HUB_BUILD, doc["release"]["build"].as<const char*>()) == 0 && strcmp(NUKI_HUB_DATE, doc["release"]["time"].as<const char*>()) == 0)
{
Expand Down Expand Up @@ -294,7 +294,7 @@ void NukiNetworkLock::onMqttDataReceived(const char* topic, byte* payload, const
delay(200);
restartEsp(RestartReason::OTAReboot);
}
}
}
}
else
{
Expand Down Expand Up @@ -702,6 +702,12 @@ void NukiNetworkLock::publishAuthorizationInfo(const std::list<NukiLock::LogEntr
_authId = log.authId;
memset(_authName, 0, sizeof(_authName));
memcpy(_authName, authName, sizeof(authName));

if(authName[sizeName - 1] != '\0' && _authEntries.count(_authId) > 0)
{
memset(_authName, 0, sizeof(_authName));
memcpy(_authName, _authEntries[_authId].c_str(), sizeof(_authEntries[_authId].c_str()));
}
}
}

Expand All @@ -710,6 +716,12 @@ void NukiNetworkLock::publishAuthorizationInfo(const std::list<NukiLock::LogEntr
entry["index"] = log.index;
entry["authorizationId"] = log.authId;
entry["authorizationName"] = authName;

if(entry["authorizationName"].as<String>().length() == 0 && _authEntries.count(log.authId) > 0)
{
entry["authorizationName"] = _authEntries[log.authId];
}

entry["timeYear"] = log.timeStampYear;
entry["timeMonth"] = log.timeStampMonth;
entry["timeDay"] = log.timeStampDay;
Expand Down Expand Up @@ -1368,6 +1380,7 @@ void NukiNetworkLock::publishAuth(const std::list<NukiLock::AuthorizationEntry>&
jsonEntry["idType"] = entry.idType; //CONSIDER INT TO STRING
jsonEntry["enabled"] = entry.enabled;
jsonEntry["name"] = entry.name;
_authEntries[jsonEntry["authId"]] = jsonEntry["name"].as<String>();
jsonEntry["remoteAllowed"] = entry.remoteAllowed;
char createdDT[20];
sprintf(createdDT, "%04d-%02d-%02d %02d:%02d:%02d", entry.createdYear, entry.createdMonth, entry.createdDay, entry.createdHour, entry.createdMinute, entry.createdSecond);
Expand Down Expand Up @@ -1612,7 +1625,7 @@ void NukiNetworkLock::publishHASSConfig(char *deviceType, const char *baseTopic,
{
_network->removeHASSConfigTopic((char*)"binary_sensor", (char*)"door_sensor", uidString);
}

#ifndef CONFIG_IDF_TARGET_ESP32H2
_network->publishHASSWifiRssiConfig(deviceType, baseTopic, name, uidString);
#endif
Expand Down
1 change: 1 addition & 0 deletions src/NukiNetworkLock.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class NukiNetworkLock : public MqttReceiver
NukiNetwork* _network;
Preferences* _preferences;

std::map<uint32_t, String> _authEntries;
std::vector<char*> _offTopics;
char _mqttPath[181] = {0};
char _offMqttPath[181] = {0};
Expand Down
13 changes: 13 additions & 0 deletions src/NukiNetworkOpener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,12 @@ void NukiNetworkOpener::publishAuthorizationInfo(const std::list<NukiOpener::Log
_authId = log.authId;
memset(_authName, 0, sizeof(_authName));
memcpy(_authName, authName, sizeof(authName));

if(authName[sizeName - 1] != '\0' && _authEntries.count(_authId) > 0)
{
memset(_authName, 0, sizeof(_authName));
memcpy(_authName, _authEntries[_authId].c_str(), sizeof(_authEntries[_authId].c_str()));
}
}
}

Expand All @@ -489,6 +495,12 @@ void NukiNetworkOpener::publishAuthorizationInfo(const std::list<NukiOpener::Log
entry["index"] = log.index;
entry["authorizationId"] = log.authId;
entry["authorizationName"] = _authName;

if(entry["authorizationName"].as<String>().length() == 0 && _authEntries.count(log.authId) > 0)
{
entry["authorizationName"] = _authEntries[log.authId];
}

entry["timeYear"] = log.timeStampYear;
entry["timeMonth"] = log.timeStampMonth;
entry["timeDay"] = log.timeStampDay;
Expand Down Expand Up @@ -1176,6 +1188,7 @@ void NukiNetworkOpener::publishAuth(const std::list<NukiOpener::AuthorizationEnt
jsonEntry["idType"] = entry.idType; //CONSIDER INT TO STRING
jsonEntry["enabled"] = entry.enabled;
jsonEntry["name"] = entry.name;
_authEntries[jsonEntry["authId"]] = jsonEntry["name"].as<String>();
jsonEntry["remoteAllowed"] = entry.remoteAllowed;
char createdDT[20];
sprintf(createdDT, "%04d-%02d-%02d %02d:%02d:%02d", entry.createdYear, entry.createdMonth, entry.createdDay, entry.createdHour, entry.createdMinute, entry.createdSecond);
Expand Down
1 change: 1 addition & 0 deletions src/NukiNetworkOpener.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class NukiNetworkOpener : public MqttReceiver

NukiNetwork* _network = nullptr;

std::map<uint32_t, String> _authEntries;
char _mqttPath[181] = {0};
bool _isConnected = false;
bool _firstTunerStatePublish = true;
Expand Down
25 changes: 15 additions & 10 deletions src/NukiOpenerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ NukiOpenerWrapper::NukiOpenerWrapper(const std::string& deviceName, NukiDeviceId

network->setLockActionReceivedCallback(nukiOpenerInst->onLockActionReceivedCallback);
network->setConfigUpdateReceivedCallback(nukiOpenerInst->onConfigUpdateReceivedCallback);
if(_preferences->getBool(preference_disable_non_json, false)) network->setKeypadCommandReceivedCallback(nukiOpenerInst->onKeypadCommandReceivedCallback);
network->setKeypadCommandReceivedCallback(nukiOpenerInst->onKeypadCommandReceivedCallback);
network->setKeypadJsonCommandReceivedCallback(nukiOpenerInst->onKeypadJsonCommandReceivedCallback);
network->setTimeControlCommandReceivedCallback(nukiOpenerInst->onTimeControlCommandReceivedCallback);
network->setAuthCommandReceivedCallback(nukiOpenerInst->onAuthCommandReceivedCallback);
Expand All @@ -49,7 +49,17 @@ NukiOpenerWrapper::~NukiOpenerWrapper()
void NukiOpenerWrapper::initialize()
{
_nukiOpener.initialize();
_nukiOpener.registerBleScanner(_bleScanner);
_nukiOpener.setEventHandler(this);
_nukiOpener.setConnectTimeout(3);
_nukiOpener.setDisconnectTimeout(5000);

_hassEnabled = _preferences->getString(preference_mqtt_hass_discovery) != "";
readSettings();
}

void NukiOpenerWrapper::readSettings()
{
esp_power_level_t powerLevel;

int pwrLvl = _preferences->getInt(preference_ble_tx_power, 9);
Expand All @@ -64,7 +74,6 @@ void NukiOpenerWrapper::initialize()
else if(pwrLvl >= -12) powerLevel = ESP_PWR_LVL_N12;

_nukiOpener.setPower(powerLevel);
_nukiOpener.registerBleScanner(_bleScanner);

_intervalLockstate = _preferences->getInt(preference_query_interval_lockstate);
_intervalConfig = _preferences->getInt(preference_query_interval_configuration);
Expand All @@ -76,10 +85,10 @@ void NukiOpenerWrapper::initialize()
_maxTimeControlEntryCount = _preferences->getUInt(preference_opener_max_timecontrol_entry_count);
_maxAuthEntryCount = _preferences->getUInt(preference_opener_max_auth_entry_count);
_restartBeaconTimeout = _preferences->getInt(preference_restart_ble_beacon_lost);
_hassEnabled = _preferences->getString(preference_mqtt_hass_discovery) != "";
_nrOfRetries = _preferences->getInt(preference_command_nr_of_retries, 200);
_retryDelay = _preferences->getInt(preference_command_retry_delay);
_rssiPublishInterval = _preferences->getInt(preference_rssi_publish_interval) * 1000;
_disableNonJSON = _preferences->getBool(preference_disable_non_json, false);
_preferences->getBytes(preference_conf_opener_basic_acl, &_basicOpenerConfigAclPrefs, sizeof(_basicOpenerConfigAclPrefs));
_preferences->getBytes(preference_conf_opener_advanced_acl, &_advancedOpenerConfigAclPrefs, sizeof(_advancedOpenerConfigAclPrefs));

Expand Down Expand Up @@ -126,10 +135,6 @@ void NukiOpenerWrapper::initialize()
_preferences->putInt(preference_restart_ble_beacon_lost, _restartBeaconTimeout);
}

_nukiOpener.setEventHandler(this);
_nukiOpener.setConnectTimeout(3);
_nukiOpener.setDisconnectTimeout(5000);

Log->print(F("Opener state interval: "));
Log->print(_intervalLockstate);
Log->print(F(" | Battery interval: "));
Expand Down Expand Up @@ -1584,7 +1589,7 @@ void NukiOpenerWrapper::gpioActionCallback(const GpioAction &action, const int&

void NukiOpenerWrapper::onKeypadCommandReceived(const char *command, const uint &id, const String &name, const String &code, const int& enabled)
{
if(_preferences->getBool(preference_disable_non_json, false)) return;
if(_disableNonJSON) return;

if(!_preferences->getBool(preference_keypad_control_enabled, false))
{
Expand Down Expand Up @@ -2625,13 +2630,13 @@ void NukiOpenerWrapper::onAuthCommandReceived(const char *value)
memcpy(&entry.name, name.c_str(), nameLen > 32 ? 32 : nameLen);
/*
memcpy(&entry.sharedKey, secretKeyK, 32);
if(idType != 1)
{
_network->publishAuthCommandResult("invalidIdType");
return;
}
entry.idType = idType;
*/
entry.remoteAllowed = remoteAllowed == 1 ? 1 : 0;
Expand Down
2 changes: 2 additions & 0 deletions src/NukiOpenerWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class NukiOpenerWrapper : public NukiOpener::SmartlockEventHandler
virtual ~NukiOpenerWrapper();

void initialize();
void readSettings();
void update();

void electricStrikeActuation();
Expand Down Expand Up @@ -104,6 +105,7 @@ class NukiOpenerWrapper : public NukiOpener::SmartlockEventHandler
int _restartBeaconTimeout = 0; // seconds
bool _publishAuthData = false;
bool _clearAuthData = false;
bool _disableNonJSON = false;
int _nrOfRetries = 0;
int _retryDelay = 0;
int _retryCount = 0;
Expand Down
Loading

0 comments on commit 22348d7

Please sign in to comment.