Compare commits
1 Commits
main
...
refactor/u
| Author | SHA1 | Date | |
|---|---|---|---|
| 9435a84438 |
278
src/MyMesh.cpp
278
src/MyMesh.cpp
@ -748,14 +748,9 @@ void MyMesh::begin(FILESYSTEM *fs) {
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef WITH_MQTT
|
||||
// Initialize MQTT gateway (WiFi)
|
||||
initMQTT();
|
||||
#endif
|
||||
|
||||
#ifdef WITH_ETHERNET
|
||||
// Initialize MQTT gateway (Ethernet)
|
||||
initEthernet();
|
||||
// Initialize network + MQTT gateway
|
||||
#ifdef WITH_NETWORK
|
||||
initNetwork();
|
||||
#endif
|
||||
|
||||
radio_set_params(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr);
|
||||
@ -1096,59 +1091,9 @@ void MyMesh::loop() {
|
||||
bridge.loop();
|
||||
#endif
|
||||
|
||||
#ifdef WITH_MQTT
|
||||
// Process WiFi, MQTT, and web server
|
||||
_wifi_mgr.loop();
|
||||
if (_mqtt_bridge) {
|
||||
_mqtt_bridge->loop();
|
||||
|
||||
// Periodic stats publish (every 30 seconds)
|
||||
if (_mqtt_bridge->isConnected() && millis() - _last_mqtt_stats > 30000) {
|
||||
_mqtt_bridge->publishStats(
|
||||
uptime_millis / 1000,
|
||||
radio_driver.getPacketsRecv(),
|
||||
radio_driver.getPacketsSent(),
|
||||
getTotalAirTime() / 1000,
|
||||
(int16_t)_radio->getNoiseFloor()
|
||||
);
|
||||
_last_mqtt_stats = millis();
|
||||
|
||||
// Update web config stats
|
||||
if (_web_config) {
|
||||
WebConfigStats stats;
|
||||
stats.uptime_secs = uptime_millis / 1000;
|
||||
stats.packets_rx = radio_driver.getPacketsRecv();
|
||||
stats.packets_tx = radio_driver.getPacketsSent();
|
||||
stats.air_time_secs = getTotalAirTime() / 1000;
|
||||
stats.noise_floor = (int16_t)_radio->getNoiseFloor();
|
||||
stats.last_rssi = (int16_t)radio_driver.getLastRSSI();
|
||||
stats.last_snr = radio_driver.getLastSNR();
|
||||
stats.tx_queue_len = _mgr->getOutboundCount(0xFFFFFFFF);
|
||||
stats.batt_mv = board.getBattMilliVolts();
|
||||
_web_config->updateStats(stats);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef WITH_ETHERNET
|
||||
// Process Ethernet and MQTT
|
||||
_eth_mgr.loop();
|
||||
if (_mqtt_bridge) {
|
||||
_mqtt_bridge->loop();
|
||||
|
||||
// Periodic stats publish (every 30 seconds)
|
||||
if (_mqtt_bridge->isConnected() && millis() - _last_mqtt_stats > 30000) {
|
||||
_mqtt_bridge->publishStats(
|
||||
uptime_millis / 1000,
|
||||
radio_driver.getPacketsRecv(),
|
||||
radio_driver.getPacketsSent(),
|
||||
getTotalAirTime() / 1000,
|
||||
(int16_t)_radio->getNoiseFloor()
|
||||
);
|
||||
_last_mqtt_stats = millis();
|
||||
}
|
||||
}
|
||||
// Unified network/MQTT processing
|
||||
#ifdef WITH_NETWORK
|
||||
loopNetwork();
|
||||
#endif
|
||||
|
||||
mesh::Mesh::loop();
|
||||
@ -1190,26 +1135,13 @@ void MyMesh::loop() {
|
||||
last_millis = now;
|
||||
}
|
||||
|
||||
#ifdef WITH_MQTT
|
||||
// MQTT initialization and helper methods
|
||||
// =============================================================================
|
||||
// Unified Network/MQTT Support (replaces separate WITH_MQTT and WITH_ETHERNET blocks)
|
||||
// =============================================================================
|
||||
#ifdef WITH_NETWORK
|
||||
|
||||
void MyMesh::initMQTT() {
|
||||
Serial.println("[MQTT] Initializing WiFi + MQTT gateway...");
|
||||
|
||||
// Configure WiFi
|
||||
memset(&_wifi_config, 0, sizeof(_wifi_config));
|
||||
#ifdef MQTT_WIFI_SSID
|
||||
strncpy(_wifi_config.ssid, MQTT_WIFI_SSID, sizeof(_wifi_config.ssid) - 1);
|
||||
#endif
|
||||
#ifdef MQTT_WIFI_PASS
|
||||
strncpy(_wifi_config.password, MQTT_WIFI_PASS, sizeof(_wifi_config.password) - 1);
|
||||
#endif
|
||||
strncpy(_wifi_config.ap_ssid, "MeshCore", sizeof(_wifi_config.ap_ssid) - 1);
|
||||
_wifi_config.connect_timeout_ms = 15000;
|
||||
_wifi_config.reconnect_interval_ms = 10000;
|
||||
_wifi_config.max_retries = 5;
|
||||
|
||||
// Configure MQTT
|
||||
void MyMesh::initNetwork() {
|
||||
// Configure MQTT (common for both WiFi and Ethernet)
|
||||
memset(&_mqtt_config, 0, sizeof(_mqtt_config));
|
||||
#ifdef MQTT_BROKER
|
||||
strncpy(_mqtt_config.broker, MQTT_BROKER, sizeof(_mqtt_config.broker) - 1);
|
||||
@ -1233,82 +1165,39 @@ void MyMesh::initMQTT() {
|
||||
strncpy(_mqtt_config.topic_prefix, "meshcore/repeater", sizeof(_mqtt_config.topic_prefix) - 1);
|
||||
#endif
|
||||
_mqtt_config.enabled = 1;
|
||||
_mqtt_config.keepalive_secs = 60;
|
||||
_mqtt_config.publish_interval_ms = 100;
|
||||
|
||||
// Initialize the appropriate network transport
|
||||
#if defined(WITH_MQTT)
|
||||
// WiFi transport
|
||||
Serial.println("[NET] Initializing WiFi + MQTT gateway...");
|
||||
|
||||
memset(&_wifi_config, 0, sizeof(_wifi_config));
|
||||
#ifdef MQTT_WIFI_SSID
|
||||
strncpy(_wifi_config.ssid, MQTT_WIFI_SSID, sizeof(_wifi_config.ssid) - 1);
|
||||
#endif
|
||||
#ifdef MQTT_WIFI_PASS
|
||||
strncpy(_wifi_config.password, MQTT_WIFI_PASS, sizeof(_wifi_config.password) - 1);
|
||||
#endif
|
||||
strncpy(_wifi_config.ap_ssid, "MeshCore", sizeof(_wifi_config.ap_ssid) - 1);
|
||||
_wifi_config.connect_timeout_ms = 15000;
|
||||
_wifi_config.reconnect_interval_ms = 10000;
|
||||
_wifi_config.max_retries = 5;
|
||||
|
||||
#ifdef MQTT_USE_TLS
|
||||
_mqtt_config.use_tls = MQTT_USE_TLS;
|
||||
#else
|
||||
_mqtt_config.use_tls = 0;
|
||||
#endif
|
||||
_mqtt_config.keepalive_secs = 60;
|
||||
_mqtt_config.publish_interval_ms = 100;
|
||||
|
||||
// Start WiFi
|
||||
_wifi_mgr.begin(_wifi_config);
|
||||
_network_mgr = &_wifi_mgr;
|
||||
|
||||
// Create MQTT bridge (pass pointer to INetworkManager interface)
|
||||
_mqtt_bridge = new MQTTBridge(&_wifi_mgr, _mgr, &rtc_clock);
|
||||
_mqtt_bridge->begin(_mqtt_config, self_id.pub_key);
|
||||
#elif defined(WITH_ETHERNET)
|
||||
// Ethernet transport
|
||||
Serial.println("[NET] Initializing Ethernet + MQTT gateway...");
|
||||
|
||||
// Create web config server
|
||||
_web_config = new WebConfig(_wifi_mgr, *_mqtt_bridge);
|
||||
_web_config->begin(&_wifi_config, &_mqtt_config, &_prefs);
|
||||
_web_config->setNodeInfo(self_id.pub_key, _prefs.node_name, FIRMWARE_VERSION);
|
||||
_web_config->setSaveCallback([]() {
|
||||
// Note: In a real implementation, we'd save to preferences file
|
||||
Serial.println("[WebConfig] Save callback triggered");
|
||||
});
|
||||
_web_config->setRebootCallback([]() {
|
||||
ESP.restart();
|
||||
});
|
||||
|
||||
_last_mqtt_stats = millis();
|
||||
|
||||
Serial.println("[MQTT] Gateway initialized");
|
||||
}
|
||||
|
||||
bool MyMesh::isMQTTConnected() const {
|
||||
return _mqtt_bridge && _mqtt_bridge->isConnected();
|
||||
}
|
||||
|
||||
bool MyMesh::isWiFiConnected() const {
|
||||
return _wifi_mgr.isConnected();
|
||||
}
|
||||
|
||||
void MyMesh::setMQTTEnabled(bool enable) {
|
||||
_mqtt_config.enabled = enable ? 1 : 0;
|
||||
if (_mqtt_bridge) {
|
||||
_mqtt_bridge->updateConfig(_mqtt_config);
|
||||
}
|
||||
}
|
||||
|
||||
const char* MyMesh::getMQTTStatus() const {
|
||||
if (!_mqtt_bridge) return "not initialized";
|
||||
if (!_mqtt_config.enabled) return "disabled";
|
||||
if (_mqtt_bridge->isConnected()) return "connected";
|
||||
return "disconnected";
|
||||
}
|
||||
|
||||
const char* MyMesh::getWiFiStatus() const {
|
||||
switch (_wifi_mgr.getState()) {
|
||||
case WiFiState::CONNECTED: return "connected";
|
||||
case WiFiState::CONNECTING: return "connecting";
|
||||
case WiFiState::AP_MODE: return "ap_mode";
|
||||
default: return "disconnected";
|
||||
}
|
||||
}
|
||||
|
||||
IPAddress MyMesh::getWiFiIP() const {
|
||||
return _wifi_mgr.getLocalIP();
|
||||
}
|
||||
|
||||
#endif // WITH_MQTT
|
||||
|
||||
#ifdef WITH_ETHERNET
|
||||
// Ethernet initialization and helper methods
|
||||
|
||||
void MyMesh::initEthernet() {
|
||||
Serial.println("[ETH] Initializing Ethernet + MQTT gateway...");
|
||||
|
||||
// Configure Ethernet (use defaults or build flags)
|
||||
memset(&_eth_config, 0, sizeof(_eth_config));
|
||||
#ifdef ETH_SPI_SCK
|
||||
_eth_config.spi_sck = ETH_SPI_SCK;
|
||||
@ -1334,52 +1223,68 @@ void MyMesh::initEthernet() {
|
||||
_eth_config.reconnect_interval_ms = 5000;
|
||||
_eth_config.use_dhcp = true;
|
||||
|
||||
// Configure MQTT
|
||||
memset(&_mqtt_config, 0, sizeof(_mqtt_config));
|
||||
#ifdef MQTT_BROKER
|
||||
strncpy(_mqtt_config.broker, MQTT_BROKER, sizeof(_mqtt_config.broker) - 1);
|
||||
#else
|
||||
strncpy(_mqtt_config.broker, "mqtt.example.com", sizeof(_mqtt_config.broker) - 1);
|
||||
#endif
|
||||
#ifdef MQTT_PORT
|
||||
_mqtt_config.port = MQTT_PORT;
|
||||
#else
|
||||
_mqtt_config.port = 1883;
|
||||
#endif
|
||||
#ifdef MQTT_USER
|
||||
strncpy(_mqtt_config.user, MQTT_USER, sizeof(_mqtt_config.user) - 1);
|
||||
#endif
|
||||
#ifdef MQTT_PASS
|
||||
strncpy(_mqtt_config.password, MQTT_PASS, sizeof(_mqtt_config.password) - 1);
|
||||
#endif
|
||||
#ifdef MQTT_TOPIC_PREFIX
|
||||
strncpy(_mqtt_config.topic_prefix, MQTT_TOPIC_PREFIX, sizeof(_mqtt_config.topic_prefix) - 1);
|
||||
#else
|
||||
strncpy(_mqtt_config.topic_prefix, "meshcore/repeater", sizeof(_mqtt_config.topic_prefix) - 1);
|
||||
#endif
|
||||
_mqtt_config.enabled = 1;
|
||||
_mqtt_config.use_tls = 0; // TLS not typically used with local Ethernet
|
||||
_mqtt_config.keepalive_secs = 60;
|
||||
_mqtt_config.publish_interval_ms = 100;
|
||||
|
||||
// Start Ethernet
|
||||
_eth_mgr.begin(_eth_config);
|
||||
_network_mgr = &_eth_mgr;
|
||||
#endif
|
||||
|
||||
// Create MQTT bridge (pass pointer to INetworkManager interface)
|
||||
_mqtt_bridge = new MQTTBridge(&_eth_mgr, _mgr, &rtc_clock);
|
||||
// Create MQTT bridge using the unified network manager interface
|
||||
_mqtt_bridge = new MQTTBridge(_network_mgr, _mgr, &rtc_clock);
|
||||
_mqtt_bridge->begin(_mqtt_config, self_id.pub_key);
|
||||
|
||||
_last_mqtt_stats = millis();
|
||||
// WiFi-specific: Create web config server
|
||||
#ifdef WITH_MQTT
|
||||
_web_config = new WebConfig(_wifi_mgr, *_mqtt_bridge);
|
||||
_web_config->begin(&_wifi_config, &_mqtt_config, &_prefs);
|
||||
_web_config->setNodeInfo(self_id.pub_key, _prefs.node_name, FIRMWARE_VERSION);
|
||||
_web_config->setSaveCallback([]() {
|
||||
Serial.println("[WebConfig] Save callback triggered");
|
||||
});
|
||||
_web_config->setRebootCallback([]() {
|
||||
ESP.restart();
|
||||
});
|
||||
#endif
|
||||
|
||||
Serial.println("[ETH] Gateway initialized");
|
||||
_last_mqtt_stats = millis();
|
||||
Serial.println("[NET] Gateway initialized");
|
||||
}
|
||||
|
||||
void MyMesh::loopNetwork() {
|
||||
// Process network manager (WiFi or Ethernet)
|
||||
#ifdef WITH_MQTT
|
||||
_wifi_mgr.loop();
|
||||
// Note: WebConfig uses AsyncWebServer, no loop() needed
|
||||
#endif
|
||||
#ifdef WITH_ETHERNET
|
||||
_eth_mgr.loop();
|
||||
#endif
|
||||
|
||||
// Process MQTT bridge
|
||||
if (_mqtt_bridge) {
|
||||
_mqtt_bridge->loop();
|
||||
|
||||
// Periodic stats publishing
|
||||
if (_mqtt_bridge->isConnected() && millis() - _last_mqtt_stats > 60000) {
|
||||
uint32_t up_secs = uptime_millis / 1000;
|
||||
uint32_t pkts_rx = radio_driver.getPacketsRecv();
|
||||
uint32_t pkts_tx = radio_driver.getPacketsSent();
|
||||
uint32_t air_time = getTotalAirTime() / 1000;
|
||||
int16_t noise = (int16_t)_radio->getNoiseFloor();
|
||||
_mqtt_bridge->publishStats(up_secs, pkts_rx, pkts_tx, air_time, noise);
|
||||
_last_mqtt_stats = millis();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Unified helper methods (no more duplication!)
|
||||
|
||||
bool MyMesh::isMQTTConnected() const {
|
||||
return _mqtt_bridge && _mqtt_bridge->isConnected();
|
||||
}
|
||||
|
||||
bool MyMesh::isEthernetConnected() const {
|
||||
return _eth_mgr.isConnected();
|
||||
bool MyMesh::isNetworkConnected() const {
|
||||
return _network_mgr && _network_mgr->isConnected();
|
||||
}
|
||||
|
||||
void MyMesh::setMQTTEnabled(bool enable) {
|
||||
@ -1396,17 +1301,20 @@ const char* MyMesh::getMQTTStatus() const {
|
||||
return "disconnected";
|
||||
}
|
||||
|
||||
const char* MyMesh::getEthernetStatus() const {
|
||||
switch (_eth_mgr.getState()) {
|
||||
const char* MyMesh::getNetworkStatus() const {
|
||||
if (!_network_mgr) return "not initialized";
|
||||
switch (_network_mgr->getState()) {
|
||||
case NetworkState::CONNECTED: return "connected";
|
||||
case NetworkState::CONNECTING: return "connecting";
|
||||
case NetworkState::AP_MODE: return "ap_mode";
|
||||
case NetworkState::ERROR: return "error";
|
||||
default: return "disconnected";
|
||||
}
|
||||
}
|
||||
|
||||
IPAddress MyMesh::getEthernetIP() const {
|
||||
return _eth_mgr.getLocalIP();
|
||||
IPAddress MyMesh::getNetworkIP() const {
|
||||
if (!_network_mgr) return IPAddress(0, 0, 0, 0);
|
||||
return _network_mgr->getLocalIP();
|
||||
}
|
||||
|
||||
#endif // WITH_ETHERNET
|
||||
#endif // WITH_NETWORK
|
||||
|
||||
58
src/MyMesh.h
58
src/MyMesh.h
@ -23,15 +23,20 @@
|
||||
#define WITH_BRIDGE
|
||||
#endif
|
||||
|
||||
// Unified network/MQTT support
|
||||
#if defined(WITH_MQTT) || defined(WITH_ETHERNET)
|
||||
#define WITH_NETWORK // Common flag for network-enabled builds
|
||||
#include "INetworkManager.h"
|
||||
#include "MQTTBridge.h"
|
||||
#endif
|
||||
|
||||
#ifdef WITH_MQTT
|
||||
#include "WiFiManager.h"
|
||||
#include "MQTTBridge.h"
|
||||
#include "WebConfig.h"
|
||||
#endif
|
||||
|
||||
#ifdef WITH_ETHERNET
|
||||
#include "EthernetManager.h"
|
||||
#include "MQTTBridge.h"
|
||||
#endif
|
||||
|
||||
#include <helpers/AdvertDataHelpers.h>
|
||||
@ -123,25 +128,26 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
||||
ESPNowBridge bridge;
|
||||
#endif
|
||||
|
||||
#ifdef WITH_MQTT
|
||||
WiFiManager _wifi_mgr;
|
||||
// Unified network/MQTT support
|
||||
#ifdef WITH_NETWORK
|
||||
INetworkManager* _network_mgr; // Points to either _wifi_mgr or _eth_mgr
|
||||
MQTTBridge* _mqtt_bridge;
|
||||
WebConfig* _web_config;
|
||||
WiFiConfig _wifi_config;
|
||||
MQTTConfig _mqtt_config;
|
||||
unsigned long _last_mqtt_stats;
|
||||
|
||||
void initMQTT();
|
||||
void initNetwork(); // Common initialization
|
||||
void loopNetwork(); // Common loop processing
|
||||
#endif
|
||||
|
||||
#ifdef WITH_MQTT
|
||||
WiFiManager _wifi_mgr;
|
||||
WebConfig* _web_config;
|
||||
WiFiConfig _wifi_config;
|
||||
#endif
|
||||
|
||||
#ifdef WITH_ETHERNET
|
||||
EthernetManager _eth_mgr;
|
||||
MQTTBridge* _mqtt_bridge;
|
||||
MQTTConfig _mqtt_config;
|
||||
EthernetConfig _eth_config;
|
||||
unsigned long _last_mqtt_stats;
|
||||
|
||||
void initEthernet();
|
||||
#endif
|
||||
|
||||
void putNeighbour(const mesh::Identity& id, uint32_t timestamp, float snr);
|
||||
@ -238,24 +244,28 @@ public:
|
||||
void handleCommand(uint32_t sender_timestamp, char* command, char* reply);
|
||||
void loop();
|
||||
|
||||
#ifdef WITH_MQTT
|
||||
// MQTT gateway methods (WiFi)
|
||||
// Unified network/MQTT gateway methods
|
||||
#ifdef WITH_NETWORK
|
||||
bool isMQTTConnected() const;
|
||||
bool isWiFiConnected() const;
|
||||
bool isNetworkConnected() const;
|
||||
void setMQTTEnabled(bool enable);
|
||||
const char* getMQTTStatus() const;
|
||||
const char* getWiFiStatus() const;
|
||||
IPAddress getWiFiIP() const;
|
||||
const char* getNetworkStatus() const;
|
||||
IPAddress getNetworkIP() const;
|
||||
#endif
|
||||
|
||||
#ifdef WITH_MQTT
|
||||
// WiFi-specific aliases for backward compatibility
|
||||
bool isWiFiConnected() const { return isNetworkConnected(); }
|
||||
const char* getWiFiStatus() const { return getNetworkStatus(); }
|
||||
IPAddress getWiFiIP() const { return getNetworkIP(); }
|
||||
#endif
|
||||
|
||||
#ifdef WITH_ETHERNET
|
||||
// MQTT gateway methods (Ethernet)
|
||||
bool isMQTTConnected() const;
|
||||
bool isEthernetConnected() const;
|
||||
void setMQTTEnabled(bool enable);
|
||||
const char* getMQTTStatus() const;
|
||||
const char* getEthernetStatus() const;
|
||||
IPAddress getEthernetIP() const;
|
||||
// Ethernet-specific aliases for backward compatibility
|
||||
bool isEthernetConnected() const { return isNetworkConnected(); }
|
||||
const char* getEthernetStatus() const { return getNetworkStatus(); }
|
||||
IPAddress getEthernetIP() const { return getNetworkIP(); }
|
||||
#endif
|
||||
|
||||
#if defined(WITH_BRIDGE)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user