fix opener rssi publish interval, add further check to make sure commands aren't executed after reconnect

This commit is contained in:
technyon
2023-01-23 21:44:03 +01:00
parent 22d365d1db
commit 899cfc026f
8 changed files with 27 additions and 16 deletions

View File

@@ -1,4 +1,4 @@
#pragma once #pragma once
#define MQTT_QOS_LEVEL 0 #define MQTT_QOS_LEVEL 1
#define MQTT_CLEAN_SESSIONS true #define MQTT_CLEAN_SESSIONS false

View File

@@ -237,7 +237,7 @@ int Network::update()
bool Network::reconnect() bool Network::reconnect()
{ {
_mqttConnected = false; _mqttConnectionState = 0;
int port = _preferences->getInt(preference_mqtt_broker_port); int port = _preferences->getInt(preference_mqtt_broker_port);
while (!_device->mqttClient()->connected() && millis() > _nextReconnect) while (!_device->mqttClient()->connected() && millis() > _nextReconnect)
@@ -260,7 +260,7 @@ bool Network::reconnect()
if (success) if (success)
{ {
Log->println(F("MQTT connected")); Log->println(F("MQTT connected"));
_mqttConnected = true; _mqttConnectionState = 1;
delay(100); delay(100);
_device->mqttClient()->onMessage(Network::onMqttDataReceivedCallback); _device->mqttClient()->onMessage(Network::onMqttDataReceivedCallback);
for(const String& topic : _subscribedTopics) for(const String& topic : _subscribedTopics)
@@ -277,6 +277,12 @@ bool Network::reconnect()
_device->mqttClient()->endMessage(); _device->mqttClient()->endMessage();
} }
} }
for(int i=0; i<10; i++)
{
_device->mqttClient()->poll();
delay(100);
}
_mqttConnectionState = 2;
} }
else else
{ {
@@ -284,11 +290,11 @@ bool Network::reconnect()
Log->println(_device->mqttClient()->connectError()); Log->println(_device->mqttClient()->connectError());
_device->printError(); _device->printError();
_device->mqttClient()->stop(); _device->mqttClient()->stop();
_mqttConnected = false; _mqttConnectionState = 0;
_nextReconnect = millis() + 5000; _nextReconnect = millis() + 5000;
} }
} }
return _mqttConnected; return _mqttConnectionState > 0;
} }
void Network::subscribe(const char* prefix, const char *path) void Network::subscribe(const char* prefix, const char *path)
@@ -382,9 +388,9 @@ void Network::disableAutoRestarts()
_restartOnDisconnect = false; _restartOnDisconnect = false;
} }
bool Network::isMqttConnected() int Network::mqttConnectionState()
{ {
return _mqttConnected; return _mqttConnectionState;
} }

View File

@@ -44,7 +44,7 @@ public:
void publishPresenceDetection(char* csv); void publishPresenceDetection(char* csv);
MqttClient* mqttClient(); MqttClient* mqttClient();
bool isMqttConnected(); int mqttConnectionState(); // 0 = not connected; 1 = connected; 2 = connected and mqtt processed
const NetworkDeviceType networkDeviceType(); const NetworkDeviceType networkDeviceType();
@@ -60,7 +60,7 @@ private:
Preferences* _preferences; Preferences* _preferences;
String _hostname; String _hostname;
NetworkDevice* _device = nullptr; NetworkDevice* _device = nullptr;
bool _mqttConnected = false; int _mqttConnectionState = 0;
unsigned long _nextReconnect = 0; unsigned long _nextReconnect = 0;
char _mqttBrokerAddr[101] = {0}; char _mqttBrokerAddr[101] = {0};

View File

@@ -81,14 +81,16 @@ void NetworkLock::onMqttDataReceived(const char* topic, byte* payload, const uns
value[i] = payload[i]; value[i] = payload[i];
} }
if(comparePrefixedPath(topic, mqtt_topic_reset) && strcmp(value, "1") == 0) bool processActions = _network->mqttConnectionState() >= 2;
if(processActions && comparePrefixedPath(topic, mqtt_topic_reset) && strcmp(value, "1") == 0)
{ {
Log->println(F("Restart requested via MQTT.")); Log->println(F("Restart requested via MQTT."));
delay(200); delay(200);
ESP.restart(); ESP.restart();
} }
if(comparePrefixedPath(topic, mqtt_topic_lock_action)) if(processActions && comparePrefixedPath(topic, mqtt_topic_lock_action))
{ {
if(strcmp(value, "") == 0 || strcmp(value, "--") == 0 || strcmp(value, "ack") == 0 || strcmp(value, "unknown_action") == 0) return; if(strcmp(value, "") == 0 || strcmp(value, "--") == 0 || strcmp(value, "ack") == 0 || strcmp(value, "unknown_action") == 0) return;
@@ -102,7 +104,7 @@ void NetworkLock::onMqttDataReceived(const char* topic, byte* payload, const uns
publishString(mqtt_topic_lock_action, success ? "ack" : "unknown_action"); publishString(mqtt_topic_lock_action, success ? "ack" : "unknown_action");
} }
if(comparePrefixedPath(topic, mqtt_topic_keypad_command_action)) if(processActions && comparePrefixedPath(topic, mqtt_topic_keypad_command_action))
{ {
if(_keypadCommandReceivedReceivedCallback != nullptr) if(_keypadCommandReceivedReceivedCallback != nullptr)
{ {

View File

@@ -67,7 +67,9 @@ void NetworkOpener::onMqttDataReceived(const char* topic, byte* payload, const u
value[i] = payload[i]; value[i] = payload[i];
} }
if(comparePrefixedPath(topic, mqtt_topic_lock_action)) bool processActions = _network->mqttConnectionState() >= 2;
if(processActions && comparePrefixedPath(topic, mqtt_topic_lock_action))
{ {
if(strcmp(value, "") == 0 || strcmp(value, "--") == 0 || strcmp(value, "ack") == 0 || strcmp(value, "unknown_action") == 0) return; if(strcmp(value, "") == 0 || strcmp(value, "--") == 0 || strcmp(value, "ack") == 0 || strcmp(value, "unknown_action") == 0) return;

View File

@@ -133,6 +133,7 @@ void NukiOpenerWrapper::update()
setupHASS(); setupHASS();
} }
} }
if(_rssiPublishInterval > 0 && (_nextRssiTs == 0 || ts > _nextRssiTs)) if(_rssiPublishInterval > 0 && (_nextRssiTs == 0 || ts > _nextRssiTs))
{ {
_nextRssiTs = ts + _rssiPublishInterval; _nextRssiTs = ts + _rssiPublishInterval;

View File

@@ -77,7 +77,7 @@ private:
bool _paired = false; bool _paired = false;
bool _statusUpdated = false; bool _statusUpdated = false;
unsigned long _rssiPublishInterval = 0; long _rssiPublishInterval = 0;
unsigned long _nextLockStateUpdateTs = 0; unsigned long _nextLockStateUpdateTs = 0;
unsigned long _nextBatteryReportTs = 0; unsigned long _nextBatteryReportTs = 0;
unsigned long _nextConfigUpdateTs = 0; unsigned long _nextConfigUpdateTs = 0;

View File

@@ -460,7 +460,7 @@ void WebCfgServer::buildHtml(String& response)
response.concat("<table>"); response.concat("<table>");
printParameter(response, "Hostname", _hostname.c_str()); printParameter(response, "Hostname", _hostname.c_str());
printParameter(response, "MQTT Connected", _network->isMqttConnected() ? "Yes" : "No"); printParameter(response, "MQTT Connected", _network->mqttConnectionState() > 0 ? "Yes" : "No");
if(_nuki != nullptr) if(_nuki != nullptr)
{ {
char lockstateArr[20]; char lockstateArr[20];