Skip to content
This repository was archived by the owner on Jul 31, 2026. It is now read-only.

Commit 55bb006

Browse files
fix: signal gContainerFetchDone immediately after container fetch (not after twin)
Root cause of double CONTAINER_GET (seen in logs): - cloudWorkerTask was setting gContainerFetchDone=true AFTER the twin fetch - Twin fetch takes ~2s; scan timeout and twin completion landed at same tick - SCANNING phase exited to STABLE_WAIT before picking up gContainerFetchDone - wfContainerFetched stayed false → computeWeightAvailable did a sync HTTP call during SENDING, adding 500ms + one extra Firestore connection - The extra connection likely caused the subsequent PATCH HTTP=-1 failures Fix: - Set gContainerFetchDone=true immediately after container fetch result is stored - Set gContainerFetchPending=false at the same time - Twin fetch and uid_b container fetch continue afterward without blocking the signal - In SCANNING: check gTwinFetchDone independently (separate from gContainerFetchDone) so twin display still works even if SCANNING exits before twin fetch completes Result: with a ~5s scan window, container is signaled within 500ms, twin within 2.5s. No more double CONTAINER_GET. PATCH calls get clean connections. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent f7d14f0 commit 55bb006

1 file changed

Lines changed: 26 additions & 23 deletions

File tree

‎src/main.cpp‎

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@
2020
// §11 FIREBASE AUTHENTICATION 906– 1099
2121
// §12 FIRESTORE SCALE HEARTBEAT & SYNC 1100– 2030
2222
// §13 WEBSOCKET 2031– 2057
23-
// §14 5 — CLOUD WORKER TASK (non-blocking Firestore on core 0) 2058– 2098
24-
// §15 5 — UNIFIED WS FRAME BUILDER 2099– 2138
25-
// §16 WEIGHT FILTER HELPERS 2139– 2153
26-
// §17 POST-SEND STATE RESET (shared by all send paths) 2154– 2172
27-
// §18 SHARED WEIGHT PUSH HANDLER (used by /api/weight and /api/push-weight) 2173– 2245
28-
// §19 WEB SERVER 2246– 2946
29-
// §20 CLOUD COMMUNICATION 2947– 3129
30-
// §21 WEIGH WORKFLOW (IDLE → SCANNING → STABLE_WAIT → SENDING) 3130– 3364
31-
// §22 mDNS 3365– 3402
32-
// §23 SCALE 3403– 3494
33-
// §24 RFID 3495– 3824
34-
// §25 OTA — Over-the-air firmware + filesystem update 3825– 4193
35-
// §26 SETUP & LOOP 4194– 4566
23+
// §14 5 — CLOUD WORKER TASK (non-blocking Firestore on core 0) 2058– 2100
24+
// §15 5 — UNIFIED WS FRAME BUILDER 2101– 2140
25+
// §16 WEIGHT FILTER HELPERS 2141– 2155
26+
// §17 POST-SEND STATE RESET (shared by all send paths) 2156– 2174
27+
// §18 SHARED WEIGHT PUSH HANDLER (used by /api/weight and /api/push-weight) 2175– 2247
28+
// §19 WEB SERVER 2248– 2948
29+
// §20 CLOUD COMMUNICATION 2949– 3131
30+
// §21 WEIGH WORKFLOW (IDLE → SCANNING → STABLE_WAIT → SENDING) 3132– 3367
31+
// §22 mDNS 3368– 3405
32+
// §23 SCALE 3406– 3497
33+
// §24 RFID 3498– 3827
34+
// §25 OTA — Over-the-air firmware + filesystem update 3828– 4196
35+
// §26 SETUP & LOOP 4197– 4569
3636
//
3737
// To regenerate this block: ./scripts/update_toc.sh
3838
// ─── TOC END ───────────────────────────────────────────────
@@ -2070,7 +2070,11 @@ static void cloudWorkerTask(void* /*param*/) {
20702070
String uid_a = gContainerFetchUID; // local copy (thread-safe read)
20712071
float cw_a = readInventoryContainerWeight(uid_a);
20722072
gContainerFetchResult = cw_a;
2073-
// Prefetch twin tag for uid_a — avoids a TWIN_GET during SENDING (Case 1).
2073+
gContainerFetchPending = false;
2074+
gContainerFetchDone = true; // Signal immediately — SCANNING picks it up NOW,
2075+
// before twin fetch occupies the next ~2 seconds.
2076+
// Twin fetch continues after container is signaled.
2077+
// pushWeightToCloud will consume gTwinFetchResult from cache.
20742078
gTwinFetchUID = uid_a;
20752079
gTwinFetchResult = "";
20762080
readInventoryDocTwinTag(uid_a, gTwinFetchResult);
@@ -2082,8 +2086,6 @@ static void cloudWorkerTask(void* /*param*/) {
20822086
gContainerBFetchDone = true;
20832087
netLog("CONTAINER_B prefetch=" + String(gContainerBFetchResult,1) + "g uid=" + gContainerBFetchUID);
20842088
}
2085-
gContainerFetchPending = false; // clear before Done for coherent state
2086-
gContainerFetchDone = true;
20872089
}
20882090
// ── Cloud send ───────────────────────────────────────────────────────
20892091
if (gCloudSendPending) {
@@ -3233,18 +3235,19 @@ void handleWeighWorkflow(float w) {
32333235
if (gContainerFetchPending && gContainerBFetchUID.length() == 0 && lastUID2.length() > 0) {
32343236
gContainerBFetchUID = lastUID2;
32353237
}
3236-
// Pick up result when the worker task is done (non-blocking)
3238+
// Pick up container result as soon as it's ready (now signaled BEFORE twin fetch)
32373239
if (gContainerFetchDone) {
32383240
wfContainerWeight = gContainerFetchResult;
32393241
wfContainerFetched = true;
32403242
gContainerFetchDone = false;
32413243
gLastContainer = wfContainerWeight; // immediate OLED/WS update
3242-
// Pick up prefetched twin — only display if no physical 2nd reader detected
3243-
if (gTwinFetchDone && lastUID2.length() == 0) {
3244-
lastUIDTwin = gTwinFetchResult;
3245-
gTwinFetchDone = false;
3246-
netLog("TWIN display=" + (lastUIDTwin.length() > 0 ? lastUIDTwin : "none (no twin in DB)"));
3247-
}
3244+
}
3245+
// Pick up twin display result independently — twin fetch completes AFTER container.
3246+
// Check separately so we don't miss it if SCANNING exits before twin is ready.
3247+
if (gTwinFetchDone && lastUID2.length() == 0 && lastUIDTwin.length() == 0) {
3248+
lastUIDTwin = gTwinFetchResult;
3249+
gTwinFetchDone = false;
3250+
netLog("TWIN display=" + (lastUIDTwin.length() > 0 ? lastUIDTwin : "none (no twin in DB)"));
32483251
}
32493252

32503253
// Countdown scan

0 commit comments

Comments
 (0)