feat: auto-install CUPS printer on startup when work_place != notconfig

This commit is contained in:
2026-04-24 15:52:04 +03:00
parent a94fc40a9e
commit 07096ad4aa

49
app.py
View File

@@ -477,6 +477,55 @@ print("=" * 60)
print("CONTINUING WITH NORMAL INITIALIZATION" if not CONFIGURATION_MODE else "CONFIGURATION MODE ACTIVE") print("CONTINUING WITH NORMAL INITIALIZATION" if not CONFIGURATION_MODE else "CONFIGURATION MODE ACTIVE")
print("=" * 60) print("=" * 60)
# ---------------------------------------------------------------------------
# Auto-printer setup
# ---------------------------------------------------------------------------
def _ensure_printer(device_name):
"""
Check whether a CUPS printer named *device_name* is installed.
If not found, install it automatically using lpadmin.
Only called when work_place is set to a real name (not 'notconfig').
"""
print(f"🖨 Checking printer for work_place='{device_name}' ...")
try:
import cups
conn = cups.Connection()
printers = conn.getPrinters()
if device_name in printers:
print(f"✓ Printer '{device_name}' already installed — no action needed.")
return
# Printer not found — install it
print(f"🖨 Printer '{device_name}' not found. Starting installation ...")
cmd = [
"sudo", "/usr/sbin/lpadmin",
"-p", device_name,
"-E",
"-v", "usb://CITIZEN/CT-S310II?serial=00000000",
"-m", "CTS310II.ppd",
]
result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
if result.returncode == 0:
print(f"✓ Printer '{device_name}' installed successfully.")
else:
print(f"✗ lpadmin returned code {result.returncode}: {result.stderr.strip()}")
except ImportError:
print("⚠ python3-cups not available — printer check skipped. "
"Install with: sudo apt install python3-cups")
except Exception as e:
print(f"⚠ Printer setup skipped: {e}")
# Run auto-printer setup only when device has a real work_place
_auto_printer_name = APP_CONFIG.get("device_name", "notconfig")
if not CONFIGURATION_MODE and _auto_printer_name.lower() != "notconfig":
_ensure_printer(_auto_printer_name)
else:
if _auto_printer_name.lower() == "notconfig":
print("🖨 Printer setup skipped (work_place=notconfig).")
def check_system_requirements(): def check_system_requirements():
""" """
Check and set up system requirements for the application Check and set up system requirements for the application