Refactor BlockNotify to a class, use websocketsClient

This commit is contained in:
Djuri 2025-01-06 00:43:31 +01:00
parent ebbec75e6b
commit e330984ba2
Signed by: djuri
GPG key ID: 61B9B2DDE5AA3AC1
9 changed files with 239 additions and 257 deletions

View file

@ -4,8 +4,7 @@
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <esp_timer.h>
#include <esp_websocket_client.h>
#include <WebSocketsClient.h>
#include <cstring>
#include <string>
@ -14,28 +13,54 @@
#include "lib/timers.hpp"
#include "lib/shared.hpp"
// using namespace websockets;
class BlockNotify {
public:
static BlockNotify& getInstance() {
static BlockNotify instance;
return instance;
}
void setupBlockNotify();
// Delete copy constructor and assignment operator
BlockNotify(const BlockNotify&) = delete;
void operator=(const BlockNotify&) = delete;
void onWebsocketBlockEvent(void *handler_args, esp_event_base_t base,
int32_t event_id, void *event_data);
void onWebsocketBlockMessage(esp_websocket_event_data_t *event_data);
// Block notification setup and control
void setup();
void stop();
void restart();
bool isConnected() const;
bool isInitialized() const;
void setBlockHeight(uint32_t newBlockHeight);
uint32_t getBlockHeight();
// Block height management
void setBlockHeight(uint32_t newBlockHeight);
uint32_t getBlockHeight() const;
void setBlockMedianFee(uint16_t blockMedianFee);
uint16_t getBlockMedianFee();
// Block fee management
void setBlockMedianFee(uint16_t blockMedianFee);
uint16_t getBlockMedianFee() const;
bool isBlockNotifyConnected();
void stopBlockNotify();
void restartBlockNotify();
// Block processing
void processNewBlock(uint32_t newBlockHeight);
void processNewBlockFee(uint16_t newBlockFee);
void processNewBlock(uint32_t newBlockHeight);
void processNewBlockFee(uint16_t newBlockFee);
// Block fetch and update tracking
int fetchLatestBlock();
uint getLastBlockUpdate() const;
void setLastBlockUpdate(uint lastUpdate);
bool getBlockNotifyInit();
uint32_t getLastBlockUpdate();
int getBlockFetch();
void setLastBlockUpdate(uint32_t lastUpdate);
// Task handling
static void taskNotify(void* pvParameters);
private:
BlockNotify() = default; // Private constructor for singleton
void setupTask();
static void onWebsocketEvent(WStype_t type, uint8_t* payload, size_t length);
static WebSocketsClient webSocket;
static uint32_t currentBlockHeight;
static uint16_t blockMedianFee;
static bool notifyInit;
static unsigned long int lastBlockUpdate;
static TaskHandle_t taskHandle;
};