This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
CerCollettiva is a Django-based Energy Community Management System (Comunità Energetica Rinnovabile - CER) with IoT device integration via MQTT. The system manages renewable energy communities, energy plants, IoT devices, and document processing including GAUDI (Italian grid integration) documents.
CerCollettiva/
├── cercollettiva/ # Django project settings and configuration
├── core/ # Main CER management application
├── energy/ # IoT devices and energy measurements
├── documents/ # Document management with GAUDI processor
├── users/ # User authentication and profiles
├── templates/ # Django templates
├── static/ # Static files (CSS, JS, images)
├── media/ # User uploaded files
├── scripts/ # Server management scripts
├── utilities/ # Python utilities
├── docs/ # Documentation
│ └── install/ # Installation scripts
├── venv/ # Python virtual environment
├── manage.py # Django management script
└── .env # Environment configuration
Note: The old app/ directory structure has been flattened to the project root for simpler management.
# Create virtual environment
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Upgrade pip
pip install --upgrade pip
# Install core Django dependencies first
pip install Django==5.0 psycopg2-binary python-dotenv
# Install other core dependencies
pip install djangorestframework channels django-crispy-forms crispy-bootstrap5
pip install django-widget-tweaks django-filter django-extensions paho-mqtt Pillow
# Install all remaining dependencies from requirements file
pip install -r requirements.txt
# Alternative: Install dependencies step by step if encountering issues
# pip install django djangorestframework channels paho-mqtt
# pip install psycopg2-binary python-dotenv django-crispy-forms crispy-bootstrap5
# pip install django-widget-tweaks django-filters whitenoise geopy
# pip install openpyxl pandas django-extensions django-cors-headers
# pip install daphne channels-redis cryptography django-encrypted-model-fields Pillow# Run migrations
python manage.py migrate
# Create superuser
python manage.py createsuperuser
# Make migrations after model changes
python manage.py makemigrations# Run development server
python manage.py runserver
# Run with specific settings
DJANGO_SETTINGS_MODULE=cercollettiva.settings.local python manage.py runserver# Run all tests
python manage.py test
# Run specific app tests
python manage.py test core
python manage.py test energy
python manage.py test documents# Collect static files
python manage.py collectstatic --noinput# Update plant coordinates from addresses
python manage.py update_plant_coordinates
# Debug MQTT configuration
python manage.py debug_config-
core - Main application logic for CER management
- Models: CERConfiguration, Plant, Alert, CERMembership
- Views: Dashboard, CER management, Plant CRUD operations
- Admin interface customization at
/ceradmin/
-
energy - IoT device and energy measurement management
- MQTT client for real-time device communication
- Device registry system with vendor-specific implementations (Shelly, Tasmota, Huawei)
- Energy calculation services with aggregation and caching
- Models: DeviceConfiguration, Measurement, MQTTBroker
-
documents - Document management with GAUDI processor
- GAUDI document parsing and validation
- Document storage with user association
- Excel/PDF processing capabilities
-
users - User authentication and profile management
- Custom user profiles with GDPR compliance
- CER membership management
The system uses a sophisticated MQTT client (energy/mqtt/client.py) with:
- Thread-safe message handling with queue and buffer management
- Device auto-discovery and registration
- Real-time measurement processing
- ACL-based topic authorization
- Automatic reconnection with exponential backoff
Device data flow:
- IoT devices publish to
energia/<device_type>/<device_id>/... - MQTT client processes messages via DeviceManager
- Measurements stored in PostgreSQL with time-series optimization
- Energy calculator aggregates data for reporting
- Settings:
cercollettiva/settings/(base.py, local.py, production.py) - Environment:
.envfile with database, MQTT, and Django settings - URLs: Modular URL configuration with API and template namespacing
PostgreSQL database with:
- Connection pooling (CONN_MAX_AGE: 600)
- Test database:
test_cercollettiva - Migrations in each app's
migrations/directory
- Django templates with Bootstrap 5
- Custom admin dashboard with energy statistics
- Real-time MQTT status monitoring
- Chart.js for power consumption visualization
- Initialized on app startup via
energy/apps.py - Runs in daemon thread to avoid blocking
- Disabled during testing (checks
settings.TESTING) - Credentials stored in environment variables
- Uses Nominatim for address-to-coordinates conversion
- Implements retry logic with timeouts
- Caches results to minimize API calls
- GAUDI documents parsed with
openpyxl - Extracts plant data, POD codes, and grid information
- Validates against Italian energy regulations
- Field-level encryption for sensitive data
- GDPR compliance tracking for user documents
- Admin interface restricted to
/ceradmin/path - CSRF protection enabled
- User role-based access control (ADMIN, MEMBER, VIEWER)
-
"Couldn't import Django" error:
# Reinstall Django specifically pip uninstall django -y pip install Django==5.0 -
"No module named 'rest_framework'" error:
# Install Django REST Framework pip install djangorestframework -
"No module named 'paho'" error:
# Install MQTT client pip install paho-mqtt -
"No module named 'django_extensions'" error:
# Install Django Extensions pip install django-extensions -
Virtual environment issues on WSL/Linux:
# If getting externally-managed-environment error rm -rf venv python3 -m venv venv source venv/bin/activate
-
Database connection issues:
- Verify PostgreSQL is running
- Check database credentials in .env file
- Ensure database
cercollettiva_devexists
#!/bin/bash
# Quick setup script for CerCollettiva
# Remove old venv if exists
rm -rf venv
# Create new virtual environment
python3 -m venv venv
source venv/bin/activate
# Upgrade pip
pip install --upgrade pip
# Install core dependencies
pip install Django==5.0 psycopg2-binary python-dotenv
pip install djangorestframework channels django-crispy-forms crispy-bootstrap5
pip install django-widget-tweaks django-filter django-extensions paho-mqtt
# Install all dependencies (if requirements.txt exists)
if [ -f "app/requirements.txt" ]; then
pip install -r app/requirements.txt
fi
# Run migrations
python manage.py migrate
# Create superuser (optional)
# python manage.py createsuperuser
# Start development server
python manage.py runserverWhen modifying energy device integrations:
- Check device vendor implementation in
energy/devices/vendors/ - Update device registry if adding new device types
- Test MQTT message handling with
debug_configcommand - Verify measurements are stored correctly
When working with GAUDI documents:
- Review processor in
documents/processors/gaudi.py - Test with sample GAUDI Excel files
- Ensure plant data extraction is accurate
- Validate coordinate geocoding
When updating CER management:
- Models in
core/models.pydefine CER structure - Views handle member management and plant associations
- Admin customizations in
core/admin.py - Dashboard aggregates energy statistics