Skip to content

Latest commit

 

History

History
271 lines (233 loc) · 14 KB

CLAUDE.md

File metadata and controls

271 lines (233 loc) · 14 KB

CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

Virtual Environment

  • Must use venv for all Python operations
  • Activate with: source venv/bin/activate
  • If venv has issues, recreate with: python3 -m venv venv && source venv/bin/activate && pip install -r requirements.txt
  • For development: pip install -r requirements-dev.txt (includes pytest, black, pylint, bandit)

Common Commands

Running the Application

# Start both data collector and web server (development)
source venv/bin/activate
python data_collector.py &  # Runs in background, collects data based on API_DAILY_LIMIT (default: every 48 min for 30 calls/day)
python -m src.web.app       # Starts Flask dev server on port 5000 (or PORT env var)

# Production with gunicorn
gunicorn --bind 0.0.0.0:5000 --workers 4 --threads 2 --timeout 120 src.web.app:app

# Test single data collection
python data_collector.py --once

Docker Operations

docker compose up -d        # Start all services
docker compose down         # Stop all services
docker compose logs -f      # View logs
docker compose logs -f --tail=100  # View last 100 lines

# IMPORTANT: Code changes require rebuild since Dockerfile COPYs code (not mounted)
docker compose down && docker compose build && docker compose up -d

# Access container shell for debugging
docker exec -it pyvisionic sh

Code Quality

# Format with black (required before commits)
black src/ tools/ data_collector.py

# Lint with pylint (minimum score 8.0 required)
pylint src/ tools/ data_collector.py

# Run security checks
bandit -r src/ tools/ data_collector.py

CI / Makefile Shortcuts

# Install dev dependencies (includes pytest, black, pylint, bandit)
make install-dev

# Run all CI checks locally (format-check + lint + security + test)
make ci

# Individual targets
make format          # Auto-format code
make format-check    # Check formatting without modifying
make lint            # Run pylint
make security        # Run bandit
make test            # Run pytest
make coverage        # Run tests with coverage report
make clean           # Remove generated files

# Pre-commit hooks (one-time setup)
source venv/bin/activate
pre-commit install

Data Management Tools

# All tools require venv activation first
python tools/backfill_from_cache.py                        # Harvest cache snapshots the CSVs missed (dry run; --write to apply)
python tools/backfill_ambient.py --preview                 # Recover missing ambient temperature from Open-Meteo history (drop --preview to apply)
python tools/deduplicate_trips_v2.py                      # Remove duplicate trips
python tools/rebuild_sessions_from_battery.py              # Rebuild charging sessions from battery history
python tools/rebuild_sessions_from_battery.py --preview    # Preview rebuild without writing
python tools/rebuild_charging_sessions.py                  # Merge fragmented charging sessions
python tools/recompute_is_cached.py                        # Recompute is_cached flags (dry run)
python tools/recompute_is_cached.py --write-cache          # Recompute and persist changes
python tools/analyze_errors.py                             # Analyze API error patterns
python tools/trip_readiness_check.py --watch                # Assess stored trip plans, announce status changes (cron every 15 min)

# Archived one-time scripts are in tools/archive/

Testing

# Run unit tests
pytest tests/

# Run with coverage
pytest tests/ --cov=src --cov-report=term-missing

Development and Debugging

# Run with debug mode enabled for verbose logging
DEBUG_MODE=true python -m src.web.app

High-Level Architecture

Core Components Interaction

  1. Data Collector (data_collector.py):

    • Runs continuously in background
    • Fetches vehicle data based on API_DAILY_LIMIT setting (default 30/day = every 48 min)
    • Manages API rate limiting with call history tracking
    • Stores data via CSVStorage
  2. CachedVehicleClient (src/api/client.py):

    • Handles all Hyundai/Kia Connect API communication
    • Implements smart caching with separate validity and retention periods:
      • Cache validity: Calculated from API_DAILY_LIMIT (e.g., 30 calls/day = 45.6 min validity)
      • Cache retention: Configured via CACHE_DURATION_HOURS (default 48 hours)
    • Manages authentication and error handling with custom APIError class
    • Uses SSL patch for certificate issues when DISABLE_SSL_VERIFY=true
  3. Storage Layer (src/storage/):

    • StorageBackend (base.py): Abstract base class defining the storage interface
    • CSVStorage (csv_store.py): CSV file backend — manages 4 CSV files
    • Handles automatic deduplication via pandas
    • Tracks charging sessions with state management (incomplete/complete)
    • Integrates weather data from Open-Meteo API or vehicle sensor based on WEATHER_SOURCE
  4. Flask Web App (src/web/app.py):

    • Serves dashboard at configured PORT (default 5000)
    • Provides 12+ API endpoints for data access
    • Cache management UI via cache_bp blueprint at /cache
    • Debug routes via debug_bp blueprint when DEBUG_MODE=true
    • Optional Entra ID auth via auth_bp; route decorators are no-ops unless AUTH_ENABLED=true
    • Handles NaN values for JSON serialization

Data Flow

Hyundai/Kia API → CachedVehicleClient → Cache Files → CSVStorage → Flask API → Web Dashboard
                       ↓                     ↓              ↓
                  Rate Limiting          Deduplication   CSV files
                  SSL Patching          Weather          Session Track
                  Error Classification

Key Patterns

  • Caching Strategy: Two-tier system with validity period for API calls and longer retention for data availability
  • Error Handling: APIError class with error types and user-friendly messages
  • Data Validation: Debug utilities with DataValidator for type checking (handles numpy types)
  • Session Management: Charging sessions tracked with start/end times, energy added, location
  • Authentication: Opt-in Entra ID (OIDC). login_required/api_login_required/admin_required decorators pass through when AUTH_ENABLED=false; admin routes gate on ADMIN_USERS
  • Docker Deployment: Code is COPY'd into image, not mounted. Only data/logs/cache/sessions are volumes
  • Blueprint Architecture: Modular routes using Flask blueprints (cache_bp, debug_bp)

API Endpoints

Flask app provides endpoints at http://localhost:{PORT}/api/:

  • /trips - Trip history with pagination, filtering (page, per_page, start_date, end_date, hours)
  • /trip/<trip_id> - Individual trip details with energy breakdown
  • /battery-history - Battery level history with temperature (hours, start_date, end_date params)
  • /current-status - Current battery, charging, range, odometer, temperature
  • /locations - GPS coordinates for all trips (supports hours/date filtering)
  • /charging-sessions - Charging session history (supports hours/date filtering)
  • /efficiency-stats - Efficiency statistics by time period (day/week/month/year)
  • /temperature-efficiency - Trip efficiency correlated with temperature
  • /charging-temperature-impact - Charging power/speed by temperature
  • /collection-status - Data collection status (calls_today, next_collection)
  • /planner/destinations - Places visited more than once, with measured per-visit energy
  • /planner/assess - Trip reachability; costs the trip from the measured temperature band at the forecast departure temperature
  • /planner/trips (GET/POST/DELETE) - Named trip plans, re-assessed each collector cycle; alerts on status transitions
  • /polling-status - Polling observability: mode, recent scheduling decisions from data/polling_log.csv, budget usage
  • /plug-sample (POST) - Token-authenticated webhook for Home Assistant smart-plug power samples (appends to data/plug_power.csv; collector refines sessions from it)
  • /force-update - Force cache refresh (respects rate limits)
  • /clear-cache - Clear non-history cache files
  • /refresh - Refresh data with timeout handling
  • /debug - Debug info (config, cache status)

Web UI routes:

  • / - Main dashboard
  • /cache - Cache management UI (view/download/delete cached files)
  • /debug/* - Debug routes (only when DEBUG_MODE=true)

Environment Variables (.env)

Critical settings that must be configured:

  • BLUELINKVID: Vehicle ID - obtain from first run if not set
  • BLUELINKUSER: Hyundai/Kia Connect account username
  • BLUELINKPASS: Hyundai/Kia Connect account password
  • BLUELINKPIN: Account PIN for remote commands
  • BLUELINKREGION: 1=Europe, 2=Canada, 3=USA, 4=China, 5=Australia
  • BLUELINKBRAND: 1=Kia, 2=Hyundai, 3=Genesis
  • API_DAILY_LIMIT: Default 30, used to calculate cache validity
  • ADAPTIVE_POLLING: Default false. When true, the collector polls faster around charging/trip transitions (15-20 min) and slower when idle (60-150 min), with a budget clamp that keeps daily calls within API_DAILY_LIMIT
  • BATTERY_USABLE_KWH: Default 74.0 (Ioniq 5 long-range: 77.4 kWh total). Usable pack capacity used to estimate charging energy from SOC deltas
  • PLUG_WEBHOOK_TOKEN: Shared secret for POST /api/plug-sample; when set, Home Assistant can push smart-plug power samples that refine L1 session data
  • CACHE_DURATION_HOURS: How long to retain cache files (default 48)
  • DEBUG_MODE: Enables verbose logging and debug routes
  • TZ: Timezone for data collection (default: America/Chicago)
  • PORT: Web server port (default: 5000)
  • SECRET_KEY: Flask secret key (defaults to dev-secret-key; set in production)
  • WEATHER_SOURCE: "meteo" for Open-Meteo API or "vehicle" for car sensor
  • TRIP_ALERTS_ENABLED: Default false. Alerting is owned by the host-side watcher (tools/trip_readiness_check.py --watch, cron every 15 min), because NATS binds loopback on the host and its CLI is not in the container. Set true only where the collector can genuinely reach NATS
  • TRIP_ALERT_SUBJECT: Default fleet.karwyn.notify. One subject only — fleet.digest forwards to the same chat, so publishing to both delivers every alert twice

Project Structure

pyvisionic/
├── src/
│   ├── api/
│   │   ├── client.py          # CachedVehicleClient - API communication, caching, rate limiting
│   │   └── ssl_patch.py       # SSL certificate workarounds
│   ├── storage/
│   │   ├── csv_store.py       # CSVStorage - manages 4 CSV files, deduplication, session tracking
│   │   └── plan_store.py      # PlanStore - named trip plans in data/trip_plans.json
│   ├── utils/
│   │   ├── weather.py         # WeatherService - Open-Meteo current conditions and forecast
│   │   ├── trip_planner.py    # Planning core - shared by the web app, collector and CLI
│   │   ├── efficiency.py      # Temperature-banded efficiency, energy-weighted
│   │   ├── notify.py          # NATS publishing
│   │   └── debug.py           # DebugLogger, DataValidator - debugging utilities
│   └── web/
│       ├── app.py             # Flask app - main routes, API endpoints
│       ├── cache_routes.py    # Blueprint for /cache routes
│       ├── debug_routes.py    # Blueprint for /debug routes (DEBUG_MODE only)
│       ├── templates/         # HTML templates
│       └── static/            # CSS, JS, images
├── tools/                      # Active data management scripts (archived ones in tools/archive/)
│   └── archive/                # One-time migration/fix scripts (already applied)
├── data/                       # CSV files (trips, battery_status, locations, charging_sessions)
├── cache/                      # Cached API responses (timestamped history files)
├── logs/                       # Application logs
└── data_collector.py           # Background data collector (runs every ~48 min)

Key Implementation Details

Data Flow for API Calls

  1. data_collector.py calls client.get_vehicle_data() at scheduled intervals
  2. CachedVehicleClient checks cache validity (based on API_DAILY_LIMIT)
  3. If cache invalid: calls Hyundai API, compares timestamps to detect freshness, saves to cache
  4. Returns data with is_cached and hyundai_data_fresh flags
  5. CSVStorage.store_vehicle_data() processes and saves to 4 CSV files with deduplication

Charging Session State Machine

  • Start: is_charging=True + no active session → create new session with is_complete=False
  • Update: is_charging=True + active session → update end_battery, end_time, energy_added
  • Gap Detection: time gap > charging_gap_threshold → complete old session, start new one
  • Complete: is_charging=False + active session → set is_complete=True
  • Energy calculation: (battery_delta / 100) * BATTERY_CAPACITY_KWH
  • Average power: energy_added / (duration_minutes / 60)

Cache Freshness Detection

Two-tier cache system prevents unnecessary API calls:

  • Validity Period (45.6 min for 30 calls/day): When to make new API call
  • Retention Period (48 hours): How long to keep historical cache files
  • Freshness Detection: Compares api_last_updated timestamps and raw data hash
  • Stale Detection: If timestamp unchanged and payload unchanged → mark as not fresh

Temperature Data Sources

Two sources configured via WEATHER_SOURCE:

  • meteo: Open-Meteo API (weather service) - more reliable
  • vehicle: Vehicle sensor from airTemp field - may be stale Both sources stored in separate columns (meteo_temp, vehicle_temp) for comparison

Known Issues & Workarounds

  • Temperature stale: Vehicle sensor may not update; use WEATHER_SOURCE=meteo
  • numpy types: DataValidator handles numpy.int64 validation to prevent JSON errors
  • vehicleStatus KeyError: Hyundai API maintenance windows → gracefully use last cache
  • SSL certificate issues: Set DISABLE_SSL_VERIFY=true (uses ssl_patch.py)
  • Rate limiting: Automatic backoff extends next collection interval by 1.5x (max 4x)
  • Cross-platform venv: Recreate venv when moving between macOS and Linux