# pySonnerie Backend Backend Python avec API REST HTTPS authentifiée pour piloter la lecture audio à partir de différent types de triggers. ## Fonctions implementees - API REST securisée - HTTPS avec generation auto d'un certificat autosigne si absent - Gestion des triggers via `data/conf.json` (trigger type GPIO uniquement pour l'instant) - Lecture audio par trigger ou via API - Arret de la sortie audio via API ## Arborescence ```text backend/ app/ data/ conf.json musiques/ certs/ # cree au premier lancement run.py init.py requirements.txt ``` ## Prerequis - Python 3.11+ - `sox` installe (commandes `play` et `soxi`) - `libsox-fmt-mp3` installe (support MP3 pour sox) - Un serveur son installé et configuré (exemple: `alsa` sur debian avec alsa-utils et configuration de la carte son) - acces au port serie (exemple: `/dev/ttyUSB0`) Installation audio recommandee (Debian/Ubuntu): ```bash sudo apt update sudo apt install sox libsox-fmt-mp3 ffmpeg ``` ## 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 script `init.py` crée `data/conf.json` avec des valeurs par défaut et un mot de passe admin aléatoire : ```bash cd backend python init.py ``` Le mot de passe généré est affiché une seule fois dans le terminal. Le fichier est créé avec les permissions `600`. 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, "volume": 80, "fade_in_seconds": 0.3, "fade_out_seconds": 0.4, "normalize_audio": true } ``` ## 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. ## Changelog - Le changelog global du projet est dans `../CHANGELOG.md`. - Les regles de mise a jour sont decrites dans `../docs/CHANGELOG_GUIDE.md`.