#include "aircraft_details.h" #include #include #include #include #include #include namespace AircraftDetails { namespace { SemaphoreHandle_t mutex = nullptr; char pendingHex[7] = {0}; bool hasPending = false; char cachedHex[7] = {0}; Info cached; void ensureMutex() { if (mutex == nullptr) mutex = xSemaphoreCreateMutex(); } bool httpGetString(WiFiClientSecure& client, const String& url, String& outBody) { HTTPClient http; http.setTimeout(5000); if (!http.begin(client, url)) return false; int code = http.GET(); bool ok = (code == HTTP_CODE_OK); if (ok) outBody = http.getString(); http.end(); return ok; } } void request(const char* hex) { ensureMutex(); xSemaphoreTake(mutex, portMAX_DELAY); if (strcmp(cachedHex, hex) != 0 && strcmp(pendingHex, hex) != 0) { strncpy(pendingHex, hex, sizeof(pendingHex) - 1); hasPending = true; } xSemaphoreGive(mutex); } Info get(const char* hex) { ensureMutex(); xSemaphoreTake(mutex, portMAX_DELAY); Info out; if (strcmp(cachedHex, hex) == 0) { out = cached; } else if (strcmp(pendingHex, hex) == 0 && hasPending) { out.loading = true; } xSemaphoreGive(mutex); return out; } void update() { ensureMutex(); char hex[7] = {0}; bool doWork = false; xSemaphoreTake(mutex, portMAX_DELAY); if (hasPending) { strncpy(hex, pendingHex, sizeof(hex) - 1); doWork = true; } xSemaphoreGive(mutex); if (!doWork) return; Info result; WiFiClientSecure client; client.setInsecure(); // hexdb.io zuerst versuchen (bisherige Quelle) - aber mit kuerzerem // Timeout (3s statt 5s), damit ein kompletter Ausfall des Dienstes die // ADS-B-Abfrage nicht unnoetig lange blockiert, bevor der Fallback greift. client.setTimeout(3000); String body; if (httpGetString(client, String("https://hexdb.io/api/v1/aircraft/") + hex, body)) { JsonDocument doc; DeserializationError err = deserializeJson(doc, body); if (!err) { const char* manufacturer = doc["Manufacturer"] | ""; const char* type = doc["Type"] | ""; if (manufacturer[0] && type[0]) { snprintf(result.model, sizeof(result.model), "%s %s", manufacturer, type); } else if (type[0]) { strncpy(result.model, type, sizeof(result.model) - 1); } } } // Fallback: hexdb.io war nicht erreichbar/lieferte kein Modell - // adsbdb.com als zweite, unabhaengige Quelle versuchen (andere API-Form, // aber inhaltlich aequivalent: Hersteller + Typ ueber den Hex-Code). if (!result.model[0]) { client.setTimeout(4000); String body2; if (httpGetString(client, String("https://api.adsbdb.com/v0/aircraft/") + hex, body2)) { JsonDocument doc2; DeserializationError err2 = deserializeJson(doc2, body2); if (!err2) { const char* manufacturer = doc2["response"]["aircraft"]["manufacturer"] | ""; const char* type = doc2["response"]["aircraft"]["type"] | ""; if (manufacturer[0] && type[0]) { snprintf(result.model, sizeof(result.model), "%s %s", manufacturer, type); } else if (type[0]) { strncpy(result.model, type, sizeof(result.model) - 1); } } } } xSemaphoreTake(mutex, portMAX_DELAY); strncpy(cachedHex, hex, sizeof(cachedHex) - 1); cached = result; hasPending = false; xSemaphoreGive(mutex); } }