Compare commits

...

2 Commits

Author SHA1 Message Date
Catering-PC fe412ed9e9 Hotfix: Add missing import 2026-04-29 12:00:15 +02:00
David Rodenkirchen 147508184f Make USB printer more resilient 2026-04-29 11:51:07 +02:00
+8 -9
View File
@@ -1,5 +1,6 @@
from datetime import datetime
from typing import List
import unicodedata
import uvicorn
from fastapi import FastAPI, HTTPException, Header
@@ -9,8 +10,6 @@ from escpos.printer import Usb
from escpos.capabilities import get_profile
from PIL import Image
PRINTER = Usb(0x28e9, 0x0289, profile="simple")
MAX_LINE_LEN = 32
SECRET_PASSWORD = "Alkohol1"
@@ -27,13 +26,11 @@ class Order(BaseModel):
seat_id: str
items: List[OrderItem]
def get_printer() -> Usb:
return Usb(0x28e9, 0x0289, profile="simple")
def sanitize_text(text: str) -> str:
return (text.replace("ä", "ae")
.replace("ö", "oe")
.replace("ü", "ue")
.replace("ß", "ss"))
return unicodedata.normalize("NFKD", text).encode("ascii", "ignore").decode()
def build_header(order: Order, copy_num: int) -> str:
return (
@@ -89,7 +86,7 @@ api = FastAPI()
@api.post("/print_order")
async def print_order_api_endpoint(order: Order, x_password: str = Header(None)):
def print_order_api_endpoint(order: Order, x_password: str = Header(None)):
if x_password != SECRET_PASSWORD:
raise HTTPException(status_code=401, detail="Unauthorized")
@@ -99,7 +96,9 @@ async def print_order_api_endpoint(order: Order, x_password: str = Header(None))
for copy_num in range(1, 3):
try:
print_order(order, PRINTER, copy_num)
printer = get_printer()
print_order(order, printer, copy_num)
printer.close()
except Exception as e:
raise HTTPException(status_code=500, detail=f"Printing failed: {str(e)}")