Robustness fixes

This commit is contained in:
2026-02-06 17:35:31 +00:00
parent 0fab91c077
commit b412900b4b
3 changed files with 37 additions and 9 deletions

View File

@@ -6,7 +6,6 @@ ENV PYTHONUNBUFFERED=1
# Install system dependencies # Install system dependencies
RUN apt-get update && apt-get install -y \ RUN apt-get update && apt-get install -y \
fonts-dejavu \ fonts-dejavu \
usbutils \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*
# Set working directory # Set working directory
@@ -16,9 +15,6 @@ WORKDIR /app
COPY requirements.txt . COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt RUN pip install --no-cache-dir -r requirements.txt
# Copy the application # Application files will be mounted via volume
COPY client.py .
COPY templates.py .
# Run the application # Run the application
CMD ["python", "client.py"] CMD ["python", "client.py"]

View File

@@ -22,15 +22,22 @@ LABEL_SIZE_DEFAULT = os.getenv('LABEL_SIZE_DEFAULT', '29x90')
def print_label(image, printer=PRINTER_DEVICE, model=PRINTER_MODEL, label=LABEL_SIZE_DEFAULT): def print_label(image, printer=PRINTER_DEVICE, model=PRINTER_MODEL, label=LABEL_SIZE_DEFAULT):
"""Print the label directly using brother_ql module""" """Print the label directly using brother_ql module"""
import os
try: try:
# Check if printer device exists
if not os.path.exists(printer):
raise Exception(f"Printer device {printer} not found. Make sure the printer is powered on and connected via USB.")
qlr = BrotherQLRaster(model) qlr = BrotherQLRaster(model)
qlr.exception_on_warning = True qlr.exception_on_warning = True
# Convert the PIL image to instructions # Convert the PIL image to instructions
instructions = convert(qlr=qlr, images=[image], label=label, cut=True) instructions = convert(qlr=qlr, images=[image], label=label, cut=True)
# Send to printer # Send to printer using linux_kernel backend
status = send(instructions=instructions, printer_identifier=printer, backend_identifier='linux_kernel', blocking=True) print(f"Sending to printer: {printer}")
status = send(instructions=instructions, printer_identifier=f"file://{printer}", backend_identifier='linux_kernel', blocking=True)
return status return status
except Exception as e: except Exception as e:
@@ -82,6 +89,29 @@ def on_message(client, userdata, msg):
print("Printing label...") print("Printing label...")
status = print_label(image, label=label_size) status = print_label(image, label=label_size)
print(f"Print status: {status}") print(f"Print status: {status}")
# Check for print errors
if not status.get('did_print', False):
printer_state = status.get('printer_state', {})
status_type = printer_state.get('status_type', 'Unknown')
media_type = printer_state.get('media_type', 'Unknown')
media_width = printer_state.get('media_width', 'Unknown')
errors = printer_state.get('errors', [])
error_details = {
"error": f"Print failed: {status_type}",
"status_type": status_type,
"media_type": media_type,
"media_width": media_width,
"errors": errors,
"outcome": status.get('outcome', 'unknown'),
"original_payload": raw_payload
}
error_msg = f"Print failed: {status_type}. Media: {media_type} ({media_width}mm)"
print(error_msg)
client.publish(MQTT_TOPIC_PUB_ERRORS, json.dumps(error_details))
raise Exception(error_msg)
except json.JSONDecodeError as e: except json.JSONDecodeError as e:
error_msg = f"Invalid JSON in message: {e}. Raw payload: {raw_payload}" error_msg = f"Invalid JSON in message: {e}. Raw payload: {raw_payload}"

View File

@@ -5,8 +5,10 @@ services:
build: . build: .
env_file: env_file:
- .env - .env
devices: privileged: true # Required for /dev access when printer powers on/off
- /dev/usb/lp0:/dev/usb/lp0
volumes: volumes:
- ./output:/app/output # For test PNGs - ./output:/app/output # For test PNGs
- ./client.py:/app/client.py # Mount source for live updates
- ./templates.py:/app/templates.py # Mount templates for live updates
- /dev:/dev # Mount /dev for dynamic device access
restart: unless-stopped restart: unless-stopped