128 lines
4.8 KiB
C++
128 lines
4.8 KiB
C++
/*
|
|
* ═══════════════════════════════════════════════════════════════════════════════════
|
|
* COMMUNICATIONROUTER.HPP - Multi-Protocol Communication Router v4.0
|
|
* ═══════════════════════════════════════════════════════════════════════════════════
|
|
*
|
|
* 📡 THE COMMUNICATION ROUTER OF VESPER 📡
|
|
*
|
|
* Routes messages between protocols and command handlers:
|
|
* • MQTTAsyncClient: AsyncMqttClient for non-blocking MQTT
|
|
* • WebSocketServer: Multi-client WebSocket management
|
|
* • CommandHandler: Unified command processing
|
|
* • ResponseBuilder: Structured response generation
|
|
*
|
|
* 🏗️ ARCHITECTURE:
|
|
* • Message routing between protocols and handlers
|
|
* • MQTT on dedicated RTOS task (Core 0)
|
|
* • Unified command processing
|
|
* • Thread-safe message routing
|
|
*
|
|
* 📡 SUPPORTED PROTOCOLS:
|
|
* • MQTT: AsyncMqttClient for reliable async connectivity
|
|
* • WebSocket: Real-time multi-client web interface
|
|
* • UDP Discovery: Auto-discovery service
|
|
*
|
|
* 📋 VERSION: 5.0 (AsyncMqttClient)
|
|
* 📅 DATE: 2025-10-01
|
|
* 👨💻 AUTHOR: Advanced Bell Systems
|
|
* ═══════════════════════════════════════════════════════════════════════════════════
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <Arduino.h>
|
|
#include <ESPAsyncWebServer.h>
|
|
#include <AsyncUDP.h>
|
|
#include <ArduinoJson.h>
|
|
#include "../MQTTAsyncClient/MQTTAsyncClient.hpp"
|
|
#include "../WebSocketServer/WebSocketServer.hpp"
|
|
#include "../CommandHandler/CommandHandler.hpp"
|
|
#include "../ResponseBuilder/ResponseBuilder.hpp"
|
|
#include "../HTTPRequestHandler/HTTPRequestHandler.hpp"
|
|
#include "../../ClientManager/ClientManager.hpp"
|
|
#include "../../SettingsWebServer/SettingsWebServer.hpp"
|
|
|
|
class ConfigManager;
|
|
class OTAManager;
|
|
class Player;
|
|
class FileManager;
|
|
class Timekeeper;
|
|
class Networking;
|
|
class FirmwareValidator;
|
|
class Telemetry;
|
|
|
|
class CommunicationRouter {
|
|
public:
|
|
explicit CommunicationRouter(ConfigManager& configManager,
|
|
OTAManager& otaManager,
|
|
Networking& networking,
|
|
AsyncWebServer& server,
|
|
AsyncWebSocket& webSocket,
|
|
AsyncUDP& udp);
|
|
|
|
~CommunicationRouter();
|
|
|
|
void begin();
|
|
void setPlayerReference(Player* player);
|
|
void setFileManagerReference(FileManager* fm);
|
|
void setTimeKeeperReference(Timekeeper* tk);
|
|
void setFirmwareValidatorReference(FirmwareValidator* fv);
|
|
void setTelemetryReference(Telemetry* telemetry);
|
|
void setupUdpDiscovery();
|
|
|
|
// Status methods
|
|
bool isMqttConnected() const;
|
|
bool hasActiveWebSocketClients() const;
|
|
size_t getWebSocketClientCount() const;
|
|
bool isHealthy() const;
|
|
|
|
// Component accessors
|
|
MQTTAsyncClient& getMQTTClient() { return _mqttClient; }
|
|
|
|
// Broadcast methods
|
|
void broadcastStatus(const String& statusMessage);
|
|
void broadcastStatus(const JsonDocument& statusJson);
|
|
void broadcastToMasterClients(const String& message);
|
|
void broadcastToSecondaryClients(const String& message);
|
|
void broadcastToAllWebSocketClients(const String& message);
|
|
void broadcastToAllWebSocketClients(const JsonDocument& message);
|
|
void publishToMqtt(const String& data);
|
|
|
|
// Bell overload notification
|
|
void sendBellOverloadNotification(const std::vector<uint8_t>& bellNumbers,
|
|
const std::vector<uint16_t>& bellLoads,
|
|
const String& severity);
|
|
|
|
// Network connection callbacks
|
|
void onNetworkConnected();
|
|
void onNetworkDisconnected();
|
|
|
|
private:
|
|
// Dependencies
|
|
ConfigManager& _configManager;
|
|
OTAManager& _otaManager;
|
|
Networking& _networking;
|
|
AsyncWebServer& _server;
|
|
AsyncWebSocket& _webSocket;
|
|
AsyncUDP& _udp;
|
|
Player* _player;
|
|
FileManager* _fileManager;
|
|
Timekeeper* _timeKeeper;
|
|
FirmwareValidator* _firmwareValidator;
|
|
|
|
// Communication subsystems
|
|
MQTTAsyncClient _mqttClient;
|
|
ClientManager _clientManager;
|
|
WebSocketServer _wsServer;
|
|
CommandHandler _commandHandler;
|
|
HTTPRequestHandler _httpHandler;
|
|
SettingsWebServer _settingsServer;
|
|
|
|
// Message handlers
|
|
void onMqttMessage(const String& topic, const String& payload);
|
|
void onWebSocketMessage(uint32_t clientId, const JsonDocument& message);
|
|
|
|
// Response routing
|
|
void sendResponse(const String& response, const CommandHandler::MessageContext& context);
|
|
};
|