minor refactoring of network code

This commit is contained in:
technyon
2023-02-18 19:14:38 +01:00
parent 9e92ea8cb3
commit d0fbaefd77
7 changed files with 18 additions and 13 deletions

View File

@@ -9,6 +9,7 @@
Network* Network::_inst = nullptr;
unsigned long Network::_ignoreSubscriptionsTs = 0;
RTC_NOINIT_ATTR char WiFi_fallbackDetect[14];
@@ -359,6 +360,7 @@ bool Network::reconnect()
_mqttConnectionState = 1;
delay(100);
_ignoreSubscriptionsTs = millis() + 2000;
_device->mqttOnMessage(Network::onMqttDataReceivedCallback);
for(const String& topic : _subscribedTopics)
{
@@ -372,7 +374,6 @@ bool Network::reconnect()
_device->mqttPublish(it.first.c_str(), MQTT_QOS_LEVEL, true, it.second.c_str());
}
}
delay(1000);
_mqttConnectionState = 2;
for(const auto& callback : _reconnectedCallbacks)
{
@@ -436,6 +437,11 @@ void Network::registerMqttReceiver(MqttReceiver* receiver)
void Network::onMqttDataReceivedCallback(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total)
{
if(millis() < _ignoreSubscriptionsTs)
{
return;
}
uint8_t value[50] = {0};
size_t l = min(len, sizeof(value)-1);

View File

@@ -91,6 +91,7 @@ private:
unsigned long _lastConnectedTs = 0;
unsigned long _lastMaintenanceTs = 0;
unsigned long _lastRssiTs = 0;
static unsigned long _ignoreSubscriptionsTs;
long _rssiPublishInterval = 0;
std::function<void()> _keepAliveCallback = nullptr;
std::vector<std::function<void()>> _reconnectedCallbacks;

View File

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

View File

@@ -88,9 +88,7 @@ void NetworkOpener::onMqttDataReceived(const char* topic, byte* payload, const u
{
char* value = (char*)payload;
bool processActions = _network->mqttConnectionState() >= 2;
if(processActions && comparePrefixedPath(topic, mqtt_topic_lock_action))
if(comparePrefixedPath(topic, mqtt_topic_lock_action))
{
if(strcmp((char*)payload, "") == 0 || strcmp(value, "--") == 0 || strcmp(value, "ack") == 0 || strcmp(value, "unknown_action") == 0) return;
@@ -104,7 +102,7 @@ void NetworkOpener::onMqttDataReceived(const char* topic, byte* payload, const u
publishString(mqtt_topic_lock_action, success ? "ack" : "unknown_action");
}
if(processActions && comparePrefixedPath(topic, mqtt_topic_keypad_command_action))
if(comparePrefixedPath(topic, mqtt_topic_keypad_command_action))
{
if(_keypadCommandReceivedReceivedCallback != nullptr)
{

View File

@@ -780,7 +780,7 @@ void WebCfgServer::buildInfoHtml(String &response)
response.concat("Restart reason FW: ");
response.concat(getRestartReason());
response.concat("\n");
response.concat( "\n");
response.concat("Restart reason ESP: ");
response.concat(getEspRestartReason());

View File

@@ -154,6 +154,7 @@ void setup()
{
Serial.begin(115200);
Log = &Serial;
initializeRestartReason();
Log->print(F("NUKI Hub version ")); Log->println(NUKI_HUB_VERSION);
@@ -170,6 +171,7 @@ void setup()
const String mqttLockPath = preferences->getString(preference_mqtt_lock_path);
network = new Network(preferences, mqttLockPath);
network->initialize();
networkLock = new NetworkLock(network, preferences);
networkLock->initialize();

View File

@@ -143,11 +143,11 @@ void W5500Device::resetDevice()
Log->println(F("Resetting network hardware."));
pinMode(_resetPin, OUTPUT);
digitalWrite(_resetPin, HIGH);
delay(250);
delay(50);
digitalWrite(_resetPin, LOW);
delay(50);
digitalWrite(_resetPin, HIGH);
delay(1500);
delay(50);
}