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"
TaskHandle_t bitaxeFetchTaskHandle;
uint64_t bitaxeHashrate;
uint64_t bitaxeBestDiff;
uint64_t getBitAxeHashRate()
{
return bitaxeHashrate;
void BitAxeFetch::taskWrapper(void* pvParameters) {
BitAxeFetch::getInstance().task();
}
uint64_t getBitaxeBestDiff()
{
return bitaxeBestDiff;
uint64_t BitAxeFetch::getHashRate() const {
return hashrate;
}
void taskBitaxeFetch(void *pvParameters)
{
for (;;)
{
uint64_t BitAxeFetch::getBestDiff() const {
return bestDiff;
}
void BitAxeFetch::task() {
for (;;) {
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
HTTPClient http;
@ -28,46 +23,38 @@ void taskBitaxeFetch(void *pvParameters)
int httpCode = http.GET();
if (httpCode == 200)
{
if (httpCode == 200) {
String payload = http.getString();
JsonDocument doc;
deserializeJson(doc, payload);
// Convert GH/s to H/s (multiply by 10^9)
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
std::string diffStr = doc["bestDiff"].as<std::string>();
char diffUnit = diffStr[diffStr.length() - 1];
if (std::isalpha(diffUnit)) {
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 {
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};
xQueueSend(workQueue, &priceUpdate, portMAX_DELAY);
}
}
else
{
Serial.print(
F("Error retrieving BitAxe data. HTTP status code: "));
} else {
Serial.print(F("Error retrieving BitAxe data. HTTP status code: "));
Serial.println(httpCode);
Serial.println(bitaxeApiUrl);
}
}
}
void setupBitaxeFetchTask()
{
xTaskCreate(taskBitaxeFetch, "bitaxeFetch", (3 * 1024), NULL, tskIDLE_PRIORITY,
&bitaxeFetchTaskHandle);
xTaskNotifyGive(bitaxeFetchTaskHandle);
void BitAxeFetch::setup() {
xTaskCreate(taskWrapper, "bitaxeFetch", (3 * 1024), NULL, tskIDLE_PRIORITY, &taskHandle);
xTaskNotifyGive(taskHandle);
}

View file

@ -7,10 +7,28 @@
#include "lib/config.hpp"
#include "lib/shared.hpp"
extern TaskHandle_t bitaxeFetchTaskHandle;
class BitAxeFetch {
public:
static BitAxeFetch& getInstance() {
static BitAxeFetch instance;
return instance;
}
void setupBitaxeFetchTask();
void taskBitaxeFetch(void *pvParameters);
void setup();
uint64_t getHashRate() const;
uint64_t getBestDiff() const;
static void taskWrapper(void* pvParameters);
TaskHandle_t getTaskHandle() const { return taskHandle; }
uint64_t getBitAxeHashRate();
uint64_t getBitaxeBestDiff();
private:
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))
{
setupBitaxeFetchTask();
BitAxeFetch::getInstance().setup();
}
if (preferences.getBool("miningPoolStats", DEFAULT_MINING_POOL_STATS_ENABLED))
@ -221,7 +221,7 @@ void setupWifi()
// waitUntilNoneBusy();
// 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
// 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

View file

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

View file

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