Restructure main class to activate worker and use tasks, print queue,

update Printer
This commit is contained in:
n07070
2026-05-21 02:34:12 +02:00
parent e8ec9b74c0
commit c50922790d
2 changed files with 94 additions and 39 deletions

View File

@@ -23,6 +23,7 @@ import sys
import os # For VARS from the shell.
import pprint # To pretty print JSON
import toml # Used for the config file parsing
import threading
from flask import (
Flask,
request,
@@ -39,11 +40,15 @@ from flask_limiter.util import get_remote_address
from printer import Printer # The wrapper for the printer class
from raspberry import Raspberry # The Raspberry pi control Class
from web import Web # Wrapper for the web routes and API
from print_queue import PrintQueue
from worker import PrintWorker
# Variables
# We create the main Flask object
app = Flask(__name__)
socketio = SocketIO(app)
socketio = SocketIO(app, cors_allowed_origins="*")
# Global variables
ALLOWED_EXTENSIONS = {"png", "jpg", "jpeg", "gif", "webp"}
# Load the configuration file
@@ -116,18 +121,21 @@ rpi = Raspberry(
RASPBERRY_PI_CONNECTED = rpi.is_raspberry_pi()
# Queue creation
print_queue = PrintQueue(app)
# Web & API routes
web = Web(app, print_queue)
web = Web(app, printer)
if __name__ == "__main__":
app.run(debug=True, use_reloader=False, host="0.0.0.0", ssl_context="adhoc")
# Start worker thread
worker = PrintWorker(app, print_queue, printer, socketio)
worker.start()
limiter = Limiter(
get_remote_address, app=app, default_limits=["1500 per day", "500 per hour"]
)
# General routes
@app.route("/")
@limiter.limit("1/second", override_defaults=False)
@@ -144,6 +152,8 @@ def webcam():
app.logger.debug("Loading webcam interface")
return render_template("webcam.html")
# Form treatement
@app.route("/web/print/sms", methods=["POST"])
@limiter.limit("6/minute", override_defaults=False)
def web_print_sms():
@@ -299,7 +309,7 @@ def api_print_image():
@app.route("/api/camera/picture", methods=["GET"])
def camera_picture():
"""Returns a picture taken by the camera"""
"""Returns a picture taken by the camera on a raspberry pi"""
if RASPBERRY_PI_CONNECTED:
try:
return rpi.camera_picture()
@@ -308,6 +318,26 @@ def camera_picture():
else:
return jsonify({"message": "No camera present"}), 500
@app.route('/api/queue', methods=["GET"])
def api_queue_status():
"""API endpoint for entire queue"""
return jsonify(web.get_queue_state())
@app.route('/api/worker', methods=["GET"])
def api_worker_state():
"""API endpoint to get the worker state"""
return jsonify(worker.current_state())
@app.route('/api/worker/start')
def api_worker_start():
worker.start_worker()
return jsonify(worker.current_state())
@app.route('/api/worker/stop')
def api_worker_stop():
worker.stop_worker()
return jsonify(worker.current_state())
## Authentification
@@ -362,3 +392,6 @@ def camera_status():
socketio.emit("camera_status", True)
else:
socketio.emit("camera_status", False)
if __name__ == "__main__":
app.run(debug=True, use_reloader=False, host="0.0.0.0", ssl_context="adhoc")