summaryrefslogtreecommitdiffstats
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
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 '')
-rw-r--r--freestyle_hid/_session.py20
-rwxr-xr-xfreestyle_hid/tools/hid_console.py10
2 files changed, 18 insertions, 12 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}")
diff --git a/freestyle_hid/tools/hid_console.py b/freestyle_hid/tools/hid_console.py
index b3b3fee..2c395bd 100755
--- a/freestyle_hid/tools/hid_console.py
+++ b/freestyle_hid/tools/hid_console.py
@@ -39,6 +39,13 @@ click_log.basic_config(logger)
type=int,
help="Optional product ID (in alternative to the device path)",
)
+@click.option(
+ "--encoding",
+ "-e",
+ type=str,
+ help="Encoding to use to decode commands returned by the meter",
+ default="ascii",
+)
@click.argument(
"device-path",
type=click.Path(exists=True, dir_okay=False, writable=True, allow_dash=False),
@@ -51,6 +58,7 @@ def main(
text_reply_type: int,
product_id: Optional[int],
device_path: Optional[pathlib.Path],
+ encoding: str,
):
if not product_id and not device_path:
raise click.UsageError(
@@ -58,7 +66,7 @@ def main(
)
session = freestyle_hid.Session(
- product_id, device_path, text_command_type, text_reply_type
+ product_id, device_path, text_command_type, text_reply_type, encoding=encoding
)
session.connect()