81 lines
2.7 KiB
Python
81 lines
2.7 KiB
Python
|
|
import os
|
||
|
|
|
||
|
|
from labeler.app.labeler import Application
|
||
|
|
from labeler.infra.e550w_printer.printer import E550W
|
||
|
|
from labeler.infra.renderer import PILRenderer
|
||
|
|
|
||
|
|
from fastapi import FastAPI
|
||
|
|
|
||
|
|
app = FastAPI()
|
||
|
|
|
||
|
|
|
||
|
|
class LabelingBot:
|
||
|
|
def __init__(self, app: Application):
|
||
|
|
self.app = app
|
||
|
|
|
||
|
|
def media_info(self):
|
||
|
|
media = self.app.get_installed_media()
|
||
|
|
print(f"Installed medium: {media}")
|
||
|
|
|
||
|
|
def simple_label(self, label_text, label_length=0):
|
||
|
|
try:
|
||
|
|
label = self.app.print_label(text=label_text, length=label_length)
|
||
|
|
except Exception as e:
|
||
|
|
print(f"There was an exception: {e}")
|
||
|
|
|
||
|
|
def get_qrcode(self, label_text, label_length=0):
|
||
|
|
label = self.app.render_qrcode_preview(text=label_text, length=label_length)
|
||
|
|
return label
|
||
|
|
|
||
|
|
def print_qrcode(self, label_text, label_length=0):
|
||
|
|
try:
|
||
|
|
label = self.app.print_qrcode(text=label_text, length=label_length)
|
||
|
|
except Exception as e:
|
||
|
|
print(f"There was an exception: {e}")
|
||
|
|
|
||
|
|
# async def label_length(self):
|
||
|
|
# await update.message.reply_text(
|
||
|
|
# "Hello! Please tell me the length of the label, enter 0 for auto:"
|
||
|
|
# )
|
||
|
|
# return LABEL_LENGTH
|
||
|
|
#
|
||
|
|
# async def label_text(self, update: Update, context: CallbackContext) -> int:
|
||
|
|
# user_input = update.message.text
|
||
|
|
# context.user_data["length"] = int(user_input)
|
||
|
|
# await update.message.reply_text("Now, please tell me the text of the label:")
|
||
|
|
# return LABEL_TEXT
|
||
|
|
#
|
||
|
|
# async def simple_label(self, update: Update, context: CallbackContext) -> int:
|
||
|
|
# user_input = update.message.text
|
||
|
|
# context.user_data["label"] = user_input
|
||
|
|
# try:
|
||
|
|
# label = self.app.print_label(
|
||
|
|
# text=context.user_data["label"], length=context.user_data["length"]
|
||
|
|
# )
|
||
|
|
# except Exception as e:
|
||
|
|
# await update.message.reply_text(f"There was an exception: {e}")
|
||
|
|
# return ConversationHandler.END
|
||
|
|
#
|
||
|
|
# await update.message.reply_photo(
|
||
|
|
# label.bytes, f'Your label is: {context.user_data["label"]}'
|
||
|
|
# )
|
||
|
|
# return ConversationHandler.END
|
||
|
|
#
|
||
|
|
# async def cancel(self, update: Update, context: CallbackContext) -> int:
|
||
|
|
# await update.message.reply_text("Cancelled.")
|
||
|
|
# return ConversationHandler.END
|
||
|
|
|
||
|
|
|
||
|
|
@app.get("/print/{item_id}")
|
||
|
|
def print_item(item_id: int, q: str | None = None):
|
||
|
|
application = Application(PILRenderer(), E550W(os.environ.get("PRINTER_IP")))
|
||
|
|
bot = LabelingBot(application)
|
||
|
|
|
||
|
|
LABEL_LENGTH, LABEL_TEXT = range(2)
|
||
|
|
|
||
|
|
bot.media_info()
|
||
|
|
label = bot.get_qrcode(item_id, 25)
|
||
|
|
with open("label.png", "wb") as preview:
|
||
|
|
preview.write(label.bytes)
|
||
|
|
bot.print_qrcode(item_id, 25)
|