btclock_v3/src/lib/price_notify.cpp

64 lines
1.9 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;
esp_websocket_client_handle_t clientPrice;
unsigned long int currentPrice;
void setupPriceNotify()
{
esp_websocket_client_config_t config = {
.uri = wsServerPrice,
};
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;
String init;
String sub;
switch (event_id)
{
case WEBSOCKET_EVENT_CONNECTED:
Serial.println("Connected to CoinCap.io WebSocket");
break;
case WEBSOCKET_EVENT_DATA:
onWebsocketPriceMessage(data);
break;
case WEBSOCKET_EVENT_ERROR:
Serial.println("Connnection error");
break;
case WEBSOCKET_EVENT_DISCONNECTED:
Serial.println("Connnection Closed");
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>()) {
2023-11-07 20:26:15 +00:00
// Serial.printf("New price %lu\r\n", currentPrice);
2023-11-07 00:11:12 +00:00
const unsigned long oldPrice = currentPrice;
currentPrice = doc["bitcoin"].as<long>();
// if (abs((int)(oldPrice-currentPrice)) > round(0.0015*oldPrice)) {
2023-11-07 20:26:15 +00:00
if (priceUpdateTaskHandle != nullptr && (getCurrentScreen() == SCREEN_BTC_TICKER || getCurrentScreen() == SCREEN_MSCW_TIME))
2023-11-07 00:11:12 +00:00
xTaskNotifyGive(priceUpdateTaskHandle);
//}
}
}
}
unsigned long getPrice() {
return currentPrice;
}