# Energy Management Microservices Architecture This directory contains independent microservices based on the tiocps/iot-building-monitoring system, redesigned for modular deployment and scalability. ## Services Overview ### 1. **Token Service** (`token-service/`) - JWT token generation, validation, and management - Resource-based access control - Authentication service for all other services - **Port**: 8001 ### 2. **Battery Management Service** (`battery-service/`) - Battery monitoring, charging, and discharging - Energy storage optimization - Battery health and state tracking - **Port**: 8002 ### 3. **Demand Response Service** (`demand-response-service/`) - Grid interaction and demand response events - Load shifting coordination - Event scheduling and management - **Port**: 8003 ### 4. **P2P Energy Trading Service** (`p2p-trading-service/`) - Peer-to-peer energy marketplace - Transaction management and pricing - Energy trading optimization - **Port**: 8004 ### 5. **Forecasting Service** (`forecasting-service/`) - ML-based consumption and generation forecasting - Historical data analysis - Predictive analytics for optimization - **Port**: 8005 ### 6. **IoT Control Service** (`iot-control-service/`) - IoT device management and control - Device instructions and automation - Real-time device monitoring - **Port**: 8006 ### 7. **API Gateway** (`api-gateway/`) - Central entry point for all services - Request routing and load balancing - Authentication and rate limiting - **Port**: 8000 ## Architecture Principles - **Independent Deployment**: Each service can be deployed, scaled, and updated independently - **Database per Service**: Each microservice has its own database/collection - **Event-Driven Communication**: Services communicate via Redis pub/sub for real-time events - **REST APIs**: Synchronous communication between services via REST - **Containerized**: Each service runs in its own Docker container ## Communication Patterns 1. **API Gateway → Services**: HTTP REST calls 2. **Inter-Service Communication**: HTTP REST + Redis pub/sub for events 3. **Real-time Updates**: Redis channels for WebSocket broadcasting 4. **Data Persistence**: MongoDB with service-specific collections ## Deployment Each service includes: - `main.py` - FastAPI application - `models.py` - Pydantic models - `database.py` - Database connection - `requirements.txt` - Dependencies - `Dockerfile` - Container configuration - `docker-compose.yml` - Service orchestration ## Getting Started ```bash # Start all services docker-compose up -d # Start individual service cd token-service && python main.py # API Gateway (main entry point) curl http://localhost:8000/health ``` ## Service Dependencies ``` API Gateway (8000) ├── Token Service (8001) - Authentication ├── Battery Service (8002) ├── Demand Response Service (8003) ├── P2P Trading Service (8004) ├── Forecasting Service (8005) └── IoT Control Service (8006) ```