# CLAUDE.md This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. ## Project Overview Weather dashboard for WeatherFlow Tempest data with two pages: weather conditions and hardware status. Vanilla JavaScript frontend using Chart.js for visualization. ## Architecture **Dual Backend Options**: - `app.py`: Python/Flask backend for InfluxDB 2.x (Flux query language) - `server.js`: Node.js/Express backend for InfluxDB 1.x (InfluxQL) **Frontend** (public/): - `index.html` + `app.js`: Weather dashboard (temperature, humidity, pressure, wind, solar, UV, precipitation) - `hardware.html` + `hardware.js`: Hardware status (hub/station uptime, RSSI, voltage, firmware) - `style.css`: Shared styles - Chart.js integration with auto-refresh (stats every 30-60s, charts every 5m) - API calls use relative URLs (`/api`) to work in both dev and prod **Data Flow**: InfluxDB → Backend Flux/InfluxQL queries → REST API → Frontend polling → Chart.js rendering ## Development Commands ```bash # Python backend (InfluxDB 2.x) pip install -r requirements.txt python app.py # Dev server on port 3000 gunicorn -w 4 -b 0.0.0.0:3000 app:app # Production # Node.js backend (InfluxDB 1.x) npm install npm start # Production server npm run dev # Dev with nodemon # Docker (uses Python backend) docker-compose up -d # Start docker-compose logs -f # View logs docker-compose down # Stop ``` ## Environment Configuration Required in `.env` (copy from `.env.example`): **For InfluxDB 2.x (app.py)**: ``` INFLUX_HOST=localhost INFLUX_PORT=8086 INFLUX_ORG=your_org INFLUX_BUCKET=homeassistant INFLUX_TOKEN=your_api_token PORT=3000 ``` **For InfluxDB 1.x (server.js)**: ``` INFLUX_HOST=localhost INFLUX_PORT=8086 INFLUX_DATABASE=homeassistant INFLUX_USERNAME=your_username INFLUX_PASSWORD=your_password PORT=3000 ``` ## InfluxDB Schema **Python backend (app.py)** uses specific WeatherFlow measurements (not generic entity_id filtering): - Weather: `ST-00196107_observation` with fields: `temperature_air`, `r-humidity`, `pressure_station`, `wind_speed`, `wind_direction`, `rain_accumulation`, `uv`, `irradiance_sun`, `strike_count` - Hub status: `HB-00196305_status` with fields: `uptime`, `rssi_self`, `firmware_revision` - Station status: `ST-00196107_status` with fields: `uptime`, `voltage`, `rssi_hub`, `firmware_revision` - Radio: `HB-00196305_radio` with field: `version` To customize for different device IDs, update measurement constants in app.py:32-35. **Node.js backend (server.js)** filters by `entity_id =~ /tempest/` across measurements: `temperature`, `humidity`, `pressure`, `wind_speed`, `wind_direction`, `precipitation`, `uv_index`, `solar_radiation`, `lightning_strike_count`. ## API Endpoints **Weather**: - `/api/weather/stats`: Current weather stats with unit conversions (C→F, m/s→mph, mm→in) - `/api/weather/history/?hours=24`: Historical data (fields: temperature, humidity, pressure, wind) **Hardware**: - `/api/hardware/stats`: Hub and station status - `/api/hardware/history//?hours=6`: Historical hardware metrics **Legacy** (for server.js compatibility): - `/api/current`: Current measurements (all fields) - `/api/history/?hours=24`: Historical data **Utility**: - `/api/health`: Database connectivity check - `/api/debug/measurements`: List available measurements ## Unit Conversions Python backend auto-converts to imperial units: - Temperature: °C × 9/5 + 32 = °F - Wind: m/s × 2.237 = mph - Precipitation: mm ÷ 25.4 = inches - Pressure: Pa ÷ 100 = mbar ## Caching Strategy **Backend Caching** (app.py): - In-memory cache with 30s TTL for `/api/weather/stats` and `/api/hardware/stats` - Background threads refresh cache every 30s to reduce InfluxDB queries **HTTP Caching Headers**: - Static assets (CSS/JS/images): `Cache-Control: public, max-age=31536000, immutable` (1 year) - HTML pages: `Cache-Control: public, max-age=300` (5 minutes) - API stats endpoints: `Cache-Control: public, max-age=30` (30 seconds) - API history endpoints: `Cache-Control: public, max-age=300` (5 minutes) **Cloudflare CDN** (see CLOUDFLARE_CACHING.md): - Configured to cache all static assets and API responses - Expected bandwidth savings: ~97% reduction in origin traffic - Cache busting: Use `./add-cache-busting.sh` before deploy to update asset versions ## Deployment **Docker** (recommended): Uses Python backend, runs on port 9431 in host network mode. **PM2**: `pm2 start server.js --name weather` (Node.js backend) **Systemd**: Create service pointing to `gunicorn -w 4 -b 0.0.0.0:3000 app:app` (Python) or `node server.js` (Node.js) **Nginx**: Use nginx.conf as reverse proxy template. SSL with Let's Encrypt recommended. **Deploy Workflow**: 1. Run `./add-cache-busting.sh` to add version tags to static assets 2. Deploy updated files 3. Purge Cloudflare cache (optional, if immediate update needed)