# pySonnerie Backend Backend Python avec API REST HTTPS authentifiee pour piloter la lecture audio a partir de triggers serie (ESP32). ## Fonctions implementees - API REST securisee par authentification HTTP Basic - HTTPS avec generation auto d'un certificat autosigne si absent - Gestion des triggers via `data/conf.json` - Lecture audio par trigger serie (`GPIOX`) ou via API de forgage manuel - Arret de la sortie audio via API ## Arborescence ```text backend/ app/ data/ conf.json musiques/ certs/ # cree au premier lancement run.py requirements.txt ``` ## Prerequis - Python 3.11+ - `ffplay` installe (paquet ffmpeg) - acces au port serie (exemple: `/dev/ttyUSB0`) ## Installation ```bash cd backend python3 -m venv .venv source .venv/bin/activate pip install -r requirements.txt ``` Si présence d'un proxy, la dernière commande sera `pip install -r requirements.txt --proxy http://proxy:port`. ## Configuration Le fichier `data/conf.json` contient: - `server.host`, `server.port`, `server.tls_cert`, `server.tls_key` - `auth.username`, `auth.password` - `serial.enabled`, `serial.port`, `serial.baudrate`, `serial.timeout` - `triggers` Exemple d'entree trigger: ```json "GPIO23": { "name": "Bouton entree", "type": "GPIO23", "music_file": "bell.mp3", "start_seconds": 2.5, "end_seconds": 10.0 } ``` ## Lancement ```bash cd backend source .venv/bin/activate python run.py ``` API dispo sur `https://:`. ## Endpoints REST Tous sauf `/api/health` exigent auth Basic. - `GET /api/health` - `GET /api/config` - `GET /api/triggers` - `PUT /api/triggers/{trigger_id}` - `PATCH /api/triggers/{trigger_id}` - `DELETE /api/triggers/{trigger_id}` - `GET /api/play/{trigger_id}` - `GET /api/stop` ### Exemples cURL ```bash curl -k -u admin:change-me https://127.0.0.1:8443/api/triggers ``` ```bash curl -k -u admin:change-me \ -H "Content-Type: application/json" \ -X PUT https://127.0.0.1:8443/api/triggers/GPIO23 \ -d '{ "name": "Bouton entree", "type": "GPIO23", "music_file": "bell.mp3", "start_seconds": 0, "end_seconds": null }' ``` ```bash curl -k -u admin:change-me \ https://127.0.0.1:8443/api/play/GPIO23 ``` ```bash curl -k -u admin:change-me https://127.0.0.1:8443/api/stop ``` ## Service Debian Le fichier `systemd/pysonnerie-backend.service` est fourni comme base. ```bash sudo cp backend/systemd/pysonnerie-backend.service /etc/systemd/system/ sudo systemctl daemon-reload sudo systemctl enable --now pysonnerie-backend ``` Adapte les chemins `WorkingDirectory` et `ExecStart` avant activation.