switch to use grocy feature
This commit is contained in:
parent
a5e0f82771
commit
bbab3193af
1 changed files with 21 additions and 17 deletions
38
app.py
38
app.py
|
|
@ -1,8 +1,7 @@
|
|||
# app.py
|
||||
import asyncio
|
||||
from functools import lru_cache
|
||||
from io import BytesIO
|
||||
from typing import List
|
||||
from typing import List, Optional
|
||||
|
||||
from barcode import Code128
|
||||
from barcode.writer import ImageWriter
|
||||
|
|
@ -20,6 +19,21 @@ if not hasattr(Image, "ANTIALIAS"):
|
|||
app = FastAPI(title="QL-1060N Stacked Barcode Printer")
|
||||
|
||||
|
||||
class PrintRequest(BaseModel):
|
||||
# from LABEL_PRINTER_PARAMS
|
||||
printer_ip: str = Field(..., description="QL-1060N IP on LAN")
|
||||
model: str = Field(..., description="Brother model")
|
||||
label: str = Field(..., description="12 mm tape")
|
||||
# grocy payload:
|
||||
grocycode: str = Field(..., description="Raw GrocyCode")
|
||||
product: str = Field(..., description="Product name")
|
||||
due_date: str = Field(..., description="Due date from stock entry")
|
||||
|
||||
class Config:
|
||||
# ignore any other fields Grocy might send
|
||||
extra = "ignore"
|
||||
|
||||
|
||||
@lru_cache(maxsize=256)
|
||||
def make_code128(data: str) -> Image.Image:
|
||||
# 1) Instantiate the barcode object (checksum auto-added for Code128)
|
||||
|
|
@ -49,15 +63,6 @@ def make_datamatrix(data: str) -> Image.Image:
|
|||
return pil_img.convert("1")
|
||||
|
||||
|
||||
class PrintRequest(BaseModel):
|
||||
data: str = Field(..., description="String to encode")
|
||||
printer_ip: str = Field(
|
||||
"192.168.178.49", description="Brother QL-1060N IP on your LAN"
|
||||
)
|
||||
model: str = Field("QL-1060N", description="Brother model")
|
||||
label: str = Field("12", description="12 mm continuous tape")
|
||||
|
||||
|
||||
def compose_label(images: List[Image.Image]) -> Image.Image:
|
||||
"""
|
||||
Horizontally stack each barcode image with padding,
|
||||
|
|
@ -119,16 +124,15 @@ def send_to_printer(image: Image.Image, printer_ip: str, model: str, label: str)
|
|||
|
||||
|
||||
@app.post("/print", summary="Print Code128 + DataMatrix side-by-side")
|
||||
async def print_stacked(req: PrintRequest):
|
||||
async def print_from_grocy(req: PrintRequest):
|
||||
try:
|
||||
loop = asyncio.get_running_loop()
|
||||
code_task = loop.run_in_executor(None, make_code128, req.data)
|
||||
dm_task = loop.run_in_executor(None, make_datamatrix, req.data)
|
||||
code_img, dm_img = await asyncio.gather(code_task, dm_task)
|
||||
|
||||
code_img, dm_img = await asyncio.gather(
|
||||
loop.run_in_executor(None, make_code128, req.grocycode),
|
||||
loop.run_in_executor(None, make_datamatrix, req.grocycode),
|
||||
)
|
||||
label_img = compose_label([code_img, dm_img])
|
||||
send_to_printer(label_img, req.printer_ip, req.model, req.label)
|
||||
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue