Skip to content

Commit 7319a27

Browse files
committed
feat: add comprehensive README documenting project features, setup, API, and project structure.
1 parent d915c9a commit 7319a27

1 file changed

Lines changed: 165 additions & 0 deletions

File tree

readme.md

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# 🚀 PCB Backend API
2+
3+
Backend service yang handal untuk sistem IoT PCB, dibangun menggunakan **FastAPI** (Python). Proyek ini menyediakan API untuk manajemen perangkat, kontrol relay secara real-time via **MQTT**, penyimpanan log sensor, dan autentikasi pengguna yang aman.
4+
5+
---
6+
7+
## ✨ Fitur Utama
8+
9+
* **⚡ High Performance API**: Dibangun di atas FastAPI (Asynchronous).
10+
* **🔐 Secure Authentication**: Terintegrasi penuh dengan **Firebase Auth** (Token Validation).
11+
* **📡 Real-time Control**: Kontrol perangkat (Lampu, Pompa, dll) menggunakan protokol **MQTT**.
12+
* **🗄️ Database Power**: Menggunakan **PostgreSQL** untuk data persisten yang handal.
13+
* **🛡️ Rate Limiting**: Perlindungan terhadap spam/brute-force menggunakan **Redis**.
14+
* **🐳 Dockerized**: Mudah di-deploy menggunakan Docker Compose.
15+
* **🤖 Auto Register**: Mekanisme pendaftaran otomatis untuk perangkat keras (ESP32) yang valid.
16+
17+
---
18+
19+
## 🛠️ Teknologi yang Digunakan
20+
21+
* **Bahasa**: Python 3.11+
22+
* **Framework**: FastAPI + Uvicorn
23+
* **Database**: PostgreSQL
24+
* **Cache & Limiter**: Redis
25+
* **Messaging**: Paho MQTT
26+
* **Auth**: Firebase Admin SDK
27+
* **DevOps**: Docker & Docker Compose
28+
29+
---
30+
31+
## 📋 Prasyarat
32+
33+
Sebelum memulai, pastikan Anda telah menginstal:
34+
35+
1. **Docker** & **Docker Compose** (Sangat Disarankan)
36+
2. **Git**
37+
3. Akun **Firebase** (untuk Authentication dan `serviceAccountKey.json`)
38+
39+
---
40+
41+
## 🚀 Instalasi & Menjalankan (Docker)
42+
43+
Cara termudah untuk menjalankan proyek ini adalah menggunakan Docker Compose.
44+
45+
### 1. Clone Repository
46+
```bash
47+
git clone https://github.com/username/project-ini.git
48+
cd API-PCB
49+
```
50+
51+
### 2. Konfigurasi Firebase
52+
Simpan file kredensial Firebase Admin SDK Anda ke lokasi berikut:
53+
```bash
54+
app/serviceAccountKey.json
55+
```
56+
> **Penting**: File ini bersifat rahasia. Jangan pernah commit file ini ke repository publik!
57+
58+
### 3. Konfigurasi Environment Variable
59+
Buat file `.env` di root folder dan sesuaikan isinya:
60+
61+
```env
62+
# Database Credentials
63+
DB_USER=iot_user
64+
DB_PASSWORD=iot_password
65+
DB_NAME=iot_db
66+
67+
# Redis URL (Default Docker)
68+
REDIS_URL=redis://redis:6379/0
69+
70+
# (Opsional) Lainnya sesuai kebutuhan
71+
```
72+
73+
### 4. Jalankan Aplikasi
74+
```bash
75+
docker-compose up -d --build
76+
```
77+
Aplikasi akan berjalan di:
78+
* **API**: `http://localhost:8006`
79+
* **Swagger Docs**: `http://localhost:8006/docs`
80+
* **Redoc**: `http://localhost:8006/redoc`
81+
82+
---
83+
84+
## 📡 Dokumentasi API
85+
86+
Seluruh endpoint membutuhkan **Header Authorization** (Bearer Token dari Firebase Client), kecuali endpoint *Auto Register*.
87+
88+
### 📱 1. Device Management (`/api/devices`)
89+
90+
| Method | Endpoint | Deskripsi | Limit Rate |
91+
| :--- | :--- | :--- | :--- |
92+
| `GET` | `/my-devices` | Mengambil daftar alat milik user. | 20/menit |
93+
| `POST` | `/claim` | Mengklaim alat baru menggunakan ID & PIN. | 5/menit |
94+
| `PUT` | `/{device_id}` | Mengubah nama alat. | 10/menit |
95+
| `DELETE` | `/{device_id}` | Menghapus alat dari akun (Unclaim). | 5/menit |
96+
97+
### 🎛️ 2. Device Control (`/api/devices`)
98+
99+
| Method | Endpoint | Deskripsi | Parameter |
100+
| :--- | :--- | :--- | :--- |
101+
| `POST` | `/control-relay` | Mengirim perintah ON/OFF ke alat via MQTT. | `device_id`, `relay_name`, `state` (bool) |
102+
| `POST` | `/auto-register` | (Internal) Pendaftaran otomatis oleh ESP32. | *Requires Factory Secret* |
103+
104+
> **Relay Mapping**:
105+
> * `relay_1` -> Topik `.../cmd/lampu`
106+
> * `relay_2` -> Topik `.../cmd/pompa_minum`
107+
> * `relay_3` -> Topik `.../cmd/pompa_siram`
108+
109+
### 📊 3. Sensor Logs (`/api/logs`)
110+
111+
| Method | Endpoint | Deskripsi |
112+
| :--- | :--- | :--- |
113+
| `GET` | `/{device_id}` | Mengambil data log sensor dari alat tertentu. |
114+
115+
---
116+
117+
## 📂 Struktur Proyek
118+
119+
```
120+
API-PCB/
121+
├── app/
122+
│ ├── api/ # Router & Logic API per versi (v1)
123+
│ │ └── v1/
124+
│ │ ├── auth.py # Helper Auth Firebase
125+
│ │ ├── devices.py # Endpoint Device & Control
126+
│ │ └── logs.py # Endpoint Logs
127+
│ ├── core/ # Konfigurasi Database & Global
128+
│ ├── crud/ # Operasi Database (Create, Read, etc)
129+
│ ├── models/ # Schema Database (SQLAlchemy)
130+
│ ├── mqtt/ # Client & Handler MQTT
131+
│ ├── schemas/ # Validasi Data (Pydantic)
132+
│ ├── main.py # Entry Point Aplikasi (Lifespan, Startup)
133+
│ └── serviceAccountKey.json # (WAJIB ADA) Kunci Firebase
134+
├── docker-compose.yml # Orkestrasi Container (App, DB, Redis)
135+
├── dockerfile # Definisi Image App
136+
├── requirements.txt # Dependensi Python
137+
├── test_client.py # Script Python untuk mencoba API manual
138+
└── readme.md # Dokumentasi ini
139+
```
140+
141+
---
142+
143+
## 🧪 Testing Manual
144+
145+
Terdapat script `test_client.py` untuk mencoba API tanpa frontend.
146+
147+
1. Pastikan `requests` terinstall: `pip install requests`
148+
2. Edit `test_client.py`:
149+
* Masukkan `FIREBASE_WEB_API_KEY` (dari Firebase Console).
150+
* Masukkan credentials user dummy (Email/Pass).
151+
* Set `DEVICE_ID` target.
152+
3. Jalankan:
153+
```bash
154+
python test_client.py
155+
```
156+
157+
---
158+
159+
## 🤝 Kontribusi
160+
161+
Pull requests dipersilakan. Untuk perubahan besar, harap buka issue terlebih dahulu untuk mendiskusikan apa yang ingin Anda ubah.
162+
163+
---
164+
165+
**Happy Coding!** 🚀

0 commit comments

Comments
 (0)