Make USB printer more resilient
This commit is contained in:
parent
fef55c722b
commit
147508184f
16
main.py
16
main.py
@ -9,8 +9,6 @@ from escpos.printer import Usb
|
|||||||
from escpos.capabilities import get_profile
|
from escpos.capabilities import get_profile
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
PRINTER = Usb(0x28e9, 0x0289, profile="simple")
|
|
||||||
|
|
||||||
MAX_LINE_LEN = 32
|
MAX_LINE_LEN = 32
|
||||||
SECRET_PASSWORD = "Alkohol1"
|
SECRET_PASSWORD = "Alkohol1"
|
||||||
|
|
||||||
@ -27,13 +25,11 @@ class Order(BaseModel):
|
|||||||
seat_id: str
|
seat_id: str
|
||||||
items: List[OrderItem]
|
items: List[OrderItem]
|
||||||
|
|
||||||
|
def get_printer() -> Usb:
|
||||||
|
return Usb(0x28e9, 0x0289, profile="simple")
|
||||||
|
|
||||||
def sanitize_text(text: str) -> str:
|
def sanitize_text(text: str) -> str:
|
||||||
return (text.replace("ä", "ae")
|
return unicodedata.normalize("NFKD", text).encode("ascii", "ignore").decode()
|
||||||
.replace("ö", "oe")
|
|
||||||
.replace("ü", "ue")
|
|
||||||
.replace("ß", "ss"))
|
|
||||||
|
|
||||||
|
|
||||||
def build_header(order: Order, copy_num: int) -> str:
|
def build_header(order: Order, copy_num: int) -> str:
|
||||||
return (
|
return (
|
||||||
@ -89,7 +85,7 @@ api = FastAPI()
|
|||||||
|
|
||||||
|
|
||||||
@api.post("/print_order")
|
@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:
|
if x_password != SECRET_PASSWORD:
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
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):
|
for copy_num in range(1, 3):
|
||||||
try:
|
try:
|
||||||
print_order(order, PRINTER, copy_num)
|
printer = get_printer()
|
||||||
|
print_order(order, printer, copy_num)
|
||||||
|
printer.close()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise HTTPException(status_code=500, detail=f"Printing failed: {str(e)}")
|
raise HTTPException(status_code=500, detail=f"Printing failed: {str(e)}")
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user