6.2 KiB
6.2 KiB
SolarPower Agents Guide
This document provides context, commands, and guidelines for AI agents working on the SolarPower codebase.
1. Project Overview
SolarPower is an integrated solar power monitoring system consisting of:
- Crawler: Python-based data collector running on Oracle Cloud (formerly Synology NAS). Uses Tailscale to route via NAS IP to avoid blocks.
- API Server: FastAPI backend on Oracle Cloud acting as a middleware.
- App: React Native (Expo) mobile/web application for monitoring.
- Database: Supabase (PostgreSQL) for data persistence.
2. Directory Structure
crawler/: Python crawlers and scheduler.crawlers/: Individual site crawler implementations.crawler_manager.py: Smart scheduler using SQLite (crawler_manager.db).
api_server/: FastAPI backend.app/routers/: API endpoints (plants,stats,upload).app/core/: Configuration and utilities.
app/: React Native Expo frontend.components/: Reusable UI components.screens/: Application screens.
3. Development Commands
📱 Mobile App (app/)
- Environment: Node.js, Expo
- Install:
npm install - Run (Dev):
npx expo start- Press
wfor web,afor Android.
- Press
- Build (Web):
npx expo export -p web- Outputs to
dist/directory. - Use this command for deployment builds.
- Outputs to
- Lint: No strict ESLint config. Follow existing code patterns (functional components, hooks).
- Test:
- E2E: Use
playwrightskill/tool to verify the web build running on a local server. - Manual: Run
npx expo startand use Expo Go.
- E2E: Use
⚡ API Server (api_server/)
- Environment: Python 3.10+
- Install:
pip install -r requirements.txt - Run (Dev):
uvicorn app.main:app --reload- Runs on port 8000 by default.
- Run (Prod): Managed via systemd:
sudo systemctl restart solar-api - Docs: Access Swagger UI at
http://localhost:8000/docs - Test:
- No automated test suite (pytest) currently.
- Verify endpoints manually using
curlor Swagger UI.
🐍 Crawler (crawler/)
- Environment: Python 3.10+
- Install: Manual dependency installation (requests, python-dotenv, supabase).
- Run (Manual):
python main.py- Runs in
LEARNINGorOPTIMIZEDmode based oncrawler_manager.db.
- Runs in
- Run (Force):
python main.py --force(Ignores scheduler constraints) - GUI Tool:
python crawler_gui.py(For historical data recovery and log cleaning) - Testing:
- No standard test runner. Use standalone scripts in
tests/. - Run Single Test:
python tests/debug_kremc.py(or similar script for specific site).
- No standard test runner. Use standalone scripts in
4. Code Style & Standards
🐍 Python (Crawler & API)
- Style: Follow PEP 8 standards.
- Indentation: 4 spaces.
- Typing: Use type hints where possible (
def func(a: int) -> str:). - Frameworks:
- FastAPI: Use Pydantic models for request/response schemas.
- Crawler: Use
crawler_managerfor scheduling. Do not hardcode run times.
- Imports: Group standard lib, third-party, then local modules.
- Error Handling:
- API: Raise
HTTPExceptionwith clear status codes. - Crawler: Catch exceptions per site to prevent one failure from stopping others.
- API: Raise
- Async: API Server uses
async/await. Crawler is synchronous (mostly). - Naming:
snake_casefor functions/variables,PascalCasefor classes.
⚛️ JavaScript/React Native (app/)
- Style: Functional Components with Hooks (
useState,useEffect). - Indentation: 2 spaces.
- Quotes: Single quotes
'preferred for strings. - Semicolons: Always use semicolons.
- Naming:
camelCasefor variables/functions,PascalCasefor components. - Styling: Use
StyleSheet.create({}). Avoid inline style objects if possible. - Navigation: Uses
@react-navigation. Ensure strict typing for route params. - Libraries:
- Charts:
react-native-gifted-charts - UI:
react-native-svg,expo-linear-gradient
- Charts:
- State: Local state preferred. Keep components pure where possible.
5. Deployment Workflows
🚀 Web App Deployment
- Build: Run
npx expo export -p weblocally to generatedist/. - Upload: Use SCP to transfer
dist/to the server.- Example:
scp -r dist user@server:~/dist_temp
- Example:
- Deploy: Move the uploaded folder to the web root (
/var/www/html/dist).- Command:
ssh user@server "sudo rm -rf /var/www/html/dist && sudo mv ~/dist_temp /var/www/html/dist"
- Command:
- Refresh:
sudo systemctl reload nginx(optional, for cache clearing).
☁️ Server Deployment
- Push: Commit and push changes to the remote Git repository.
- Pull: SSH into the server and pull changes.
- Command:
ssh user@server "cd ~/solorpower_server && git pull"
- Command:
- Restart: Restart the API service.
- Command:
sudo systemctl restart solar-api
- Command:
6. Architecture & Patterns
🧠 Crawler Scheduler (crawler_manager.py)
- LEARNING Mode: Runs every cycle to determine update patterns.
- OPTIMIZED Mode: Runs only during specific 10-minute windows around the learned update time.
- Storage: Uses local SQLite (
site_rulestable) to persist scheduling rules.
🌐 API Structure
- Routers:
plants: Basic plant info and real-time status.stats: Aggregated statistics (daily, monthly, yearly).upload: Excel file processing for manual data entry.
- CORS: Configured to allow all origins (
*) in development.
7. Critical Rules for Agents
- Timezones: All internal logic should handle KST (UTC+9) correctly. Be careful with UTC conversions in DB interactions.
- Data Integrity:
crawler_managerprevents duplicate runs. Respectshould_runchecks. - Error Recovery: Crawlers must be robust against network failures (retry logic).
- Secrets: Never commit
.envfiles. Usepython-dotenv. - Testing: Always verify changes locally before deploying.
- App: Check UI changes on local web server.
- API: Check endpoints response.
- Crawler: Run debug scripts.
- Networking: When modifying crawlers, be aware that Oracle Cloud uses Tailscale to route traffic through the Synology NAS (Exit Node) to avoid IP blocks from monitoring sites. Ensure Tailscale is active on the host.