| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
|
| 3 |
import logging |
| 4 |
from threading import Lock |
| 5 |
import win32print |
| 6 |
|
| 7 |
from odoo.addons.iot_drivers.interface import Interface |
| 8 |
|
| 9 |
_logger = logging.getLogger(__name__) |
| 10 |
|
| 11 |
win32print_lock = Lock() # Calling win32print in parallel can cause failed prints |
| 12 |
|
| 13 |
|
| 14 |
class PrinterInterface(Interface): |
| 15 |
_loop_delay = 30 |
| 16 |
connection_type = 'printer' |
| 17 |
|
| 18 |
def get_devices(self): |
| 19 |
printer_devices = {} |
| 20 |
with win32print_lock: |
| 21 |
printers = win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL) |
| 22 |
|
| 23 |
for printer in printers: |
| 24 |
identifier = printer[2] |
| 25 |
handle_printer = win32print.OpenPrinter(identifier) |
| 26 |
# The value "2" is the level of detail we want to get from the printer, see: |
| 27 |
# https://learn.microsoft.com/en-us/windows/win32/printdocs/getprinter#parameters |
| 28 |
printer_details = win32print.GetPrinter(handle_printer, 2) |
| 29 |
printer_port = None |
| 30 |
if printer_details: |
| 31 |
# see: https://learn.microsoft.com/en-us/windows/win32/printdocs/printer-info-2#members |
| 32 |
printer_port = printer_details.get('pPortName') |
| 33 |
if printer_port is None: |
| 34 |
_logger.warning('Printer "%s" has no port name. Used dummy port', identifier) |
| 35 |
printer_port = 'IOT_DUMMY_PORT' |
| 36 |
|
| 37 |
if printer_port == "PORTPROMPT:": |
| 38 |
# discard virtual printers (like "Microsoft Print to PDF") as they will trigger dialog boxes prompt |
| 39 |
continue |
| 40 |
|
| 41 |
printer_devices[identifier] = { |
| 42 |
'identifier': identifier, |
| 43 |
'printer_handle': handle_printer, |
| 44 |
'port': printer_port, |
| 45 |
} |
| 46 |
return printer_devices |