Browse Source

fix printer error, status

latapp 9 months ago
parent
commit
927fb25005
3 changed files with 17 additions and 14 deletions
  1. 1 1
      config/mails.py
  2. 3 3
      routes/orders.py
  3. 13 10
      services/print_service.py

+ 1 - 1
config/mails.py

@@ -282,7 +282,7 @@ REGISTER_MAIL = {
     """
 }
 PRINTER_DISCONNECTED_MAIL = {
-    "subject": "Printer Disconnected",
+    "subject": "Printer {location} Disconnected",
     "body": """
 <!DOCTYPE html>
 <html>

+ 3 - 3
routes/orders.py

@@ -42,7 +42,7 @@ async def printer_order(order: OrderWeb, current_user: User = Depends(get_curren
     # Printer status validation
     if not DEVELOPMENT:
         try:
-            printer_status = ps.get_status()
+            printer_status = filter(lambda x: x == False, [ps.get_status(Locations.BAR), ps.get_status(Locations.KITCHEN), ps.get_status(Locations.COCTELERY)])
             if not printer_status:
                 logger.error(f"Printer is not connected. Order from user {current_user.email} cannot be processed.")
        
@@ -51,8 +51,8 @@ async def printer_order(order: OrderWeb, current_user: User = Depends(get_curren
                 email_thread = Thread(
                     target=get_email_sender().send_email,
                     args=(
-                        PRINTER_DISCONNECTED_MAIL["subject"], 
-                        PRINTER_DISCONNECTED_MAIL["body"], 
+                        PRINTER_DISCONNECTED_MAIL["subject"],
+                        PRINTER_DISCONNECTED_MAIL["body"].format(location=", ".join([loc.value for loc in printer_status])),
                         ["erwinjacimino2003@gmail.com", "mompyn@gmail.com"]
                     ),
                     daemon=True

+ 13 - 10
services/print_service.py

@@ -9,6 +9,14 @@ from enums.locations import Locations
 logger = getLogger(__name__)
 user_data_service = DataServiceFactory.get_user_service()
 
+def get_printer_url(location: Locations) -> str:
+    if location == Locations.KITCHEN:
+        return "http://localhost:6002"
+    elif location == Locations.COCTELERY:
+        return "http://localhost:6004"
+    else:
+        return "http://localhost:6000"
+
 def print_order(order: Order, location:Locations ):
     """Send order to printer"""
     logger.info(f"Attempting to print order for table {order.table}")
@@ -21,12 +29,7 @@ def print_order(order: Order, location:Locations ):
     #     case Locations.COCTELERY:
     #         printer_url = "http://localhost:6004"
 
-    if location == Locations.KITCHEN:
-        printer_url = "http://localhost:6002"
-    elif location == Locations.COCTELERY:
-        printer_url = "http://localhost:6004"
-    else:
-        printer_url = "http://localhost:6000"
+    printer_url = get_printer_url(location)
 
     try:
         if not order.items or not order.table:
@@ -79,7 +82,7 @@ def print_order(order: Order, location:Locations ):
 def print_ticket(number_table: int):
     """Send a ticket to the printer"""
     logger.info(f"Attempting to print ticket for table {number_table}")
-    
+    printer_url = get_printer_url(Locations.BAR)
     try:
 
         
@@ -110,13 +113,13 @@ def print_ticket(number_table: int):
         
         raise
 
-def get_status():
+def get_status(location: Locations):
     """Get the status of the printer service"""
     logger.info("Checking printer service status")
     
+    printer_url = get_printer_url(location)
+
     try:
-        
-        
         response = requests.get(
             f"{printer_url}/status", 
             headers={"Authorization": f"Bearer PRINTER123cerveza@"},