btclock_v3/src/lib/price_notify.cpp

76 lines
2.5 KiB
C++
Raw Normal View History

2023-11-07 00:11:12 +00:00
#include "price_notify.hpp"
const char *wsServerPrice = "wss://ws.coincap.io/prices?assets=bitcoin";
// WebsocketsClient client;
2023-11-08 11:18:59 +00:00
esp_websocket_client_handle_t clientPrice = NULL;
unsigned long int currentPrice = 30000;
unsigned long int lastPriceUpdate;
2023-11-07 00:11:12 +00:00
void setupPriceNotify()
{
esp_websocket_client_config_t config = {
.uri = wsServerPrice,
.user_agent = USER_AGENT,
2023-11-07 00:11:12 +00:00
};
clientPrice = esp_websocket_client_init(&config);
esp_websocket_register_events(clientPrice, WEBSOCKET_EVENT_ANY, onWebsocketPriceEvent, clientPrice);
esp_websocket_client_start(clientPrice);
}
void onWebsocketPriceEvent(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-07 00:11:12 +00:00
switch (event_id)
{
case WEBSOCKET_EVENT_CONNECTED:
Serial.println(F("Connected to CoinCap.io WebSocket"));
2023-11-07 00:11:12 +00:00
break;
case WEBSOCKET_EVENT_DATA:
onWebsocketPriceMessage(data);
break;
case WEBSOCKET_EVENT_ERROR:
Serial.println(F("Price WS Connnection error"));
2023-11-07 00:11:12 +00:00
break;
case WEBSOCKET_EVENT_DISCONNECTED:
Serial.println(F("Price WS Connnection Closed"));
2023-11-07 00:11:12 +00:00
break;
}
}
void onWebsocketPriceMessage(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("bitcoin")) {
if (currentPrice != doc["bitcoin"].as<long>()) {
uint minSecPriceUpd = preferences.getUInt("minSecPriceUpd", DEFAULT_SECONDS_BETWEEN_PRICE_UPDATE);
uint currentTime = esp_timer_get_time() / 1000000;
2023-11-07 00:11:12 +00:00
if (lastPriceUpdate == 0 || (currentTime - lastPriceUpdate) > minSecPriceUpd) {
// const unsigned long oldPrice = currentPrice;
currentPrice = doc["bitcoin"].as<long>();
lastPriceUpdate = currentTime;
// if (abs((int)(oldPrice-currentPrice)) > round(0.0015*oldPrice)) {
if (priceUpdateTaskHandle != nullptr && (getCurrentScreen() == SCREEN_BTC_TICKER || getCurrentScreen() == SCREEN_MSCW_TIME || getCurrentScreen() == SCREEN_MARKET_CAP))
xTaskNotifyGive(priceUpdateTaskHandle);
//}
}
2023-11-07 00:11:12 +00:00
}
}
}
unsigned long getPrice() {
return currentPrice;
2023-11-08 11:18:59 +00:00
}
bool isPriceNotifyConnected() {
if (clientPrice == NULL)
return false;
return esp_websocket_client_is_connected(clientPrice);
2023-11-07 00:11:12 +00:00
}