Terminal Firmware Integration Guide

This guide describes the API integration for scale terminal firmware. The terminal is a remote self-service scale device that communicates with the server to manage weighing sessions and orders.

Base URL: /functions/v1 Authentication: All terminal endpoints require the x-terminal-key header.

x-terminal-key: your-terminal-api-key

Architecture Overview

Key Concepts

  • Terminal — Remote self-service scale device running firmware
  • Scale — Server-side entity representing the scale’s state
  • Session — A lock mechanism to prevent concurrent access to the scale
  • Order — A completed weighing transaction with payment
  • Scale Command — Server-initiated actions for the terminal to execute
  • WebSocket Realtime — Used for unidirectional state synchronization from server to terminal

State Synchronization

DirectionMechanism
Terminal → ServerPATCH /api/terminal/scales/{scaleId}
Server → Terminalscale_commands INSERT events via WebSocket

The terminal subscribes to WebSocket for:

  • scales table UPDATE events (detect session changes)
  • scale_commands table INSERT events (receive new commands)

Order Creation Flows

There are two distinct flows — see order-flows for full details.

FlowTriggerUse case
Terminal-DrivenTerminal controls entire processWalk-up kiosk
User/Mobile-DrivenMobile app initiates, terminal responds to commandsRemote-triggered weighing

Scale State Synchronization

Terminal Push (Heartbeat)

Terminal should periodically update its state even without commands:

Endpoint: PATCH /api/terminal/scales/{scaleId}

{
  "stableWeight": 25000,
  "status": "IDLE"
}

Get Full Scale State

Endpoint: GET /api/terminal/scales/{scaleId}

Best Practices

Idempotency

  • Always check command status before executing
  • Handle already-completed commands gracefully
  • Use unique order IDs generated by terminal

State Synchronization

  • Subscribe to WebSocket updates immediately on startup
  • Poll for commands on reconnection
  • Use scaleSeq to detect missed commands

Session Management

  • Prolong sessions regularly (every 2–3 minutes if 5-minute duration)
  • Always end sessions explicitly
  • Handle session timeouts gracefully

Error Recovery

  • Retry failed operations with exponential backoff
  • Mark commands as FAILED with descriptive errors
  • Log all API interactions for debugging

Photo Upload

  • Compress images before upload (target: < 1 MB per photo)
  • Upload photos immediately after order creation
  • Retry failed uploads

Database Schema (Reference)

Key Tables

TablePurpose
scalesScale state and configuration
scale_sessionsActive and historical sessions
scale_commandsServer-to-terminal commands
ordersCompleted orders
order_photosPhotos associated with orders
scale_camerasCameras associated with each scale

Important Fields

scales table: id, name, number, enabled, status, stableWeight, truckNumber, truckTrailerNumber, axis1–axis12, activeSessionId

scale_commands table: id, scaleId, name, status, payload, scaleSeq, createdAt, ackedAt, endedAt

scale_sessions table: id, scaleId, userId, lockedBy, lockedUntil, status (ACTIVE/ENDED/EXPIRED), reason