Restructure the code and implement a printing queue #29

Merged
n07070 merged 21 commits from restructure-printing-queue into master 2026-05-27 00:00:56 +02:00
Showing only changes of commit cba34744f6 - Show all commits

View File

@@ -1,3 +1,11 @@
"""
This class executes when we are on a raspberry Pi.
It handles the press of a button via GPIO,
activates a flash and prints out the picture
that was taken via a USB Webcam.
"""
import io # To check if we are on a Raspberry Pi
import subprocess
import os
@@ -5,6 +13,7 @@ from time import sleep, gmtime, strftime
from flask_socketio import SocketIO
from gpiozero import Button, LED, DigitalOutputDevice
from PIL import Image
from task import TextTask, ImageTask, CutTask
class Raspberry():
"""
@@ -18,7 +27,7 @@ class Raspberry():
def __init__(
self,
printer,
print_queue,
app,
socketio,
button_gpio_port_number,
@@ -26,7 +35,7 @@ class Raspberry():
flash_gpio_port_number,
is_flash_present,
):
self.printer = printer
self.print_queue = print_queue
self.socketio = socketio
self.app = app
@@ -37,9 +46,13 @@ class Raspberry():
self.image_path = self.app.config["UPLOAD_FOLDER"] + "/image.jpg"
def is_raspberry_pi(self, raise_on_errors=False):
"""
Checking if we are on a Raspberry Pi by checking
information on the /proc/cpuinfo file
"""
# Check if we are running on a raspberry pi
try:
with io.open("/proc/cpuinfo", "r") as cpuinfo:
with io.open("/proc/cpuinfo", "r", encoding="utf-8") as cpuinfo:
found = False
for line in cpuinfo:
if line.startswith("Hardware"):
@@ -140,7 +153,7 @@ class Raspberry():
f"Unable to take a picture. Error: {e.stderr.decode()}"
)
return False
except Exception as e:
except RuntimeError as e:
# Catch any unexpected errors
self.app.logger.error(f"Unexpected error while taking picture: {str(e)}")
return False
@@ -197,7 +210,7 @@ class Raspberry():
output_path = image_path # Overwrite the original image if no output path is given
image.save(output_path)
except Exception as e:
except RuntimeError as e:
self.app.logger.error(f"Error overlaying logo: {e}")
return False
@@ -224,7 +237,7 @@ class Raspberry():
image.save(output_path)
except Exception as e:
except RuntimeError as e:
self.app.logger.error(f"Error cropping image to square: {e}")
return False
@@ -241,7 +254,7 @@ class Raspberry():
try:
self.flash.on()
self.take_picture()
except Exception as e:
except RuntimeError as e:
self.app.logger.error(
"Could not take a picture after the button press : " + str(e)
)
@@ -250,15 +263,11 @@ class Raspberry():
self.app.logger.debug("Printing picture")
self.led.on()
self.crop_to_square(self.image_path)
self.printer.print_img("src/static/images/extase-club.png", process=True)
self.printer.print_img(self.image_path, process=True)
self.printer.print_sms("")
self.printer.print_sms("With Love From Société.Vide", signature="", bold=True)
self.printer.print_sms("Printed by LittlePrynter", signature="")
self.printer.print_sms("n07070.xyz", signature="")
self.printer.print_sms(strftime("%Y-%m-%d %H:%M", gmtime()), signature="")
self.printer.qr("https://n07070.xyz/articles/littleprynter")
self.printer.cut()
self.print_queue.enqueue(ImageTask(self.image_path,signature="",process=True))
self.print_queue.enqueue(TextTask(content="Imprimé par LittlePrynter", signature=""))
time = strftime("%Y-%m-%d %H:%M", gmtime())
self.print_queue.enqueue(TextTask(content=time, signature=""))
self.print_queue.enqueue(CutTask())
self.led.off()
self.app.logger.debug("Done printing picture")
self.app.logger.debug("Added a photomaton picture to the print queue")
return True