Register /gpu-fan handler in setup() using global server variable

- Add webHandlerRegistered flag to prevent duplicate registration
- Call registerWebHandler() in both setup() and connected()
- Use global 'server' variable directly with lambda
- Removed unused methods
This commit is contained in:
dawie 2026-01-30 19:23:36 +02:00
parent e5b02c65e8
commit 7816ecf949

View File

@ -41,6 +41,7 @@ class GPUFanControllerUsermod : public Usermod {
// Configuration // Configuration
bool enabled = true; bool enabled = true;
bool initDone = false; bool initDone = false;
bool webHandlerRegistered = false;
int8_t pwmPin = PWM_FAN_PIN; int8_t pwmPin = PWM_FAN_PIN;
#ifdef ARDUINO_ARCH_ESP32 #ifdef ARDUINO_ARCH_ESP32
@ -219,9 +220,16 @@ class GPUFanControllerUsermod : public Usermod {
} }
} }
// Serve the curve editor page // Register web handler - call this once
static void serveGpuFanPage(AsyncWebServerRequest *request) { void registerWebHandler() {
request->send_P(200, "text/html", GPU_FAN_HTML); if (webHandlerRegistered) return;
server.on("/gpu-fan", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send_P(200, "text/html", GPU_FAN_HTML);
});
webHandlerRegistered = true;
DEBUG_PRINTLN(F("GPU Fan: Web handler registered at /gpu-fan"));
} }
public: public:
@ -231,11 +239,16 @@ class GPUFanControllerUsermod : public Usermod {
setFanPWM((fixedSpeedPct * 255) / 100); // Initial speed setFanPWM((fixedSpeedPct * 255) / 100); // Initial speed
lastTempUpdate = millis(); lastTempUpdate = millis();
initDone = true; initDone = true;
// Register web handler immediately during setup
registerWebHandler();
DEBUG_PRINTLN(F("GPU Fan Controller initialized")); DEBUG_PRINTLN(F("GPU Fan Controller initialized"));
} }
void connected() override { void connected() override {
// Nothing special needed on WiFi connect // Also try to register here in case setup was too early
registerWebHandler();
} }
void loop() override { void loop() override {
@ -265,16 +278,6 @@ class GPUFanControllerUsermod : public Usermod {
return false; return false;
} }
// Register the /gpu-fan endpoint
void addToJsonState(JsonObject& root) override {
// This is called during setup, we can use it to register our endpoint
}
// Called when web server is ready - register our custom page
void onStateChange(uint8_t callMode) override {
// Not the best place, but works
}
// Add info to the WLED info panel // Add info to the WLED info panel
void addToJsonInfo(JsonObject& root) override { void addToJsonInfo(JsonObject& root) override {
JsonObject user = root["u"]; JsonObject user = root["u"];
@ -291,7 +294,7 @@ class GPUFanControllerUsermod : public Usermod {
uiDomString += F("}});\"><i class=\"icons "); uiDomString += F("}});\"><i class=\"icons ");
uiDomString += enabled ? "on" : "off"; uiDomString += enabled ? "on" : "off";
uiDomString += F("\">&#xe08f;</i></button>"); uiDomString += F("\">&#xe08f;</i></button>");
uiDomString += F(" <a href=\"/gpu-fan\" style=\"color:#e94560;font-size:0.9em;\">[Curve Editor]</a>"); uiDomString += F(" <a href=\"/gpu-fan\" target=\"_blank\" style=\"color:#e94560;font-size:0.9em;\">[Editor]</a>");
infoArr.add(uiDomString); infoArr.add(uiDomString);
if (enabled) { if (enabled) {
@ -450,11 +453,6 @@ class GPUFanControllerUsermod : public Usermod {
return !top["curve-t1"].isNull(); return !top["curve-t1"].isNull();
} }
// Register web handler
void addToWebHandlers(AsyncWebServer& server) {
server.on("/gpu-fan", HTTP_GET, serveGpuFanPage);
}
uint16_t getId() override { uint16_t getId() override {
return USERMOD_ID_UNSPECIFIED; // Change this if you add to const.h return USERMOD_ID_UNSPECIFIED; // Change this if you add to const.h
} }