-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdevice_handler.py
76 lines (66 loc) · 2.23 KB
/
device_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from brother_ql.raster import BrotherQLRaster
from brother_ql.conversion import convert
from brother_ql.backends.helpers import send
import usb.core
import streamlit as st
def process_print_job(image, printer_info, temp_file_path, rotate=0, dither=False, label_type="102", debug=False):
"""
Process a single print job.
Returns (success, error_message)
"""
# Get debug flag from secrets if not explicitly passed
if not debug and 'debug' in st.secrets:
debug = st.secrets['debug']
try:
# Prepare the image for printing
qlr = BrotherQLRaster(printer_info["model"])
# Debug print before conversion
if debug:
print(f"Starting print job with label_type: {label_type}")
instructions = convert(
qlr=qlr,
images=[temp_file_path],
label=label_type,
rotate=rotate,
threshold=70,
dither=dither,
compress=True,
red=False,
dpi_600=False,
hq=False,
cut=True,
)
# Debug logging
if debug:
print(f"""
Print parameters:
- Label type: {label_type}
- Rotate: {rotate}
- Dither: {dither}
- Model: {printer_info['model']}
- Backend: {printer_info['backend']}
- Identifier: {printer_info['identifier']}
""")
# Try to print using Python API
success = send(
instructions=instructions,
printer_identifier=printer_info["identifier"],
backend_identifier="pyusb",
)
if not success:
return False, "Failed to print using Python API"
return True, None
except usb.core.USBError as e:
if "timeout error" in str(e):
if debug:
print("USB timeout error occurred, but it's okay.")
return True, None
error_msg = f"USBError encountered: {e}"
if debug:
print(error_msg)
return False, error_msg
except Exception as e:
error_msg = f"Unexpected error during printing: {str(e)}"
if debug:
print(error_msg)
return False, error_msg