How aarya695/decentralized-energy-trading-system works
Purpose and Entry Points
Smart Energy Platform is an in-memory simulator for peer-to-peer household solar-energy trading. A FastAPI backend advances a simulated grid every three seconds and exposes REST/WebSocket state; a Flutter client presents system, grid-flow, household, and billing views.
Backend startup is backend/main.py (uvicorn main:app from backend/). Flutter startup and navigation are rooted at frontend/lib/main.dart.
Backend Orchestration and State
backend/main.py owns the FastAPI application, simulation loop, and real-time broadcast lifecycle. backend/state.py is the singleton, process-local source of truth for the 12 simulated houses, market data, transactions, tick/time counters, and billing state. There is no database, persistence layer, or distributed coordination: reset/restart discards state.
Core domain/Pydantic structures defining houses, orders, trades, and bills reside in backend/models.py.
Per-Tick Energy Simulation
Each tick represents one simulated hour. The engine in backend/simulation.py calculates per-house solar generation and time/weather-sensitive consumption, then derives net surplus or deficit. Surplus becomes sellable supply; deficits become buy demand.
The system’s energy-source priority is a material control boundary: self-produced solar is consumed first, then locally matched P2P energy, with the government grid used only for unmet demand.
P2P Market and Price Formation
backend/marketplace.py converts positions into orders and matches lowest-priced sellers against highest-priced buyers when bids are compatible. Matched quantity is bounded by both parties’ remaining supply/demand; the transaction price is derived from their prices. Transactions update household balances and downstream billing inputs.
backend/pricing.py computes the market’s dynamic reference pricing from aggregate supply versus demand, bounded to ₹2–₹12/kWh. It influences offers and reflects scarcity/abundance, while unmatched deficits retain the fixed ₹8/kWh grid fallback in backend/simulation.py.
Billing and Settlement
backend/billing.py accumulates P2P purchase cost, grid cost, and P2P sale earnings. At 720 ticks (30 simulated days), it produces per-house bills: costs minus P2P earnings, allowing a negative amount to represent net earnings. Bill retrieval and payment are simulation-state operations, not integrations with a real payment processor.
API and Real-Time Boundary
backend/router.py exposes REST reads for houses, market/dashboard aggregates, transactions, personal views, and bills, plus manual tick/reset and bill-payment commands. backend/main.py exposes health/application lifecycle and distributes full-state JSON snapshots to connected WebSocket clients after each tick. FastAPI provides interactive documentation at /docs.
The API boundary is consumed by frontend/lib/services/api_service.dart, which combines HTTP fetching with WebSocket-driven refreshes.
Flutter Presentation Layer
frontend/lib/models/models.dart maps backend payloads into Dart-side models. frontend/lib/main.dart composes app-level navigation and screens.
Primary views are:
• System-wide market metrics: frontend/lib/screens/dashboard_screen.dart
• Animated house/grid energy-flow visualization: frontend/lib/screens/grid_screen.dart
• Selected household status and activity: frontend/lib/screens/my_dashboard_screen.dart
• Current and historical billing presentation: frontend/lib/screens/my_bill_screen.dart
Runtime and Infrastructure
Backend runtime is Python 3.9+ with FastAPI/Uvicorn dependencies declared in backend/requirements.txt. Frontend is Flutter/Dart, targeting at least web and Windows project scaffolding, with package dependencies in frontend/pubspec.yaml. External infrastructure is intentionally minimal: no database, broker, smart-meter integration, payment gateway, or external energy-market service is represented.
Main entry points: models.dart Client payload models · state.py Process-local state · main.dart App navigation · simulation.py Energy simulation
Components 17
Python backend
- FastAPI runtime · Advances simulation every three seconds and broadcasts snapshots.
backend/main.py - REST commands and reads · Serves state views, ticks, resets, bills, and payments.
backend/router.py - WebSocket clients · Receives full-state snapshots after ticks.
backend/main.py - Process-local state · In-memory source of truth; reset or restart discards it.
backend/state.py - Energy domain models · Defines houses, orders, trades, and bills.
backend/models.py - Energy simulation · Calculates generation, consumption, and net household positions.
backend/simulation.py - Energy allocation priority · Solar first, then P2P, then ₹8/kWh grid fallback.
backend/simulation.py - Dynamic market pricing · Uses aggregate supply and demand within ₹2–₹12/kWh.
backend/pricing.py - P2P order matching · Matches compatible seller offers and buyer bids.
backend/marketplace.py - Billing and settlement · Settles bills every 720 ticks; bills may be negative.
backend/billing.py
Flutter client
- App navigation · Composes application navigation and screens.
frontend/lib/main.dart - API and socket client · Combines HTTP requests with WebSocket-driven refreshes.
frontend/lib/services/api_service.dart - Client payload models · Maps backend payloads into presentation models.
frontend/lib/models/models.dart - Market dashboard · Shows system-wide market metrics.
frontend/lib/screens/dashboard_screen.dart - Grid flow view · Visualizes household and grid energy flows.
frontend/lib/screens/grid_screen.dart - Household dashboard · Shows selected household status and activity.
frontend/lib/screens/my_dashboard_screen.dart - Billing view · Shows current and historical bills.
frontend/lib/screens/my_bill_screen.dart
Connections 24
- FastAPI runtime → Energy simulation: runs each tick
- Energy simulation → Energy allocation priority: derives positions
- Energy allocation priority → Dynamic market pricing: aggregate P2P supply/demand
- Dynamic market pricing → P2P order matching: reference prices
- P2P order matching → Billing and settlement: trade costs and earnings
- Energy simulation → Billing and settlement: grid fallback costs
- Energy simulation → Process-local state: updates household state
- P2P order matching → Process-local state: records orders and trades
- Billing and settlement → Process-local state: stores billing state
- Energy domain models → Process-local state: state structures
- REST commands and reads → Process-local state: reads and commands
- FastAPI runtime → REST commands and reads: mounts REST API
- FastAPI runtime → WebSocket clients: broadcasts snapshots
- App navigation → Market dashboard: navigates
- App navigation → Grid flow view: navigates
- App navigation → Household dashboard: navigates
- App navigation → Billing view: navigates
- API and socket client → REST commands and reads: HTTP REST
- API and socket client → WebSocket clients: WebSocket refreshes
- API and socket client → Client payload models: maps payloads
- Client payload models → Market dashboard: view data
- Client payload models → Grid flow view: view data
- Client payload models → Household dashboard: view data
- Client payload models → Billing view: view data