btclock_v3/src/lib/price_notify.cpp

205 lines
5.3 KiB
C++
Raw Normal View History

2023-11-07 01:11:12 +01:00
#include "price_notify.hpp"
const char *wsOwnServerPrice = "wss://ws.btclock.dev/ws?assets=bitcoin";
const char *wsOwnServerV2 = "wss://ws-staging.btclock.dev/api/v2/ws";
2023-11-07 01:11:12 +01:00
const char *wsServerPrice = "wss://ws.coincap.io/prices?assets=bitcoin";
2023-11-07 01:11:12 +01:00
// WebsocketsClient client;
2023-11-08 12:18:59 +01:00
esp_websocket_client_handle_t clientPrice = NULL;
esp_websocket_client_config_t config;
2024-12-05 18:28:46 +01:00
uint currentPrice = 90000;
unsigned long int lastPriceUpdate;
bool priceNotifyInit = false;
std::map<char, std::uint64_t> currencyMap;
std::map<char, unsigned long int> lastUpdateMap;
WebSocketsClient priceNotifyWs;
2023-11-07 01:11:12 +01:00
void setupPriceNotify()
{
if (preferences.getBool("ownDataSource", DEFAULT_OWN_DATA_SOURCE))
{
config = {.uri = wsOwnServerPrice,
.user_agent = USER_AGENT};
}
else
{
config = {.uri = wsServerPrice,
.user_agent = USER_AGENT};
config.cert_pem = isrg_root_x1cert;
config.task_stack = (6*1024);
}
2023-11-30 22:38:01 +01:00
clientPrice = esp_websocket_client_init(&config);
esp_websocket_register_events(clientPrice, WEBSOCKET_EVENT_ANY,
onWebsocketPriceEvent, clientPrice);
esp_websocket_client_start(clientPrice);
// priceNotifyWs.beginSSL("ws.coincap.io", 443, "/prices?assets=bitcoin");
// priceNotifyWs.onEvent(onWebsocketPriceEvent);
// priceNotifyWs.setReconnectInterval(5000);
// priceNotifyWs.enableHeartbeat(15000, 3000, 2);
2023-11-07 01:11:12 +01:00
}
// void onWebsocketPriceEvent(WStype_t type, uint8_t * payload, size_t length) {
// switch(type) {
// case WStype_DISCONNECTED:
// Serial.printf("[WSc] Disconnected!\n");
// break;
// case WStype_CONNECTED:
// {
// Serial.printf("[WSc] Connected to url: %s\n", payload);
// break;
// }
// case WStype_TEXT:
// String message = String((char*)payload);
// onWebsocketPriceMessage(message);
// break;
// case WStype_BIN:
// break;
// case WStype_ERROR:
// case WStype_FRAGMENT_TEXT_START:
// case WStype_FRAGMENT_BIN_START:
// case WStype_FRAGMENT:
// case WStype_PING:
// case WStype_PONG:
// case WStype_FRAGMENT_FIN:
// break;
// }
// }
2023-11-30 22:38:01 +01:00
void onWebsocketPriceEvent(void *handler_args, esp_event_base_t base,
int32_t event_id, void *event_data)
{
2023-11-30 22:38:01 +01:00
esp_websocket_event_data_t *data = (esp_websocket_event_data_t *)event_data;
switch (event_id)
{
case WEBSOCKET_EVENT_CONNECTED:
Serial.println("Connected to " + String(config.uri) + " WebSocket");
priceNotifyInit = true;
break;
case WEBSOCKET_EVENT_DATA:
onWebsocketPriceMessage(data);
if (preferences.getBool("ownDataSource", DEFAULT_OWN_DATA_SOURCE))
{
onWebsocketBlockMessage(data);
}
break;
case WEBSOCKET_EVENT_ERROR:
Serial.println(F("Price WS Connnection error"));
break;
case WEBSOCKET_EVENT_DISCONNECTED:
Serial.println(F("Price WS Connnection Closed"));
break;
2023-11-30 22:38:01 +01:00
}
2023-11-07 01:11:12 +01:00
}
void onWebsocketPriceMessage(esp_websocket_event_data_t *event_data)
{
JsonDocument doc;
2023-11-30 22:38:01 +01:00
deserializeJson(doc, (char *)event_data->data_ptr);
if (doc.containsKey("bitcoin"))
{
if (currentPrice != doc["bitcoin"].as<long>())
{
processNewPrice(doc["bitcoin"].as<long>(), CURRENCY_USD);
2023-11-07 01:11:12 +01:00
}
2023-11-30 22:38:01 +01:00
}
2023-11-07 01:11:12 +01:00
}
void processNewPrice(uint newPrice, char currency)
2024-07-11 22:08:42 +02:00
{
uint minSecPriceUpd = preferences.getUInt(
"minSecPriceUpd", DEFAULT_SECONDS_BETWEEN_PRICE_UPDATE);
uint currentTime = esp_timer_get_time() / 1000000;
if (lastUpdateMap.find(currency) == lastUpdateMap.end() ||
(currentTime - lastUpdateMap[currency]) > minSecPriceUpd)
2024-07-11 22:08:42 +02:00
{
// const unsigned long oldPrice = currentPrice;
currencyMap[currency] = newPrice;
if (currency == CURRENCY_USD && ( lastUpdateMap[currency] == 0 ||
(currentTime - lastUpdateMap[currency]) > 120))
{
preferences.putUInt("lastPrice", currentPrice);
}
lastUpdateMap[currency] = currentTime;
2024-07-11 22:08:42 +02:00
// if (abs((int)(oldPrice-currentPrice)) > round(0.0015*oldPrice)) {
if (workQueue != nullptr && (getCurrentScreen() == SCREEN_BTC_TICKER ||
getCurrentScreen() == SCREEN_SATS_PER_CURRENCY ||
2024-07-11 22:08:42 +02:00
getCurrentScreen() == SCREEN_MARKET_CAP))
{
WorkItem priceUpdate = {TASK_PRICE_UPDATE, currency};
2024-07-11 22:08:42 +02:00
xQueueSend(workQueue, &priceUpdate, portMAX_DELAY);
}
//}
}
}
uint getLastPriceUpdate(char currency)
{
if (lastUpdateMap.find(currency) == lastUpdateMap.end())
{
return 0;
}
return lastUpdateMap[currency];
}
uint getPrice(char currency)
{
if (currencyMap.find(currency) == currencyMap.end())
{
return 0;
}
return currencyMap[currency];
}
2023-11-08 12:18:59 +01:00
void setPrice(uint newPrice, char currency)
{
currencyMap[currency] = newPrice;
}
bool isPriceNotifyConnected()
{
if (clientPrice == NULL)
return false;
2023-11-30 22:38:01 +01:00
return esp_websocket_client_is_connected(clientPrice);
}
bool getPriceNotifyInit()
{
2024-01-31 23:45:26 +01:00
return priceNotifyInit;
}
void stopPriceNotify()
{
if (clientPrice == NULL)
return;
esp_websocket_client_close(clientPrice, pdMS_TO_TICKS(5000));
2023-11-30 22:38:01 +01:00
esp_websocket_client_stop(clientPrice);
esp_websocket_client_destroy(clientPrice);
2024-01-31 23:45:26 +01:00
clientPrice = NULL;
}
void restartPriceNotify()
{
stopPriceNotify();
if (clientPrice == NULL)
{
setupPriceNotify();
return;
}
// esp_websocket_client_close(clientPrice, pdMS_TO_TICKS(5000));
// esp_websocket_client_stop(clientPrice);
// esp_websocket_client_start(clientPrice);
2023-11-07 01:11:12 +01:00
}