This commit is contained in:
zman 2025-04-07 17:32:59 -04:00
parent 935344d581
commit 96ee09c7be

19
app.py
View File

@ -11,6 +11,7 @@ import mimetypes
import logging
from logging.handlers import RotatingFileHandler
import time
import usb.core # For USB device re-initialization
app = Flask(__name__)
@ -50,6 +51,19 @@ 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:
@ -81,8 +95,8 @@ def print_address_label(pdf_path):
)
# Try to acquire the printer lock with a longer timeout
max_retries = 3
retry_delay = 10 # 10 seconds retry delay
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:
@ -102,6 +116,7 @@ def print_address_label(pdf_path):
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: