Merge branch 'technyon:master' into update-tasks

This commit is contained in:
iranl
2024-05-21 20:53:36 +02:00
committed by GitHub
9 changed files with 206 additions and 111 deletions

View File

@@ -405,11 +405,8 @@ void NetworkLock::publishState(NukiLock::LockState lockState)
void NetworkLock::publishAuthorizationInfo(const std::list<NukiLock::LogEntry>& logEntries)
{
char str[50];
_authId = 0;
memset(_authName, 0, sizeof(_authName));
_authName[0] = '\0';
_authFound = false;
char authName[33];
bool authFound = false;
JsonDocument json;
@@ -421,20 +418,31 @@ void NetworkLock::publishAuthorizationInfo(const std::list<NukiLock::LogEntry>&
break;
}
--i;
if((log.loggingType == NukiLock::LoggingType::LockAction || log.loggingType == NukiLock::LoggingType::KeypadAction) && ! _authFound)
memset(authName, 0, sizeof(authName));
authName[0] = '\0';
if((log.loggingType == NukiLock::LoggingType::LockAction || log.loggingType == NukiLock::LoggingType::KeypadAction))
{
_authFound = true;
_authId = log.authId;
int sizeName = sizeof(log.name);
memcpy(_authName, log.name, sizeName);
if(_authName[sizeName - 1] != '\0') _authName[sizeName] = '\0';
memcpy(authName, log.name, sizeName);
if(authName[sizeName - 1] != '\0') authName[sizeName] = '\0';
if(!authFound)
{
authFound = true;
_authFound = true;
_authId = log.authId;
memset(_authName, 0, sizeof(_authName));
memcpy(_authName, authName, sizeof(authName));
}
}
auto entry = json.add<JsonVariant>();
entry["index"] = log.index;
entry["authorizationId"] = log.authId;
entry["authorizationName"] = _authName;
entry["authorizationName"] = authName;
entry["timeYear"] = log.timeStampYear;
entry["timeMonth"] = log.timeStampMonth;
entry["timeDay"] = log.timeStampDay;
@@ -500,7 +508,7 @@ void NetworkLock::publishAuthorizationInfo(const std::list<NukiLock::LogEntry>&
serializeJson(json, _buffer, _bufferSize);
publishString(mqtt_topic_lock_log, _buffer);
if(_authFound)
if(authFound)
{
publishUInt(mqtt_topic_lock_auth_id, _authId);
publishString(mqtt_topic_lock_auth_name, _authName);

View File

@@ -266,7 +266,7 @@ void NetworkOpener::publishKeyTurnerState(const NukiOpener::OpenerState& keyTurn
}
json["trigger"] = str;
json["ringToOpenTimer"] = keyTurnerState.ringToOpenTimer;
char curTime[20];
sprintf(curTime, "%04d-%02d-%02d %02d:%02d:%02d", keyTurnerState.currentTimeYear, keyTurnerState.currentTimeMonth, keyTurnerState.currentTimeDay, keyTurnerState.currentTimeHour, keyTurnerState.currentTimeMinute, keyTurnerState.currentTimeSecond);
@@ -373,11 +373,8 @@ void NetworkOpener::publishState(NukiOpener::OpenerState lockState)
void NetworkOpener::publishAuthorizationInfo(const std::list<NukiOpener::LogEntry>& logEntries)
{
char str[50];
_authId = 0;
memset(_authName, 0, sizeof(_authName));
_authName[0] = '\0';
_authFound = false;
char authName[33];
bool authFound = false;
JsonDocument json;
@@ -390,13 +387,23 @@ void NetworkOpener::publishAuthorizationInfo(const std::list<NukiOpener::LogEntr
}
--i;
if((log.loggingType == NukiOpener::LoggingType::LockAction || log.loggingType == NukiOpener::LoggingType::KeypadAction) && ! _authFound)
memset(authName, 0, sizeof(authName));
authName[0] = '\0';
if((log.loggingType == NukiOpener::LoggingType::LockAction || log.loggingType == NukiOpener::LoggingType::KeypadAction))
{
_authFound = true;
_authId = log.authId;
int sizeName = sizeof(log.name);
memcpy(_authName, log.name, sizeName);
if(_authName[sizeName - 1] != '\0') _authName[sizeName] = '\0';
memcpy(authName, log.name, sizeName);
if(authName[sizeName - 1] != '\0') authName[sizeName] = '\0';
if(!authFound)
{
authFound = true;
_authFound = true;
_authId = log.authId;
memset(_authName, 0, sizeof(_authName));
memcpy(_authName, authName, sizeof(authName));
}
}
auto entry = json.add<JsonVariant>();
@@ -494,7 +501,7 @@ void NetworkOpener::publishAuthorizationInfo(const std::list<NukiOpener::LogEntr
serializeJson(json, _buffer, _bufferSize);
publishString(mqtt_topic_lock_log, _buffer);
if(_authFound)
if(authFound)
{
publishUInt(mqtt_topic_lock_auth_id, _authId);
publishString(mqtt_topic_lock_auth_name, _authName);
@@ -891,7 +898,7 @@ void NetworkOpener::publishStatusUpdated(const bool statusUpdated)
{
publishBool(mqtt_topic_lock_status_updated, statusUpdated);
}
void NetworkOpener::setLockActionReceivedCallback(LockActionResult (*lockActionReceivedCallback)(const char *))
{
_lockActionReceivedCallback = lockActionReceivedCallback;

View File

@@ -305,6 +305,11 @@ bool NukiOpenerWrapper::isPinSet()
return _nukiOpener.getSecurityPincode() != 0;
}
bool NukiOpenerWrapper::isPinValid()
{
return _preferences->getInt(preference_opener_pin_status, 4) == 1;
}
void NukiOpenerWrapper::setPin(const uint16_t pin)
{
_nukiOpener.saveSecurityPincode(pin);
@@ -373,15 +378,18 @@ void NukiOpenerWrapper::updateKeyTurnerState()
if(_publishAuthData)
{
Log->println(F("Publishing auth data"));
updateAuthData();
Log->println(F("Done publishing auth data"));
}
postponeBleWatchdog();
Log->println(F("Done querying lock state"));
}
void NukiOpenerWrapper::updateBatteryState()
{
Log->print("Querying opener battery state: ");
Log->print(F("Querying opener battery state: "));
Nuki::CmdResult result = _nukiOpener.requestBatteryReport(&_batteryReport);
printCommandResult(result);
if(result == Nuki::CmdResult::Success)
@@ -389,6 +397,7 @@ void NukiOpenerWrapper::updateBatteryState()
_network->publishBatteryReport(_batteryReport);
}
postponeBleWatchdog();
Log->println(F("Done querying lock battery state"));
}
void NukiOpenerWrapper::updateConfig()
@@ -470,27 +479,28 @@ void NukiOpenerWrapper::updateConfig()
void NukiOpenerWrapper::updateAuthData()
{
if(!isPinSet()) return;
if(!isPinValid())
{
Log->println(F("No valid PIN set"));
return;
}
Nuki::CmdResult result = _nukiOpener.retrieveLogEntries(0, 0, 0, true);
Nuki::CmdResult result = _nukiOpener.retrieveLogEntries(0, 5, 1, false);
Log->print(F("Retrieve log entries: "));
Log->println(result);
if(result != Nuki::CmdResult::Success)
{
return;
}
delay(100);
uint16_t count = _nukiOpener.getLogEntryCount();
result = _nukiOpener.retrieveLogEntries(0, count < 5 ? count : 5, 1, false);
if(result != Nuki::CmdResult::Success)
{
return;
}
delay(1000);
std::list<NukiOpener::LogEntry> log;
_nukiOpener.getLogEntries(&log);
Log->print(F("Log size: "));
Log->println(log.size());
if(log.size() > 0)
{
_network->publishAuthorizationInfo(log);
@@ -526,9 +536,12 @@ void NukiOpenerWrapper::updateKeypad()
_keypadCodeIds.clear();
_keypadCodeIds.reserve(entries.size());
_keypadCodes.clear();
_keypadCodes.reserve(entries.size());
for(const auto& entry : entries)
{
_keypadCodeIds.push_back(entry.codeId);
_keypadCodes.push_back(entry.code);
}
}
@@ -780,9 +793,9 @@ void NukiOpenerWrapper::onConfigUpdateReceived(const char *value)
return;
}
if(!isPinSet())
if(!isPinValid())
{
jsonResult["general"] = "noPinSet";
jsonResult["general"] = "noValidPinSet";
serializeJson(jsonResult, _resbuf, sizeof(_resbuf));
_network->publishConfigCommandResult(_resbuf);
return;
@@ -1412,9 +1425,9 @@ void NukiOpenerWrapper::onKeypadCommandReceived(const char *command, const uint
void NukiOpenerWrapper::onKeypadJsonCommandReceived(const char *value)
{
if(!isPinSet())
if(!isPinValid())
{
_network->publishKeypadJsonCommandResult("noPinSet");
_network->publishKeypadJsonCommandResult("noValidPinSet");
return;
}
@@ -1478,7 +1491,7 @@ void NukiOpenerWrapper::onKeypadJsonCommandReceived(const char *value)
if(idExists)
{
result = _nukiOpener.deleteKeypadEntry(codeId);
Log->print("Delete keypad code: ");
Log->print(F("Delete keypad code: "));
Log->println((int)result);
}
else
@@ -1505,7 +1518,7 @@ void NukiOpenerWrapper::onKeypadJsonCommandReceived(const char *value)
return;
}
}
else
else if (strcmp(action, "update") != 0)
{
_network->publishKeypadJsonCommandResult("noCodeSet");
return;
@@ -1664,7 +1677,7 @@ void NukiOpenerWrapper::onKeypadJsonCommandReceived(const char *value)
}
result = _nukiOpener.addKeypadEntry(entry);
Log->print("Add keypad code: ");
Log->print(F("Add keypad code: "));
Log->println((int)result);
}
else if (strcmp(action, "update") == 0)
@@ -1686,7 +1699,14 @@ void NukiOpenerWrapper::onKeypadJsonCommandReceived(const char *value)
entry.codeId = codeId;
size_t nameLen = strlen(name);
memcpy(&entry.name, name, nameLen > 20 ? 20 : nameLen);
entry.code = code;
if(code) entry.code = code;
else
{
auto it = std::find(_keypadCodeIds.begin(), _keypadCodeIds.end(), codeId);
entry.code = _keypadCodes[(it - _keypadCodeIds.begin())];
}
entry.enabled = enabled == 0 ? 0 : 1;
entry.timeLimited = timeLimited == 1 ? 1 : 0;
@@ -1725,7 +1745,7 @@ void NukiOpenerWrapper::onKeypadJsonCommandReceived(const char *value)
}
result = _nukiOpener.updateKeypadEntry(entry);
Log->print("Update keypad code: ");
Log->print(F("Update keypad code: "));
Log->println((int)result);
}
}
@@ -1760,9 +1780,9 @@ void NukiOpenerWrapper::onTimeControlCommandReceived(const char *value)
return;
}
if(!isPinSet())
if(!isPinValid())
{
_network->publishTimeControlCommandResult("noPinSet");
_network->publishTimeControlCommandResult("noValidPinSet");
return;
}
@@ -1815,7 +1835,7 @@ void NukiOpenerWrapper::onTimeControlCommandReceived(const char *value)
if(idExists)
{
result = _nukiOpener.removeTimeControlEntry(entryId);
Log->print("Delete time control ");
Log->print(F("Delete time control "));
Log->println((int)result);
}
else
@@ -1880,7 +1900,7 @@ void NukiOpenerWrapper::onTimeControlCommandReceived(const char *value)
entry.lockAction = timeControlLockAction;
result = _nukiOpener.addTimeControlEntry(entry);
Log->print("Add time control: ");
Log->print(F("Add time control: "));
Log->println((int)result);
}
else if (strcmp(action, "update") == 0)
@@ -1906,7 +1926,7 @@ void NukiOpenerWrapper::onTimeControlCommandReceived(const char *value)
entry.lockAction = timeControlLockAction;
result = _nukiOpener.updateTimeControlEntry(entry);
Log->print("Update time control: ");
Log->print(F("Update time control: "));
Log->println((int)result);
}
}

View File

@@ -25,6 +25,7 @@ public:
void deactivateCM();
bool isPinSet();
bool isPinValid();
void setPin(const uint16_t pin);
void unpair();
@@ -105,6 +106,7 @@ private:
int _retryLockstateCount = 0;
unsigned long _nextRetryTs = 0;
std::vector<uint16_t> _keypadCodeIds;
std::vector<uint32_t> _keypadCodes;
std::vector<uint8_t> _timeControlIds;
NukiOpener::OpenerState _lastKeyTurnerState;

View File

@@ -309,6 +309,11 @@ bool NukiWrapper::isPinSet()
return _nukiLock.getSecurityPincode() != 0;
}
bool NukiWrapper::isPinValid()
{
return _preferences->getInt(preference_lock_pin_status, 4) == 1;
}
void NukiWrapper::setPin(const uint16_t pin)
{
_nukiLock.saveSecurityPincode(pin);
@@ -347,7 +352,9 @@ void NukiWrapper::updateKeyTurnerState()
if(_publishAuthData)
{
Log->println(F("Publishing auth data"));
updateAuthData();
Log->println(F("Done publishing auth data"));
}
_network->publishKeyTurnerState(_keyTurnerState, _lastKeyTurnerState);
@@ -358,11 +365,12 @@ void NukiWrapper::updateKeyTurnerState()
Log->println(lockStateStr);
postponeBleWatchdog();
Log->println(F("Done querying lock state"));
}
void NukiWrapper::updateBatteryState()
{
Log->print("Querying lock battery state: ");
Log->print(F("Querying lock battery state: "));
Nuki::CmdResult result = _nukiLock.requestBatteryReport(&_batteryReport);
printCommandResult(result);
if(result == Nuki::CmdResult::Success)
@@ -370,6 +378,7 @@ void NukiWrapper::updateBatteryState()
_network->publishBatteryReport(_batteryReport);
}
postponeBleWatchdog();
Log->println(F("Done querying lock battery state"));
}
void NukiWrapper::updateConfig()
@@ -451,27 +460,28 @@ void NukiWrapper::updateConfig()
void NukiWrapper::updateAuthData()
{
if(!isPinSet()) return;
if(!isPinValid())
{
Log->println(F("No valid PIN set"));
return;
}
Nuki::CmdResult result = _nukiLock.retrieveLogEntries(0, 0, 0, true);
Nuki::CmdResult result = _nukiLock.retrieveLogEntries(0, 5, 1, false);
Log->print(F("Retrieve log entries: "));
Log->println(result);
if(result != Nuki::CmdResult::Success)
{
return;
}
delay(100);
uint16_t count = _nukiLock.getLogEntryCount();
result = _nukiLock.retrieveLogEntries(0, count < 5 ? count : 5, 1, false);
if(result != Nuki::CmdResult::Success)
{
return;
}
delay(1000);
std::list<NukiLock::LogEntry> log;
_nukiLock.getLogEntries(&log);
Log->print(F("Log size: "));
Log->println(log.size());
if(log.size() > 0)
{
_network->publishAuthorizationInfo(log);
@@ -507,9 +517,12 @@ void NukiWrapper::updateKeypad()
_keypadCodeIds.clear();
_keypadCodeIds.reserve(entries.size());
_keypadCodes.clear();
_keypadCodes.reserve(entries.size());
for(const auto& entry : entries)
{
_keypadCodeIds.push_back(entry.codeId);
_keypadCodes.push_back(entry.code);
}
}
@@ -716,9 +729,9 @@ void NukiWrapper::onConfigUpdateReceived(const char *value)
return;
}
if(!isPinSet())
if(!isPinValid())
{
jsonResult["general"] = "noPinSet";
jsonResult["general"] = "noValidPinSet";
serializeJson(jsonResult, _resbuf, sizeof(_resbuf));
_network->publishConfigCommandResult(_resbuf);
return;
@@ -1398,9 +1411,9 @@ void NukiWrapper::onKeypadCommandReceived(const char *command, const uint &id, c
void NukiWrapper::onKeypadJsonCommandReceived(const char *value)
{
if(!isPinSet())
if(!isPinValid())
{
_network->publishKeypadJsonCommandResult("noPinSet");
_network->publishKeypadJsonCommandResult("noValidPinSet");
return;
}
@@ -1464,7 +1477,7 @@ void NukiWrapper::onKeypadJsonCommandReceived(const char *value)
if(idExists)
{
result = _nukiLock.deleteKeypadEntry(codeId);
Log->print("Delete keypad code: ");
Log->print(F("Delete keypad code: "));
Log->println((int)result);
}
else
@@ -1491,7 +1504,7 @@ void NukiWrapper::onKeypadJsonCommandReceived(const char *value)
return;
}
}
else
else if (strcmp(action, "update") != 0)
{
_network->publishKeypadJsonCommandResult("noCodeSet");
return;
@@ -1650,7 +1663,7 @@ void NukiWrapper::onKeypadJsonCommandReceived(const char *value)
}
result = _nukiLock.addKeypadEntry(entry);
Log->print("Add keypad code: ");
Log->print(F("Add keypad code: "));
Log->println((int)result);
}
else if (strcmp(action, "update") == 0)
@@ -1672,7 +1685,14 @@ void NukiWrapper::onKeypadJsonCommandReceived(const char *value)
entry.codeId = codeId;
size_t nameLen = strlen(name);
memcpy(&entry.name, name, nameLen > 20 ? 20 : nameLen);
entry.code = code;
if(code) entry.code = code;
else
{
auto it = std::find(_keypadCodeIds.begin(), _keypadCodeIds.end(), codeId);
entry.code = _keypadCodes[(it - _keypadCodeIds.begin())];
}
entry.enabled = enabled == 0 ? 0 : 1;
entry.timeLimited = timeLimited == 1 ? 1 : 0;
@@ -1711,7 +1731,7 @@ void NukiWrapper::onKeypadJsonCommandReceived(const char *value)
}
result = _nukiLock.updateKeypadEntry(entry);
Log->print("Update keypad code: ");
Log->print(F("Update keypad code: "));
Log->println((int)result);
}
}
@@ -1746,9 +1766,9 @@ void NukiWrapper::onTimeControlCommandReceived(const char *value)
return;
}
if(!isPinSet())
if(!isPinValid())
{
_network->publishTimeControlCommandResult("noPinSet");
_network->publishTimeControlCommandResult("noValidPinSet");
return;
}
@@ -1801,7 +1821,7 @@ void NukiWrapper::onTimeControlCommandReceived(const char *value)
if(idExists)
{
result = _nukiLock.removeTimeControlEntry(entryId);
Log->print("Delete time control ");
Log->print(F("Delete time control: "));
Log->println((int)result);
}
else
@@ -1866,7 +1886,7 @@ void NukiWrapper::onTimeControlCommandReceived(const char *value)
entry.lockAction = timeControlLockAction;
result = _nukiLock.addTimeControlEntry(entry);
Log->print("Add time control: ");
Log->print(F("Add time control: "));
Log->println((int)result);
}
else if (strcmp(action, "update") == 0)
@@ -1892,7 +1912,7 @@ void NukiWrapper::onTimeControlCommandReceived(const char *value)
entry.lockAction = timeControlLockAction;
result = _nukiLock.updateTimeControlEntry(entry);
Log->print("Update time control: ");
Log->print(F("Update time control: "));
Log->println((int)result);
}
}

View File

@@ -25,6 +25,7 @@ public:
void lockngounlatch();
bool isPinSet();
bool isPinValid();
void setPin(const uint16_t pin);
void unpair();
@@ -94,6 +95,7 @@ private:
bool _publishAuthData = false;
bool _clearAuthData = false;
std::vector<uint16_t> _keypadCodeIds;
std::vector<uint32_t> _keypadCodes;
std::vector<uint8_t> _timeControlIds;
NukiLock::KeyTurnerState _lastKeyTurnerState;

View File

@@ -1545,8 +1545,8 @@ void WebCfgServer::buildInfoHtml(String &response)
response.concat(_nuki->hardwareVersion().c_str());
response.concat("\nLock paired: ");
response.concat(_nuki->isPaired() ? "Yes\n" : "No\n");
response.concat("Lock PIN set: ");
response.concat(_nuki->isPaired() ? _nuki->isPinSet() ? "Yes\n" : "No\n" : "-\n");
response.concat("Lock valid PIN set: ");
response.concat(_nuki->isPaired() ? _nuki->isPinValid() ? "Yes\n" : "No\n" : "-\n");
response.concat("Lock has door sensor: ");
response.concat(_nuki->hasDoorSensor() ? "Yes\n" : "No\n");
response.concat("Lock has keypad: ");
@@ -1659,8 +1659,8 @@ void WebCfgServer::buildInfoHtml(String &response)
response.concat("\nOpener hardware version: ");
response.concat(_nukiOpener->hardwareVersion().c_str()); response.concat("\nOpener paired: ");
response.concat(_nukiOpener->isPaired() ? "Yes\n" : "No\n");
response.concat("Opener PIN set: ");
response.concat(_nukiOpener->isPaired() ? _nukiOpener->isPinSet() ? "Yes\n" : "No\n" : "-\n");
response.concat("Opener valid PIN set: ");
response.concat(_nukiOpener->isPaired() ? _nukiOpener->isPinValid() ? "Yes\n" : "No\n" : "-\n");
response.concat("Opener has keypad: ");
response.concat(_nukiOpener->hasKeypad() ? "Yes\n" : "No\n");
response.concat("Opener ACL (Activate Ring-to-Open): ");