Fix hangup on no internet

This commit is contained in:
iranl
2025-09-10 22:11:15 +02:00
parent 34d56c12a8
commit 20eb79ec30
16 changed files with 1272 additions and 21 deletions

View File

@@ -5,7 +5,7 @@
#define NUKI_HUB_VERSION "9.12"
#define NUKI_HUB_VERSION_INT (uint32_t)912
#define NUKI_HUB_BUILD "unknownbuildnr"
#define NUKI_HUB_DATE "2025-09-09"
#define NUKI_HUB_DATE "2025-09-10"
#define GITHUB_LATEST_RELEASE_URL (char*)"https://github.com/technyon/nuki_hub/releases/latest"
#define GITHUB_OTA_MANIFEST_URL (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/manifest.json"

View File

@@ -12,6 +12,7 @@
#include "networkDevices/EthernetDevice.h"
#include "hal/wdt_hal.h"
#include "esp_mac.h"
#include <ESP32Ping.h>
NukiNetwork* NukiNetwork::_inst = nullptr;
@@ -154,6 +155,11 @@ bool NukiNetwork::isConnected()
return _device->isConnected();
}
bool NukiNetwork::isInternetConnected()
{
return _hasInternet;
}
bool NukiNetwork::mqttConnected()
{
#ifndef NUKI_HUB_UPDATER
@@ -297,7 +303,8 @@ void NukiNetwork::readSettings()
_checkUpdates = _preferences->getBool(preference_check_updates, false);
_rssiPublishInterval = _preferences->getInt(preference_rssi_publish_interval, 0) * 1000;
_retainGpio = _preferences->getBool(preference_retain_gpio, false);
_haEnabled = _preferences->getBool(preference_mqtt_hass_enabled, false);
if(_rssiPublishInterval == 0)
{
_rssiPublishInterval = 60000;
@@ -469,6 +476,8 @@ bool NukiNetwork::update()
Log->print("IP: ");
Log->println(_device->localIP());
_firstDisconnected = true;
checkInternetConnectivity();
}
if(!_logIp && _device->isConnected() && !_device->mqttConnected() )
@@ -575,7 +584,7 @@ bool NukiNetwork::update()
_lastMaintenanceTs = ts;
}
if(_checkUpdates)
if(_checkUpdates && (!_haEnabled || (_haEnabled && _haSetupDone)) && _hasInternet)
{
if(_lastUpdateCheckTs == 0 || (ts - _lastUpdateCheckTs) > 86400000)
{
@@ -665,6 +674,11 @@ bool NukiNetwork::update()
return true;
}
void NukiNetwork::checkInternetConnectivity()
{
_hasInternet = Ping.ping("github.com", 3);
}
void NukiNetwork::onMqttConnect(const bool &sessionPresent)
{
_connectReplyReceived = true;
@@ -849,6 +863,7 @@ bool NukiNetwork::reconnect(bool force)
if(_preferences->getBool(preference_mqtt_hass_enabled, false))
{
setupHASS(0, 0, {0}, {0}, {0}, false, false);
_haSetupDone = true;
}
initTopic(_maintenancePathPrefix, mqtt_topic_reset, "0");
@@ -1029,7 +1044,7 @@ void NukiNetwork::onMqttDataReceived(const char* topic, byte* payload, const uns
vTaskDelay(200 / portTICK_PERIOD_MS);
restartEsp(RestartReason::RequestedViaMqtt);
}
else if(comparePrefixedPath(topic, mqtt_topic_update) && strcmp(data, "1") == 0 && _preferences->getBool(preference_update_from_mqtt, false) && !mqttRecentlyConnected())
else if(comparePrefixedPath(topic, mqtt_topic_update) && strcmp(data, "1") == 0 && _preferences->getBool(preference_update_from_mqtt, false) && !mqttRecentlyConnected() && _hasInternet)
{
Log->println("Update requested via MQTT.");

View File

@@ -29,6 +29,7 @@ public:
void scan(bool passive = false, bool async = true);
bool isApOpen();
bool isConnected();
bool isInternetConnected();
bool mqttConnected();
bool wifiConnected();
void clearWifiFallback();
@@ -114,6 +115,7 @@ private:
NetworkDeviceType _networkDeviceType = (NetworkDeviceType)-1;
bool _firstBootAfterDeviceChange = false;
bool _webEnabled = true;
bool _hasInternet = false;
#ifndef NUKI_HUB_UPDATER
static void onMqttDataReceivedCallback(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total);
@@ -126,6 +128,7 @@ private:
bool comparePrefixedPath(const char* fullPath, const char* subPath);
void buildMqttPath(const char *path, char *outPath);
void buildMqttPath(char* outPath, std::initializer_list<const char*> paths);
void checkInternetConnectivity();
const char* _lastWillPayload = "offline";
char _mqttConnectionStateTopic[211] = {0};
@@ -159,6 +162,8 @@ private:
bool _publishDebugInfo = false;
bool _logIp = true;
bool _retainGpio = false;
bool _haEnabled = false;
bool _haSetupDone = false;
std::vector<String> _subscribedTopics;
std::map<String, String> _initTopics;
int64_t _lastConnectedTs = 0;

View File

@@ -1813,33 +1813,36 @@ esp_err_t WebCfgServer::buildOtaHtml(PsychicRequest *request, PsychicResponse* r
#ifndef NUKI_HUB_UPDATER
bool manifestSuccess = false;
JsonDocument doc;
NetworkClientSecure *clientOTAUpdate = new NetworkClientSecure;
if (clientOTAUpdate)
if(_network->isInternetConnected())
{
clientOTAUpdate->setCACertBundle(x509_crt_imported_bundle_bin_start, x509_crt_imported_bundle_bin_end - x509_crt_imported_bundle_bin_start);
NetworkClientSecure *clientOTAUpdate = new NetworkClientSecure;
if (clientOTAUpdate)
{
HTTPClient httpsOTAClient;
httpsOTAClient.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
httpsOTAClient.setTimeout(2500);
httpsOTAClient.useHTTP10(true);
if (httpsOTAClient.begin(*clientOTAUpdate, GITHUB_OTA_MANIFEST_URL))
clientOTAUpdate->setCACertBundle(x509_crt_imported_bundle_bin_start, x509_crt_imported_bundle_bin_end - x509_crt_imported_bundle_bin_start);
{
int httpResponseCodeOTA = httpsOTAClient.GET();
HTTPClient httpsOTAClient;
httpsOTAClient.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
httpsOTAClient.setTimeout(2500);
httpsOTAClient.useHTTP10(true);
if (httpResponseCodeOTA == HTTP_CODE_OK || httpResponseCodeOTA == HTTP_CODE_MOVED_PERMANENTLY)
if (httpsOTAClient.begin(*clientOTAUpdate, GITHUB_OTA_MANIFEST_URL))
{
DeserializationError jsonError = deserializeJson(doc, httpsOTAClient.getStream());
if (!jsonError)
int httpResponseCodeOTA = httpsOTAClient.GET();
if (httpResponseCodeOTA == HTTP_CODE_OK || httpResponseCodeOTA == HTTP_CODE_MOVED_PERMANENTLY)
{
manifestSuccess = true;
DeserializationError jsonError = deserializeJson(doc, httpsOTAClient.getStream());
if (!jsonError)
{
manifestSuccess = true;
}
}
httpsOTAClient.end();
}
httpsOTAClient.end();
}
delete clientOTAUpdate;
}
delete clientOTAUpdate;
}
response.print("<div id=\"msgdiv\" style=\"visibility:hidden\">Initiating Over-the-air update. This will take about two minutes, please be patient.<br>You will be forwarded automatically when the update is complete.</div>");
@@ -6365,6 +6368,9 @@ esp_err_t WebCfgServer::buildInfoHtml(PsychicRequest *request, PsychicResponse*
response.print(_network->isConnected() ? "Yes" : "No");
if(_network->isConnected())
{
response.print("\nInternet connected: ");
response.print(_network->isInternetConnected() ? "Yes" : "No");
response.print("\nIP Address: ");
response.print(_network->localIP());