You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

3.0 KiB

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

backend/
  app/
  data/
    conf.json
    musiques/
  certs/                 # cree au premier lancement
  run.py
  init.py
  requirements.txt

Prerequis

  • Python 3.11+
  • ffplay installe (paquet ffmpeg)
  • 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

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 :

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:

"GPIO23": {
  "name": "Bouton entree",
  "type": "GPIO23",
  "music_file": "bell.mp3",
  "start_seconds": 2.5,
  "end_seconds": 10.0,
  "volume": 0.8
}

Lancement

cd backend
source .venv/bin/activate
python run.py

API dispo sur https://<host>:<port>.

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

curl -k -u admin:change-me https://127.0.0.1:8443/api/triggers
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
  }'
curl -k -u admin:change-me \
  https://127.0.0.1:8443/api/play/GPIO23
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.

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.