k
This commit is contained in:
		
							
								
								
									
										19
									
								
								app.py
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								app.py
									
									
									
									
									
								
							| @@ -11,6 +11,7 @@ import mimetypes | |||||||
| import logging | import logging | ||||||
| from logging.handlers import RotatingFileHandler | from logging.handlers import RotatingFileHandler | ||||||
| import time | import time | ||||||
|  | import usb.core  # For USB device re-initialization | ||||||
|  |  | ||||||
| app = Flask(__name__) | app = Flask(__name__) | ||||||
|  |  | ||||||
| @@ -50,6 +51,19 @@ def convert_pdf_to_image(pdf_path): | |||||||
|         app.logger.error(f"Error converting PDF: {str(e)}") |         app.logger.error(f"Error converting PDF: {str(e)}") | ||||||
|         return None |         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 | # Function to print address labels | ||||||
| def print_address_label(pdf_path): | def print_address_label(pdf_path): | ||||||
|     try: |     try: | ||||||
| @@ -81,8 +95,8 @@ def print_address_label(pdf_path): | |||||||
|         ) |         ) | ||||||
|  |  | ||||||
|         # Try to acquire the printer lock with a longer timeout |         # Try to acquire the printer lock with a longer timeout | ||||||
|         max_retries = 3 |         max_retries = 5  # Retry up to 5 times | ||||||
|         retry_delay = 10  # 10 seconds retry delay |         retry_delay = 5  # Retry every 5 seconds | ||||||
|         for attempt in range(max_retries): |         for attempt in range(max_retries): | ||||||
|             if printer_lock.acquire(timeout=30):  # 30 seconds timeout to allow more time for the printer |             if printer_lock.acquire(timeout=30):  # 30 seconds timeout to allow more time for the printer | ||||||
|                 try: |                 try: | ||||||
| @@ -102,6 +116,7 @@ def print_address_label(pdf_path): | |||||||
|                         time.sleep(retry_delay) |                         time.sleep(retry_delay) | ||||||
|                     else: |                     else: | ||||||
|                         app.logger.error("Max retries reached. Failed to send print job.") |                         app.logger.error("Max retries reached. Failed to send print job.") | ||||||
|  |                         reinitialize_printer()  # Reinitialize printer after retries | ||||||
|             else: |             else: | ||||||
|                 app.logger.error(f"Failed to acquire printer lock (Attempt {attempt + 1}). Printer is busy.") |                 app.logger.error(f"Failed to acquire printer lock (Attempt {attempt + 1}). Printer is busy.") | ||||||
|                 if attempt < max_retries - 1: |                 if attempt < max_retries - 1: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user