publish first config values via mqtt

This commit is contained in:
technyon
2022-04-09 13:20:53 +02:00
parent 1ac49cc2c3
commit 3111c59e0b
5 changed files with 62 additions and 11 deletions

View File

@@ -14,4 +14,8 @@
#define mqtt_topic_door_sensor_state "/lock/doorSensorState"
#define mqtt_topic_lockstate_action "/lock/action"
#define mqtt_topic_config_button_enabled "/configuration/buttonEnabled"
#define mqtt_topic_config_led_enabled "/configuration/ledEnabled"
#define mqtt_topic_config_led_brightness "/configuration/ledBrightness"
#define mqtt_topic_presence "/presence/devices"

View File

@@ -128,10 +128,10 @@ bool Network::reconnect()
Serial.println(F("MQTT connected"));
_mqttConnected = true;
delay(200);
char path[200] = {0};
buildMqttPath(mqtt_topic_lockstate_action, path);
// ... and resubscribe
_mqttClient.subscribe(path);
subscribe(mqtt_topic_lockstate_action);
subscribe(mqtt_topic_config_button_enabled);
subscribe(mqtt_topic_config_led_enabled);
subscribe(mqtt_topic_config_led_brightness);
}
else
{
@@ -177,7 +177,7 @@ void Network::onMqttDataReceivedCallback(char *topic, byte *payload, unsigned in
void Network::onMqttDataReceived(char *&topic, byte *&payload, unsigned int &length)
{
char value[50];
char value[50] = {0};
size_t l = min(length, sizeof(value)-1);
for(int i=0; i<l; i++)
@@ -185,12 +185,7 @@ void Network::onMqttDataReceived(char *&topic, byte *&payload, unsigned int &len
value[i] = payload[i];
}
value[l] = 0;
char path[200] = {0};
buildMqttPath(mqtt_topic_lockstate_action, path);
if(strcmp(topic, path) == 0)
if(comparePrefixedPath(topic, mqtt_topic_lockstate_action))
{
if(strcmp(value, "") == 0) return;
@@ -257,6 +252,13 @@ void Network::publishBatteryReport(const Nuki::BatteryReport& batteryReport)
publishInt(mqtt_topic_battery_lock_distance, batteryReport.lockDistance); // degrees
}
void Network::publishConfig(const Nuki::Config &config)
{
publishBool(mqtt_topic_config_button_enabled, config.buttonEnabled == 1);
publishBool(mqtt_topic_config_led_enabled, config.ledEnabled == 1);
publishInt(mqtt_topic_config_led_brightness, config.ledBrightness);
}
void Network::publishPresenceDetection(char *csv)
{
_presenceCsv = csv;
@@ -330,9 +332,23 @@ void Network::buildMqttPath(const char* path, char* outPath)
outPath[i+1] = 0x00;
}
void Network::subscribe(const char *path)
{
char prefixedPath[500];
buildMqttPath(path, prefixedPath);
_mqttClient.subscribe(prefixedPath);
}
void Network::restartAndConfigureWifi()
{
_cookie.set();
delay(200);
ESP.restart();
}
bool Network::comparePrefixedPath(const char *fullPath, const char *subPath)
{
char prefixedPath[500];
buildMqttPath(subPath, prefixedPath);
return strcmp(fullPath, prefixedPath) == 0;
}

View File

@@ -19,6 +19,7 @@ public:
void publishKeyTurnerState(const Nuki::KeyTurnerState& keyTurnerState, const Nuki::KeyTurnerState& lastKeyTurnerState);
void publishBatteryReport(const Nuki::BatteryReport& batteryReport);
void publishConfig(const Nuki::Config& config);
void publishPresenceDetection(char* csv);
void setLockActionReceived(void (*lockActionReceivedCallback)(const char* value));
@@ -28,6 +29,7 @@ public:
private:
static void onMqttDataReceivedCallback(char* topic, byte* payload, unsigned int length);
void onMqttDataReceived(char*& topic, byte*& payload, unsigned int& length);
bool comparePrefixedPath(const char* fullPath, const char* subPath);
void publishFloat(const char* topic, const float value, const uint8_t precision = 2);
void publishInt(const char* topic, const int value);
@@ -35,6 +37,7 @@ private:
void publishString(const char* topic, const char* value);
void buildMqttPath(const char* path, char* outPath);
void subscribe(const char* path);
bool reconnect();

View File

@@ -93,6 +93,12 @@ void NukiWrapper::update()
_nextBatteryReportTs = ts + _intervalBattery * 1000;
updateBatteryState();
}
if(_nextConfigUpdateTs == 0 || ts > _nextConfigUpdateTs)
{
_nextConfigUpdateTs = ts + _intervalConfig * 1000;
updateConfig();
}
if(_nextLockAction != (Nuki::LockAction)0xff)
{
_nukiBle.lockAction(_nextLockAction, 0, 0);
@@ -136,6 +142,12 @@ void NukiWrapper::updateBatteryState()
_network->publishBatteryReport(_batteryReport);
}
void NukiWrapper::updateConfig()
{
readConfig();
_network->publishConfig(_nukiConfig);
}
Nuki::LockAction NukiWrapper::lockActionToEnum(const char *str)
{
if(strcmp(str, "unlock") == 0) return Nuki::LockAction::Unlock;
@@ -177,3 +189,11 @@ void NukiWrapper::notify(Nuki::EventType eventType)
_statusUpdated = true;
}
}
void NukiWrapper::readConfig()
{
Serial.print(F("Reading config. Result: "));
Nuki::CmdResult result = _nukiBle.requestConfig(&_nukiConfig);
_nukiConfigValid = result == Nuki::CmdResult::Success;
Serial.println(result);
}

View File

@@ -26,6 +26,9 @@ private:
void updateKeyTurnerState();
void updateBatteryState();
void updateConfig();
void readConfig();
Nuki::LockAction lockActionToEnum(const char* str); // char array at least 14 characters
@@ -36,6 +39,7 @@ private:
Preferences* _preferences;
int _intervalLockstate = 0; // seconds
int _intervalBattery = 0; // seconds
int _intervalConfig = 60 * 60; // seconds
Nuki::KeyTurnerState _lastKeyTurnerState;
Nuki::KeyTurnerState _keyTurnerState;
@@ -43,10 +47,14 @@ private:
Nuki::BatteryReport _batteryReport;
Nuki::BatteryReport _lastBatteryReport;
Nuki::Config _nukiConfig = {0};
bool _nukiConfigValid = false;
bool _paired = false;
bool _statusUpdated = false;
unsigned long _nextLockStateUpdateTs = 0;
unsigned long _nextBatteryReportTs = 0;
unsigned long _nextConfigUpdateTs = 0;
unsigned long _nextPairTs = 0;
Nuki::LockAction _nextLockAction = (Nuki::LockAction)0xff;
};