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