#include "NetManager.h" // ── WiFi ──────────────────────────────────────────────────── void NetManager::begin(const WiFiCred* creds, int count) { WiFi.mode(WIFI_STA); for(int i = 0; i < count; i++) _multi.addAP(creds[i].ssid, creds[i].pass); Serial.println("[WIFI] Connecting..."); unsigned long t0 = millis(); while(_multi.run() != WL_CONNECTED) { if(millis() - t0 > WIFI_CONNECT_TIMEOUT_MS) { Serial.println("[WIFI] Timeout!"); return; } delay(250); } Serial.printf("[WIFI] Connected: %s %s\n", getSSID().c_str(), getIP().c_str()); } bool NetManager::checkConnection() { if(WiFi.status() == WL_CONNECTED) return true; Serial.println("[WIFI] Reconnecting..."); return _multi.run(WIFI_CONNECT_TIMEOUT_MS) == WL_CONNECTED; } bool NetManager::isConnected() { return WiFi.status() == WL_CONNECTED; } String NetManager::getSSID() { return WiFi.SSID(); } String NetManager::getIP() { return WiFi.localIP().toString(); } int NetManager::getRSSI() { return WiFi.RSSI(); } // ── NTP ───────────────────────────────────────────────────── bool NetManager::syncNTP() { Serial.println("[NTP] Starting sync..."); if(!_ntp) _ntp = new NTPClient(_udp, "pool.ntp.org", 0, 60000); _ntp->begin(); _ntp->forceUpdate(); _ntpReady = _ntp->isTimeSet(); Serial.printf("[NTP] %s: %s UTC\n", _ntpReady ? "Synced" : "FAILED", _ntpReady ? _ntp->getFormattedTime().c_str() : "--"); return _ntpReady; } String NetManager::getTimeStr() { return (_ntp && _ntpReady) ? _ntp->getFormattedTime() : "??:??:??"; } // ── Diagnostics ───────────────────────────────────────────── bool NetManager::dnsCheck(const char* host) { IPAddress ip; bool ok = WiFi.hostByName(host, ip); Serial.printf("[NET] DNS %s: %s\n", ok ? "OK" : "FAIL", ok ? ip.toString().c_str() : ""); return ok; } bool NetManager::tlsCheck(const char* host) { WiFiClientSecure c; c.setInsecure(); c.setTimeout(HTTP_TIMEOUT_MS); bool ok = c.connect(host, 443); if(ok) c.stop(); Serial.printf("[NET] TLS %s\n", ok ? "OK" : "FAIL"); return ok; } // ── HTTP helpers ──────────────────────────────────────────── int NetManager::httpGet(const char* url, String& response) { WiFiClientSecure client; client.setInsecure(); client.setTimeout(HTTP_TIMEOUT_MS); HTTPClient http; http.setTimeout(HTTP_TIMEOUT_MS); http.begin(client, url); int code = http.GET(); if(code > 0) response = http.getString(); http.end(); return code; } int NetManager::httpPost(const char* url, const String& body) { WiFiClientSecure client; client.setInsecure(); client.setTimeout(HTTP_TIMEOUT_MS); HTTPClient http; http.setTimeout(HTTP_TIMEOUT_MS); http.begin(client, url); http.addHeader("Content-Type", "text/plain"); int code = http.POST(body); http.end(); return code; }