Spaces:
Sleeping
Sleeping
Backend Architecture
Overview
The backend is a FastAPI service that combines:
- AI inference pipeline (vehicle, plate, OCR)
- Supabase data and storage persistence
- Parking session lifecycle management
- Dynamic pricing and occupancy calculations
- Gate and payment decision flows
Core Runtime Components
- API framework: FastAPI
- Inference runtime: Ultralytics YOLO + OpenCV + NumPy
- Data/Auth backend: Supabase REST + Supabase Auth token validation
- Device auth option: static barrier tokens (
BARRIER_API_TOKEN/BARRIER_API_TOKENS) - Deployment target (current): Hugging Face Spaces (Docker)
- Alternative deployment targets: Fly.io and Render
Request Processing Flow (POST /predict)
- Validate request body and optional inputs (
event_type,parking_location,camera_source). - Decode uploaded image to OpenCV matrix.
- Run AI pipeline:
- vehicle detection on full image
- plate detection on selected vehicle crop
- OCR model on selected plate crop
- Build user/admin image outputs and plate payload.
- Resolve/apply session state in Supabase.
- Vehicle matching uses both Arabic and English OCR plate formats.
- Persist event metadata and storage paths.
- Compute pricing and gate decision.
- Return unified response for Flutter client.
Session Lifecycle
The backend tracks session lifecycle through events and parking_sessions records.
Typical journey:
- Entry event creates or reuses open session (
status=entered). - Payment confirms session (
status=paid). - Exit event closes session (
status=exited) or marks overstayed. left_within_5_minutesis used by policy feeds and gate logic.
Role Model and Access
Roles are stored in Supabase profiles.role:
useradminsecuritybarrier(API-only synthetic role from barrier token auth)
Scope behavior:
- user: own records only
- admin/security: all records, with optional
for_user_idfiltering on feed/history APIs - barrier: global read scope for operational feeds/history and gate decision checks
Dynamic Pricing Engine
Pricing is computed in backend code using env-driven policies.
Policy dimensions:
- billing mode (
hourlyordaily) - base rate
- extra fee after first billing window
- app service fee
- location-specific overrides
The backend returns pricing as structured JSON, not a UI-formatted string.
Occupancy Engine
Occupancy is derived from two sources:
- open registered sessions (
check_out_at is null) - inferred unmatched inside cars from
car_events(for plates not linked to a session)
Outputs include:
- garage occupancy (
inside,left,display,total_capacity) - per-location occupancy (
inside,left,display,total_capacity) - occupancy source breakdown (
inside_from_registered_sessions,inside_from_inferred_unmatched)
Payment Integration
Two flows are supported:
- Card flow (Paymob) via create + webhook endpoints
- Manual cash confirmation flow for staff roles
Supabase Persistence Areas
Main tables:
profilesvehiclesparking_sessionscar_eventspayment_transactions
Storage buckets:
car-raw-imagescar-processed-images
Operational Endpoints
Health and operations endpoints:
/health/supabase/health/admin/maintenance/weekly-refresh
These endpoints support deployment validation and maintenance tasks.