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

@@ -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):
"""Print the label directly using brother_ql module"""
import os
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.exception_on_warning = True
# Convert the PIL image to instructions
instructions = convert(qlr=qlr, images=[image], label=label, cut=True)
# Send to printer
status = send(instructions=instructions, printer_identifier=printer, backend_identifier='linux_kernel', blocking=True)
# Send to printer using linux_kernel backend
print(f"Sending to printer: {printer}")
status = send(instructions=instructions, printer_identifier=f"file://{printer}", backend_identifier='linux_kernel', blocking=True)
return status
except Exception as e:
@@ -82,6 +89,29 @@ def on_message(client, userdata, msg):
print("Printing label...")
status = print_label(image, label=label_size)
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:
error_msg = f"Invalid JSON in message: {e}. Raw payload: {raw_payload}"