publish first config values via mqtt
This commit is contained in:
@@ -14,4 +14,8 @@
|
|||||||
#define mqtt_topic_door_sensor_state "/lock/doorSensorState"
|
#define mqtt_topic_door_sensor_state "/lock/doorSensorState"
|
||||||
#define mqtt_topic_lockstate_action "/lock/action"
|
#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"
|
#define mqtt_topic_presence "/presence/devices"
|
||||||
|
|||||||
38
Network.cpp
38
Network.cpp
@@ -128,10 +128,10 @@ bool Network::reconnect()
|
|||||||
Serial.println(F("MQTT connected"));
|
Serial.println(F("MQTT connected"));
|
||||||
_mqttConnected = true;
|
_mqttConnected = true;
|
||||||
delay(200);
|
delay(200);
|
||||||
char path[200] = {0};
|
subscribe(mqtt_topic_lockstate_action);
|
||||||
buildMqttPath(mqtt_topic_lockstate_action, path);
|
subscribe(mqtt_topic_config_button_enabled);
|
||||||
// ... and resubscribe
|
subscribe(mqtt_topic_config_led_enabled);
|
||||||
_mqttClient.subscribe(path);
|
subscribe(mqtt_topic_config_led_brightness);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -177,7 +177,7 @@ void Network::onMqttDataReceivedCallback(char *topic, byte *payload, unsigned in
|
|||||||
|
|
||||||
void Network::onMqttDataReceived(char *&topic, byte *&payload, unsigned int &length)
|
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);
|
size_t l = min(length, sizeof(value)-1);
|
||||||
|
|
||||||
for(int i=0; i<l; i++)
|
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[i] = payload[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
value[l] = 0;
|
if(comparePrefixedPath(topic, mqtt_topic_lockstate_action))
|
||||||
|
|
||||||
char path[200] = {0};
|
|
||||||
buildMqttPath(mqtt_topic_lockstate_action, path);
|
|
||||||
|
|
||||||
if(strcmp(topic, path) == 0)
|
|
||||||
{
|
{
|
||||||
if(strcmp(value, "") == 0) return;
|
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
|
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)
|
void Network::publishPresenceDetection(char *csv)
|
||||||
{
|
{
|
||||||
_presenceCsv = csv;
|
_presenceCsv = csv;
|
||||||
@@ -330,9 +332,23 @@ void Network::buildMqttPath(const char* path, char* outPath)
|
|||||||
outPath[i+1] = 0x00;
|
outPath[i+1] = 0x00;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Network::subscribe(const char *path)
|
||||||
|
{
|
||||||
|
char prefixedPath[500];
|
||||||
|
buildMqttPath(path, prefixedPath);
|
||||||
|
_mqttClient.subscribe(prefixedPath);
|
||||||
|
}
|
||||||
|
|
||||||
void Network::restartAndConfigureWifi()
|
void Network::restartAndConfigureWifi()
|
||||||
{
|
{
|
||||||
_cookie.set();
|
_cookie.set();
|
||||||
delay(200);
|
delay(200);
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Network::comparePrefixedPath(const char *fullPath, const char *subPath)
|
||||||
|
{
|
||||||
|
char prefixedPath[500];
|
||||||
|
buildMqttPath(subPath, prefixedPath);
|
||||||
|
return strcmp(fullPath, prefixedPath) == 0;
|
||||||
|
}
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ public:
|
|||||||
|
|
||||||
void publishKeyTurnerState(const Nuki::KeyTurnerState& keyTurnerState, const Nuki::KeyTurnerState& lastKeyTurnerState);
|
void publishKeyTurnerState(const Nuki::KeyTurnerState& keyTurnerState, const Nuki::KeyTurnerState& lastKeyTurnerState);
|
||||||
void publishBatteryReport(const Nuki::BatteryReport& batteryReport);
|
void publishBatteryReport(const Nuki::BatteryReport& batteryReport);
|
||||||
|
void publishConfig(const Nuki::Config& config);
|
||||||
void publishPresenceDetection(char* csv);
|
void publishPresenceDetection(char* csv);
|
||||||
|
|
||||||
void setLockActionReceived(void (*lockActionReceivedCallback)(const char* value));
|
void setLockActionReceived(void (*lockActionReceivedCallback)(const char* value));
|
||||||
@@ -28,6 +29,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
static void onMqttDataReceivedCallback(char* topic, byte* payload, unsigned int length);
|
static void onMqttDataReceivedCallback(char* topic, byte* payload, unsigned int length);
|
||||||
void onMqttDataReceived(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 publishFloat(const char* topic, const float value, const uint8_t precision = 2);
|
||||||
void publishInt(const char* topic, const int value);
|
void publishInt(const char* topic, const int value);
|
||||||
@@ -35,6 +37,7 @@ private:
|
|||||||
void publishString(const char* topic, const char* value);
|
void publishString(const char* topic, const char* value);
|
||||||
|
|
||||||
void buildMqttPath(const char* path, char* outPath);
|
void buildMqttPath(const char* path, char* outPath);
|
||||||
|
void subscribe(const char* path);
|
||||||
|
|
||||||
bool reconnect();
|
bool reconnect();
|
||||||
|
|
||||||
|
|||||||
@@ -93,6 +93,12 @@ void NukiWrapper::update()
|
|||||||
_nextBatteryReportTs = ts + _intervalBattery * 1000;
|
_nextBatteryReportTs = ts + _intervalBattery * 1000;
|
||||||
updateBatteryState();
|
updateBatteryState();
|
||||||
}
|
}
|
||||||
|
if(_nextConfigUpdateTs == 0 || ts > _nextConfigUpdateTs)
|
||||||
|
{
|
||||||
|
_nextConfigUpdateTs = ts + _intervalConfig * 1000;
|
||||||
|
updateConfig();
|
||||||
|
}
|
||||||
|
|
||||||
if(_nextLockAction != (Nuki::LockAction)0xff)
|
if(_nextLockAction != (Nuki::LockAction)0xff)
|
||||||
{
|
{
|
||||||
_nukiBle.lockAction(_nextLockAction, 0, 0);
|
_nukiBle.lockAction(_nextLockAction, 0, 0);
|
||||||
@@ -136,6 +142,12 @@ void NukiWrapper::updateBatteryState()
|
|||||||
_network->publishBatteryReport(_batteryReport);
|
_network->publishBatteryReport(_batteryReport);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NukiWrapper::updateConfig()
|
||||||
|
{
|
||||||
|
readConfig();
|
||||||
|
_network->publishConfig(_nukiConfig);
|
||||||
|
}
|
||||||
|
|
||||||
Nuki::LockAction NukiWrapper::lockActionToEnum(const char *str)
|
Nuki::LockAction NukiWrapper::lockActionToEnum(const char *str)
|
||||||
{
|
{
|
||||||
if(strcmp(str, "unlock") == 0) return Nuki::LockAction::Unlock;
|
if(strcmp(str, "unlock") == 0) return Nuki::LockAction::Unlock;
|
||||||
@@ -177,3 +189,11 @@ void NukiWrapper::notify(Nuki::EventType eventType)
|
|||||||
_statusUpdated = true;
|
_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);
|
||||||
|
}
|
||||||
|
|||||||
@@ -26,6 +26,9 @@ private:
|
|||||||
|
|
||||||
void updateKeyTurnerState();
|
void updateKeyTurnerState();
|
||||||
void updateBatteryState();
|
void updateBatteryState();
|
||||||
|
void updateConfig();
|
||||||
|
|
||||||
|
void readConfig();
|
||||||
|
|
||||||
Nuki::LockAction lockActionToEnum(const char* str); // char array at least 14 characters
|
Nuki::LockAction lockActionToEnum(const char* str); // char array at least 14 characters
|
||||||
|
|
||||||
@@ -36,6 +39,7 @@ private:
|
|||||||
Preferences* _preferences;
|
Preferences* _preferences;
|
||||||
int _intervalLockstate = 0; // seconds
|
int _intervalLockstate = 0; // seconds
|
||||||
int _intervalBattery = 0; // seconds
|
int _intervalBattery = 0; // seconds
|
||||||
|
int _intervalConfig = 60 * 60; // seconds
|
||||||
|
|
||||||
Nuki::KeyTurnerState _lastKeyTurnerState;
|
Nuki::KeyTurnerState _lastKeyTurnerState;
|
||||||
Nuki::KeyTurnerState _keyTurnerState;
|
Nuki::KeyTurnerState _keyTurnerState;
|
||||||
@@ -43,10 +47,14 @@ private:
|
|||||||
Nuki::BatteryReport _batteryReport;
|
Nuki::BatteryReport _batteryReport;
|
||||||
Nuki::BatteryReport _lastBatteryReport;
|
Nuki::BatteryReport _lastBatteryReport;
|
||||||
|
|
||||||
|
Nuki::Config _nukiConfig = {0};
|
||||||
|
bool _nukiConfigValid = false;
|
||||||
|
|
||||||
bool _paired = false;
|
bool _paired = false;
|
||||||
bool _statusUpdated = false;
|
bool _statusUpdated = false;
|
||||||
unsigned long _nextLockStateUpdateTs = 0;
|
unsigned long _nextLockStateUpdateTs = 0;
|
||||||
unsigned long _nextBatteryReportTs = 0;
|
unsigned long _nextBatteryReportTs = 0;
|
||||||
|
unsigned long _nextConfigUpdateTs = 0;
|
||||||
unsigned long _nextPairTs = 0;
|
unsigned long _nextPairTs = 0;
|
||||||
Nuki::LockAction _nextLockAction = (Nuki::LockAction)0xff;
|
Nuki::LockAction _nextLockAction = (Nuki::LockAction)0xff;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user