#pragma once #include "message.h" #include "crypto.h" #include #include #include class ServerConnectionCallbacks { public: virtual void sendServerError(SesameEvent event) = 0; virtual void handleServerMessage(uint8_t* payload, size_t length) = 0; }; class ServerConnection { public: ServerConnection(const char* url, int port, const char* path); void connect(const char* key, uint32_t reconnectTime = 5000); void loop(); /** * @brief Set the handler * * @param callback The handler to handle messages and errors */ void setCallbackHandler(ServerConnectionCallbacks* callbacks); /** * @brief Send a response message over the socket * * @param buffer The data buffer * @param length The number of bytes to send */ void sendResponse(uint8_t* buffer, uint16_t length); bool isSocketConnected() { return socketIsConnected; } private: const char* url; int port; const char* path; const char* key = NULL; // Indicator that the socket is connected. bool socketIsConnected = false; ServerConnectionCallbacks* controller = NULL; // WebSocket to connect to the control server WebSocketsClient webSocket; /** * Callback for WebSocket events. * * Updates the connection state and processes received keys. * * @param payload The pointer to received data * @param length The number of bytes received */ void webSocketEventHandler(WStype_t type, uint8_t * payload, size_t length); };