asdfdasfasd
This commit is contained in:
parent
c242c75b6b
commit
357e47cc73
59
app.py
59
app.py
@ -34,9 +34,6 @@ printer_model = "QL-1100"
|
||||
backend = 'pyusb'
|
||||
printer = 'usb://0x04f9:0x20a7'
|
||||
|
||||
# Create a lock to manage printer access
|
||||
printer_lock = threading.Lock()
|
||||
|
||||
# Convert PDF to image
|
||||
def convert_pdf_to_image(pdf_path):
|
||||
"""Converts a PDF to a PIL Image"""
|
||||
@ -50,19 +47,6 @@ def convert_pdf_to_image(pdf_path):
|
||||
app.logger.error(f"Error converting PDF: {str(e)}")
|
||||
return None
|
||||
|
||||
# Reinitialize the printer by disconnecting and reconnecting
|
||||
def reinitialize_printer():
|
||||
try:
|
||||
app.logger.info("Reinitializing printer...")
|
||||
usb_device = usb.core.find(idVendor=0x04f9, idProduct=0x20a7)
|
||||
if usb_device:
|
||||
usb_device.reset()
|
||||
app.logger.info("Printer reinitialized successfully.")
|
||||
else:
|
||||
app.logger.error("Failed to find printer device.")
|
||||
except Exception as e:
|
||||
app.logger.error(f"Error reinitializing printer: {str(e)}")
|
||||
|
||||
# Function to print address labels
|
||||
def print_address_label(pdf_path):
|
||||
try:
|
||||
@ -93,40 +77,17 @@ def print_address_label(pdf_path):
|
||||
cut=True
|
||||
)
|
||||
|
||||
# Try to acquire the printer lock with a longer timeout
|
||||
max_retries = 5 # Retry up to 5 times
|
||||
retry_delay = 5 # Retry every 5 seconds
|
||||
for attempt in range(max_retries):
|
||||
if printer_lock.acquire(timeout=30): # 30 seconds timeout to allow more time for the printer
|
||||
try:
|
||||
app.logger.info(f"Attempting to send print job (Attempt {attempt + 1})...")
|
||||
send(
|
||||
instructions=instructions,
|
||||
printer_identifier=printer,
|
||||
backend_identifier=backend,
|
||||
blocking=True # Ensure blocking, so we wait for the print to complete
|
||||
)
|
||||
app.logger.info("Print job sent successfully")
|
||||
break
|
||||
except Exception as e:
|
||||
app.logger.error(f"Error during printing: {str(e)}")
|
||||
if attempt < max_retries - 1:
|
||||
app.logger.info(f"Retrying after {retry_delay} seconds...")
|
||||
time.sleep(retry_delay)
|
||||
else:
|
||||
app.logger.error("Max retries reached. Failed to send print job.")
|
||||
reinitialize_printer() # Reinitialize printer after retries
|
||||
else:
|
||||
app.logger.error(f"Failed to acquire printer lock (Attempt {attempt + 1}). Printer is busy.")
|
||||
if attempt < max_retries - 1:
|
||||
app.logger.info(f"Retrying after {retry_delay} seconds...")
|
||||
time.sleep(retry_delay)
|
||||
else:
|
||||
app.logger.error("Printer is still busy after retries.")
|
||||
raise Exception("Printer is busy. Try again later.")
|
||||
|
||||
app.logger.info("Sending to printer...")
|
||||
send(
|
||||
instructions=instructions,
|
||||
printer_identifier=printer,
|
||||
backend_identifier=backend,
|
||||
blocking=True
|
||||
)
|
||||
app.logger.info("Print job sent successfully")
|
||||
|
||||
except Exception as e:
|
||||
app.logger.error(f"Error during label printing: {str(e)}")
|
||||
app.logger.error(f"Error during printing: {str(e)}")
|
||||
|
||||
# Worker thread that processes files from the queue
|
||||
def process_queue():
|
||||
|
Loading…
x
Reference in New Issue
Block a user