2023-11-07 00:11:12 +00:00
|
|
|
#include "block_notify.hpp"
|
|
|
|
|
2023-11-08 11:18:59 +00:00
|
|
|
char *wsServer;
|
|
|
|
esp_websocket_client_handle_t blockNotifyClient = NULL;
|
2023-11-10 18:52:06 +00:00
|
|
|
unsigned long int currentBlockHeight = 816000;
|
2023-11-07 00:11:12 +00:00
|
|
|
|
|
|
|
void setupBlockNotify()
|
|
|
|
{
|
2023-11-10 18:52:06 +00:00
|
|
|
currentBlockHeight = preferences.getULong("blockHeight", 816000);
|
|
|
|
|
2023-11-07 20:26:15 +00:00
|
|
|
IPAddress result;
|
|
|
|
|
|
|
|
int dnsErr = -1;
|
|
|
|
String mempoolInstance = preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE);
|
|
|
|
|
|
|
|
while (dnsErr != 1) {
|
|
|
|
dnsErr = WiFi.hostByName(mempoolInstance.c_str(), result);
|
|
|
|
|
|
|
|
if (dnsErr != 1) {
|
|
|
|
Serial.print(mempoolInstance);
|
2023-11-08 19:29:06 +00:00
|
|
|
Serial.println(F("mempool DNS could not be resolved"));
|
2023-11-07 20:26:15 +00:00
|
|
|
WiFi.reconnect();
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-07 00:11:12 +00:00
|
|
|
// Get current block height through regular API
|
|
|
|
HTTPClient *http = new HTTPClient();
|
2023-11-07 20:26:15 +00:00
|
|
|
http->begin("https://" + mempoolInstance + "/api/blocks/tip/height");
|
2023-11-07 00:11:12 +00:00
|
|
|
int httpCode = http->GET();
|
|
|
|
|
|
|
|
if (httpCode > 0 && httpCode == HTTP_CODE_OK)
|
|
|
|
{
|
|
|
|
String blockHeightStr = http->getString();
|
|
|
|
currentBlockHeight = blockHeightStr.toInt();
|
2023-11-08 14:33:37 +00:00
|
|
|
xTaskNotifyGive(blockUpdateTaskHandle);
|
2023-11-07 00:11:12 +00:00
|
|
|
}
|
|
|
|
|
2023-11-08 11:18:59 +00:00
|
|
|
// std::strcpy(wsServer, String("wss://" + mempoolInstance + "/api/v1/ws").c_str());
|
|
|
|
|
2023-11-07 00:11:12 +00:00
|
|
|
esp_websocket_client_config_t config = {
|
2023-11-08 23:15:04 +00:00
|
|
|
.uri = "wss://mempool.space/api/v1/ws",
|
|
|
|
.user_agent = USER_AGENT,
|
2023-11-07 00:11:12 +00:00
|
|
|
};
|
|
|
|
|
2023-11-08 11:18:59 +00:00
|
|
|
blockNotifyClient = esp_websocket_client_init(&config);
|
|
|
|
esp_websocket_register_events(blockNotifyClient, WEBSOCKET_EVENT_ANY, onWebsocketEvent, blockNotifyClient);
|
|
|
|
esp_websocket_client_start(blockNotifyClient);
|
2023-11-07 00:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onWebsocketEvent(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
|
|
|
|
{
|
|
|
|
esp_websocket_event_data_t *data = (esp_websocket_event_data_t *)event_data;
|
2023-11-08 23:15:04 +00:00
|
|
|
const String sub = "{\"action\": \"want\", \"data\":[\"blocks\"]}";
|
2023-11-07 00:11:12 +00:00
|
|
|
switch (event_id)
|
|
|
|
{
|
|
|
|
case WEBSOCKET_EVENT_CONNECTED:
|
2023-11-08 19:29:06 +00:00
|
|
|
Serial.println(F("Connected to Mempool.space WebSocket"));
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-08 23:15:04 +00:00
|
|
|
Serial.println(sub);
|
|
|
|
if (esp_websocket_client_send_text(blockNotifyClient, sub.c_str(), sub.length(), portMAX_DELAY) == -1)
|
2023-11-07 00:11:12 +00:00
|
|
|
{
|
2023-11-08 19:29:06 +00:00
|
|
|
Serial.println(F("Mempool.space WS Block Subscribe Error"));
|
2023-11-07 00:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case WEBSOCKET_EVENT_DATA:
|
|
|
|
onWebsocketMessage(data);
|
|
|
|
// Handle the received WebSocket message (block notifications) here
|
|
|
|
break;
|
|
|
|
case WEBSOCKET_EVENT_ERROR:
|
2023-11-08 19:29:06 +00:00
|
|
|
Serial.println(F("Mempool.space WS Connnection error"));
|
2023-11-07 00:11:12 +00:00
|
|
|
break;
|
|
|
|
case WEBSOCKET_EVENT_DISCONNECTED:
|
2023-11-08 19:29:06 +00:00
|
|
|
Serial.println(F("Mempool.space WS Connnection Closed"));
|
2023-11-07 00:11:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onWebsocketMessage(esp_websocket_event_data_t *event_data)
|
|
|
|
{
|
2023-11-07 20:26:15 +00:00
|
|
|
SpiRamJsonDocument doc(event_data->data_len);
|
2023-11-07 00:11:12 +00:00
|
|
|
|
|
|
|
deserializeJson(doc, (char *)event_data->data_ptr);
|
|
|
|
|
|
|
|
if (doc.containsKey("block"))
|
|
|
|
{
|
|
|
|
JsonObject block = doc["block"];
|
|
|
|
|
|
|
|
currentBlockHeight = block["height"].as<long>();
|
|
|
|
|
2023-11-10 18:52:06 +00:00
|
|
|
Serial.printf("New block found: %d\r\n", block["height"].as<long>());
|
|
|
|
preferences.putULong("blockHeight", currentBlockHeight);
|
2023-11-08 23:15:04 +00:00
|
|
|
|
2023-11-08 11:18:59 +00:00
|
|
|
if (blockUpdateTaskHandle != nullptr) {
|
2023-11-07 00:11:12 +00:00
|
|
|
xTaskNotifyGive(blockUpdateTaskHandle);
|
2023-11-10 19:59:08 +00:00
|
|
|
|
|
|
|
if (getCurrentScreen() != SCREEN_BLOCK_HEIGHT && preferences.getBool("stealFocus", true)) {
|
2023-11-08 19:29:06 +00:00
|
|
|
setCurrentScreen(SCREEN_BLOCK_HEIGHT);
|
2023-11-10 19:59:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (getCurrentScreen() == SCREEN_BLOCK_HEIGHT && preferences.getBool("ledFlashOnUpd", false)) {
|
2023-11-08 14:27:22 +00:00
|
|
|
vTaskDelay(pdMS_TO_TICKS(250)); // Wait until screens are updated
|
|
|
|
queueLedEffect(LED_FLASH_BLOCK_NOTIFY);
|
|
|
|
}
|
2023-11-08 11:18:59 +00:00
|
|
|
}
|
2023-11-08 23:15:04 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 00:11:12 +00:00
|
|
|
doc.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long getBlockHeight()
|
|
|
|
{
|
|
|
|
return currentBlockHeight;
|
2023-11-08 11:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isBlockNotifyConnected() {
|
|
|
|
if (blockNotifyClient == NULL)
|
|
|
|
return false;
|
|
|
|
return esp_websocket_client_is_connected(blockNotifyClient);
|
2023-11-07 00:11:12 +00:00
|
|
|
}
|