From 4fa145459bcdeb12d52543cba1380c8ed7ee6ff8 Mon Sep 17 00:00:00 2001 From: NaitLee Date: Mon, 13 Feb 2023 18:46:49 +0800 Subject: [PATCH] a bit code review --- printer.py | 10 +++++++--- printer_lib/commander.py | 11 +++++++++-- www/index.html | 4 ++-- www/main.js | 2 +- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/printer.py b/printer.py index 8e81390..5fd517e 100644 --- a/printer.py +++ b/printer.py @@ -315,7 +315,10 @@ class PrinterDriver(Commander): def loop(self, *futures): ''' Run coroutines in order in current event loop until complete, - return its result directly, or their result as tuple + return its result directly, or their result as tuple. + + This 1) ensures exiting gracefully (futures always get completed before exiting), + and 2) avoids function colors (use of "await", especially outside this script) ''' results = [] for future in futures: @@ -344,9 +347,9 @@ class PrinterDriver(Commander): self.model = Models[name] self.device = BleakClient(address) def notify(_char, data): - if data == b'\x51\x78\xae\x01\x01\x00\x10\x70\xff': + if data == self.data_flow_pause: self._paused = True - elif data == b'\x51\x78\xae\x01\x01\x00\x00\x00\xff': + elif data == self.data_flow_resume: self._paused = False self.loop( self.device.connect(timeout=self.connection_timeout), @@ -371,6 +374,7 @@ class PrinterDriver(Commander): name, address = identifier.split(',') if name not in Models: error('model-0-is-not-supported-yet', name, exception=PrinterError) + # TODO: is this logic correct? if address[2::3] != ':::::' and len(address.replace('-', '')) != 32: error('invalid-address-0', address, exception=PrinterError) if use_result: diff --git a/printer_lib/commander.py b/printer_lib/commander.py index 6604707..04af2c9 100644 --- a/printer_lib/commander.py +++ b/printer_lib/commander.py @@ -1,5 +1,5 @@ ''' -Cat-Printer Commander, way to communicate with cat printers via bluetooth +Cat-Printer Commander(s), binary interface(s) to communicate with cat printers No rights reserved. License CC0-1.0-only: https://directory.fsf.org/wiki/License:CC0 @@ -65,10 +65,17 @@ def int_to_bytes(i: int, big_endian=False): return result class Commander(metaclass=ABCMeta): - 'Semi-abstract class, to be inherited by `PrinterDriver`' + ''' Semi-abstract class, to be inherited by `PrinterDriver` + Contains binary data communication interface for individual functions + "Commander" of kind of printers like GB0X, GT01 + Class structure is not guaranteed to be stable + ''' dry_run: bool = False + data_flow_pause = b'\x51\x78\xae\x01\x01\x00\x10\x70\xff' + data_flow_resume = b'\x51\x78\xae\x01\x01\x00\x00\x00\xff' + def make_command(self, command_bit, payload: bytearray, *, prefix=bytearray(), suffix=bytearray()): 'Make bytes that to be used to control printer' diff --git a/www/index.html b/www/index.html index 65ef0ed..e80736f 100644 --- a/www/index.html +++ b/www/index.html @@ -45,7 +45,7 @@ Brightness: - +