Update the way a printer is found, update the configuration of Brother
Printers
This commit is contained in:
@@ -7,6 +7,7 @@ the methods expected from printers.
|
||||
|
||||
from collections.abc import Set
|
||||
import time
|
||||
import threading
|
||||
import usb.core
|
||||
import usb.util
|
||||
from printer import Printer, EscPosPrinter, BrotherPrinter
|
||||
@@ -19,10 +20,11 @@ class Printers:
|
||||
|
||||
def __init__(self, app):
|
||||
"""
|
||||
Discover printers connected to the computer and return a Collection of Printer()
|
||||
Discovers printers connected to the computer
|
||||
"""
|
||||
self.app = app
|
||||
self.printers = self._discover_printers()
|
||||
self.printers = list(self._discover_printers())
|
||||
self._lock = threading.Lock()
|
||||
|
||||
def _discover_printers(self) -> Set[Printer]:
|
||||
"""
|
||||
@@ -40,25 +42,36 @@ class Printers:
|
||||
|
||||
printers = set()
|
||||
|
||||
# Find all connected USB devices
|
||||
devices = usb.core.find(find_all=True, custom_match=_FindClass(7))
|
||||
waiting_time_for_printer = 10
|
||||
while not list(devices):
|
||||
self.app.logger.warning(str(waiting_time_for_printer) + " Found no printers, waiting for a printer to connect to the system...")
|
||||
time.sleep(1)
|
||||
waiting_time_for_printer = waiting_time_for_printer - 1
|
||||
devices = usb.core.find(find_all=True, custom_match=_FindClass(7))
|
||||
if waiting_time_for_printer == 0:
|
||||
# TODO : this could go in the config ?
|
||||
waiting_time_for_printer = 5
|
||||
|
||||
while True:
|
||||
# Find all connected USB devices
|
||||
# the find() fnc returns a generator
|
||||
devices = list(
|
||||
usb.core.find(
|
||||
find_all=True,
|
||||
custom_match=_FindClass(self.app,7)
|
||||
)
|
||||
)
|
||||
|
||||
if devices:
|
||||
break
|
||||
|
||||
if waiting_time_for_printer <= 0:
|
||||
self.app.logger.warning(
|
||||
"No USB devices of class 7 ( printers ) found or pyusb could not access the bus."
|
||||
)
|
||||
raise RuntimeError(
|
||||
"No USB devices of class 7 ( printers ) found or pyusb could not access the bus."
|
||||
)
|
||||
|
||||
if not list(devices):
|
||||
self.app.logger.warning(
|
||||
"No USB devices of class 7 ( printers ) found or pyusb could not access the bus."
|
||||
)
|
||||
raise RuntimeError(
|
||||
"No USB devices of class 7 ( printers ) found or pyusb could not access the bus."
|
||||
)
|
||||
"No printers found; waiting for a printer to connect..."
|
||||
)
|
||||
|
||||
time.sleep(1)
|
||||
waiting_time_for_printer -= 1
|
||||
|
||||
for dev in devices:
|
||||
# Attempt to get the manufacturer and product strings
|
||||
@@ -107,8 +120,7 @@ class Printers:
|
||||
self.app.logger.debug("Trying to create a new BROTHER printer")
|
||||
|
||||
brother_printer = BrotherPrinter(
|
||||
self.app, vendor_id=dev.idVendor, device_id=dev.idProduct
|
||||
)
|
||||
self.app, vendor_id=dev.idVendor, device_id=dev.idProduct)
|
||||
except Exception as e:
|
||||
self.app.logger.error(
|
||||
"Could not create a %s printer class with %s:%s" , product,
|
||||
@@ -129,43 +141,45 @@ class Printers:
|
||||
|
||||
return printers
|
||||
|
||||
def any(self) -> Printer:
|
||||
"""
|
||||
Return a dict key: UUID, value: Printer, with any connected printer.
|
||||
"""
|
||||
if len(self.printers) > 0:
|
||||
for i in self.printers:
|
||||
return i
|
||||
else:
|
||||
raise RuntimeError("No printers available")
|
||||
def acquire_available_printer(self, start_index=0):
|
||||
"""Atomically acquire and return an available printer, or None."""
|
||||
printers = self.printers
|
||||
if not printers:
|
||||
self.app.logger.error("No printers available for acquisition")
|
||||
return None
|
||||
|
||||
# def get_printer(self, printer_type):
|
||||
# """
|
||||
# Return a specific printer
|
||||
with self._lock:
|
||||
for offset in range(len(printers)):
|
||||
index = (start_index + offset) % len(printers)
|
||||
printer = printers[index]
|
||||
|
||||
# printer_type -- a printer type
|
||||
# """
|
||||
# return NotImplementedError()
|
||||
if not printer.used:
|
||||
printer.use_printer()
|
||||
return index, printer
|
||||
|
||||
return None
|
||||
|
||||
class _FindClass:
|
||||
"""
|
||||
Use by usb.core to modify the way USB devices are found
|
||||
Taken from pyUSB documentation on Github
|
||||
"""
|
||||
def __init__(self, class_):
|
||||
def __init__(self, app, class_):
|
||||
self._class = class_
|
||||
self.app = app
|
||||
|
||||
def __call__(self, device):
|
||||
# first, let's check the device
|
||||
self.app.logger.debug("Found a USB device of class %s", str(device.bDeviceClass))
|
||||
if device.bDeviceClass == self._class:
|
||||
return True
|
||||
# ok, transverse all devices to find an
|
||||
# interface that matches our class
|
||||
for cfg in device:
|
||||
# find_descriptor: what's it?
|
||||
intf = usb.util.find_descriptor(cfg, bInterfaceClass=self._class)
|
||||
if intf is not None:
|
||||
for configuration in device:
|
||||
# Every device can have one or more configuration,
|
||||
interface = usb.util.find_descriptor(configuration, bInterfaceClass=self._class)
|
||||
# each config can have several interfaces
|
||||
if interface is not None:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user