Compare commits
23 Commits
1e46de4b3c
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 0e3cc46a41 | |||
|
|
bbfe1936da | ||
|
|
8134c5e892 | ||
|
|
934f766cf3 | ||
|
|
eb9e1ec200 | ||
|
|
bc035508cd | ||
|
|
cba34744f6 | ||
|
|
0c8c40098c | ||
|
|
3b640dc549 | ||
|
|
2daafe28f2 | ||
|
|
c50922790d | ||
|
|
e8ec9b74c0 | ||
|
|
9dee67c333 | ||
|
|
42bf6d6496 | ||
|
|
a38088bd05 | ||
|
|
cb3e0d900f | ||
|
|
c5a8019fbe | ||
|
|
e926ee9163 | ||
|
|
3f915a1b25 | ||
|
|
a06086521a | ||
|
|
ee27c62d0f | ||
|
|
2a11239c1e | ||
|
|
bd9888caf7 |
@@ -7,7 +7,7 @@ authors = [
|
|||||||
]
|
]
|
||||||
license = "AGPLv3"
|
license = "AGPLv3"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.14"
|
requires-python = ">=3.13"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"flask (>=3.1.3,<4.0.0)",
|
"flask (>=3.1.3,<4.0.0)",
|
||||||
"numpy (>=2.3.4)",
|
"numpy (>=2.3.4)",
|
||||||
|
|||||||
118
src/printer.py
118
src/printer.py
@@ -1,4 +1,6 @@
|
|||||||
|
"""
|
||||||
|
This class manages connexion to a Printer
|
||||||
|
"""
|
||||||
# import brother_ql
|
# import brother_ql
|
||||||
from time import sleep
|
from time import sleep
|
||||||
import os.path
|
import os.path
|
||||||
@@ -10,7 +12,7 @@ import numpy as np
|
|||||||
import escpos.printer
|
import escpos.printer
|
||||||
|
|
||||||
|
|
||||||
class Printer(object):
|
class Printer():
|
||||||
"""
|
"""
|
||||||
# The connection is based on the ESC/POS library
|
# The connection is based on the ESC/POS library
|
||||||
|
|
||||||
@@ -31,7 +33,7 @@ class Printer(object):
|
|||||||
ready = False
|
ready = False
|
||||||
|
|
||||||
def __init__(self, app, device_id, vendor_id):
|
def __init__(self, app, device_id, vendor_id):
|
||||||
super(Printer, self).__init__()
|
super().__init__()
|
||||||
self.app = app
|
self.app = app
|
||||||
self.ready = False
|
self.ready = False
|
||||||
self.printer = None
|
self.printer = None
|
||||||
@@ -69,7 +71,7 @@ class Printer(object):
|
|||||||
|
|
||||||
# TODO: This could happen directly when creating a new Printer class
|
# TODO: This could happen directly when creating a new Printer class
|
||||||
if os.getenv("FLASK_DEBUG"):
|
if os.getenv("FLASK_DEBUG"):
|
||||||
waiting_elapsed = 15
|
waiting_elapsed = 3
|
||||||
else:
|
else:
|
||||||
waiting_elapsed = 10
|
waiting_elapsed = 10
|
||||||
|
|
||||||
@@ -82,25 +84,23 @@ class Printer(object):
|
|||||||
p = escpos.printer.Usb(
|
p = escpos.printer.Usb(
|
||||||
self.device_id, self.vendor_id, 0, profile="TM-P80"
|
self.device_id, self.vendor_id, 0, profile="TM-P80"
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except RuntimeError as e:
|
||||||
self.app.logger.error(
|
self.app.logger.error(
|
||||||
"The USB device is not plugged in, trying again %s : %s",
|
"The USB device is not plugged in, trying again %s : %s",
|
||||||
waiting_elapsed,
|
waiting_elapsed,
|
||||||
str(e),
|
str(e),
|
||||||
)
|
)
|
||||||
pass
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if p.is_online():
|
if p.is_online():
|
||||||
self.ready = True
|
self.ready = True
|
||||||
self.app.logger.debug("Printer online !")
|
self.app.logger.debug("Printer online !")
|
||||||
except Exception as e:
|
except RuntimeError as e:
|
||||||
self.app.logger.error(
|
self.app.logger.error(
|
||||||
"Error while getting the printer online %s : %s",
|
"Error while getting the printer online %s : %s",
|
||||||
waiting_elapsed,
|
waiting_elapsed,
|
||||||
str(e),
|
str(e),
|
||||||
)
|
)
|
||||||
pass
|
|
||||||
|
|
||||||
sleep(1)
|
sleep(1)
|
||||||
waiting_elapsed -= 1
|
waiting_elapsed -= 1
|
||||||
@@ -154,7 +154,7 @@ class Printer(object):
|
|||||||
self.app.logger.warning(
|
self.app.logger.warning(
|
||||||
"Could not print message of this length: " + str(len(clean_msg))
|
"Could not print message of this length: " + str(len(clean_msg))
|
||||||
)
|
)
|
||||||
raise Exception(
|
raise RuntimeError(
|
||||||
"Could not print message of this length :"
|
"Could not print message of this length :"
|
||||||
+ str(len(clean_msg))
|
+ str(len(clean_msg))
|
||||||
+ ", needs to be below 4096 caracters long."
|
+ ", needs to be below 4096 caracters long."
|
||||||
@@ -164,7 +164,7 @@ class Printer(object):
|
|||||||
self.app.logger.warning(
|
self.app.logger.warning(
|
||||||
"Could not print signature of this length: " + str(len(clean_signature))
|
"Could not print signature of this length: " + str(len(clean_signature))
|
||||||
)
|
)
|
||||||
raise Exception(
|
raise RuntimeError(
|
||||||
"Could not print signature of this length :"
|
"Could not print signature of this length :"
|
||||||
+ str(len(clean_signature))
|
+ str(len(clean_signature))
|
||||||
+ ", needs to be below 256 caracters long."
|
+ ", needs to be below 256 caracters long."
|
||||||
@@ -208,7 +208,7 @@ class Printer(object):
|
|||||||
+ str(path)
|
+ str(path)
|
||||||
+ " wasn't found. Please try again."
|
+ " wasn't found. Please try again."
|
||||||
)
|
)
|
||||||
else:
|
|
||||||
self.app.logger.debug("Printing file from " + str(path))
|
self.app.logger.debug("Printing file from " + str(path))
|
||||||
|
|
||||||
if process:
|
if process:
|
||||||
@@ -256,7 +256,7 @@ class Printer(object):
|
|||||||
self.printer.open(self.usb_args)
|
self.printer.open(self.usb_args)
|
||||||
self.printer.qr(content, center=True)
|
self.printer.qr(content, center=True)
|
||||||
self.printer.close()
|
self.printer.close()
|
||||||
except Exception as e:
|
except RuntimeError as e:
|
||||||
self.printer.close()
|
self.printer.close()
|
||||||
self.app.logger.error(str(e))
|
self.app.logger.error(str(e))
|
||||||
return False
|
return False
|
||||||
@@ -310,7 +310,8 @@ def _process_image(self, path):
|
|||||||
self.app.logger.debug("Resized the image")
|
self.app.logger.debug("Resized the image")
|
||||||
|
|
||||||
# # Convert to grayscale for dithering
|
# # Convert to grayscale for dithering
|
||||||
# dithered_img = original_img.convert("L").convert("1") # Dithering using default method (Floyd–Steinberg)
|
# dithered_img = original_img.convert("L").convert("1")
|
||||||
|
# Dithering using default method (Floyd–Steinberg)
|
||||||
# self.app.logger.debug("Dithered the image")
|
# self.app.logger.debug("Dithered the image")
|
||||||
|
|
||||||
# Compute brightness of original image (grayscale average)
|
# Compute brightness of original image (grayscale average)
|
||||||
@@ -344,94 +345,3 @@ def _process_image(self, path):
|
|||||||
self.app.logger.debug("Processed and saved image.")
|
self.app.logger.debug("Processed and saved image.")
|
||||||
|
|
||||||
return jpeg_path
|
return jpeg_path
|
||||||
|
|
||||||
|
|
||||||
def discover_printers():
|
|
||||||
"""
|
|
||||||
We try to find all the connected printers ( 0 or n ) to this system.
|
|
||||||
|
|
||||||
For every type of supported printer, we try to autodiscover them.
|
|
||||||
|
|
||||||
http://www.linux-usb.org/usb.ids A list of USB vendor IDs
|
|
||||||
|
|
||||||
04b8 Seiko Epson Corp.
|
|
||||||
04f9 Brother Industries, Ltd
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
def find_and_parse_borther_ql_printer():
|
|
||||||
|
|
||||||
## We might be able to no use this because there is a `discover` command in https://github.com/pklaus/brother_ql#usage
|
|
||||||
|
|
||||||
## Code stolen from https://framagit.org/stickoeur/diagnostickoeur/-/blob/no-masters/printit.py?ref_type=heads
|
|
||||||
|
|
||||||
"""Find and parse Brother QL printer information."""
|
|
||||||
|
|
||||||
model_manager = ModelsManager()
|
|
||||||
|
|
||||||
# Debug print to show we're searching
|
|
||||||
# print("Searching for Brother QL printer...")
|
|
||||||
|
|
||||||
for backend_name in ["pyusb", "linux_kernel"]:
|
|
||||||
try:
|
|
||||||
# print(f"Trying backend: {backend_name}")
|
|
||||||
backend = backend_factory(backend_name)
|
|
||||||
available_devices = backend["list_available_devices"]()
|
|
||||||
# print(f"Found {len(available_devices)} devices with {backend_name} backend")
|
|
||||||
|
|
||||||
for printer in available_devices:
|
|
||||||
# print(f"Found device: {printer}")
|
|
||||||
identifier = printer["identifier"]
|
|
||||||
parts = identifier.split("/")
|
|
||||||
|
|
||||||
if len(parts) < 4:
|
|
||||||
# print(f"Skipping device with invalid identifier format: {identifier}")
|
|
||||||
continue
|
|
||||||
|
|
||||||
protocol = parts[0]
|
|
||||||
device_info = parts[2]
|
|
||||||
serial_number = parts[3]
|
|
||||||
|
|
||||||
try:
|
|
||||||
vendor_id, product_id = device_info.split(":")
|
|
||||||
except ValueError:
|
|
||||||
# print(f"Invalid device info format: {device_info}")
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Default model
|
|
||||||
model = "QL-570"
|
|
||||||
|
|
||||||
# Try to match product ID to determine actual model
|
|
||||||
try:
|
|
||||||
product_id_int = int(product_id, 16)
|
|
||||||
for m in model_manager.iter_elements():
|
|
||||||
if m.product_id == product_id_int:
|
|
||||||
model = m.identifier
|
|
||||||
break
|
|
||||||
# print(f"Matched printer model: {model}")
|
|
||||||
except ValueError:
|
|
||||||
# print(f"Invalid product ID format: {product_id}")
|
|
||||||
continue
|
|
||||||
|
|
||||||
printer_info = {
|
|
||||||
"identifier": identifier,
|
|
||||||
"backend": backend_name,
|
|
||||||
"model": model,
|
|
||||||
"protocol": protocol,
|
|
||||||
"vendor_id": vendor_id,
|
|
||||||
"product_id": product_id,
|
|
||||||
"serial_number": serial_number,
|
|
||||||
}
|
|
||||||
# print(f"Found printer: {printer_info}")
|
|
||||||
return printer_info
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
# print(f"Error with backend {backend_name}: {str(e)}")
|
|
||||||
continue
|
|
||||||
|
|
||||||
print("No Brother QL printer found")
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def fint_and_parse_epson_printer():
|
|
||||||
pass
|
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ from abc import ABC, abstractmethod
|
|||||||
|
|
||||||
## See https://docs.python.org/3/library/abc.html to learn more about this
|
## See https://docs.python.org/3/library/abc.html to learn more about this
|
||||||
|
|
||||||
# from dataclasses import dataclass
|
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user