minor refactoring of network code
This commit is contained in:
@@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
|
|
||||||
Network* Network::_inst = nullptr;
|
Network* Network::_inst = nullptr;
|
||||||
|
unsigned long Network::_ignoreSubscriptionsTs = 0;
|
||||||
|
|
||||||
RTC_NOINIT_ATTR char WiFi_fallbackDetect[14];
|
RTC_NOINIT_ATTR char WiFi_fallbackDetect[14];
|
||||||
|
|
||||||
@@ -359,6 +360,7 @@ bool Network::reconnect()
|
|||||||
_mqttConnectionState = 1;
|
_mqttConnectionState = 1;
|
||||||
delay(100);
|
delay(100);
|
||||||
|
|
||||||
|
_ignoreSubscriptionsTs = millis() + 2000;
|
||||||
_device->mqttOnMessage(Network::onMqttDataReceivedCallback);
|
_device->mqttOnMessage(Network::onMqttDataReceivedCallback);
|
||||||
for(const String& topic : _subscribedTopics)
|
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());
|
_device->mqttPublish(it.first.c_str(), MQTT_QOS_LEVEL, true, it.second.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
delay(1000);
|
|
||||||
_mqttConnectionState = 2;
|
_mqttConnectionState = 2;
|
||||||
for(const auto& callback : _reconnectedCallbacks)
|
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)
|
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};
|
uint8_t value[50] = {0};
|
||||||
size_t l = min(len, sizeof(value)-1);
|
size_t l = min(len, sizeof(value)-1);
|
||||||
|
|
||||||
|
|||||||
@@ -91,6 +91,7 @@ private:
|
|||||||
unsigned long _lastConnectedTs = 0;
|
unsigned long _lastConnectedTs = 0;
|
||||||
unsigned long _lastMaintenanceTs = 0;
|
unsigned long _lastMaintenanceTs = 0;
|
||||||
unsigned long _lastRssiTs = 0;
|
unsigned long _lastRssiTs = 0;
|
||||||
|
static unsigned long _ignoreSubscriptionsTs;
|
||||||
long _rssiPublishInterval = 0;
|
long _rssiPublishInterval = 0;
|
||||||
std::function<void()> _keepAliveCallback = nullptr;
|
std::function<void()> _keepAliveCallback = nullptr;
|
||||||
std::vector<std::function<void()>> _reconnectedCallbacks;
|
std::vector<std::function<void()>> _reconnectedCallbacks;
|
||||||
|
|||||||
@@ -90,16 +90,14 @@ void NetworkLock::onMqttDataReceived(const char* topic, byte* payload, const uns
|
|||||||
{
|
{
|
||||||
char* value = (char*)payload;
|
char* value = (char*)payload;
|
||||||
|
|
||||||
bool processActions = _network->mqttConnectionState() >= 2;
|
if(comparePrefixedPath(topic, mqtt_topic_reset) && strcmp(value, "1") == 0)
|
||||||
|
|
||||||
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);
|
||||||
restartEsp(RestartReason::RequestedViaMqtt);
|
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;
|
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");
|
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)
|
if(_keypadCommandReceivedReceivedCallback != nullptr)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -88,9 +88,7 @@ void NetworkOpener::onMqttDataReceived(const char* topic, byte* payload, const u
|
|||||||
{
|
{
|
||||||
char* value = (char*)payload;
|
char* value = (char*)payload;
|
||||||
|
|
||||||
bool processActions = _network->mqttConnectionState() >= 2;
|
if(comparePrefixedPath(topic, mqtt_topic_lock_action))
|
||||||
|
|
||||||
if(processActions && comparePrefixedPath(topic, mqtt_topic_lock_action))
|
|
||||||
{
|
{
|
||||||
if(strcmp((char*)payload, "") == 0 || strcmp(value, "--") == 0 || strcmp(value, "ack") == 0 || strcmp(value, "unknown_action") == 0) return;
|
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");
|
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)
|
if(_keypadCommandReceivedReceivedCallback != nullptr)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -780,7 +780,7 @@ void WebCfgServer::buildInfoHtml(String &response)
|
|||||||
|
|
||||||
response.concat("Restart reason FW: ");
|
response.concat("Restart reason FW: ");
|
||||||
response.concat(getRestartReason());
|
response.concat(getRestartReason());
|
||||||
response.concat("\n");
|
response.concat( "\n");
|
||||||
|
|
||||||
response.concat("Restart reason ESP: ");
|
response.concat("Restart reason ESP: ");
|
||||||
response.concat(getEspRestartReason());
|
response.concat(getEspRestartReason());
|
||||||
|
|||||||
2
main.cpp
2
main.cpp
@@ -154,6 +154,7 @@ void setup()
|
|||||||
{
|
{
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
Log = &Serial;
|
Log = &Serial;
|
||||||
|
initializeRestartReason();
|
||||||
|
|
||||||
Log->print(F("NUKI Hub version ")); Log->println(NUKI_HUB_VERSION);
|
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);
|
const String mqttLockPath = preferences->getString(preference_mqtt_lock_path);
|
||||||
network = new Network(preferences, mqttLockPath);
|
network = new Network(preferences, mqttLockPath);
|
||||||
network->initialize();
|
network->initialize();
|
||||||
|
|
||||||
networkLock = new NetworkLock(network, preferences);
|
networkLock = new NetworkLock(network, preferences);
|
||||||
networkLock->initialize();
|
networkLock->initialize();
|
||||||
|
|
||||||
|
|||||||
@@ -143,11 +143,11 @@ void W5500Device::resetDevice()
|
|||||||
Log->println(F("Resetting network hardware."));
|
Log->println(F("Resetting network hardware."));
|
||||||
pinMode(_resetPin, OUTPUT);
|
pinMode(_resetPin, OUTPUT);
|
||||||
digitalWrite(_resetPin, HIGH);
|
digitalWrite(_resetPin, HIGH);
|
||||||
delay(250);
|
delay(50);
|
||||||
digitalWrite(_resetPin, LOW);
|
digitalWrite(_resetPin, LOW);
|
||||||
delay(50);
|
delay(50);
|
||||||
digitalWrite(_resetPin, HIGH);
|
digitalWrite(_resetPin, HIGH);
|
||||||
delay(1500);
|
delay(50);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user