This commit is contained in:
Mictronics 2025-02-15 10:35:31 +01:00
commit 4d4d183dae
5 changed files with 11 additions and 35 deletions

View File

@ -5,9 +5,6 @@
#include "configuration.h"
#include "main.h"
#include "mesh/api/ethServerAPI.h"
#if !MESHTASTIC_EXCLUDE_MQTT
#include "mqtt/MQTT.h"
#endif
#include "target_specific.h"
#include <RAK13800_W5100S.h>
#include <SPI.h>
@ -72,12 +69,6 @@ static int32_t reconnectETH()
ethStartupComplete = true;
}
#if !MESHTASTIC_EXCLUDE_MQTT
// FIXME this is kinda yucky, instead we should just have an observable for 'wifireconnected'
if (mqtt && !moduleConfig.mqtt.proxy_to_client_enabled && !mqtt->isConnectedDirectly()) {
mqtt->reconnect();
}
#endif
}
#ifndef DISABLE_NTP

View File

@ -7,9 +7,6 @@
#include "main.h"
#include "mesh/api/WiFiServerAPI.h"
#if !MESHTASTIC_EXCLUDE_MQTT
#include "mqtt/MQTT.h"
#endif
#include "target_specific.h"
#include <WiFi.h>
#include <WiFiUdp.h>
@ -111,12 +108,6 @@ static void onNetworkConnected()
#endif
APStartupComplete = true;
}
// FIXME this is kinda yucky, instead we should just have an observable for 'wifireconnected'
#ifndef MESHTASTIC_EXCLUDE_MQTT
if (mqtt)
mqtt->reconnect();
#endif
}
static int32_t reconnectWiFi()

View File

@ -412,36 +412,28 @@ void MQTT::reconnect()
const char *serverAddr = default_mqtt_address;
const char *mqttUsername = default_mqtt_username;
const char *mqttPassword = default_mqtt_password;
MQTTClient *clientConnection = mqttClient.get();
if (*moduleConfig.mqtt.address) {
serverAddr = moduleConfig.mqtt.address;
mqttUsername = moduleConfig.mqtt.username;
mqttPassword = moduleConfig.mqtt.password;
}
#if HAS_WIFI && !defined(ARCH_PORTDUINO)
#if !defined(CONFIG_IDF_TARGET_ESP32C6)
#if HAS_WIFI && !defined(ARCH_PORTDUINO) && !defined(CONFIG_IDF_TARGET_ESP32C6)
if (moduleConfig.mqtt.tls_enabled) {
// change default for encrypted to 8883
try {
serverPort = 8883;
wifiSecureClient.setInsecure();
pubSub.setClient(wifiSecureClient);
LOG_INFO("Use TLS-encrypted session");
clientConnection = &wifiSecureClient;
} catch (const std::exception &e) {
LOG_ERROR("MQTT ERROR: %s", e.what());
}
} else {
LOG_INFO("Use non-TLS-encrypted session");
pubSub.setClient(*mqttClient);
}
#else
pubSub.setClient(*mqttClient);
#endif
#elif HAS_NETWORKING
pubSub.setClient(*mqttClient);
#endif
std::pair<String, uint16_t> hostAndPort = parseHostAndPort(serverAddr, serverPort);
serverAddr = hostAndPort.first.c_str();
serverPort = hostAndPort.second;
@ -451,13 +443,14 @@ void MQTT::reconnect()
LOG_INFO("Connect directly to MQTT server %s, port: %d, username: %s, password: %s", serverAddr, serverPort, mqttUsername,
mqttPassword);
pubSub.setClient(*clientConnection);
bool connected = pubSub.connect(owner.id, mqttUsername, mqttPassword);
if (connected) {
LOG_INFO("MQTT connected");
enabled = true; // Start running background process again
runASAP = true;
reconnectCount = 0;
isMqttServerAddressPrivate = isPrivateIpAddress(mqttClient->remoteIP());
isMqttServerAddressPrivate = isPrivateIpAddress(clientConnection->remoteIP());
publishNodeInfo();
sendSubscriptions();

View File

@ -47,10 +47,6 @@ class MQTT : private concurrency::OSThread
*/
void onSend(const meshtastic_MeshPacket &mp_encrypted, const meshtastic_MeshPacket &mp_decoded, ChannelIndex chIndex);
/** Attempt to connect to server if necessary
*/
void reconnect();
bool isConnectedDirectly();
bool publish(const char *topic, const char *payload, bool retained);
@ -115,6 +111,10 @@ class MQTT : private concurrency::OSThread
*/
bool wantsLink() const;
/** Attempt to connect to server if necessary
*/
void reconnect();
/** Tell the server what subscriptions we want (based on channels.downlink_enabled)
*/
void sendSubscriptions();

View File

@ -242,6 +242,7 @@ class MQTTUnitTest : public MQTT
mqttClient.release();
delete pubsub;
}
using MQTT::reconnect;
int queueSize() { return mqttQueue.numUsed(); }
void reportToMap(std::optional<uint32_t> precision = std::nullopt)
{
@ -488,7 +489,7 @@ void test_reconnectProxyDoesNotReconnectMqtt(void)
moduleConfig.mqtt.proxy_to_client_enabled = true;
MQTTUnitTest::restart();
mqtt->reconnect();
unitTest->reconnect();
TEST_ASSERT_FALSE(pubsub->connected_);
}