2022-04-07 17:45:23 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "message.h"
|
|
|
|
#include "crypto.h"
|
|
|
|
#include <WiFiMulti.h>
|
|
|
|
#include <WiFiClientSecure.h>
|
|
|
|
#include <WebSocketsClient.h>
|
|
|
|
|
2023-08-09 15:02:24 +02:00
|
|
|
struct ServerConfiguration {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The url of the remote server to connect to
|
|
|
|
*/
|
|
|
|
const char* url;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The server port
|
|
|
|
*/
|
|
|
|
int port;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The path on the server
|
|
|
|
*/
|
|
|
|
const char* path;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The authentication key for the server
|
|
|
|
*/
|
|
|
|
const char* key;
|
|
|
|
|
|
|
|
uint32_t reconnectTime;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2023-08-09 12:55:11 +02:00
|
|
|
class ServerConnectionCallbacks {
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
virtual void sendServerError(SesameEvent event) = 0;
|
|
|
|
virtual void handleServerMessage(uint8_t* payload, size_t length) = 0;
|
|
|
|
};
|
|
|
|
|
2022-04-07 17:45:23 +02:00
|
|
|
class ServerConnection {
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2023-08-09 15:02:24 +02:00
|
|
|
ServerConnection();
|
2022-04-07 17:45:23 +02:00
|
|
|
|
2023-08-09 12:55:11 +02:00
|
|
|
/**
|
2023-08-09 15:02:24 +02:00
|
|
|
* @brief Set the configuration and the callback handler
|
2023-08-09 12:55:11 +02:00
|
|
|
*
|
|
|
|
* @param callback The handler to handle messages and errors
|
|
|
|
*/
|
2023-08-09 15:02:24 +02:00
|
|
|
void configure(ServerConfiguration configuration, ServerConnectionCallbacks* callbacks);
|
|
|
|
|
|
|
|
void connect();
|
|
|
|
|
|
|
|
void loop();
|
2022-04-07 17:45:23 +02:00
|
|
|
|
2023-08-09 12:55:11 +02:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
2022-04-07 17:45:23 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2023-08-09 15:02:24 +02:00
|
|
|
ServerConfiguration configuration;
|
2022-04-07 17:45:23 +02:00
|
|
|
|
2023-08-09 12:55:11 +02:00
|
|
|
// Indicator that the socket is connected.
|
|
|
|
bool socketIsConnected = false;
|
|
|
|
|
|
|
|
ServerConnectionCallbacks* controller = NULL;
|
2022-04-07 17:45:23 +02:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
};
|