96 lines
3.0 KiB
Python
96 lines
3.0 KiB
Python
from fastapi import FastAPI, Response
|
|
from escpos.printer import Dummy, Network
|
|
import sys
|
|
import json
|
|
|
|
from pydantic import BaseModel
|
|
from time import sleep
|
|
from datetime import datetime
|
|
|
|
app = FastAPI(
|
|
title="Print Server FastAPI",
|
|
description="Server that receive request for printing",
|
|
version="0.0.1"
|
|
)
|
|
|
|
class Info(BaseModel):
|
|
content : str
|
|
ip_printer : str
|
|
|
|
|
|
def imprimir_ticket_de_prueba(data, address):
|
|
d = data
|
|
# Crea una instancia de la impresora ficticia
|
|
#printer = Network(str(address))
|
|
#printer.open()
|
|
printer = Dummy()
|
|
|
|
# Imprime el encabezado
|
|
printer.set(align='center', bold=False, height=1, width=1)
|
|
printer.text("Pedido Por: " + str(d['user'])+ '\n')
|
|
format_date_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
|
printer.text(str(format_date_time)+'\n')
|
|
printer.set(align='center', bold=False, height=2, width=2, custom_size=True)
|
|
printer.text('========================\n')
|
|
text = 'MESA: ' + str(d['table'] + "\n")
|
|
printer.text(text)
|
|
printer.text('========================\n')
|
|
|
|
printer.set(align='left', bold=False, height=6, width=6)
|
|
pizza = None
|
|
for line in d["lines"]:
|
|
if line['type'] != 'title':
|
|
if combination_pizza and pizza < 2:
|
|
printer.set(align='center', bold=False, height=2, width=2, custom_size=True)
|
|
pizza += 1
|
|
elif pizza >=2:
|
|
combination_pizza = False
|
|
printer.set(align='left', bold=False, height=2, width=2, custom_size=True)
|
|
else:
|
|
printer.set(align='left', bold=False, height=2, width=2, custom_size=True)
|
|
|
|
|
|
text = line['product'] +" "+str(line['quantity'])+"\n"
|
|
printer.text(text)
|
|
if pizza == 2:
|
|
printer.text('\n')
|
|
pizza = 0
|
|
combination_pizza = False
|
|
else:
|
|
printer.set(align='left', bold=True, height=2, width=2, custom_size=True)
|
|
printer.text("\nPIZZA COMBINADA\n")
|
|
combination_pizza = True
|
|
pizza = 0
|
|
# Corta el papel (solo para impresoras que soportan esta función)
|
|
printer.cut()
|
|
printer.close()
|
|
# Obtiene el contenido del ticket de prueba
|
|
ticket_contenido = printer.output
|
|
|
|
# Imprime el contenido en la consola
|
|
sys.stdout.write(ticket_contenido.decode('utf-8'))
|
|
|
|
@app.post("/order_kitchen")
|
|
def print_ticket_file_kitchen(info : Info):
|
|
info = dict(info)
|
|
data = info["content"]
|
|
address = info["ip_printer"]
|
|
data = json.loads(data.replace("'", "\""))
|
|
imprimir_ticket_de_prueba(data, address)
|
|
|
|
message = "!Impresión Realizada!"
|
|
|
|
return Response(content=message, status_code=200)
|
|
|
|
@app.post("/order_bar")
|
|
def print_ticket_file_bar(info : Info):
|
|
info = dict(info)
|
|
data = info["content"]
|
|
data = json.loads(data.replace("'", "\""))
|
|
address = info["ip_printer"]
|
|
imprimir_ticket_de_prueba(data, address)
|
|
|
|
message = "!Impresión Realizada!"
|
|
|
|
return Response(content=message, status_code=200)
|