Make USB printer more resilient #1

Merged
Typhus merged 1 commits from feature/make-usb-printer-more-resilient into main 2026-04-29 09:51:22 +00:00

16
main.py
View File

@ -9,8 +9,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 +25,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 +85,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 +95,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)}")