Files
photoBooth/photobooth-app/script/script_print.sh
T
admin dd5c4be555
🚀 Deploy — JH Photomaton / 🔍 Vérification (push) Has been cancelled
🚀 Deploy — JH Photomaton / 🍓 Deploy sur le Pi (push) Has been cancelled
Changes to be committed:
modified:   Opencode/scripts/script_print.sh
	modified:   photobooth-app/script/script_print.sh
2026-07-17 18:20:29 +02:00

126 lines
4.6 KiB
Bash

#!/bin/bash
# =============================================================================
# script_print.sh - Impression photo pour photomaton
# =============================================================================
# Usage: script_print.sh "<filename>" "<media_type>" "<action_config_name>" "<copies>"
# Appele par photobooth-app via l'action Share "Impression"
#
# Imprimantes: Canon Selphy CP1300 x2 via WiFi (blanche + noire)
# Load balancing: utilise l'imprimante idle, sinon file d'attente sur la 1ere
#
# Sortie stdout parsable par printer_service.py:
# PRINTED:<printer_name>:<copies> en cas de succes
# PRINT_ERROR:<printer_name>:<raison> en cas d'erreur
# =============================================================================
#
# ── CALIBRAGE IMPRESSION ─────────────────────────────────────────────────────
#
# Selphy CP1300 — papier Postcard borderless 100x148mm
# Image traitée par photobooth-app : 2000x1333
# Overscan : 2114x1418 avec fond blanc (la Selphy rogne les bords en borderless)
# Décalage : -roll -12+8 (calibré pour l'offset naturel de l'imprimante)
# Option CUPS : PageSize=Postcard.Borderless (défaut imprimante = om_postcard-borderless_100x148mm)
# PAS de -o raw : le driver CUPS gère le rendu correctement
# =============================================================================
# (plus de TEST_MODE — paramètres calibrés définitifs ci-dessous)
set -uo pipefail
# --- Parametres ---
filename="${1:-}"
media_type="${2:-}"
action_config_name="${3:-}"
copies="${4:-1}"
# --- Noms des imprimantes CUPS (WiFi) ---
PRINTER_1="Selphy_Blanche_WiFi"
PRINTER_2="Selphy_Noire_WiFi"
# --- Logging ---
LOG_TAG="photomaton-print"
log_info() { logger -t "$LOG_TAG" "[INFO] $1"; echo "[INFO] $1"; }
log_error() { logger -t "$LOG_TAG" "[ERROR] $1"; echo "[ERROR] $1"; }
# --- Validation ---
if [ -z "$filename" ]; then
log_error "Aucun fichier specifie"
exit 1
fi
if [ ! -f "$filename" ]; then
log_error "Fichier introuvable: $filename"
exit 1
fi
if [ "$copies" -lt 1 ] || [ "$copies" -gt 3 ]; then
log_error "Nombre de copies invalide: $copies (doit etre entre 1 et 3)"
exit 1
fi
# --- Preparation de l'image pour impression ---
filename_no_ext="${filename%.jpg}"
filename_print="${filename_no_ext}_print.jpg"
if ! command -v convert &>/dev/null; then
log_error "ImageMagick (convert) non installe — impression de l'original"
FILE_TO_PRINT="$filename"
PRINT_OPTIONS="-o PageSize=Postcard.Borderless"
else
log_info "Preparation image pour impression borderless..."
if convert "$filename" \
-resize 2000x1333 \
-background white \
-gravity center \
-extent 2114x1418 \
-roll -12+8 \
-quality 95 \
"$filename_print" 2>/dev/null; then
FILE_TO_PRINT="$filename_print"
PRINT_OPTIONS="-o PageSize=Postcard.Borderless"
log_info "Image preparee: 2114x1418 (overscan+roll) → $filename_print"
else
log_error "Echec convert — impression de l'original"
FILE_TO_PRINT="$filename"
PRINT_OPTIONS="-o PageSize=Postcard.Borderless"
fi
fi
# --- Selection de l'imprimante (load balancing) ---
printer_1_status=$(lpstat -p "$PRINTER_1" 2>/dev/null || echo "not found")
printer_2_status=$(lpstat -p "$PRINTER_2" 2>/dev/null || echo "not found")
SELECTED_PRINTER=""
if [[ "$printer_1_status" == *"idle"* ]]; then
SELECTED_PRINTER="$PRINTER_1"
log_info "Imprimante selectionnee: $PRINTER_1 (idle)"
elif [[ "$printer_2_status" == *"idle"* ]]; then
SELECTED_PRINTER="$PRINTER_2"
log_info "Imprimante selectionnee: $PRINTER_2 (idle)"
else
jobs_1=$(lpstat -o "$PRINTER_1" 2>/dev/null | wc -l)
jobs_2=$(lpstat -o "$PRINTER_2" 2>/dev/null | wc -l)
if [ "$jobs_1" -le "$jobs_2" ] 2>/dev/null; then
SELECTED_PRINTER="$PRINTER_1"
else
SELECTED_PRINTER="$PRINTER_2"
fi
log_info "Aucune imprimante idle, file sur: $SELECTED_PRINTER (P1=$jobs_1 jobs, P2=$jobs_2 jobs)"
fi
# --- Impression ---
log_info "Impression x${copies} sur $SELECTED_PRINTER [mode ${TEST_MODE}]: $FILE_TO_PRINT"
job_output=$(lp -n "$copies" -d "$SELECTED_PRINTER" $PRINT_OPTIONS "$FILE_TO_PRINT" 2>&1)
lp_exit=$?
if [ $lp_exit -ne 0 ]; then
log_error "lp a echoue (exit $lp_exit): $job_output"
echo "PRINT_ERROR:${SELECTED_PRINTER}:lp_failed"
[ -f "$filename_print" ] && rm -f "$filename_print" 2>/dev/null
exit 1
fi
# Extraction du job ID (ex: "request id is Selphy_Blanche_WiFi-42 (1 file(s