Restructure web class to use print queue and tasks
This commit is contained in:
86
src/web.py
86
src/web.py
@@ -1,16 +1,16 @@
|
|||||||
from flask import Flask, request, flash
|
from flask import flash
|
||||||
from werkzeug.utils import secure_filename
|
from werkzeug.utils import secure_filename
|
||||||
from printer import Printer
|
|
||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
|
from task import TextTask, ImageTask, CutTask
|
||||||
|
|
||||||
|
|
||||||
class Web(object):
|
class Web(object):
|
||||||
"""Web is the class that gets all of the information from web calls ( API and Web page ) and provides checks before sending stuff to printing"""
|
"""Web is the class that gets all of the information from web calls ( API and Web page ) and provides checks before sending stuff to printing"""
|
||||||
|
|
||||||
def __init__(self, app, printer):
|
def __init__(self, app, print_queue):
|
||||||
super(Web).__init__()
|
super(Web).__init__()
|
||||||
self.printer = printer
|
self.print_queue = print_queue
|
||||||
self.app = app
|
self.app = app
|
||||||
|
|
||||||
def print_sms(self, texte, sign: str) -> bool:
|
def print_sms(self, texte, sign: str) -> bool:
|
||||||
@@ -18,13 +18,19 @@ class Web(object):
|
|||||||
Get text and a signature, prints the text and cuts after that.
|
Get text and a signature, prints the text and cuts after that.
|
||||||
"""
|
"""
|
||||||
self.app.logger.debug("Printing : " + str(texte) + " from " + str(sign))
|
self.app.logger.debug("Printing : " + str(texte) + " from " + str(sign))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.printer.print_sms(texte, sign)
|
# We create two new tasks and add them directly to the queue
|
||||||
self.printer.cut()
|
# TODO: this might need to be improved because
|
||||||
|
# !! there is no garantee !! that both the SMS task and the Cut task
|
||||||
|
# are added back to back, another task could be
|
||||||
|
# inserted between the two.
|
||||||
|
sms = self.print_queue.enqueue(TextTask(content=texte, signature=sign))
|
||||||
|
cut = self.print_queue.enqueue(CutTask())
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.app.logger.error(e)
|
self.app.logger.error(e)
|
||||||
raise RuntimeError("Could not print SMS, " + str(e)) from e
|
raise RuntimeError("Could not add SMS to queue, " + str(e)) from e
|
||||||
|
self.app.logger.info("Added two new tasks at position %s and %s", sms, cut )
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def print_image(self, image, sign: str) -> bool:
|
def print_image(self, image, sign: str) -> bool:
|
||||||
@@ -32,28 +38,28 @@ class Web(object):
|
|||||||
Get an image and a signature, prints the image and cuts after that.
|
Get an image and a signature, prints the image and cuts after that.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
self.upload_file(image)
|
file_uploaded = self.upload_file(image)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.app.logger.error(e)
|
self.app.logger.error(e)
|
||||||
raise RuntimeError("Could not upload file") from e
|
raise RuntimeError("Could not upload file") from e
|
||||||
|
|
||||||
self.app.logger.debug("File has been uploaded, printing...")
|
if file_uploaded:
|
||||||
|
self.app.logger.debug("File has been uploaded, printing...")
|
||||||
|
try:
|
||||||
try:
|
img = self.print_queue.enqueue(ImageTask(os.path.join(
|
||||||
self.printer.print_img(
|
|
||||||
os.path.join(
|
|
||||||
self.app.config["UPLOAD_FOLDER"],
|
self.app.config["UPLOAD_FOLDER"],
|
||||||
secure_filename(image.filename),
|
secure_filename(image.filename),
|
||||||
),
|
),
|
||||||
sign=sign,
|
signature=sign,
|
||||||
process=True,
|
process=True))
|
||||||
)
|
|
||||||
self.printer.cut()
|
cut = self.print_queue.enqueue(CutTask())
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise RuntimeError("Could not print file") from e
|
raise RuntimeError("Could not add IMG to queue" + str(e)) from e
|
||||||
|
|
||||||
|
self.app.logger.info("Added two new tasks at position %s and %s", img, cut )
|
||||||
|
|
||||||
|
|
||||||
self.app.logger.debug("Image printed and cut !")
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def login(self, username: str, password: str) -> bool:
|
def login(self, username: str, password: str) -> bool:
|
||||||
@@ -74,20 +80,28 @@ class Web(object):
|
|||||||
|
|
||||||
def upload_file(self, image) -> bool:
|
def upload_file(self, image) -> bool:
|
||||||
self.app.logger.debug("Validating file")
|
self.app.logger.debug("Validating file")
|
||||||
if image and self.allowed_file(image.filename):
|
if image:
|
||||||
filename = secure_filename(image.filename)
|
if self.allowed_file(image.filename):
|
||||||
self.app.logger.debug("File valid")
|
filename = secure_filename(image.filename)
|
||||||
try:
|
self.app.logger.debug("File valid")
|
||||||
image.save(os.path.join(self.app.config["UPLOAD_FOLDER"], filename))
|
try:
|
||||||
except Exception as e:
|
image.save(os.path.join(self.app.config["UPLOAD_FOLDER"], filename))
|
||||||
self.app.logger.error("Could not save file %s", e)
|
except OSError as e:
|
||||||
return False
|
self.app.logger.error("Could not save file %s", e)
|
||||||
|
return False
|
||||||
|
|
||||||
self.app.logger.debug(
|
self.app.logger.debug(
|
||||||
"File saved to "
|
"File saved to "
|
||||||
+ str(os.path.join(self.app.config["UPLOAD_FOLDER"], filename))
|
+ str(os.path.join(self.app.config["UPLOAD_FOLDER"], filename))
|
||||||
)
|
)
|
||||||
return True
|
return True
|
||||||
|
else:
|
||||||
|
self.app.logger.error("Could not save file because the filename is forbidden")
|
||||||
|
return False
|
||||||
else:
|
else:
|
||||||
self.app.logger.error("Could not save file " + str(filename))
|
self.app.logger.error("Could not save file, it seems to be null ? : " + str(filename))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def get_queue_state(self):
|
||||||
|
"""Return current queue state"""
|
||||||
|
return self.print_queue.get_queue_state()
|
||||||
|
|||||||
Reference in New Issue
Block a user