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-12 11:38:28 +00:00
|
|
|
uint currentBlockHeight = 816000;
|
|
|
|
|
|
|
|
// const char *mempoolWsCert = R"(-----BEGIN CERTIFICATE-----
|
|
|
|
// MIIHfTCCBmWgAwIBAgIRANFX3mhqRYDt1NFuENoSyaAwDQYJKoZIhvcNAQELBQAw
|
|
|
|
// gZUxCzAJBgNVBAYTAkdCMRswGQYDVQQIExJHcmVhdGVyIE1hbmNoZXN0ZXIxEDAO
|
|
|
|
// BgNVBAcTB1NhbGZvcmQxGDAWBgNVBAoTD1NlY3RpZ28gTGltaXRlZDE9MDsGA1UE
|
|
|
|
// AxM0U2VjdGlnbyBSU0EgT3JnYW5pemF0aW9uIFZhbGlkYXRpb24gU2VjdXJlIFNl
|
|
|
|
// cnZlciBDQTAeFw0yMzA3MjQwMDAwMDBaFw0yNDA4MjIyMzU5NTlaMFcxCzAJBgNV
|
|
|
|
// BAYTAkpQMQ4wDAYDVQQIEwVUb2t5bzEgMB4GA1UEChMXTUVNUE9PTCBTUEFDRSBD
|
|
|
|
// Ty4sIExURC4xFjAUBgNVBAMTDW1lbXBvb2wuc3BhY2UwggEiMA0GCSqGSIb3DQEB
|
|
|
|
// AQUAA4IBDwAwggEKAoIBAQCqmiPRWgo58d25R0biQjAksXMq5ciH7z7ZQo2w2AbB
|
|
|
|
// rHxpnlIry74b9S4wRY5UJeYmd6ZwA76NdSioDvxTJc29bLplY+Ftmfc4ET0zYb2k
|
|
|
|
// Fi86z7GOWb6Ezor/qez9uMM9cxd021Bvcs0/2OrL6Sgp66u9keDZv9NyvFPpXfuR
|
|
|
|
// tdV2r4HF57VJqZn105PN4k80kNWgDbae8aw+BuUNvQYKEe71yfB7Bh6zSh9pCSfM
|
|
|
|
// I6pIJdQzoada2uY1dQMoJeI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3sx3SeYXVimr
|
|
|
|
// ew==
|
|
|
|
// -----END CERTIFICATE-----)";
|
2023-11-07 00:11:12 +00:00
|
|
|
|
|
|
|
void setupBlockNotify()
|
|
|
|
{
|
2023-11-12 11:38:28 +00:00
|
|
|
//currentBlockHeight = preferences.getUInt("blockHeight", 816000);
|
2023-11-10 18:52:06 +00:00
|
|
|
|
2023-11-07 20:26:15 +00:00
|
|
|
IPAddress result;
|
|
|
|
|
|
|
|
int dnsErr = -1;
|
|
|
|
String mempoolInstance = preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE);
|
|
|
|
|
2023-11-12 11:38:28 +00:00
|
|
|
while (dnsErr != 1)
|
|
|
|
{
|
2023-11-07 20:26:15 +00:00
|
|
|
dnsErr = WiFi.hostByName(mempoolInstance.c_str(), result);
|
|
|
|
|
2023-11-12 11:38:28 +00:00
|
|
|
if (dnsErr != 1)
|
|
|
|
{
|
2023-11-07 20:26:15 +00:00
|
|
|
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-12 11:38:28 +00:00
|
|
|
// xTaskNotifyGive(blockUpdateTaskHandle);
|
|
|
|
if (workQueue != nullptr)
|
|
|
|
{
|
|
|
|
WorkItem blockUpdate = {TASK_BLOCK_UPDATE, 0};
|
|
|
|
xQueueSend(workQueue, &blockUpdate, portMAX_DELAY);
|
|
|
|
}
|
2023-11-07 00:11:12 +00:00
|
|
|
}
|
|
|
|
|
2023-11-12 11:38:28 +00:00
|
|
|
// std::strcpy(wsServer, String("wss://" + mempoolInstance + "/api/v1/ws").c_str());
|
2023-11-08 11:18:59 +00:00
|
|
|
|
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",
|
2023-11-12 11:38:28 +00:00
|
|
|
// .task_stack = (6*1024),
|
|
|
|
// .cert_pem = mempoolWsCert,
|
2023-11-08 23:15:04 +00:00
|
|
|
.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-12 11:38:28 +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"];
|
|
|
|
|
2023-11-12 11:38:28 +00:00
|
|
|
currentBlockHeight = block["height"].as<uint>();
|
2023-11-07 00:11:12 +00:00
|
|
|
|
2023-11-12 11:38:28 +00:00
|
|
|
Serial.printf("New block found: %d\r\n", block["height"].as<uint>());
|
|
|
|
size_t prefWrite = preferences.putUInt("blockHeight", currentBlockHeight);
|
|
|
|
Serial.printf("Wrote %d for block\r\n", prefWrite);
|
2023-11-08 23:15:04 +00:00
|
|
|
|
2023-11-12 11:38:28 +00:00
|
|
|
if (workQueue != nullptr)
|
|
|
|
{
|
|
|
|
WorkItem blockUpdate = {TASK_BLOCK_UPDATE, 0};
|
|
|
|
xQueueSend(workQueue, &blockUpdate, portMAX_DELAY);
|
|
|
|
// xTaskNotifyGive(blockUpdateTaskHandle);
|
2023-11-10 19:59:08 +00:00
|
|
|
|
2023-11-12 11:38:28 +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
|
|
|
}
|
|
|
|
|
2023-11-12 11:38:28 +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-12 11:38:28 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 00:11:12 +00:00
|
|
|
doc.clear();
|
|
|
|
}
|
|
|
|
|
2023-11-12 11:38:28 +00:00
|
|
|
uint getBlockHeight()
|
2023-11-07 00:11:12 +00:00
|
|
|
{
|
|
|
|
return currentBlockHeight;
|
2023-11-08 11:18:59 +00:00
|
|
|
}
|
|
|
|
|
2023-11-12 11:38:28 +00:00
|
|
|
void setBlockHeight(uint newBlockHeight)
|
|
|
|
{
|
|
|
|
currentBlockHeight = newBlockHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isBlockNotifyConnected()
|
|
|
|
{
|
2023-11-08 11:18:59 +00:00
|
|
|
if (blockNotifyClient == NULL)
|
|
|
|
return false;
|
|
|
|
return esp_websocket_client_is_connected(blockNotifyClient);
|
2023-11-10 22:18:14 +00:00
|
|
|
}
|
|
|
|
|
2023-11-12 11:38:28 +00:00
|
|
|
void stopBlockNotify()
|
|
|
|
{
|
2023-11-10 22:18:14 +00:00
|
|
|
esp_websocket_client_stop(blockNotifyClient);
|
|
|
|
esp_websocket_client_destroy(blockNotifyClient);
|
2023-11-07 00:11:12 +00:00
|
|
|
}
|