# Deploying the BLE Admin Panel on a local server (Windows)

The whole product — REST API **and** the admin panel — runs from **one**
FastAPI server on port **8000**. PostgreSQL holds the data.

```
Browser ─┐
Phone app ┼──►  http://<server-ip>:8000  ──►  PostgreSQL (bluetooth_db)
         ─┘        (FastAPI + static panel)
```

---

## 1. Prerequisites (once)

- **Python 3.11+** installed.
- **PostgreSQL** running, with a database `bluetooth_db`
  (connection string lives in `app/database.py`:
  `postgresql://postgres:1234@localhost:5432/bluetooth_db`).
- Virtual environment + dependencies:

```bat
cd D:\projects\ble_backend
python -m venv venv
venv\Scripts\python -m pip install fastapi uvicorn sqlalchemy psycopg2-binary "pydantic[email]" bcrypt email-validator
```

Tables are created automatically on first start, and a default admin
`admin` / `admin123` is seeded (only if no admin exists).

---

## 2. Start the server

Double-click **`start_server.bat`**, or run:

```bat
cd D:\projects\ble_backend\app
..\venv\Scripts\python -m uvicorn main:app --host 0.0.0.0 --port 8000
```

- `--host 0.0.0.0` = reachable from other devices on the LAN (needed for the phone).
- No `--reload` — that flag is only for development.
- Optional: add `--workers 2` for more concurrency.

### URLs

| What | URL |
|------|-----|
| **Admin panel** | `http://localhost:8000/app/` (root `/` redirects here) |
| API docs (Swagger) | `http://localhost:8000/docs` |
| Health check | `http://localhost:8000/health` |

The panel calls the API on the **same origin**, so nothing needs editing when
the server moves to another machine or its IP changes.

---

## 3. LAN access (phone + other computers)

1. Find this PC's IP:  `ipconfig`  → IPv4 Address (e.g. `192.168.10.84`).
2. Open the firewall port (run **Command Prompt as Administrator**, once):

```bat
netsh advfirewall firewall add rule name="BLE Admin 8000" dir=in action=allow protocol=TCP localport=8000
```

3. From any device on the same Wi-Fi: `http://192.168.10.84:8000/app/`

### Mobile app
The Android app talks to the API directly, so set the server address once in
`app/src/main/java/com/patientmanagement/app/net/Api.kt`:

```kotlin
var BASE_URL = "http://192.168.10.84:8000"   // this server's LAN IP
```

(An emulator uses `http://10.0.2.2:8000`.) Rebuild/reinstall after changing it.
A fixed IP for the server (DHCP reservation / static IP) avoids re-editing this.

---

## 4. Keep it running automatically

**Option A — Task Scheduler (simplest):** Create Task → *Run whether user is
logged on or not* → Trigger: *At startup* → Action: Start
`D:\projects\ble_backend\start_server.bat`.

**Option B — Windows service via NSSM** (`https://nssm.cc`):

```bat
nssm install BLEAdmin "D:\projects\ble_backend\venv\Scripts\python.exe" "-m uvicorn main:app --host 0.0.0.0 --port 8000"
nssm set BLEAdmin AppDirectory "D:\projects\ble_backend\app"
nssm start BLEAdmin
```

Either way, make sure the **PostgreSQL** service is also set to start
automatically (it is by default).

---

## 5. Before real use (hardening — optional)

- **Change the admin password.** The default is only seeded when no admin
  exists. Set a new one, e.g. from `python` in the venv:
  ```python
  from database import SessionLocal; from model.admin import Admin
  from core.security import hash_password
  db=SessionLocal(); a=db.query(Admin).first(); a.password=hash_password("NEW_PASSWORD"); db.commit()
  ```
- **DB credentials:** change the Postgres password and update the string in
  `app/database.py` (or move it to an environment variable).
- **CORS:** `main.py` allows all origins (`*`) — fine on a private LAN. If you
  expose it more widely, restrict `allow_origins` to the panel's URL.
- **Backups:** dump the database periodically —
  `pg_dump -U postgres bluetooth_db > backup.sql`.
- **Auth tokens:** login endpoints currently validate credentials but issue no
  JWT/session; add token protection before using outside a trusted network.

---

## 6. Stopping / restarting

- Foreground (`start_server.bat`): close the window or press `Ctrl+C`.
- Service: `nssm restart BLEAdmin` / `nssm stop BLEAdmin`.
- If port 8000 is "in use" after a crash, find and kill the stray process:
  `netstat -ano | findstr :8000`  then  `taskkill /F /PID <pid>`.
