| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
""" |
| 4 |
Helpers for the subpackage brother_ql.backends |
| 5 |
|
| 6 |
* device discovery |
| 7 |
* printing |
| 8 |
""" |
| 9 |
|
| 10 |
import logging, time |
| 11 |
|
| 12 |
from brother_ql.backends import backend_factory, guess_backend |
| 13 |
from brother_ql.reader import interpret_response |
| 14 |
|
| 15 |
logger = logging.getLogger(__name__) |
| 16 |
|
| 17 |
def discover(backend_identifier='linux_kernel'): |
| 18 |
|
| 19 |
be = backend_factory(backend_identifier) |
| 20 |
list_available_devices = be['list_available_devices'] |
| 21 |
BrotherQLBackend = be['backend_class'] |
| 22 |
|
| 23 |
available_devices = list_available_devices() |
| 24 |
return available_devices |
| 25 |
|
| 26 |
def send(instructions, printer_identifier=None, backend_identifier=None, blocking=True): |
| 27 |
""" |
| 28 |
Send instruction bytes to a printer. |
| 29 |
|
| 30 |
:param bytes instructions: The instructions to be sent to the printer. |
| 31 |
:param str printer_identifier: Identifier for the printer. |
| 32 |
:param str backend_identifier: Can enforce the use of a specific backend. |
| 33 |
:param bool blocking: Indicates whether the function call should block while waiting for the completion of the printing. |
| 34 |
""" |
| 35 |
|
| 36 |
status = { |
| 37 |
'instructions_sent': True, # The instructions were sent to the printer. |
| 38 |
'outcome': 'unknown', # String description of the outcome of the sending operation like: 'unknown', 'sent', 'printed', 'error' |
| 39 |
'printer_state': None, # If the selected backend supports reading back the printer state, this key will contain it. |
| 40 |
'did_print': False, # If True, a print was produced. It defaults to False if the outcome is uncertain (due to a backend without read-back capability). |
| 41 |
'ready_for_next_job': False, # If True, the printer is ready to receive the next instructions. It defaults to False if the state is unknown. |
| 42 |
} |
| 43 |
selected_backend = None |
| 44 |
if backend_identifier: |
| 45 |
selected_backend = backend_identifier |
| 46 |
else: |
| 47 |
try: |
| 48 |
selected_backend = guess_backend(printer_identifier) |
| 49 |
except: |
| 50 |
logger.info("No backend stated. Selecting the default linux_kernel backend.") |
| 51 |
selected_backend = 'linux_kernel' |
| 52 |
|
| 53 |
be = backend_factory(selected_backend) |
| 54 |
list_available_devices = be['list_available_devices'] |
| 55 |
BrotherQLBackend = be['backend_class'] |
| 56 |
|
| 57 |
printer = BrotherQLBackend(printer_identifier) |
| 58 |
|
| 59 |
start = time.time() |
| 60 |
logger.info('Sending instructions to the printer. Total: %d bytes.', len(instructions)) |
| 61 |
printer.write(instructions) |
| 62 |
status['outcome'] = 'sent' |
| 63 |
|
| 64 |
if not blocking: |
| 65 |
return status |
| 66 |
if selected_backend == 'network': |
| 67 |
""" No need to wait for completion. The network backend doesn't support readback. """ |
| 68 |
return status |
| 69 |
|
| 70 |
while time.time() - start < 10: |
| 71 |
data = printer.read() |
| 72 |
if not data: |
| 73 |
time.sleep(0.005) |
| 74 |
continue |
| 75 |
try: |
| 76 |
result = interpret_response(data) |
| 77 |
except ValueError: |
| 78 |
logger.error("TIME %.3f - Couln't understand response: %s", time.time()-start, data) |
| 79 |
continue |
| 80 |
status['printer_state'] = result |
| 81 |
logger.debug('TIME %.3f - result: %s', time.time()-start, result) |
| 82 |
if result['errors']: |
| 83 |
logger.error('Errors occured: %s', result['errors']) |
| 84 |
status['outcome'] = 'error' |
| 85 |
break |
| 86 |
if result['status_type'] == 'Printing completed': |
| 87 |
status['did_print'] = True |
| 88 |
status['outcome'] = 'printed' |
| 89 |
if result['status_type'] == 'Phase change' and result['phase_type'] == 'Waiting to receive': |
| 90 |
status['ready_for_next_job'] = True |
| 91 |
if status['did_print'] and status['ready_for_next_job']: |
| 92 |
break |
| 93 |
|
| 94 |
if not status['did_print']: |
| 95 |
logger.warning("'printing completed' status not received.") |
| 96 |
if not status['ready_for_next_job']: |
| 97 |
logger.warning("'waiting to receive' status not received.") |
| 98 |
if (not status['did_print']) or (not status['ready_for_next_job']): |
| 99 |
logger.warning('Printing potentially not successful?') |
| 100 |
if status['did_print'] and status['ready_for_next_job']: |
| 101 |
logger.info("Printing was successful. Waiting for the next job.") |
| 102 |
|
| 103 |
return status |