# Server (combined)

Single Flask app on **port 5000** (server IP **192.168.50.5**) combining:

- **Temps** – dashboard and API for latest temp/humidity per unit (MySQL `shop_stats.Temps`)
- **MoistureMeeter** – ESP32 readings, device commands, dashboard (MySQL `shop_stats`)
- **FillDrainPump** – schedule sync for ESP32 (file-based)

## Run

```bash
cd C:\Users\Bill\Software\Server
pip install -r requirements.txt
python app.py
```

Or with Flask CLI:

```bash
flask --app app run --host 0.0.0.0 --port 5000
```

Server listens on `http://0.0.0.0:5000` (all interfaces), so it accepts connections from other machines.

## Running on a remote server

The app is set up to run on a remote server:

- **Binding:** It uses `host="0.0.0.0"`, so it listens on all interfaces and is reachable from the network (not only localhost).
- **Port:** Ensure **port 5000** is open in the server firewall and any cloud/network ACLs so clients and ESP32 devices can reach it.
- **Configuration:** Edit `config.py` on the server. If MySQL runs on a different host, set `MYSQL_HOST` to that host’s IP or hostname. FillDrain paths default to the project’s `filldrain/` folder; use absolute paths in config if the app runs from a different working directory.
- **Production (optional):** For a more robust deployment, run with Gunicorn instead of the built-in server:
  ```bash
  pip install gunicorn
  gunicorn -w 2 -b 0.0.0.0:5000 app:app
  ```
  Run from the project directory so paths in `config.py` resolve correctly.

## URL map

| Path | Description |
|------|-------------|
| `/` | Hub – links to each app |
| `/temps/` | Temps dashboard |
| `/temps/api/latest` | Temps API (JSON) |
| `/moisture/` | Moisture Meter dashboard |
| `/moisture/reading` | POST – ESP32 submit reading |
| `/moisture/device/<id>/command` | PUT/POST – set sleep/nosleep |
| `/moisture/device/<id>/device_id` | PUT/POST – assign new device ID |
| `/moisture/device/<id>/node_name` | PUT/POST – assign new node name (max 50 chars) |
| `/moisture/api/readings` | GET – recent readings |
| `/moisture/api/readings/latest` | GET – latest per device |
| `/moisture/api/readings/clear-old` | POST/DELETE – delete old records |
| `/moisture/api/device/<id>/command` | GET – current command |
| `/moisture/health` | GET – health check |
| `/filldrain/sync` | GET/POST – ESP32 schedule sync (plain text) |

## Configuration

All settings are in **`config.py`**. Temps and MoistureMeeter share a common MySQL database.

**MySQL** (used by both Temps and Moisture):

- `MYSQL_HOST` – database host (default: localhost; use 192.168.50.5 or the DB server IP if MySQL is on another machine)
- `MYSQL_PORT` – 3306
- `MYSQL_DATABASE` – shop_stats
- `MYSQL_USER` – bill
- `MYSQL_PASSWORD` – (set in config)

**FillDrain** (file paths, relative to project directory):

- `FILLDRAIN_SCHEDULE_FILE` – path to schedule file (default: `filldrain/schedule.txt`)
- `FILLDRAIN_LOG_FILE` – path to request log (default: `filldrain/filldrain.log`)

Schedule file format: three lines – `on1` (e.g. 08:00), `on2` (e.g. 19:00), `duration` (e.g. 5).

**Security:** `config.py` contains the database password. If the repo is shared, add `config.py` to `.gitignore` and use a `config.example.py` (without the real password) as a template.

## Firmware / client updates

After moving to this combined server (192.168.50.5):

- **Temps:** Open dashboard at `http://192.168.50.5:5000/temps/`. No firmware change.
- **MoistureMeeter ESP32:** Point to `http://192.168.50.5:5000/moisture/reading` (was port 5005 and path `/reading`).
- **FillDrainPump ESP32:** Point to `http://192.168.50.5:5000/filldrain/sync` (was port 5002 and path `/sync`).

## Adding a new project

1. Add a new blueprint under `blueprints/`, e.g. `blueprints/widget.py`, with `url_prefix="/widget"`.
2. Register it in `app.py`: `app.register_blueprint(widget_bp, url_prefix="/widget")`.
3. Optionally add `templates/widget/` and a link on `templates/index.html`.
