# counter_dashboard.py — API Documentation ## Page ### `GET /` Renders the main dashboard HTML page (`dashboard.html`). --- ## API Endpoints ### `GET /api/live-video` MJPEG streaming endpoint. Serves live frames from the path configured in `LIVE_STREAM_FRAME_PATH` (default `/dev/shm/feedmill/live_frame.jpg`) as `multipart/x-mixed-replace`. **Response:** JPEG stream with `--frame` boundary delimiters. --- ### `GET /api/current-batch` Returns the currently active batch's data or idle state with next batch number. **Success response (running):** ```json { "success": true, "active": true, "status": "running", "batch_mode": "manual", "counting_date": "2024-01-01", "batch_number": 12, "object_label": "mixed", "count": 342, "sack_count": 210, "box_count": 132, "object_labels": ["sack", "box"], "counts": {"sack": 210, "box": 132}, "plate": null, "source": null, "fcp_batch_number": null, "start_time": "2024-01-01T14:30:00", "last_detection_time": "2024-01-01T14:35:00" } ``` `count` is the combined total. `sack_count` and `box_count` are the separate class totals. **Stopped response (no active batch, 200):** ```json { "success": false, "active": false, "status": "stopped", "batch_mode": "manual", "error": "No active batch", "count": 0, "sack_count": 0, "box_count": 0, "object_labels": [], "counts": {"sack": 0, "box": 0}, "plate": null, "source": null, "fcp_batch_number": null, "batch_number": null, "next_batch_number": 13, "counting_date": "2024-01-01" } ``` --- ### `POST /api/batch/start` Manually starts a new unified batch on the counter engine. One batch counts sacks and boxes together and reports separate class totals. **Success response (200):** ```json { "success": true, "message": "Started Batch #13", "batch_number": 13, "object_label": "mixed", "object_labels": ["sack", "box"], "count": 0, "sack_count": 0, "box_count": 0, "counts": {"sack": 0, "box": 0}, "plate": null, "status": "running" } ``` **Legacy aliases:** `POST /api/batch/start/sack` and `POST /api/batch/start/box` still work and start the same unified batch. The class argument is ignored. **Error response (503 if engine offline):** ```json { "success": false, "error": "Engine counter tidak aktif. Pastikan service feedmill-counter berjalan." } ``` --- ### `POST /api/batch/stop` Manually stops the currently active batch, saves records to SQLite (`batches` and `daily_summaries`), and returns state to idle. **Success response (200):** ```json { "success": true, "message": "Batch #13 stopped (342 items)", "batch_number": 13, "object_label": "mixed", "count": 342, "sack_count": 210, "box_count": 132, "object_labels": ["sack", "box"], "counts": {"sack": 210, "box": 132}, "plate": null, "persisted": true, "status": "stopped" } ``` --- ### `GET /api/batch/status` Returns real-time status of the counter engine and batch state. **Success response (200):** ```json { "success": true, "active": true, "status": "running", "batch_mode": "manual", "batch_number": 13, "object_label": "mixed", "count": 342, "sack_count": 210, "box_count": 132, "object_labels": ["sack", "box"], "counts": {"sack": 210, "box": 132}, "plate": null, "counting_date": "2024-01-01", "engine_alive": true } ``` --- ### `GET /api/previous-batch` Returns the most recently completed batch from the `batches` database table, with computed `duration_minutes`. **Success response:** ```json { "success": true, "date": "2024-01-01", "batch_number": 11, "object_label": "mixed", "count": 287, "sack_count": 190, "box_count": 97, "start_time": "2024-01-01T14:00:00", "end_time": "2024-01-01T14:27:00", "duration_minutes": 27.0 } ``` --- ### `GET /api/summary` Returns today, yesterday, and all-time aggregate statistics plus average per day and best day. **Success response:** ```json { "today": { "date": "2024-01-01", "total_count": 1500, "total_batches": 8, "count": 1500, "sack_count": 980, "box_count": 520 }, "yesterday": { "date": "2023-12-31", "total_count": 1342, "total_batches": 7, "count": 1342, "sack_count": 860, "box_count": 482 }, "all_time": { "grand_total": 50000, "grand_batches": 260, "total_days": 45, "count": 50000, "sack_count": 32000, "box_count": 18000 }, "average_per_day": 1111.1, "best_day": { "date": "2023-12-15", "count": 2100 } } ``` --- ### `GET /api/daily-data` Returns daily summary rows for charting. **Query params:** | Param | Type | Default | Description | |--------|------|---------|---------------------------------| | `days` | int | 30 | Number of days to look back | **Response:** ```json [ { "date": "2024-01-01", "total_count": 1500, "total_batches": 8, "avg_per_batch": 187.5 } ] ``` --- ### `GET /api/day-detail/` Returns all batch records for a specific `counting_date` along with summary totals. **Path params:** | Param | Type | Description | |--------|--------|------------------------| | `date` | string | Date in `YYYY-MM-DD` | **Success response:** ```json { "date": "2024-01-01", "total_count": 1500, "total_batches": 8, "sack_count": 980, "box_count": 520, "total_duration_minutes": 216.0, "avg_duration_minutes": 27.0, "batches": [ { "batch_number": 1, "object_label": "mixed", "count": 187, "sack_count": 120, "box_count": 67, "start_time": "2024-01-01T08:00:00", "end_time": "2024-01-01T08:27:00", "duration_minutes": 27.0 } ] } ``` --- ### `GET /api/recent-batches` Returns the most recent batches with computed durations. **Query params:** | Param | Type | Default | Description | |---------|------|---------|---------------------------------| | `limit` | int | 10 | Max number of batches to return | **Response:** ```json [ { "date": "2024-01-01", "batch_number": 8, "object_label": "mixed", "count": 213, "sack_count": 140, "box_count": 73, "start_time": "2024-01-01T16:30:00", "end_time": "2024-01-01T16:58:00", "duration_minutes": 28.0 } ] ``` --- ### `GET /api/available-dates` Returns all dates with data from the `daily_summaries` table, ordered by date descending. **Response:** ```json [ { "date": "2024-01-01", "total_count": 1500, "total_batches": 8 } ] ``` --- ## Export Endpoints (XLSX) ### `GET /api/export-daily-csv` Exports batch detail records for the last N days as an `.xlsx` file. **Query params:** | Param | Type | Default | Description | |--------|------|---------|-----------------------------| | `days` | int | 30 | Number of days to look back | **Response:** XLSX file download with columns: Date, Batch #, Type, Sack, Box, Total, Start Time, End Time, Duration (min). Filename format: `{SITE_NAME}_daily_records_{timestamp}.xlsx` --- ### `GET /api/export-day-csv/` Exports all batch records for a single date as an `.xlsx` file. **Path params:** | Param | Type | Description | |--------|--------|----------------------| | `date` | string | Date in `YYYY-MM-DD` | **Response:** XLSX file download with columns: Batch Number, Type, Sack, Box, Total, Start Time, End Time, Duration (min). Filename format: `{SITE_NAME}_day_detail_{date}.xlsx` --- ## Error Handling All endpoints return `{"success": false, "error": ""}` with HTTP 500 on unexpected errors. Database-unavailable errors (`sqlite3.OperationalError`) return HTTP 200 with an empty/default data structure. The `/api/live-video` endpoint returns HTTP 503 if the frame file is not found. --- ## Peer Sync (FCP) `POST /api/batch/start` and `POST /api/batch/stop` accept an optional `X-API-Key` header. - If the header is **absent**, the request is processed normally (dashboard UI). - If the header is **present**, it must equal `PEER_API_TOKEN` (from `.env` or `docs/peer_sync_token.txt`); otherwise returns `401 {"success": false, "error": "unauthorized"}`. Request body (JSON) is forwarded to the internal control server, supporting: `source`, `object_label`, `object_labels`, `fcp_batch_number`, `counting_date`, `plate`, `do_numbers`, `expected_sack`, `expected_box`, `started_at`, `net_sack`, `net_box`, `ended_at`. Responses include `object_labels`, `counts`, and `plate` for peer compatibility. --- ## Auto-mode peer sync When `BATCH_MODE=auto`, `POST /api/batch/start` and `POST /api/batch/stop` forwarded with `X-Peer-Sync: 1` (peer requests) return `200` with `ignored: true` and the current batch state; the local counter keeps running. Non-peer (local dashboard) calls still work.