convert lock state to string

This commit is contained in:
technyon
2022-03-25 20:57:40 +01:00
parent 0c03a64a86
commit 66e0239589
4 changed files with 51 additions and 7 deletions

View File

@@ -124,9 +124,7 @@ void Network::onMqttDataReceived(char *&topic, byte *&payload, unsigned int &len
}
}
void Network::publishKeyTurnerState(const KeyTurnerState &state)
void Network::publishKeyTurnerState(const char* state)
{
char cstr[10];
itoa((int)state.lockState, cstr, 10);
_mqttClient.publish(mqtt_topc_lockstate, cstr);
_mqttClient.publish(mqtt_topc_lockstate, state);
}

View File

@@ -12,7 +12,7 @@ public:
void initialize();
void update();
void publishKeyTurnerState(const KeyTurnerState& state);
void publishKeyTurnerState(const char* state);
private:
static void onMqttDataReceivedCallback(char* topic, byte* payload, unsigned int length);

View File

@@ -25,21 +25,65 @@ void Nuki::update()
}
else
{
vTaskDelay( 200 / portTICK_PERIOD_MS);
return;
}
}
vTaskDelay( 100 / portTICK_PERIOD_MS);
_nukiBle.requestKeyTurnerState(&_keyTurnerState);
char str[20];
stateToString(_keyTurnerState.lockState, str);
Serial.print(F("Nuki lock state: "));
Serial.println((int)_keyTurnerState.lockState);
Serial.println(str);
if(_keyTurnerState.lockState != _lastKeyTurnerState.lockState)
{
_network->publishKeyTurnerState(_keyTurnerState);
_network->publishKeyTurnerState(str);
}
memcpy(&_lastKeyTurnerState, &_keyTurnerState, sizeof(KeyTurnerState));
vTaskDelay( 20000 / portTICK_PERIOD_MS);
}
void Nuki::stateToString(LockState state, char* str)
{
switch(state)
{
case LockState::uncalibrated:
strcpy(str, "uncalibrated");
break;
case LockState::locked:
strcpy(str, "locked");
break;
case LockState::locking:
strcpy(str, "locking");
break;
case LockState::unlocked:
strcpy(str, "unlocked");
break;
case LockState::unlatched:
strcpy(str, "unlatched");
break;
case LockState::unlockedLnga:
strcpy(str, "unlockedLnga");
break;
case LockState::unlatching:
strcpy(str, "unlatching");
break;
case LockState::calibration:
strcpy(str, "calibration");
break;
case LockState::bootRun:
strcpy(str, "bootRun");
break;
case LockState::motorBlocked:
strcpy(str, "motorBlocked");
break;
default:
strcpy(str, "undefined");
break;
}
}

2
Nuki.h
View File

@@ -13,6 +13,8 @@ public:
void update();
private:
void stateToString(LockState state, char* str); // char array at least 14 characters
NukiBle _nukiBle;
Network* _network;