diff --git a/src/lib/config.cpp b/src/lib/config.cpp index 2dce75e..f0db973 100644 --- a/src/lib/config.cpp +++ b/src/lib/config.cpp @@ -105,7 +105,7 @@ void setup() if (preferences.getBool("miningPoolStats", DEFAULT_MINING_POOL_STATS_ENABLED)) { - setupMiningPoolStatsFetchTask(); + MiningPoolStatsFetch::getInstance().setup(); } ButtonHandler::setup(); @@ -374,7 +374,7 @@ void setupPreferences() if (preferences.getBool("miningPoolStats", DEFAULT_MINING_POOL_STATS_ENABLED)) { addScreenMapping(SCREEN_MINING_POOL_STATS_HASHRATE, "Mining Pool Hashrate"); - if (getMiningPool()->supportsDailyEarnings()) { + if (MiningPoolStatsFetch::getInstance().getPool()->supportsDailyEarnings()) { addScreenMapping(SCREEN_MINING_POOL_STATS_EARNINGS, "Mining Pool Earnings"); } } diff --git a/src/lib/epd.cpp b/src/lib/epd.cpp index d9b5303..04d3c5c 100644 --- a/src/lib/epd.cpp +++ b/src/lib/epd.cpp @@ -600,7 +600,7 @@ bool renderIcon(const uint dispNum, const String &text, bool partial) } else if (text.endsWith("miningpool")) { - LogoData logo = getMiningPoolLogo(); + LogoData logo = MiningPoolStatsFetch::getInstance().getLogo(); if (logo.size == 0) { diff --git a/src/lib/mining_pool_stats_fetch.cpp b/src/lib/mining_pool_stats_fetch.cpp index a35a00e..e04f208 100644 --- a/src/lib/mining_pool_stats_fetch.cpp +++ b/src/lib/mining_pool_stats_fetch.cpp @@ -1,95 +1,98 @@ #include "mining_pool_stats_fetch.hpp" -TaskHandle_t miningPoolStatsFetchTaskHandle; - -std::string miningPoolName; -std::string miningPoolStatsHashrate; -int miningPoolStatsDailyEarnings; - -std::string getMiningPoolStatsHashRate() -{ - return miningPoolStatsHashrate; +void MiningPoolStatsFetch::taskWrapper(void* pvParameters) { + MiningPoolStatsFetch::getInstance().task(); } -int getMiningPoolStatsDailyEarnings() -{ - return miningPoolStatsDailyEarnings; +void MiningPoolStatsFetch::downloadLogoTaskWrapper(void* pvParameters) { + MiningPoolStatsFetch::getInstance().downloadLogoTask(); } -void taskMiningPoolStatsFetch(void *pvParameters) -{ +std::string MiningPoolStatsFetch::getHashRate() const { + return hashrate; +} + +int MiningPoolStatsFetch::getDailyEarnings() const { + return dailyEarnings; +} + +MiningPoolInterface* MiningPoolStatsFetch::getPool() { + if (!currentPool) { + std::string poolName = preferences.getString("miningPoolName", DEFAULT_MINING_POOL_NAME).c_str(); + currentPool = PoolFactory::createPool(poolName); + } + return currentPool.get(); +} + +const MiningPoolInterface* MiningPoolStatsFetch::getPool() const { + return currentPool.get(); +} + +LogoData MiningPoolStatsFetch::getLogo() const { + if (const auto* pool = getPool()) { + return pool->getLogo(); + } + return LogoData{}; +} + +void MiningPoolStatsFetch::task() { std::string poolName = preferences.getString("miningPoolName", DEFAULT_MINING_POOL_NAME).c_str(); - auto poolInterface = PoolFactory::createPool(poolName); + auto* poolInterface = getPool(); + if (!poolInterface) return; std::string poolUser = preferences.getString("miningPoolUser", DEFAULT_MINING_POOL_USER).c_str(); // Main stats fetching loop - for (;;) - { + for (;;) { ulTaskNotifyTake(pdTRUE, portMAX_DELAY); HTTPClient http; http.setUserAgent(USER_AGENT); - - - + poolInterface->setPoolUser(poolUser); std::string apiUrl = poolInterface->getApiUrl(); http.begin(apiUrl.c_str()); - if (debugLogEnabled()) - { + if (debugLogEnabled()) { Serial.printf("Fetching mining pool stats from %s\r\n", apiUrl.c_str()); } poolInterface->prepareRequest(http); int httpCode = http.GET(); - if (httpCode == 200) - { + if (httpCode == 200) { String payload = http.getString(); JsonDocument doc; deserializeJson(doc, payload); - if (debugLogEnabled()) - { + if (debugLogEnabled()) { Serial.printf("Mining pool stats response: %s\r\n", payload.c_str()); } PoolStats stats = poolInterface->parseResponse(doc); + hashrate = stats.hashrate; - miningPoolStatsHashrate = stats.hashrate; - - if (debugLogEnabled()) - { + if (debugLogEnabled()) { Serial.printf("Mining pool stats parsed hashrate: %s\r\n", stats.hashrate.c_str()); } - if (stats.dailyEarnings) - { - miningPoolStatsDailyEarnings = *stats.dailyEarnings; - } - else - { - miningPoolStatsDailyEarnings = 0; // or any other default value - } + dailyEarnings = stats.dailyEarnings ? *stats.dailyEarnings : 0; - if (workQueue != nullptr && (ScreenHandler::getCurrentScreen() == SCREEN_MINING_POOL_STATS_HASHRATE || ScreenHandler::getCurrentScreen() == SCREEN_MINING_POOL_STATS_EARNINGS)) - { + if (workQueue != nullptr && (ScreenHandler::getCurrentScreen() == SCREEN_MINING_POOL_STATS_HASHRATE || + ScreenHandler::getCurrentScreen() == SCREEN_MINING_POOL_STATS_EARNINGS)) { WorkItem priceUpdate = {TASK_MINING_POOL_STATS_UPDATE, 0}; xQueueSend(workQueue, &priceUpdate, portMAX_DELAY); } - } - else - { - Serial.print( - F("Error retrieving mining pool data. HTTP status code: ")); + } else { + Serial.print(F("Error retrieving mining pool data. HTTP status code: ")); Serial.println(httpCode); } } } -void downloadMiningPoolLogoTask(void *pvParameters) { +void MiningPoolStatsFetch::downloadLogoTask() { std::string poolName = preferences.getString("miningPoolName", DEFAULT_MINING_POOL_NAME).c_str(); - auto poolInterface = PoolFactory::createPool(poolName); - PoolFactory::downloadPoolLogo(poolName, poolInterface.get()); + auto* poolInterface = getPool(); + if (!poolInterface) return; + + PoolFactory::downloadPoolLogo(poolName, poolInterface); // If we're on the mining pool stats screen, trigger a display update if (ScreenHandler::getCurrentScreen() == SCREEN_MINING_POOL_STATS_HASHRATE) { @@ -97,41 +100,22 @@ void downloadMiningPoolLogoTask(void *pvParameters) { xQueueSend(workQueue, &priceUpdate, portMAX_DELAY); } - xTaskNotifyGive(miningPoolStatsFetchTaskHandle); + xTaskNotifyGive(taskHandle); vTaskDelete(NULL); } -void setupMiningPoolStatsFetchTask() -{ - xTaskCreate(downloadMiningPoolLogoTask, +void MiningPoolStatsFetch::setup() { + xTaskCreate(downloadLogoTaskWrapper, "logoDownload", (6 * 1024), NULL, tskIDLE_PRIORITY, NULL); - xTaskCreate(taskMiningPoolStatsFetch, + xTaskCreate(taskWrapper, "miningPoolStatsFetch", (6 * 1024), NULL, tskIDLE_PRIORITY, - &miningPoolStatsFetchTaskHandle); -} - -std::unique_ptr& getMiningPool() -{ - static std::unique_ptr currentMiningPool; - - if (!currentMiningPool) { - std::string poolName = preferences.getString("miningPoolName", DEFAULT_MINING_POOL_NAME).c_str(); - currentMiningPool = PoolFactory::createPool(poolName); - } - - return currentMiningPool; -} - -LogoData getMiningPoolLogo() -{ - LogoData logo = getMiningPool()->getLogo(); - return logo; + &taskHandle); } diff --git a/src/lib/mining_pool_stats_fetch.hpp b/src/lib/mining_pool_stats_fetch.hpp index 7a84454..9ea351a 100644 --- a/src/lib/mining_pool_stats_fetch.hpp +++ b/src/lib/mining_pool_stats_fetch.hpp @@ -2,18 +2,44 @@ #include #include -#include "mining_pool/pool_factory.hpp" +#include +#include #include "lib/config.hpp" #include "lib/shared.hpp" +#include "lib/mining_pool/mining_pool_interface.hpp" +#include "mining_pool/pool_factory.hpp" -extern TaskHandle_t miningPoolStatsFetchTaskHandle; +class MiningPoolStatsFetch { +public: + static MiningPoolStatsFetch& getInstance() { + static MiningPoolStatsFetch instance; + return instance; + } -void setupMiningPoolStatsFetchTask(); -void taskMiningPoolStatsFetch(void *pvParameters); + void setup(); + std::string getHashRate() const; + int getDailyEarnings() const; + TaskHandle_t getTaskHandle() const { return taskHandle; } + static void taskWrapper(void* pvParameters); + static void downloadLogoTaskWrapper(void* pvParameters); + + // Pool interface methods + MiningPoolInterface* getPool(); + const MiningPoolInterface* getPool() const; + LogoData getLogo() const; -std::string getMiningPoolStatsHashRate(); -int getMiningPoolStatsDailyEarnings(); +private: + MiningPoolStatsFetch() = default; + ~MiningPoolStatsFetch() = default; + MiningPoolStatsFetch(const MiningPoolStatsFetch&) = delete; + MiningPoolStatsFetch& operator=(const MiningPoolStatsFetch&) = delete; -std::unique_ptr& getMiningPool(); -LogoData getMiningPoolLogo(); \ No newline at end of file + void task(); + void downloadLogoTask(); + + TaskHandle_t taskHandle = nullptr; + std::string hashrate; + int dailyEarnings = 0; + std::unique_ptr currentPool; +}; \ No newline at end of file diff --git a/src/lib/screen_handler.cpp b/src/lib/screen_handler.cpp index 27d83b9..0692c1d 100644 --- a/src/lib/screen_handler.cpp +++ b/src/lib/screen_handler.cpp @@ -231,9 +231,10 @@ void workerTask(void *pvParameters) { currentScreenValue != SCREEN_MINING_POOL_STATS_EARNINGS) break; taskEpdContent = (currentScreenValue == SCREEN_MINING_POOL_STATS_HASHRATE) ? - parseMiningPoolStatsHashRate(getMiningPoolStatsHashRate(), *getMiningPool()) : - parseMiningPoolStatsDailyEarnings(getMiningPoolStatsDailyEarnings(), - getMiningPool()->getDailyEarningsLabel(), *getMiningPool()); + parseMiningPoolStatsHashRate(MiningPoolStatsFetch::getInstance().getHashRate(), *MiningPoolStatsFetch::getInstance().getPool()) : + parseMiningPoolStatsDailyEarnings(MiningPoolStatsFetch::getInstance().getDailyEarnings(), + MiningPoolStatsFetch::getInstance().getPool()->getDailyEarningsLabel(), + *MiningPoolStatsFetch::getInstance().getPool()); setEpdContent(taskEpdContent); break; } diff --git a/src/lib/timers.cpp b/src/lib/timers.cpp index 3779c2d..4e67951 100644 --- a/src/lib/timers.cpp +++ b/src/lib/timers.cpp @@ -73,8 +73,9 @@ void IRAM_ATTR minuteTimerISR(void *arg) { vTaskNotifyGiveFromISR(bitaxeHandle, &xHigherPriorityTaskWoken); } - if (miningPoolStatsFetchTaskHandle != NULL) { - vTaskNotifyGiveFromISR(miningPoolStatsFetchTaskHandle, &xHigherPriorityTaskWoken); + TaskHandle_t miningPoolHandle = MiningPoolStatsFetch::getInstance().getTaskHandle(); + if (miningPoolHandle != NULL) { + vTaskNotifyGiveFromISR(miningPoolHandle, &xHigherPriorityTaskWoken); } if (xHigherPriorityTaskWoken == pdTRUE) {