forked from btclock/btclock_v3
123 lines
No EOL
4.9 KiB
C++
123 lines
No EOL
4.9 KiB
C++
#include "price_notify.hpp"
|
|
|
|
const char *wsServerPrice = "wss://ws.coincap.io/prices?assets=bitcoin";
|
|
|
|
// const char* coinCapWsCert = R"(-----BEGIN CERTIFICATE-----
|
|
// MIIFMjCCBNmgAwIBAgIQBtgXvFyc28MsvQ1HjCnXJTAKBggqhkjOPQQDAjBKMQsw
|
|
// CQYDVQQGEwJVUzEZMBcGA1UEChMQQ2xvdWRmbGFyZSwgSW5jLjEgMB4GA1UEAxMX
|
|
// Q2xvdWRmbGFyZSBJbmMgRUNDIENBLTMwHhcNMjMwNTEwMDAwMDAwWhcNMjQwNTA5
|
|
// MjM1OTU5WjB1MQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQG
|
|
// A1UEBxMNU2FuIEZyYW5jaXNjbzEZMBcGA1UEChMQQ2xvdWRmbGFyZSwgSW5jLjEe
|
|
// MBwGA1UEAxMVc25pLmNsb3VkZmxhcmVzc2wuY29tMFkwEwYHKoZIzj0CAQYIKoZI
|
|
// zj0DAQcDQgAEpvFIXzQKHuqTo+IE6c6sB4p0PMXK1KsseEGf2UN/CNRhG5hO7lr8
|
|
// JtXrPZkawWBysZxOsEoetkPrDHMugCLfXKOCA3QwggNwMB8GA1UdIwQYMBaAFKXO
|
|
// N+rrsHUOlGeItEX62SQQh5YfMB0GA1UdDgQWBBShsZDJohaR1a5E0Qj7yblZjKDC
|
|
// gDA6BgNVHREEMzAxggwqLmNvaW5jYXAuaW+CCmNvaW5jYXAuaW+CFXNuaS5jbG91
|
|
// ZGZsYXJlc3NsLmNvbTAOBgNVHQ8BAf8EBAMCB4AwHQYDVR0lBBYwFAYIKwYBBQUH
|
|
// AwEGCCsGAQUFBwMCMHsGA1UdHwR0MHIwN6A1oDOGMWh0dHA6Ly9jcmwzLmRpZ2lj
|
|
// ZXJ0LmNvbS9DbG91ZGZsYXJlSW5jRUNDQ0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UQi2n2IDTEM=
|
|
// -----END CERTIFICATE-----)";
|
|
|
|
// WebsocketsClient client;
|
|
esp_websocket_client_handle_t clientPrice = NULL;
|
|
uint currentPrice = 30000;
|
|
unsigned long int lastPriceUpdate;
|
|
|
|
void setupPriceNotify()
|
|
{
|
|
// currentPrice = preferences.get("lastPrice", 30000);
|
|
|
|
esp_websocket_client_config_t config = {
|
|
.uri = wsServerPrice,
|
|
// .task_stack = (7*1024),
|
|
// .cert_pem = coinCapWsCert,
|
|
.user_agent = USER_AGENT
|
|
};
|
|
|
|
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;
|
|
|
|
switch (event_id)
|
|
{
|
|
case WEBSOCKET_EVENT_CONNECTED:
|
|
Serial.println(F("Connected to CoinCap.io WebSocket"));
|
|
break;
|
|
case WEBSOCKET_EVENT_DATA:
|
|
onWebsocketPriceMessage(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;
|
|
}
|
|
}
|
|
|
|
void onWebsocketPriceMessage(esp_websocket_event_data_t* event_data)
|
|
{
|
|
SpiRamJsonDocument doc(event_data->data_len);
|
|
|
|
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;
|
|
|
|
if (lastPriceUpdate == 0 || (currentTime - lastPriceUpdate) > minSecPriceUpd) {
|
|
// const unsigned long oldPrice = currentPrice;
|
|
currentPrice = doc["bitcoin"].as<uint>();
|
|
preferences.putUInt("lastPrice", currentPrice);
|
|
lastPriceUpdate = currentTime;
|
|
// if (abs((int)(oldPrice-currentPrice)) > round(0.0015*oldPrice)) {
|
|
if (workQueue != nullptr && (getCurrentScreen() == SCREEN_BTC_TICKER || getCurrentScreen() == SCREEN_MSCW_TIME || getCurrentScreen() == SCREEN_MARKET_CAP)) {
|
|
WorkItem priceUpdate = {TASK_PRICE_UPDATE, 0};
|
|
xQueueSend(workQueue, &priceUpdate, portMAX_DELAY);
|
|
}
|
|
//}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
uint getPrice() {
|
|
return currentPrice;
|
|
}
|
|
|
|
void setPrice(uint newPrice) {
|
|
currentPrice = newPrice;
|
|
}
|
|
|
|
bool isPriceNotifyConnected() {
|
|
if (clientPrice == NULL)
|
|
return false;
|
|
return esp_websocket_client_is_connected(clientPrice);
|
|
}
|
|
|
|
void stopPriceNotify() {
|
|
esp_websocket_client_stop(clientPrice);
|
|
esp_websocket_client_destroy(clientPrice);
|
|
} |