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 "configuration.h"
#include "main.h" #include "main.h"
#include "mesh/api/ethServerAPI.h" #include "mesh/api/ethServerAPI.h"
#if !MESHTASTIC_EXCLUDE_MQTT
#include "mqtt/MQTT.h"
#endif
#include "target_specific.h" #include "target_specific.h"
#include <RAK13800_W5100S.h> #include <RAK13800_W5100S.h>
#include <SPI.h> #include <SPI.h>
@ -72,12 +69,6 @@ static int32_t reconnectETH()
ethStartupComplete = true; 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 #ifndef DISABLE_NTP

View File

@ -7,9 +7,6 @@
#include "main.h" #include "main.h"
#include "mesh/api/WiFiServerAPI.h" #include "mesh/api/WiFiServerAPI.h"
#if !MESHTASTIC_EXCLUDE_MQTT
#include "mqtt/MQTT.h"
#endif
#include "target_specific.h" #include "target_specific.h"
#include <WiFi.h> #include <WiFi.h>
#include <WiFiUdp.h> #include <WiFiUdp.h>
@ -111,12 +108,6 @@ static void onNetworkConnected()
#endif #endif
APStartupComplete = true; 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() static int32_t reconnectWiFi()

View File

@ -412,36 +412,28 @@ void MQTT::reconnect()
const char *serverAddr = default_mqtt_address; const char *serverAddr = default_mqtt_address;
const char *mqttUsername = default_mqtt_username; const char *mqttUsername = default_mqtt_username;
const char *mqttPassword = default_mqtt_password; const char *mqttPassword = default_mqtt_password;
MQTTClient *clientConnection = mqttClient.get();
if (*moduleConfig.mqtt.address) { if (*moduleConfig.mqtt.address) {
serverAddr = moduleConfig.mqtt.address; serverAddr = moduleConfig.mqtt.address;
mqttUsername = moduleConfig.mqtt.username; mqttUsername = moduleConfig.mqtt.username;
mqttPassword = moduleConfig.mqtt.password; mqttPassword = moduleConfig.mqtt.password;
} }
#if HAS_WIFI && !defined(ARCH_PORTDUINO) #if HAS_WIFI && !defined(ARCH_PORTDUINO) && !defined(CONFIG_IDF_TARGET_ESP32C6)
#if !defined(CONFIG_IDF_TARGET_ESP32C6)
if (moduleConfig.mqtt.tls_enabled) { if (moduleConfig.mqtt.tls_enabled) {
// change default for encrypted to 8883 // change default for encrypted to 8883
try { try {
serverPort = 8883; serverPort = 8883;
wifiSecureClient.setInsecure(); wifiSecureClient.setInsecure();
pubSub.setClient(wifiSecureClient);
LOG_INFO("Use TLS-encrypted session"); LOG_INFO("Use TLS-encrypted session");
clientConnection = &wifiSecureClient;
} catch (const std::exception &e) { } catch (const std::exception &e) {
LOG_ERROR("MQTT ERROR: %s", e.what()); LOG_ERROR("MQTT ERROR: %s", e.what());
} }
} else { } else {
LOG_INFO("Use non-TLS-encrypted session"); LOG_INFO("Use non-TLS-encrypted session");
pubSub.setClient(*mqttClient);
} }
#else
pubSub.setClient(*mqttClient);
#endif #endif
#elif HAS_NETWORKING
pubSub.setClient(*mqttClient);
#endif
std::pair<String, uint16_t> hostAndPort = parseHostAndPort(serverAddr, serverPort); std::pair<String, uint16_t> hostAndPort = parseHostAndPort(serverAddr, serverPort);
serverAddr = hostAndPort.first.c_str(); serverAddr = hostAndPort.first.c_str();
serverPort = hostAndPort.second; 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, LOG_INFO("Connect directly to MQTT server %s, port: %d, username: %s, password: %s", serverAddr, serverPort, mqttUsername,
mqttPassword); mqttPassword);
pubSub.setClient(*clientConnection);
bool connected = pubSub.connect(owner.id, mqttUsername, mqttPassword); bool connected = pubSub.connect(owner.id, mqttUsername, mqttPassword);
if (connected) { if (connected) {
LOG_INFO("MQTT connected"); LOG_INFO("MQTT connected");
enabled = true; // Start running background process again enabled = true; // Start running background process again
runASAP = true; runASAP = true;
reconnectCount = 0; reconnectCount = 0;
isMqttServerAddressPrivate = isPrivateIpAddress(mqttClient->remoteIP()); isMqttServerAddressPrivate = isPrivateIpAddress(clientConnection->remoteIP());
publishNodeInfo(); publishNodeInfo();
sendSubscriptions(); 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); 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 isConnectedDirectly();
bool publish(const char *topic, const char *payload, bool retained); bool publish(const char *topic, const char *payload, bool retained);
@ -115,6 +111,10 @@ class MQTT : private concurrency::OSThread
*/ */
bool wantsLink() const; bool wantsLink() const;
/** Attempt to connect to server if necessary
*/
void reconnect();
/** Tell the server what subscriptions we want (based on channels.downlink_enabled) /** Tell the server what subscriptions we want (based on channels.downlink_enabled)
*/ */
void sendSubscriptions(); void sendSubscriptions();

View File

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