add mqtt maintenance section

This commit is contained in:
technyon
2022-07-23 08:43:18 +02:00
parent f265fbe95a
commit 3ea014956e
6 changed files with 69 additions and 7 deletions

View File

@@ -48,6 +48,21 @@ void NetworkLock::initialize()
{
_network->subscribe(_mqttPath, topic);
}
_network->subscribe(_mqttPath, mqtt_topic_reset);
_network->initTopic(_mqttPath, mqtt_topic_reset, "0");
}
void NetworkLock::update()
{
unsigned long ts = millis();
if(_lastMaintenanceTs == 0 || (ts - _lastMaintenanceTs) > 30000)
{
_lastMaintenanceTs = ts;
publishULong(mqtt_topic_uptime, ts / 1000 / 60);
publishUInt(mqtt_topic_freeheap, esp_get_free_heap_size());
}
}
void NetworkLock::onMqttDataReceived(char *&topic, byte *&payload, unsigned int &length)
@@ -60,6 +75,13 @@ void NetworkLock::onMqttDataReceived(char *&topic, byte *&payload, unsigned int
value[i] = payload[i];
}
if(comparePrefixedPath(topic, mqtt_topic_reset) && strcmp(value, "1") == 0)
{
Serial.println(F("Restart requested via MQTT."));
delay(200);
ESP.restart();
}
if(comparePrefixedPath(topic, mqtt_topic_lock_action))
{
if(strcmp(value, "") == 0 || strcmp(value, "ack") == 0 || strcmp(value, "unknown_action") == 0) return;
@@ -241,3 +263,9 @@ bool NetworkLock::publishString(const char *topic, const char *value)
{
return _network->publishString(_mqttPath, topic, value);
}
void NetworkLock::publishULong(const char *topic, const unsigned long value)
{
return _network->publishULong(_mqttPath, topic, value);
}