#pragma once #include #include #include #include #include #ifndef LED_BUILTIN #define LED_BUILTIN 8 #endif #ifndef FACTORY_RESET_PIN #define FACTORY_RESET_PIN 4 #endif #ifndef FACTORY_RESET_ACTIVE_LEVEL #define FACTORY_RESET_ACTIVE_LEVEL LOW #endif #ifndef DS18B20_PIN #define DS18B20_PIN 3 #endif #ifndef PUMP_PIN #define PUMP_PIN 5 #endif #ifndef PUMP_ACTIVE_LEVEL #define PUMP_ACTIVE_LEVEL HIGH #endif #ifndef PROGRAM_DEFAULT_TARGET_C #define PROGRAM_DEFAULT_TARGET_C 22.0f #endif #ifndef PROGRAM_TOLERANCE_C #define PROGRAM_TOLERANCE_C 0.25f #endif #ifndef PROJECT_NAME #define PROJECT_NAME "TSL Bed Cooler" #endif extern const char *APP_VERSION; extern const char *PROJECT_NAME_VALUE; extern const char *DEFAULT_ADMIN; extern const char *DEFAULT_UPDATE_URL; extern const uint32_t FACTORY_RESET_HOLD_MS; extern const size_t MAX_USERS; extern const size_t MAX_TOKENS; extern const bool DEFAULT_LED_INVERTED; extern const byte DNS_PORT; extern const IPAddress SETUP_AP_IP; extern const IPAddress SETUP_AP_GATEWAY; extern const IPAddress SETUP_AP_SUBNET; extern const char *WWW_ROOT; extern const char *LOG_DIR; extern const char *LOG_FILE_PATH; enum LogLevel : uint8_t { LOG_ERROR = 0, LOG_WARN = 1, LOG_SECURITY_AUDIT = 2, LOG_INFO = 3, LOG_DEBUG = 4 }; enum ProgramMode : uint8_t { PROGRAM_OFF = 0, PROGRAM_COOL = 1, PROGRAM_WARM = 2 }; struct User { String name; String passwordHash; String roles; bool active; }; struct Token { String value; String user; String roles; uint32_t lastSeen; }; typedef void (*RouteHandler)(); struct ApiDef { const char *path; const char *method; const char *defaultRole; bool publicByDefault; RouteHandler handler; RouteHandler uploadHandler; }; extern WebServer server; extern DNSServer dnsServer; extern Preferences prefs; extern Token tokens[]; extern ApiDef apiDefs[]; extern const size_t API_DEF_COUNT; extern ApiDef customApiDefs[]; extern const size_t CUSTOM_API_DEF_COUNT; extern LogLevel currentLogLevel; extern size_t maxLogBytes; extern bool setupMode; extern uint8_t ledBrightness; extern bool ledInverted; extern bool pumpEnabled; extern ProgramMode programMode; extern float programTargetC; extern String updateUrl; extern String networkHostname; extern bool networkDhcp; extern String networkIp; extern String networkGateway; extern String networkSubnet; extern String networkDns1; extern String networkDns2; String jsonEscape(const String &s); String jsonOk(const String &payload = ""); String jsonError(const String &message); void sendJson(int code, const String &body); String requestBody(); String jsonStringValue(const String &json, const char *key, const String &fallback = ""); int jsonIntValue(const String &json, const char *key, int fallback = 0); bool jsonBoolValue(const String &json, const char *key, bool fallback = false); String jsonRolesValue(const String &json, const char *key, const String &fallback = ""); String chipId(); String deviceName(); String defaultDeviceName(); String sha256(const String &input); String passwordHash(const String &password); String prefString(const char *key, const String &fallback = ""); void loadSettings(); bool hasRole(const String &roles, const String &role); bool validName(const String &s); String cleanRoles(const String &roles); bool isKnownRole(const String &role); bool isSystemRole(const String &role); String allRoles(); String rolesJson(); bool addCustomRole(const String &role); bool deleteCustomRole(const String &role); size_t parseUsers(User *users, size_t maxUsers); void saveUsers(User *users, size_t count); bool findUser(const String &name, User &user); String createToken(const User &user); Token *currentToken(); ApiDef *findApi(const String &path, const String &method); String configuredRole(ApiDef *api); bool authorize(); void appendLog(LogLevel level, const String &message); void applyLed(); void applyPump(); void updateTemperatureSensor(); float currentTemperatureC(); String programModeName(); void updateProgram(); void factoryReset(); void checkFactoryResetPin(); bool connectWifi(); void handleSetupPage(); bool handleHtmlFileRequest(); void redirectToSetupPage(); void handleCaptiveProbe(); void handleFavicon(); void handleAdminPage(); void handleWifiScan(); void handleSetupSubmit(); void handleLogin(); void handleLogout(); void handleMe(); void handleUsersGet(); void handleUsersPost(); void handlePassword(); void handleRolesGet(); void handleRolesPost(); void handleRolesDelete(); void handleApisGet(); void handleApisPost(); void handleSettingsGet(); void handleSettingsPost(); void handleLogsGet(); void handleLogsClear(); void handleFilesList(); void handleFileDownload(); void handleFactoryReset(); void handleConfigExport(); void handleConfigImport(); void handleOtaCheck(); void handleOtaRun(); void handleUpdateUploadDone(); void handleUpdateUploadChunk(); void handleCertGet(); void handleCertPost(); void registerRoutes();