Files
nano-fcp/docs/peer_sync_handoff.md

11 KiB
Raw Permalink Blame History

Handoff: FCP → zenai_nano_fcp batch sync

Status: FCP side is DONE and shipped. zenai side is NOT done yet.

0. Test topology (read first)

Role Host Ports Notes
FCP mirror (test target) 192.168.192.89 operator :5000, office :5721 Repo checkout + systemd services (karung-counter, karung-counter-dashboard) run here. Use this host for all manual start/stop tests.
zenai_nano_fcp (peer counter, NanoPi) 192.168.192.209 dashboard :7000, internal control 127.0.0.1:5000, camera RTSP :8554 Peer API lives here. PEER_API_URL=http://192.168.192.209:7000
LIVE FCP other host (unknown yet) — DO NOT touch. No deploy, no restart, no batch start/stop there until the owner says so.

Rules for the zenai-side developer:

  • Test only against 192.168.192.89 (FCP mirror) and 192.168.192.209 (peer).
  • The live FCP host is out of scope. Do not point PEER_API_URL at it.
  • PEER_API_URL lives in the FCP .env (gitignored) — never commit it.
  • The zenai dashboard currently has no active batch and sits in manual mode; a test start/stop pair is safe.
  • FCP mirror ports: operator UI http://192.168.192.89:5000, office UI http://192.168.192.89:5721 (office only: mode / model_mode / require_plate / require_do writes, batch delete, OCR engine).
  • FCP mirror batch mode was switched auto → manual on 2026-11-13 for these tests (/opt/jetson-counter/batch_mode.json). Manual start/stop is rejected in auto mode (HTTP 409 wrong_mode).

FCP is the source of truth for counts. FCP pushes every manual batch start/stop to the zenai counter (RK3588/NanoPi device, /opt/feedmill, dashboard port 7000) so both sides count the same load.

Until the zenai work below is implemented, FCP already sends the requests and zenai answers with its single-label behaviour. FCP detects that and shows the operator a warning (Peer hanya menerima satu label (sack) …).

1. What FCP sends

Base URL from FCP .env:

PEER_API_URL=http://192.168.192.209:7000   # zenai peer (empty => peer sync disabled)
PEER_API_TOKEN=<shared secret>              # -> X-API-Key header (value in docs/peer_sync_token.txt)
PEER_API_TIMEOUT=5                          # seconds, per request

Requests are plain JSON POSTs. Headers: Content-Type: application/json, X-API-Key: <PEER_API_TOKEN>.

1.1 Start — POST {PEER_API_URL}/api/batch/start

{
  "source": "fcp-semarang",
  "object_label": "sack",
  "object_labels": ["sack", "box"],
  "fcp_batch_number": 12,
  "counting_date": "2026-11-13",
  "plate": "B 1234 XYZ",
  "do_numbers": ["DO-2026-0011"],
  "expected_sack": 40,
  "expected_box": 10,
  "started_at": "2026-11-13T09:12:03"
}

1.2 Stop — POST {PEER_API_URL}/api/batch/stop

{
  "source": "fcp-semarang",
  "fcp_batch_number": 12,
  "counting_date": "2026-11-13",
  "plate": "B 1234 XYZ",
  "net_sack": 38,
  "net_box": 9,
  "ended_at": "2026-11-13T10:04:57"
}

net_sack / net_box are FCP's final numbers — reference only. zenai keeps its own counts; FCP stays authoritative for reporting.

2. What FCP expects back

FCP accepts the existing zenai reply shape, no change required:

{"success": true, "message": "Started Batch #7 (SACK)", "batch_number": 7,
 "object_label": "sack", "status": "running"}

Rules FCP applies to the reply:

Reply FCP reaction
HTTP 200 + success: true ok
HTTP 200 + success: false, error contains No active batch (stop only) ok — peer was already stopped
HTTP 200 + success: false, other error failed, warning shown
HTTP 4xx failed, no retry
HTTP 5xx / timeout / refused failed after 2 attempts (0.5 s apart), warning shown

Peer failures never block the FCP batch (fail-soft):

  • start failed → zenai counts 0 for that batch; operator informed.
  • stop failed → zenai keeps counting; operator must stop zenai manually.

FCP also raises a warning when it asked for several labels (object_labels) but the reply only carries a single object_label — that means the peer still counts one class only.

3. Work on the zenai side (not yet done)

Repo: /home/jetson/Feedmill_Semarang_Project/zenai_nano_fcp (device path /opt/feedmill).

3.1 Multi-label batches (required)

Today batch_store.py is single-label:

  • _normalize_batch_label() accepts only sack / box.
  • start_new_batch() stores one object_label and one count.
  • record_counted_object_crossing() ignores any crossing whose label does not equal the active batch label.

Needed: one batch that counts sack and box separately.

  • _normalize_batch_label → normalise a list of labels (object_labels: ["sack", "box"]), keep the old single object_label field working for the zenai UI (it may send object_label: "sack").
  • Batch state: keep counts: {"sack": N, "box": M} (total = sum(counts.values())). Migrate the old count int for backward compatibility.
  • record_counted_object_crossing(track_id, object_label): count the crossing when the label is in the active batch's label set; keep per-label counters. The counted-event dedup key must include the label (f"{label}:{track_id}"), otherwise sack and box IDs collide.
  • end_batch() must persist per-label counts (DB migration needed for the batches table: add counts_json or box_count/sack_count columns).
  • Batch discard rule ("discard when 0 counted") must consider the total.

3.2 Forward the request body (required)

counter_dashboard.py::_send_counter_command() posts an empty body, so the JSON FCP sends is dropped before it reaches counter_control.py.

  • api_batch_start must forward the full JSON body to http://127.0.0.1:5000/api/batch/start (and api_batch_stop likewise).
  • counter_control.py must read the body: object_labels (list) or object_label (single), plus the metadata (source, fcp_batch_number, counting_date, plate, do_numbers, expected_sack, expected_box).
  • Store the metadata on the zenai batch row (new columns or a JSON blob) so zenai history can be joined with FCP history by source + fcp_batch_number.

3.3 API key (now sent by FCP)

  • FCP sends X-API-Key: <PEER_API_TOKEN> on every start/stop request.
  • The shared secret lives in docs/peer_sync_token.txt (gitignored) — copy that file together with this handoff. Same value is in the FCP .env.
  • Accept X-API-Key on /api/batch/start and /api/batch/stop.
  • Validate only when the header is present, so the zenai dashboard UI keeps working unchanged.
  • Reject with HTTP 401 + {"success": false, "error": "unauthorized"} when the header is present but wrong. FCP treats 4xx as failed (no retry) and warns.
  • Start when a batch is already active → return the existing batch, HTTP 200 (status: "running"). Already the case in counter_control.py; keep it.
  • Stop with no active batch → HTTP 200 {"success": false, "error": "No active batch to stop"}. Already the case; keep it (FCP treats it as success).
  • FCP re-sends a start when the operator presses "Mulai" twice; the peer must not create a second batch.

When the peer supports several labels it should answer with both:

{"success": true, "batch_number": 7, "object_labels": ["sack", "box"],
 "counts": {"sack": 12, "box": 3}, "status": "running"}

FCP stops warning when object_labels is present in the reply.

4. Verification checklist (zenai side)

# start (multi-label + metadata + api key)
curl -s -X POST http://127.0.0.1:7000/api/batch/start \
  -H 'Content-Type: application/json' \
  -H 'X-API-Key: <shared secret>' \
  -d '{"source":"fcp-semarang","object_label":"sack","object_labels":["sack","box"],
       "fcp_batch_number":12,"counting_date":"2026-11-13","plate":"B 1234 XYZ",
       "do_numbers":["DO-2026-0011"],"expected_sack":40,"expected_box":10,
       "started_at":"2026-11-13T09:12:03"}'

# stop
curl -s -X POST http://127.0.0.1:7000/api/batch/stop \
  -H 'Content-Type: application/json' -H 'X-API-Key: <shared secret>' \
  -d '{"source":"fcp-semarang","fcp_batch_number":12,"counting_date":"2026-11-13",
       "plate":"B 1234 XYZ","net_sack":38,"net_box":9,"ended_at":"2026-11-13T10:04:57"}'

Check:

  1. Reply has success: true and object_labels: ["sack","box"].
  2. A sack crossing and a box crossing both raise the total count, and the per-label counters differ.
  3. The stored batch row keeps source / fcp_batch_number / plate / DO numbers.
  4. Stop replies with per-label counts and the row is persisted.

FCP side (from the Jetson, PEER_API_URL set):

# manual batch start/stop in the operator UI -> watch the FCP log
journalctl -u karung-counter-dashboard | grep PEER

5. Verified test results (FCP mirror, 2026-11-13)

Full operator-button → zenai chain tested against the real peer (192.168.192.209:7000), FCP mirror 192.168.192.89 in manual batch mode:

Step FCP operator call FCP result zenai result
start POST http://192.168.192.89:5000/api/batch/start {"plate":"B 9999 TEST"} batch #12 started, peer_sync.ok=true, warning Peer hanya menerima satu label (sack) … batch #4 running, object_label: "sack"
stop POST http://192.168.192.89:5000/api/batch/stop batch #12 closed, peer_sync.ok=true batch #4 stopped, count: 0

Reproduce on the mirror:

curl -s -X POST http://192.168.192.89:5000/api/batch/start \
  -H 'Content-Type: application/json' -d '{"plate":"B 9999 TEST"}'
curl -s -X POST http://192.168.192.89:5000/api/batch/stop
curl -s http://192.168.192.209:7000/api/batch/status
journalctl -u karung-counter-dashboard | grep PEER

FCP mirror side effects from that test (harmless, mirror only): batch #12 in the mirror DB with plate B9999TEST, and the mirror batch mode left on manual.

5.1 zenai defects found during that test

  1. Single label only. Reply carries object_label: "sack"; FCP asked for object_labels: ["sack","box"]. → §3.1.
  2. Request body dropped. counter_dashboard.py::_send_counter_command() posts an empty body, so plate / do_numbers / expected_* / fcp_batch_number never reach counter_control.py. → §3.2.
  3. Stopped batch not persisted. The stop reply was {"success": true, "count": 0, "persisted": false}. GET /api/recent-batches on zenai still shows batches 1–3 for 2026-11-13 — test batch #4 is missing from zenai history. end_batch() reports the DB write as failed. Must be fixed on zenai, otherwise every synced batch vanishes from zenai history.
  4. No X-API-Key check. FCP now always sends the header (random secret, see docs/peer_sync_token.txt), but zenai ignores it — no 401 path exists yet. → §3.3.

6. Where the FCP code lives

File Role
src/peer_sync.py outbound client (stdlib urllib, retry, fail-soft)
counter_dashboard.py hooks in api_batch_start / api_batch_stop
templates/operator.html, templates/monitoring.html operator warning
tests/test_peer_sync.py mock-peer tests
docs/configuration.md env keys

FCP sends only for manual and do_manual batch modes; auto mode is not synced (auto batches are started by the pipeline, not by the operator).