Speed up USB availability after unchanged Nextcloud sync
This commit is contained in:
1 parent
6561da3903
commit
ef0dcef94c
2 files changed
+113
-14
No files matched your search
@@ -263,6 +263,14 @@ Boot -> WLAN verbinden -> Nextcloud-Ordner abfragen (WebDAV) ->
|
||||
neue/aktualisierte .tap-Dateien herunterladen -> USB startet mit sichtbarem Stick
|
||||
```
|
||||
|
||||
Der erste Start nach dieser Firmware-Aktualisierung laedt die Dateien einmal
|
||||
vollstaendig und kann deshalb weiterhin laenger dauern. Danach merkt sich der
|
||||
ESP die Nextcloud-ETags erfolgreicher Downloads. Bei weiteren Starts liest er
|
||||
die lokalen Dateien kurz zur Kontrolle und ueberspringt unveraenderte
|
||||
Downloads. Neue oder geaenderte Dateien werden weiterhin vor der USB-Anmeldung
|
||||
uebernommen. `GET /status` zeigt mit `boot_sync_ms` und `usb_ready_ms` die
|
||||
Dauer des letzten Abgleichs und den Zeitpunkt der USB-Anmeldung seit Boot.
|
||||
|
||||
Das passt zum bestehenden Workflow: der Plasmaschneider wird zwischen
|
||||
Auftraegen ausgeschaltet und der ESP32 haengt am nativen USB-Port des
|
||||
Controllers, wird also beim Wiedereinschalten mit neu gestartet - genau in
|
||||
|
||||
+105
-14
@@ -123,6 +123,8 @@ static bool g_mediaReannounceInProgress = false;
|
||||
static uint32_t g_mediaRestoreAtMs = 0;
|
||||
static uint32_t g_nextUsbBeginAttemptMs = 0;
|
||||
static uint32_t g_mediaReannounceCount = 0;
|
||||
static uint32_t g_bootSyncMs = 0;
|
||||
static uint32_t g_usbReadyMs = 0;
|
||||
static const uint32_t USB_MEDIA_REANNOUNCE_DELAY_MS = 350;
|
||||
static const uint32_t USB_BEGIN_RETRY_MS = 5000;
|
||||
// Verfolgt den tatsaechlichen Mount-Zustand unabhaengig von g_state, damit ein
|
||||
@@ -367,6 +369,7 @@ static bool startUsbMassStorage() {
|
||||
Serial.println("FEHLER: USB konnte nicht gestartet werden; neuer Versuch folgt");
|
||||
return false;
|
||||
}
|
||||
g_usbReadyMs = millis();
|
||||
Serial.println("USB-MSC gestartet");
|
||||
return true;
|
||||
}
|
||||
@@ -447,6 +450,53 @@ static String urlDecode(const String &in) {
|
||||
return out;
|
||||
}
|
||||
|
||||
// Nextcloud liefert ETags bereits in der einen PROPFIND-Antwort. Im NVS wird
|
||||
// pro Datei das zuletzt vollstaendig heruntergeladene ETag zusammen mit einem
|
||||
// Fingerabdruck der lokalen Datei gespeichert. So kosten unveraenderte Dateien
|
||||
// beim naechsten Boot nur einen kurzen SD-Lesezugriff statt je eines TLS-GETs.
|
||||
static String xmlProperty(const String &response, const char *name) {
|
||||
String openTag = String("<d:") + name + ">";
|
||||
String closeTag = String("</d:") + name + ">";
|
||||
int start = response.indexOf(openTag);
|
||||
if (start < 0) return "";
|
||||
start += openTag.length();
|
||||
int end = response.indexOf(closeTag, start);
|
||||
return end < 0 ? "" : response.substring(start, end);
|
||||
}
|
||||
|
||||
static String syncCacheKey(const String &filename) {
|
||||
uint32_t hash = 2166136261UL;
|
||||
for (size_t i = 0; i < filename.length(); ++i) {
|
||||
hash = (hash ^ (uint8_t)filename[i]) * 16777619UL;
|
||||
}
|
||||
char key[10]; // NVS-Schluessel duerfen hoechstens 15 Zeichen haben.
|
||||
snprintf(key, sizeof(key), "e%08lx", (unsigned long)hash);
|
||||
return String(key);
|
||||
}
|
||||
|
||||
static bool fileFingerprint(const String &filename, uint32_t &size, uint32_t &hash) {
|
||||
String path = String(BASE_PATH) + "/" + filename;
|
||||
FILE *in = fopen(path.c_str(), "rb");
|
||||
if (!in) return false;
|
||||
size = 0;
|
||||
hash = 2166136261UL;
|
||||
uint8_t buf[1024];
|
||||
size_t count;
|
||||
while ((count = fread(buf, 1, sizeof(buf), in)) > 0) {
|
||||
size += count;
|
||||
for (size_t i = 0; i < count; ++i) hash = (hash ^ buf[i]) * 16777619UL;
|
||||
}
|
||||
bool ok = !ferror(in);
|
||||
fclose(in);
|
||||
return ok;
|
||||
}
|
||||
|
||||
static String syncCacheRecord(const String &filename, const String &etag,
|
||||
uint32_t size, uint32_t hash) {
|
||||
// Der Dateiname im Wert schuetzt auch bei einer Kollision des kurzen Keys.
|
||||
return filename + "\n" + etag + "\n" + String(size) + "\n" + String(hash, HEX);
|
||||
}
|
||||
|
||||
static bool fetchAndSaveFile(WiFiClientSecure &client, const String &url, const String &filename) {
|
||||
HTTPClient http;
|
||||
if (!http.begin(client, url)) return false;
|
||||
@@ -476,11 +526,15 @@ static bool fetchAndSaveFile(WiFiClientSecure &client, const String &url, const
|
||||
uint8_t buf[1024];
|
||||
unsigned long lastData = millis();
|
||||
bool timedOut = false;
|
||||
bool writeFailed = false;
|
||||
while (http.connected() && (remaining > 0 || remaining == -1)) {
|
||||
size_t avail = stream->available();
|
||||
if (avail) {
|
||||
int c = stream->readBytes(buf, avail > sizeof(buf) ? sizeof(buf) : avail);
|
||||
fwrite(buf, 1, c, out);
|
||||
if (fwrite(buf, 1, c, out) != (size_t)c) {
|
||||
writeFailed = true;
|
||||
break;
|
||||
}
|
||||
if (remaining > 0) remaining -= c;
|
||||
lastData = millis();
|
||||
} else if (millis() - lastData > 10000) {
|
||||
@@ -490,10 +544,10 @@ static bool fetchAndSaveFile(WiFiClientSecure &client, const String &url, const
|
||||
delay(1);
|
||||
}
|
||||
}
|
||||
fclose(out);
|
||||
if (fclose(out) != 0) writeFailed = true;
|
||||
http.end();
|
||||
|
||||
bool complete = !timedOut && (remaining == 0 || remaining == -1);
|
||||
bool complete = !timedOut && !writeFailed && (remaining == 0 || remaining == -1);
|
||||
if (!complete) {
|
||||
Serial.printf("Nextcloud: Download von '%s' unvollstaendig, verwerfe Teildatei\n", filename.c_str());
|
||||
remove(tmpPath.c_str());
|
||||
@@ -501,7 +555,11 @@ static bool fetchAndSaveFile(WiFiClientSecure &client, const String &url, const
|
||||
}
|
||||
|
||||
remove(finalPath.c_str()); // vorherige Version ersetzen
|
||||
rename(tmpPath.c_str(), finalPath.c_str());
|
||||
if (rename(tmpPath.c_str(), finalPath.c_str()) != 0) {
|
||||
Serial.printf("Nextcloud: '%s' konnte nicht aktiviert werden\n", filename.c_str());
|
||||
remove(tmpPath.c_str());
|
||||
return false;
|
||||
}
|
||||
Serial.printf("Nextcloud: '%s' heruntergeladen\n", filename.c_str());
|
||||
return true;
|
||||
}
|
||||
@@ -528,9 +586,13 @@ static bool syncFromNextcloud() {
|
||||
String xml = http.getString();
|
||||
http.end();
|
||||
|
||||
Preferences cache;
|
||||
bool cacheOpen = cache.begin("sync-etags", false);
|
||||
|
||||
int pos = 0;
|
||||
int candidates = 0;
|
||||
int downloaded = 0;
|
||||
int unchanged = 0;
|
||||
int failed = 0;
|
||||
// Liste aller aktuell in Nextcloud vorhandenen Dateinamen (mit "|" umrahmt,
|
||||
// damit ein Teilstring-Vergleich keine falschen Treffer liefert) - fuer den
|
||||
// Spiegel-Abgleich weiter unten.
|
||||
@@ -545,16 +607,38 @@ static bool syncFromNextcloud() {
|
||||
pos = end + 9;
|
||||
|
||||
if (!href.endsWith(".tap") && !href.endsWith(".TAP")) continue;
|
||||
candidates++;
|
||||
int slash = href.lastIndexOf('/');
|
||||
String encodedName = slash >= 0 ? href.substring(slash + 1) : href;
|
||||
String filename = sanitizeFilename(urlDecode(encodedName));
|
||||
expected += filename + "|";
|
||||
|
||||
String fileUrl = "https://" + String(NEXTCLOUD_HOST) + href;
|
||||
if (fetchAndSaveFile(client, fileUrl, filename)) downloaded++;
|
||||
int responseEnd = xml.indexOf("</d:response>", end);
|
||||
String etag = responseEnd < 0 ? "" : xmlProperty(xml.substring(end + 9, responseEnd), "getetag");
|
||||
String cacheKey = syncCacheKey(filename);
|
||||
uint32_t localSize = 0;
|
||||
uint32_t localHash = 0;
|
||||
if (cacheOpen && etag.length() > 0 &&
|
||||
fileFingerprint(filename, localSize, localHash) &&
|
||||
cache.getString(cacheKey.c_str(), "") ==
|
||||
syncCacheRecord(filename, etag, localSize, localHash)) {
|
||||
unchanged++;
|
||||
continue;
|
||||
}
|
||||
Serial.printf("Nextcloud-Sync: %d/%d Datei(en) aktualisiert\n", downloaded, candidates);
|
||||
|
||||
String fileUrl = "https://" + String(NEXTCLOUD_HOST) + href;
|
||||
if (fetchAndSaveFile(client, fileUrl, filename)) {
|
||||
downloaded++;
|
||||
if (cacheOpen && etag.length() > 0 && fileFingerprint(filename, localSize, localHash)) {
|
||||
cache.putString(cacheKey.c_str(), syncCacheRecord(filename, etag, localSize, localHash));
|
||||
} else if (cacheOpen) {
|
||||
cache.remove(cacheKey.c_str());
|
||||
}
|
||||
} else {
|
||||
failed++;
|
||||
}
|
||||
}
|
||||
Serial.printf("Nextcloud-Sync: %d aktualisiert, %d unveraendert, %d fehlgeschlagen\n",
|
||||
downloaded, unchanged, failed);
|
||||
|
||||
// Spiegel-Sync: lokale .tap-Dateien loeschen, die nicht mehr in Nextcloud
|
||||
// liegen. Echte Verbindungs-/Auth-Fehler wurden oben beim PROPFIND schon
|
||||
@@ -586,15 +670,18 @@ static bool syncFromNextcloud() {
|
||||
String name = toRemove.substring(p, bar);
|
||||
p = bar + 1;
|
||||
String path = String(BASE_PATH) + "/" + name;
|
||||
remove(path.c_str());
|
||||
if (remove(path.c_str()) == 0) {
|
||||
if (cacheOpen) cache.remove(syncCacheKey(name).c_str());
|
||||
removed++;
|
||||
Serial.printf("Nextcloud-Sync: '%s' lokal entfernt (nicht mehr in Nextcloud)\n", name.c_str());
|
||||
} else {
|
||||
failed++;
|
||||
Serial.printf("Nextcloud-Sync: '%s' konnte nicht entfernt werden\n", name.c_str());
|
||||
}
|
||||
}
|
||||
if (removed > 0) Serial.printf("Nextcloud-Sync: %d Datei(en) entfernt\n", removed);
|
||||
|
||||
// Nur dann als Fehlschlag melden, wenn Dateien gefunden wurden, aber keine
|
||||
// einzige davon heruntergeladen werden konnte (z.B. Zugangsdaten falsch).
|
||||
return candidates == 0 || downloaded > 0;
|
||||
if (cacheOpen) cache.end();
|
||||
return failed == 0;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
@@ -685,6 +772,8 @@ static void handleStatus() {
|
||||
j += "\"usb_last_event_id\":" + String(g_lastUsbEventId) + ",";
|
||||
j += "\"usb_reannounce_count\":" + String(g_mediaReannounceCount) + ",";
|
||||
j += "\"storage_ready\":" + String(g_storageReady ? "true" : "false") + ",";
|
||||
j += "\"boot_sync_ms\":" + String(g_bootSyncMs) + ",";
|
||||
j += "\"usb_ready_ms\":" + String(g_usbReadyMs) + ",";
|
||||
j += "\"sector_size\":" + String(g_sectorSize) + ",";
|
||||
j += "\"sector_count\":" + String(g_sectorCount);
|
||||
j += "}";
|
||||
@@ -939,7 +1028,9 @@ void setup() {
|
||||
// Plasmaschneider sichtbar wird (typischerweise bei jedem Einschalten,
|
||||
// da der ESP32 ueber den USB/OTG-Port vom Cutter mitversorgt wird).
|
||||
if (g_storageReady) {
|
||||
uint32_t syncStart = millis();
|
||||
syncFromNextcloud();
|
||||
g_bootSyncMs = millis() - syncStart;
|
||||
} else {
|
||||
Serial.println("Nextcloud-Sync uebersprungen: SD-Speicher nicht bereit");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user