publish auth data as json array
This commit is contained in:
@@ -9,9 +9,9 @@
|
||||
#define mqtt_topic_battery_lock_distance "/battery/lockDistance"
|
||||
|
||||
#define mqtt_topic_lock_state "/lock/state"
|
||||
#define mqtt_topic_lock_state_json "/lock/stateJson"
|
||||
#define mqtt_topic_lock_binary_state "/lock/binaryState"
|
||||
#define mqtt_topic_lock_trigger "/lock/trigger"
|
||||
#define mqtt_topic_lock_log "/lock/log"
|
||||
#define mqtt_topic_lock_auth_id "/lock/authorizationId"
|
||||
#define mqtt_topic_lock_auth_name "/lock/authorizationName"
|
||||
#define mqtt_topic_lock_completionStatus "/lock/completionStatus"
|
||||
|
||||
111
NetworkLock.cpp
111
NetworkLock.cpp
@@ -210,42 +210,6 @@ void NetworkLock::publishKeyTurnerState(const NukiLock::KeyTurnerState& keyTurne
|
||||
_firstTunerStatePublish = false;
|
||||
}
|
||||
|
||||
void NetworkLock::publishStateAsJson(const char* action, const NukiLock::KeyTurnerState &keyTurnerState, const uint32_t authId, const char *authName)
|
||||
{
|
||||
char str[50];
|
||||
String json = "{\n";
|
||||
|
||||
// action
|
||||
json.concat("\"action\": \""); json.concat(action); json.concat("\",\n");
|
||||
|
||||
// state
|
||||
memset(&str, 0, sizeof(str));
|
||||
lockstateToString(keyTurnerState.lockState, str);
|
||||
json.concat("\"state\": \""); json.concat(str); json.concat("\",\n");
|
||||
|
||||
// trigger
|
||||
memset(&str, 0, sizeof(str));
|
||||
triggerToString(keyTurnerState.trigger, str);
|
||||
json.concat("\"trigger\": \""); json.concat(str); json.concat("\",\n");
|
||||
|
||||
// completion status
|
||||
memset(&str, 0, sizeof(str));
|
||||
NukiLock::completionStatusToString(keyTurnerState.lastLockActionCompletionStatus, str);
|
||||
json.concat("\"completionStatus\": \""); json.concat(str); json.concat("\",\n");
|
||||
|
||||
// Door sensor state
|
||||
memset(&str, 0, sizeof(str));
|
||||
NukiLock::doorSensorStateToString(keyTurnerState.doorSensorState, str);
|
||||
json.concat("\"doorSensorState\": \""); json.concat(str); json.concat("\",\n");
|
||||
|
||||
// Auth date
|
||||
json.concat("\"authorizationId\": "); json.concat(authId == 0xffff ? 0 : authId); json.concat(",\n");
|
||||
json.concat("\"authorizationName\": \""); json.concat(authName); json.concat("\"\n");
|
||||
|
||||
json.concat("}");
|
||||
publishString(mqtt_topic_lock_state_json, json.c_str());
|
||||
}
|
||||
|
||||
void NetworkLock::publishBinaryState(NukiLock::LockState lockState)
|
||||
{
|
||||
switch(lockState)
|
||||
@@ -267,10 +231,79 @@ void NetworkLock::publishBinaryState(NukiLock::LockState lockState)
|
||||
}
|
||||
|
||||
|
||||
void NetworkLock::publishAuthorizationInfo(const uint32_t authId, const char *authName)
|
||||
void NetworkLock::publishAuthorizationInfo(const std::list<Nuki::LogEntry>& logEntries)
|
||||
{
|
||||
publishUInt(mqtt_topic_lock_auth_id, authId);
|
||||
publishString(mqtt_topic_lock_auth_name, authName);
|
||||
char str[50];
|
||||
|
||||
String json = "[\n";
|
||||
|
||||
for(const auto& log : logEntries)
|
||||
{
|
||||
json.concat("{\n");
|
||||
|
||||
json.concat("\"index\": ");
|
||||
json.concat(log.index);
|
||||
json.concat(",\n");
|
||||
json.concat("\"authorizationId\": ");
|
||||
json.concat(log.authId);
|
||||
json.concat(",\n");
|
||||
|
||||
memset(str, 0, sizeof(str));
|
||||
memcpy(str, log.name, sizeof(log.name));
|
||||
json.concat("\"authorizationName\": \"");
|
||||
json.concat(str);
|
||||
json.concat("\",\n");
|
||||
|
||||
json.concat("\"timeYear\": ");
|
||||
json.concat(log.timeStampYear);
|
||||
json.concat(",\n");
|
||||
json.concat("\"timeMonth\": ");
|
||||
json.concat(log.timeStampMonth);
|
||||
json.concat(",\n");
|
||||
json.concat("\"timeDay\": ");
|
||||
json.concat(log.timeStampDay);
|
||||
json.concat(",\n");
|
||||
json.concat("\"timeHour\": ");
|
||||
json.concat(log.timeStampHour);
|
||||
json.concat(",\n");
|
||||
json.concat("\"timeMinute\": ");
|
||||
json.concat(log.timeStampMinute);
|
||||
json.concat(",\n");
|
||||
json.concat("\"timeSecond\": ");
|
||||
json.concat(log.timeStampSecond);
|
||||
json.concat(",\n");
|
||||
|
||||
|
||||
memset(str, 0, sizeof(str));
|
||||
loggingTypeToString(log.loggingType, str);
|
||||
json.concat("\"type\": \"");
|
||||
json.concat(str);
|
||||
json.concat("\"\n");
|
||||
|
||||
json.concat("}");
|
||||
|
||||
if(&log == &logEntries.back())
|
||||
{
|
||||
json.concat("\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
json.concat(",\n");
|
||||
}
|
||||
}
|
||||
|
||||
json.concat("]");
|
||||
publishString(mqtt_topic_lock_log, json.c_str());
|
||||
}
|
||||
//uint16_t timeStampYear;
|
||||
//uint8_t timeStampMonth;
|
||||
//uint8_t timeStampDay;
|
||||
//uint8_t timeStampHour;
|
||||
//uint8_t timeStampMinute;
|
||||
//uint8_t timeStampSecond;
|
||||
void NetworkLock::clearAuthorizationInfo()
|
||||
{
|
||||
publishString(mqtt_topic_lock_log, "");
|
||||
}
|
||||
|
||||
void NetworkLock::publishCommandResult(const char *resultStr)
|
||||
|
||||
@@ -21,9 +21,9 @@ public:
|
||||
void update();
|
||||
|
||||
void publishKeyTurnerState(const NukiLock::KeyTurnerState& keyTurnerState, const NukiLock::KeyTurnerState& lastKeyTurnerState);
|
||||
void publishStateAsJson(const char* action, const NukiLock::KeyTurnerState& keyTurnerState, const uint32_t authId, const char* authName);
|
||||
void publishBinaryState(NukiLock::LockState lockState);
|
||||
void publishAuthorizationInfo(const uint32_t authId, const char* authName);
|
||||
void publishAuthorizationInfo(const std::list<Nuki::LogEntry>& logEntries);
|
||||
void clearAuthorizationInfo();
|
||||
void publishCommandResult(const char* resultStr);
|
||||
void publishBatteryReport(const NukiLock::BatteryReport& batteryReport);
|
||||
void publishConfig(const NukiLock::Config& config);
|
||||
|
||||
@@ -125,43 +125,6 @@ void NetworkOpener::publishKeyTurnerState(const NukiOpener::OpenerState& keyTurn
|
||||
_firstTunerStatePublish = false;
|
||||
}
|
||||
|
||||
void NetworkOpener::publishStateAsJson(const char *action, const NukiOpener::OpenerState &keyTurnerState, const uint32_t authId, const char *authName)
|
||||
{
|
||||
char str[50];
|
||||
String json = "{\n";
|
||||
|
||||
// action
|
||||
json.concat("\"action\": \""); json.concat(action); json.concat("\",\n");
|
||||
|
||||
// state
|
||||
memset(&str, 0, sizeof(str));
|
||||
lockstateToString(keyTurnerState.lockState, str);
|
||||
json.concat("\"state\": \""); json.concat(str); json.concat("\",\n");
|
||||
|
||||
// trigger
|
||||
memset(&str, 0, sizeof(str));
|
||||
triggerToString(keyTurnerState.trigger, str);
|
||||
json.concat("\"trigger\": \""); json.concat(str); json.concat("\",\n");
|
||||
|
||||
// completion status
|
||||
memset(&str, 0, sizeof(str));
|
||||
NukiOpener::completionStatusToString(keyTurnerState.lastLockActionCompletionStatus, str);
|
||||
json.concat("\"completionStatus\": \""); json.concat(str); json.concat("\",\n");
|
||||
|
||||
// Door sensor state
|
||||
memset(&str, 0, sizeof(str));
|
||||
NukiOpener::doorSensorStateToString(keyTurnerState.doorSensorState, str);
|
||||
json.concat("\"doorSensorState\": \""); json.concat(str); json.concat("\",\n");
|
||||
|
||||
// Auth date
|
||||
json.concat("\"authorizationId\": "); json.concat(authId == 0xffff ? 0 : authId); json.concat(",\n");
|
||||
json.concat("\"authorizationName\": \""); json.concat(authName); json.concat("\"\n");
|
||||
|
||||
json.concat("}");
|
||||
|
||||
publishString(mqtt_topic_lock_state_json, json.c_str());
|
||||
}
|
||||
|
||||
void NetworkOpener::publishBinaryState(NukiOpener::LockState lockState)
|
||||
{
|
||||
switch(lockState)
|
||||
|
||||
@@ -19,7 +19,6 @@ public:
|
||||
void initialize();
|
||||
|
||||
void publishKeyTurnerState(const NukiOpener::OpenerState& keyTurnerState, const NukiOpener::OpenerState& lastKeyTurnerState);
|
||||
void publishStateAsJson(const char* action, const NukiOpener::OpenerState& keyTurnerState, const uint32_t authId, const char* authName);
|
||||
void publishBinaryState(NukiOpener::LockState lockState);
|
||||
void publishAuthorizationInfo(const uint32_t authId, const char* authName);
|
||||
void publishCommandResult(const char* resultStr);
|
||||
|
||||
@@ -40,7 +40,6 @@ void NukiOpenerWrapper::initialize()
|
||||
_intervalLockstate = _preferences->getInt(preference_query_interval_lockstate);
|
||||
_intervalBattery = _preferences->getInt(preference_query_interval_battery);
|
||||
_publishAuthData = _preferences->getBool(preference_publish_authdata);
|
||||
_publishJson = _preferences->getBool(preference_publish_json);
|
||||
|
||||
if(_intervalLockstate == 0)
|
||||
{
|
||||
@@ -165,11 +164,6 @@ void NukiOpenerWrapper::updateKeyTurnerState()
|
||||
{
|
||||
updateAuthData();
|
||||
}
|
||||
|
||||
if(_publishJson)
|
||||
{
|
||||
_network->publishStateAsJson(_lastLockAction, _keyTurnerState, _lastAuthId, _lastAuthName);
|
||||
}
|
||||
}
|
||||
|
||||
void NukiOpenerWrapper::updateBatteryState()
|
||||
@@ -194,7 +188,7 @@ void NukiOpenerWrapper::updateAuthData()
|
||||
_network->publishAuthorizationInfo(0, "");
|
||||
return;
|
||||
}
|
||||
vTaskDelay( 100 / portTICK_PERIOD_MS);
|
||||
delay(100);
|
||||
|
||||
result = _nukiOpener.retrieveLogEntries(_nukiOpener.getLogEntryCount() - 2, 1, 0, false);
|
||||
if(result != Nuki::CmdResult::Success)
|
||||
@@ -202,28 +196,25 @@ void NukiOpenerWrapper::updateAuthData()
|
||||
_network->publishAuthorizationInfo(0, "");
|
||||
return;
|
||||
}
|
||||
vTaskDelay( 200 / portTICK_PERIOD_MS);
|
||||
delay(200);
|
||||
|
||||
std::list<NukiOpener::LogEntry> log;
|
||||
std::list<Nuki::LogEntry> log;
|
||||
_nukiOpener.getLogEntries(&log);
|
||||
|
||||
if(log.size() > 0)
|
||||
{
|
||||
const NukiOpener::LogEntry& entry = log.front();
|
||||
const Nuki::LogEntry& entry = log.front();
|
||||
|
||||
if(entry.authId != _lastAuthId)
|
||||
{
|
||||
_network->publishAuthorizationInfo(entry.authId, (char *) entry.name);
|
||||
_lastAuthId = entry.authId;
|
||||
memset(_lastAuthName, 0, sizeof(_lastAuthName));
|
||||
memcpy(_lastAuthName, entry.name, sizeof(entry.name));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_network->publishAuthorizationInfo(0, "");
|
||||
_lastAuthId = 0;
|
||||
memset(_lastAuthName, 0, sizeof(_lastAuthName));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -242,9 +233,6 @@ NukiOpener::LockAction NukiOpenerWrapper::lockActionToEnum(const char *str)
|
||||
|
||||
bool NukiOpenerWrapper::onLockActionReceivedCallback(const char *value)
|
||||
{
|
||||
memset(&nukiOpenerInst->_lastLockAction, 0, sizeof(_lastLockAction));
|
||||
strcpy(nukiOpenerInst->_lastLockAction, value);
|
||||
|
||||
NukiOpener::LockAction action = nukiOpenerInst->lockActionToEnum(value);
|
||||
nukiOpenerInst->_nextLockAction = action;
|
||||
return (int)action != 0xff;
|
||||
|
||||
@@ -59,9 +59,7 @@ private:
|
||||
NukiOpener::OpenerState _lastKeyTurnerState;
|
||||
NukiOpener::OpenerState _keyTurnerState;
|
||||
|
||||
char _lastLockAction[25] = { 0 };
|
||||
uint32_t _lastAuthId = 0xffff;
|
||||
char _lastAuthName[33] = {0};
|
||||
|
||||
NukiOpener::BatteryReport _batteryReport;
|
||||
NukiOpener::BatteryReport _lastBatteryReport;
|
||||
@@ -73,7 +71,6 @@ private:
|
||||
|
||||
bool _paired = false;
|
||||
bool _statusUpdated = false;
|
||||
bool _publishJson = false;
|
||||
unsigned long _nextLockStateUpdateTs = 0;
|
||||
unsigned long _nextBatteryReportTs = 0;
|
||||
unsigned long _nextConfigUpdateTs = 0;
|
||||
|
||||
@@ -44,7 +44,6 @@ void NukiWrapper::initialize()
|
||||
_intervalKeypad = _preferences->getInt(preference_query_interval_keypad);
|
||||
_keypadEnabled = _preferences->getBool(preference_keypad_control_enabled);
|
||||
_publishAuthData = _preferences->getBool(preference_publish_authdata);
|
||||
_publishJson = _preferences->getBool(preference_publish_json);
|
||||
_maxKeypadCodeCount = _preferences->getUInt(preference_max_keypad_code_count);
|
||||
|
||||
if(_intervalLockstate == 0)
|
||||
@@ -142,7 +141,7 @@ void NukiWrapper::update()
|
||||
|
||||
if(_clearAuthData)
|
||||
{
|
||||
_network->publishAuthorizationInfo(0, "");
|
||||
_network->clearAuthorizationInfo();
|
||||
_clearAuthData = false;
|
||||
}
|
||||
|
||||
@@ -192,11 +191,6 @@ void NukiWrapper::updateKeyTurnerState()
|
||||
{
|
||||
updateAuthData();
|
||||
}
|
||||
|
||||
if(_publishJson)
|
||||
{
|
||||
_network->publishStateAsJson(_lastLockAction, _keyTurnerState, _lastAuthId, _lastAuthName);
|
||||
}
|
||||
}
|
||||
|
||||
void NukiWrapper::updateBatteryState()
|
||||
@@ -220,39 +214,23 @@ void NukiWrapper::updateAuthData()
|
||||
Nuki::CmdResult result = _nukiLock.retrieveLogEntries(0, 0, 0, true);
|
||||
if(result != Nuki::CmdResult::Success)
|
||||
{
|
||||
_network->publishAuthorizationInfo(0, "");
|
||||
return;
|
||||
}
|
||||
vTaskDelay( 100 / portTICK_PERIOD_MS);
|
||||
delay(100);
|
||||
|
||||
result = _nukiLock.retrieveLogEntries(_nukiLock.getLogEntryCount() - 2, 1, 0, false);
|
||||
result = _nukiLock.retrieveLogEntries(0, 5, 1, false);
|
||||
if(result != Nuki::CmdResult::Success)
|
||||
{
|
||||
_network->publishAuthorizationInfo(0, "");
|
||||
return;
|
||||
}
|
||||
vTaskDelay( 200 / portTICK_PERIOD_MS);
|
||||
delay(200);
|
||||
|
||||
std::list<Nuki::LogEntry> log;
|
||||
_nukiLock.getLogEntries(&log);
|
||||
|
||||
if(log.size() > 0)
|
||||
{
|
||||
const Nuki::LogEntry& entry = log.front();
|
||||
|
||||
if(entry.authId != _lastAuthId)
|
||||
{
|
||||
_network->publishAuthorizationInfo(entry.authId, (char *) entry.name);
|
||||
_lastAuthId = entry.authId;
|
||||
memset(_lastAuthName, 0, sizeof(_lastAuthName));
|
||||
memcpy(_lastAuthName, entry.name, sizeof(entry.name));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_network->publishAuthorizationInfo(0, "");
|
||||
_lastAuthId = 0;
|
||||
memset(_lastAuthName, 0, sizeof(_lastAuthName));
|
||||
_network->publishAuthorizationInfo(log);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -300,9 +278,6 @@ NukiLock::LockAction NukiWrapper::lockActionToEnum(const char *str)
|
||||
|
||||
bool NukiWrapper::onLockActionReceivedCallback(const char *value)
|
||||
{
|
||||
memset(&nukiInst->_lastLockAction, 0, sizeof(_lastLockAction));
|
||||
strcpy(nukiInst->_lastLockAction, value);
|
||||
|
||||
NukiLock::LockAction action = nukiInst->lockActionToEnum(value);
|
||||
nukiInst->_nextLockAction = action;
|
||||
return (int)action != 0xff;
|
||||
|
||||
@@ -66,10 +66,6 @@ private:
|
||||
NukiLock::KeyTurnerState _lastKeyTurnerState;
|
||||
NukiLock::KeyTurnerState _keyTurnerState;
|
||||
|
||||
char _lastLockAction[15] = { 0 };
|
||||
uint32_t _lastAuthId = 0xffff;
|
||||
char _lastAuthName[33] = {0};
|
||||
|
||||
NukiLock::BatteryReport _batteryReport;
|
||||
NukiLock::BatteryReport _lastBatteryReport;
|
||||
|
||||
@@ -83,7 +79,6 @@ private:
|
||||
bool _hasKeypad = false;
|
||||
bool _keypadEnabled = false;
|
||||
bool _configRead = false;
|
||||
bool _publishJson = false;
|
||||
uint _maxKeypadCodeCount = 0;
|
||||
unsigned long _nextLockStateUpdateTs = 0;
|
||||
unsigned long _nextBatteryReportTs = 0;
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
#define preference_query_interval_battery "batInterval"
|
||||
#define preference_query_interval_keypad "kpInterval"
|
||||
#define preference_keypad_control_enabled "kpEnabled"
|
||||
#define preference_publish_json "pubJson"
|
||||
#define preference_cred_user "crdusr"
|
||||
#define preference_cred_password "crdpass"
|
||||
#define preference_publish_authdata "pubauth"
|
||||
|
||||
@@ -296,11 +296,6 @@ bool WebCfgServer::processArgs(String& message)
|
||||
_preferences->putBool(preference_keypad_control_enabled, (value == "1"));
|
||||
configChanged = true;
|
||||
}
|
||||
else if(key == "PUBJSON")
|
||||
{
|
||||
_preferences->putBool(preference_publish_json, (value == "1"));
|
||||
configChanged = true;
|
||||
}
|
||||
else if(key == "PRDTMO")
|
||||
{
|
||||
_preferences->putInt(preference_presence_detection_timeout, value.toInt());
|
||||
@@ -618,7 +613,6 @@ void WebCfgServer::buildNukiConfigHtml(String &response)
|
||||
printInputField(response, "KPINT", "Query interval keypad (seconds)", _preferences->getInt(preference_query_interval_keypad), 10);
|
||||
printCheckBox(response, "KPENA", "Enabled keypad control via MQTT", _preferences->getBool(preference_keypad_control_enabled));
|
||||
}
|
||||
printCheckBox(response, "PUBJSON", "Publish additional json state", _preferences->getBool(preference_publish_json));
|
||||
printCheckBox(response, "PUBAUTH", "Publish auth data (May reduce battery life)", _preferences->getBool(preference_publish_authdata));
|
||||
printCheckBox(response, "GPLCK", "Enable control via GPIO", _preferences->getBool(preference_gpio_locking_enabled));
|
||||
printInputField(response, "PRDTMO", "Presence detection timeout (seconds; -1 to disable)", _preferences->getInt(preference_presence_detection_timeout), 10);
|
||||
|
||||
Reference in New Issue
Block a user