Rewrite price notify to websocketsclient
This commit is contained in:
parent
0999dd08ad
commit
7195b7d343
2 changed files with 71 additions and 108 deletions
|
@ -2,103 +2,64 @@
|
||||||
|
|
||||||
const char *wsServerPrice = "wss://ws.coincap.io/prices?assets=bitcoin";
|
const char *wsServerPrice = "wss://ws.coincap.io/prices?assets=bitcoin";
|
||||||
|
|
||||||
// WebsocketsClient client;
|
WebSocketsClient webSocket;
|
||||||
esp_websocket_client_handle_t clientPrice = NULL;
|
|
||||||
esp_websocket_client_config_t config;
|
|
||||||
uint currentPrice = 90000;
|
uint currentPrice = 90000;
|
||||||
unsigned long int lastPriceUpdate;
|
unsigned long int lastPriceUpdate;
|
||||||
bool priceNotifyInit = false;
|
bool priceNotifyInit = false;
|
||||||
std::map<char, std::uint64_t> currencyMap;
|
std::map<char, std::uint64_t> currencyMap;
|
||||||
std::map<char, unsigned long int> lastUpdateMap;
|
std::map<char, unsigned long int> lastUpdateMap;
|
||||||
WebSocketsClient priceNotifyWs;
|
TaskHandle_t priceNotifyTaskHandle;
|
||||||
|
|
||||||
|
void onWebsocketPriceEvent(WStype_t type, uint8_t * payload, size_t length);
|
||||||
|
|
||||||
void setupPriceNotify()
|
void setupPriceNotify()
|
||||||
{
|
{
|
||||||
config = {.uri = wsServerPrice,
|
webSocket.beginSSL("ws.coincap.io", 443, "/prices?assets=bitcoin");
|
||||||
.user_agent = USER_AGENT};
|
webSocket.onEvent([](WStype_t type, uint8_t * payload, size_t length) {
|
||||||
config.cert_pem = isrg_root_x1cert;
|
onWebsocketPriceEvent(type, payload, length);
|
||||||
|
});
|
||||||
|
webSocket.setReconnectInterval(5000);
|
||||||
|
webSocket.enableHeartbeat(15000, 3000, 2);
|
||||||
|
|
||||||
config.task_stack = (6*1024);
|
setupPriceNotifyTask();
|
||||||
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void onWebsocketPriceEvent(WStype_t type, uint8_t * payload, size_t length) {
|
||||||
|
switch(type) {
|
||||||
|
case WStype_DISCONNECTED:
|
||||||
|
Serial.println(F("Price WS Connection Closed"));
|
||||||
|
break;
|
||||||
|
case WStype_CONNECTED:
|
||||||
|
{
|
||||||
|
Serial.println("Connected to " + String(wsServerPrice));
|
||||||
|
priceNotifyInit = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case WStype_TEXT:
|
||||||
|
{
|
||||||
|
JsonDocument doc;
|
||||||
|
deserializeJson(doc, (char *)payload);
|
||||||
|
|
||||||
// void onWebsocketPriceEvent(WStype_t type, uint8_t * payload, size_t length) {
|
if (doc["bitcoin"].is<JsonObject>())
|
||||||
// switch(type) {
|
{
|
||||||
// case WStype_DISCONNECTED:
|
if (currentPrice != doc["bitcoin"].as<long>())
|
||||||
// Serial.printf("[WSc] Disconnected!\n");
|
{
|
||||||
// break;
|
processNewPrice(doc["bitcoin"].as<long>(), CURRENCY_USD);
|
||||||
// case WStype_CONNECTED:
|
}
|
||||||
// {
|
}
|
||||||
// Serial.printf("[WSc] Connected to url: %s\n", payload);
|
break;
|
||||||
|
}
|
||||||
|
case WStype_BIN:
|
||||||
// break;
|
break;
|
||||||
// }
|
case WStype_ERROR:
|
||||||
// case WStype_TEXT:
|
case WStype_FRAGMENT_TEXT_START:
|
||||||
// String message = String((char*)payload);
|
case WStype_FRAGMENT_BIN_START:
|
||||||
// onWebsocketPriceMessage(message);
|
case WStype_FRAGMENT:
|
||||||
// break;
|
case WStype_PING:
|
||||||
// case WStype_BIN:
|
case WStype_PONG:
|
||||||
// break;
|
case WStype_FRAGMENT_FIN:
|
||||||
// case WStype_ERROR:
|
break;
|
||||||
// case WStype_FRAGMENT_TEXT_START:
|
|
||||||
// case WStype_FRAGMENT_BIN_START:
|
|
||||||
// case WStype_FRAGMENT:
|
|
||||||
// case WStype_PING:
|
|
||||||
// case WStype_PONG:
|
|
||||||
// case WStype_FRAGMENT_FIN:
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
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("Connected to " + String(config.uri) + " WebSocket");
|
|
||||||
priceNotifyInit = true;
|
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
JsonDocument doc;
|
|
||||||
|
|
||||||
deserializeJson(doc, (char *)event_data->data_ptr);
|
|
||||||
|
|
||||||
if (doc.containsKey("bitcoin"))
|
|
||||||
{
|
|
||||||
if (currentPrice != doc["bitcoin"].as<long>())
|
|
||||||
{
|
|
||||||
processNewPrice(doc["bitcoin"].as<long>(), CURRENCY_USD);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void processNewPrice(uint newPrice, char currency)
|
void processNewPrice(uint newPrice, char currency)
|
||||||
|
@ -175,9 +136,7 @@ void setPrice(uint newPrice, char currency)
|
||||||
|
|
||||||
bool isPriceNotifyConnected()
|
bool isPriceNotifyConnected()
|
||||||
{
|
{
|
||||||
if (clientPrice == NULL)
|
return webSocket.isConnected();
|
||||||
return false;
|
|
||||||
return esp_websocket_client_is_connected(clientPrice);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool getPriceNotifyInit()
|
bool getPriceNotifyInit()
|
||||||
|
@ -187,24 +146,30 @@ bool getPriceNotifyInit()
|
||||||
|
|
||||||
void stopPriceNotify()
|
void stopPriceNotify()
|
||||||
{
|
{
|
||||||
if (clientPrice == NULL)
|
webSocket.disconnect();
|
||||||
return;
|
if (priceNotifyTaskHandle != NULL) {
|
||||||
esp_websocket_client_close(clientPrice, pdMS_TO_TICKS(5000));
|
vTaskDelete(priceNotifyTaskHandle);
|
||||||
esp_websocket_client_stop(clientPrice);
|
priceNotifyTaskHandle = NULL;
|
||||||
esp_websocket_client_destroy(clientPrice);
|
}
|
||||||
|
|
||||||
clientPrice = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void restartPriceNotify()
|
void restartPriceNotify()
|
||||||
{
|
{
|
||||||
stopPriceNotify();
|
stopPriceNotify();
|
||||||
if (clientPrice == NULL)
|
setupPriceNotify();
|
||||||
|
}
|
||||||
|
|
||||||
|
void taskPriceNotify(void *pvParameters)
|
||||||
|
{
|
||||||
|
for (;;)
|
||||||
{
|
{
|
||||||
setupPriceNotify();
|
webSocket.loop();
|
||||||
return;
|
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
// esp_websocket_client_close(clientPrice, pdMS_TO_TICKS(5000));
|
}
|
||||||
// esp_websocket_client_stop(clientPrice);
|
|
||||||
// esp_websocket_client_start(clientPrice);
|
void setupPriceNotifyTask()
|
||||||
|
{
|
||||||
|
xTaskCreate(taskPriceNotify, "priceNotify", (6 * 1024), NULL, tskIDLE_PRIORITY,
|
||||||
|
&priceNotifyTaskHandle);
|
||||||
}
|
}
|
|
@ -2,24 +2,22 @@
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <esp_websocket_client.h>
|
#include <WebSocketsClient.h>
|
||||||
#include "block_notify.hpp"
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "lib/screen_handler.hpp"
|
#include "lib/screen_handler.hpp"
|
||||||
|
|
||||||
|
extern TaskHandle_t priceNotifyTaskHandle;
|
||||||
|
|
||||||
void setupPriceNotify();
|
void setupPriceNotify();
|
||||||
|
void setupPriceNotifyTask();
|
||||||
|
void taskPriceNotify(void *pvParameters);
|
||||||
|
|
||||||
void onWebsocketPriceEvent(void *handler_args, esp_event_base_t base,
|
void onWebsocketPriceEvent(WStype_t type, uint8_t * payload, size_t length);
|
||||||
int32_t event_id, void *event_data);
|
|
||||||
//void onWebsocketPriceEvent(WStype_t type, uint8_t * payload, size_t length);
|
|
||||||
|
|
||||||
void onWebsocketPriceMessage(esp_websocket_event_data_t *event_data);
|
|
||||||
|
|
||||||
uint getPrice(char currency);
|
uint getPrice(char currency);
|
||||||
void setPrice(uint newPrice, char currency);
|
void setPrice(uint newPrice, char currency);
|
||||||
|
|
||||||
//void processNewPrice(uint newPrice);
|
|
||||||
void processNewPrice(uint newPrice, char currency);
|
void processNewPrice(uint newPrice, char currency);
|
||||||
|
|
||||||
bool isPriceNotifyConnected();
|
bool isPriceNotifyConnected();
|
||||||
|
|
Loading…
Reference in a new issue