summaryrefslogtreecommitdiffstats
path: root/freestyle_hid/_session.py
diff options
context:
space:
mode:
authorDiego Elio Pettenò <flameeyes@flameeyes.com>2021-03-03 14:48:38 +0100
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>2021-03-03 17:04:35 +0100
commitee214a1470db58d60b1d7612ed33691ad7cfd370 (patch)
tree3b7ee7b6f65a9d369c43863625323b9680e44ae2 /freestyle_hid/_session.py
parentCalculate multirecords checksum based on the raw bytes. (diff)
downloadfreestyle-hid-ee214a1470db58d60b1d7612ed33691ad7cfd370.tar
freestyle-hid-ee214a1470db58d60b1d7612ed33691ad7cfd370.tar.gz
freestyle-hid-ee214a1470db58d60b1d7612ed33691ad7cfd370.tar.bz2
freestyle-hid-ee214a1470db58d60b1d7612ed33691ad7cfd370.tar.lz
freestyle-hid-ee214a1470db58d60b1d7612ed33691ad7cfd370.tar.xz
freestyle-hid-ee214a1470db58d60b1d7612ed33691ad7cfd370.tar.zst
freestyle-hid-ee214a1470db58d60b1d7612ed33691ad7cfd370.zip
Diffstat (limited to 'freestyle_hid/_session.py')
-rw-r--r--freestyle_hid/_session.py20
1 files changed, 9 insertions, 11 deletions
diff --git a/freestyle_hid/_session.py b/freestyle_hid/_session.py
index aab1f6c..527a5fc 100644
--- a/freestyle_hid/_session.py
+++ b/freestyle_hid/_session.py
@@ -118,11 +118,13 @@ class Session:
device_path: Optional[pathlib.Path],
text_message_type: int,
text_reply_message_type: int,
+ encoding: str = "ascii",
) -> None:
self._handle = HidWrapper.open(device_path, ABBOTT_VENDOR_ID, product_id)
self._text_message_type = text_message_type
self._text_reply_message_type = text_reply_message_type
+ self._encoding = encoding
def connect(self):
"""Open connection to the device, starting the knocking sequence."""
@@ -191,7 +193,7 @@ class Session:
return message
- def _send_text_command(self, command: bytes) -> bytes:
+ def _send_text_command_raw(self, command: bytes) -> bytes:
"""Send a command to the device that expects a text reply."""
self.send_command(self._text_message_type, command)
@@ -226,12 +228,8 @@ class Session:
return message
- def send_text_command(self, command: bytes) -> bytes:
- # If there is anything in the response that is not ASCII-safe, this is
- # probably in the patient name. The Windows utility does not seem to
- # validate those, so just replace anything non-ASCII with the correct
- # unknown codepoint.
- return self._send_text_command(command).decode("ascii", "replace")
+ def send_text_command(self, command: bytes) -> str:
+ return self._send_text_command_raw(command).decode(self._encoding, "replace")
def query_multirecord(self, command: bytes) -> Iterator[Sequence[str]]:
"""Queries for, and returns, "multirecords" results.
@@ -250,7 +248,7 @@ class Session:
A CSV reader object that returns a record for each line in the
reply buffer.
"""
- message = self._send_text_command(command)
+ message = self._send_text_command_raw(command)
logging.debug(f"Received multi-record message:\n{message!r}")
if message == b"Log Empty\r\n":
return iter(())
@@ -259,12 +257,12 @@ class Session:
if not match:
raise CommandError(repr(message))
- records_str = match.group("message")
- _verify_checksum(records_str, match.group("checksum"))
+ records_raw = match.group("message")
+ _verify_checksum(records_raw, match.group("checksum"))
# Decode here with replacement; the software does not deal with UTF-8
# correctly, and appears to truncate incorrectly the strings.
- records_str = records_str.decode("utf-8", "replace")
+ records_str = records_raw.decode(self._encoding, "replace")
logging.debug(f"Received multi-record string: {records_str}")