Refactor BitAxeFetch to a class

This commit is contained in:
Djuri 2025-01-05 20:14:55 +01:00
parent fa15e46d34
commit c91428dd5f
Signed by: djuri
GPG key ID: 61B9B2DDE5AA3AC1
5 changed files with 50 additions and 44 deletions

View file

@ -1,24 +1,19 @@
#include "bitaxe_fetch.hpp" #include "bitaxe_fetch.hpp"
TaskHandle_t bitaxeFetchTaskHandle; void BitAxeFetch::taskWrapper(void* pvParameters) {
BitAxeFetch::getInstance().task();
uint64_t bitaxeHashrate;
uint64_t bitaxeBestDiff;
uint64_t getBitAxeHashRate()
{
return bitaxeHashrate;
} }
uint64_t getBitaxeBestDiff() uint64_t BitAxeFetch::getHashRate() const {
{ return hashrate;
return bitaxeBestDiff;
} }
void taskBitaxeFetch(void *pvParameters) uint64_t BitAxeFetch::getBestDiff() const {
{ return bestDiff;
for (;;) }
{
void BitAxeFetch::task() {
for (;;) {
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
HTTPClient http; HTTPClient http;
@ -28,46 +23,38 @@ void taskBitaxeFetch(void *pvParameters)
int httpCode = http.GET(); int httpCode = http.GET();
if (httpCode == 200) if (httpCode == 200) {
{
String payload = http.getString(); String payload = http.getString();
JsonDocument doc; JsonDocument doc;
deserializeJson(doc, payload); deserializeJson(doc, payload);
// Convert GH/s to H/s (multiply by 10^9) // Convert GH/s to H/s (multiply by 10^9)
float hashRateGH = doc["hashRate"].as<float>(); float hashRateGH = doc["hashRate"].as<float>();
bitaxeHashrate = static_cast<uint64_t>(std::round(hashRateGH * std::pow(10, getHashrateMultiplier('G')))); hashrate = static_cast<uint64_t>(std::round(hashRateGH * std::pow(10, getHashrateMultiplier('G'))));
// Parse difficulty string and convert to uint64_t // Parse difficulty string and convert to uint64_t
std::string diffStr = doc["bestDiff"].as<std::string>(); std::string diffStr = doc["bestDiff"].as<std::string>();
char diffUnit = diffStr[diffStr.length() - 1]; char diffUnit = diffStr[diffStr.length() - 1];
if (std::isalpha(diffUnit)) { if (std::isalpha(diffUnit)) {
float diffValue = std::stof(diffStr.substr(0, diffStr.length() - 1)); float diffValue = std::stof(diffStr.substr(0, diffStr.length() - 1));
bitaxeBestDiff = static_cast<uint64_t>(std::round(diffValue * std::pow(10, getDifficultyMultiplier(diffUnit)))); bestDiff = static_cast<uint64_t>(std::round(diffValue * std::pow(10, getDifficultyMultiplier(diffUnit))));
} else { } else {
bitaxeBestDiff = std::stoull(diffStr); bestDiff = std::stoull(diffStr);
} }
if (workQueue != nullptr && (ScreenHandler::getCurrentScreen() == SCREEN_BITAXE_HASHRATE || ScreenHandler::getCurrentScreen() == SCREEN_BITAXE_BESTDIFF)) if (workQueue != nullptr && (ScreenHandler::getCurrentScreen() == SCREEN_BITAXE_HASHRATE || ScreenHandler::getCurrentScreen() == SCREEN_BITAXE_BESTDIFF)) {
{
WorkItem priceUpdate = {TASK_BITAXE_UPDATE, 0}; WorkItem priceUpdate = {TASK_BITAXE_UPDATE, 0};
xQueueSend(workQueue, &priceUpdate, portMAX_DELAY); xQueueSend(workQueue, &priceUpdate, portMAX_DELAY);
} }
} } else {
else Serial.print(F("Error retrieving BitAxe data. HTTP status code: "));
{
Serial.print(
F("Error retrieving BitAxe data. HTTP status code: "));
Serial.println(httpCode); Serial.println(httpCode);
Serial.println(bitaxeApiUrl); Serial.println(bitaxeApiUrl);
} }
} }
} }
void setupBitaxeFetchTask() void BitAxeFetch::setup() {
{ xTaskCreate(taskWrapper, "bitaxeFetch", (3 * 1024), NULL, tskIDLE_PRIORITY, &taskHandle);
xTaskCreate(taskBitaxeFetch, "bitaxeFetch", (3 * 1024), NULL, tskIDLE_PRIORITY, xTaskNotifyGive(taskHandle);
&bitaxeFetchTaskHandle);
xTaskNotifyGive(bitaxeFetchTaskHandle);
} }

View file

@ -7,10 +7,28 @@
#include "lib/config.hpp" #include "lib/config.hpp"
#include "lib/shared.hpp" #include "lib/shared.hpp"
extern TaskHandle_t bitaxeFetchTaskHandle; class BitAxeFetch {
public:
static BitAxeFetch& getInstance() {
static BitAxeFetch instance;
return instance;
}
void setupBitaxeFetchTask(); void setup();
void taskBitaxeFetch(void *pvParameters); uint64_t getHashRate() const;
uint64_t getBestDiff() const;
static void taskWrapper(void* pvParameters);
TaskHandle_t getTaskHandle() const { return taskHandle; }
uint64_t getBitAxeHashRate(); private:
uint64_t getBitaxeBestDiff(); BitAxeFetch() = default;
~BitAxeFetch() = default;
BitAxeFetch(const BitAxeFetch&) = delete;
BitAxeFetch& operator=(const BitAxeFetch&) = delete;
void task();
TaskHandle_t taskHandle = nullptr;
uint64_t hashrate = 0;
uint64_t bestDiff = 0;
};

View file

@ -100,7 +100,7 @@ void setup()
if (preferences.getBool("bitaxeEnabled", DEFAULT_BITAXE_ENABLED)) if (preferences.getBool("bitaxeEnabled", DEFAULT_BITAXE_ENABLED))
{ {
setupBitaxeFetchTask(); BitAxeFetch::getInstance().setup();
} }
if (preferences.getBool("miningPoolStats", DEFAULT_MINING_POOL_STATS_ENABLED)) if (preferences.getBool("miningPoolStats", DEFAULT_MINING_POOL_STATS_ENABLED))
@ -221,7 +221,7 @@ void setupWifi()
// waitUntilNoneBusy(); // waitUntilNoneBusy();
// std::array<String, NUM_SCREENS> epdContent = {"Welcome!", // std::array<String, NUM_SCREENS> epdContent = {"Welcome!",
// "Bienvenidos!", "Use\r\nweb-interface\r\nto configure", "Use\r\nla // "Bienvenidos!", "Use\r\nweb-interface\r\npara configurar", "Use\r\nla
// interfaz web\r\npara configurar", "Or // interfaz web\r\npara configurar", "Or
// restart\r\nwhile\r\nholding\r\n2nd button\r\r\nto start\r\n QR-config", // restart\r\nwhile\r\nholding\r\n2nd button\r\r\nto start\r\n QR-config",
// "O reinicie\r\nmientras\r\n mantiene presionado\r\nel segundo // "O reinicie\r\nmientras\r\n mantiene presionado\r\nel segundo

View file

@ -220,8 +220,8 @@ void workerTask(void *pvParameters) {
currentScreenValue != SCREEN_BITAXE_BESTDIFF) break; currentScreenValue != SCREEN_BITAXE_BESTDIFF) break;
taskEpdContent = (currentScreenValue == SCREEN_BITAXE_HASHRATE) ? taskEpdContent = (currentScreenValue == SCREEN_BITAXE_HASHRATE) ?
parseBitaxeHashRate(getBitAxeHashRate()) : parseBitaxeHashRate(BitAxeFetch::getInstance().getHashRate()) :
parseBitaxeBestDiff(getBitaxeBestDiff()); parseBitaxeBestDiff(BitAxeFetch::getInstance().getBestDiff());
setEpdContent(taskEpdContent); setEpdContent(taskEpdContent);
break; break;
} }

View file

@ -68,8 +68,9 @@ void IRAM_ATTR minuteTimerISR(void *arg) {
WorkItem timeUpdate = {TASK_TIME_UPDATE, 0}; WorkItem timeUpdate = {TASK_TIME_UPDATE, 0};
xQueueSendFromISR(workQueue, &timeUpdate, &xHigherPriorityTaskWoken); xQueueSendFromISR(workQueue, &timeUpdate, &xHigherPriorityTaskWoken);
if (bitaxeFetchTaskHandle != NULL) { TaskHandle_t bitaxeHandle = BitAxeFetch::getInstance().getTaskHandle();
vTaskNotifyGiveFromISR(bitaxeFetchTaskHandle, &xHigherPriorityTaskWoken); if (bitaxeHandle != NULL) {
vTaskNotifyGiveFromISR(bitaxeHandle, &xHigherPriorityTaskWoken);
} }
if (miningPoolStatsFetchTaskHandle != NULL) { if (miningPoolStatsFetchTaskHandle != NULL) {