From 44bc1fad54b171bb758eb336796a487ca28157f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Elio=20Petten=C3=B2?= Date: Mon, 1 Jan 2018 12:50:17 +0000 Subject: otultraeasy: wrap around construct exceptions to MalformedCommand. --- glucometerutils/drivers/otultraeasy.py | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/glucometerutils/drivers/otultraeasy.py b/glucometerutils/drivers/otultraeasy.py index 0f95e80..ced1f0b 100644 --- a/glucometerutils/drivers/otultraeasy.py +++ b/glucometerutils/drivers/otultraeasy.py @@ -133,8 +133,11 @@ class Device(serial.SerialDevice): self.buffered_reader_ = construct.Rebuffered(_PACKET, tailcutoff=1024) def connect(self): - self._send_packet(b'', disconnect=True) - self._read_ack() + try: + self._send_packet(b'', disconnect=True) + self._read_ack() + except construct.ConstructError as e: + raise lifescan.MalformedCommand(str(e)) def disconnect(self): self.connect() @@ -175,20 +178,26 @@ class Device(serial.SerialDevice): assert pkt.acknowledge def _send_request(self, request_format, *args): - request = request_format.build(*args) - self._send_packet(request, acknowledge=False, disconnect=False) + try: + request = request_format.build(*args) + self._send_packet(request, acknowledge=False, disconnect=False) - self.sent_counter_ = not self.sent_counter_ - self._read_ack() + self.sent_counter_ = not self.sent_counter_ + self._read_ack() + except construct.ConstructError as e: + raise lifescan.MalformedCommand(str(e)) def _read_response(self, response_format): - pkt = self._read_packet() - assert not pkt.acknowledge + try: + pkt = self._read_packet() + assert not pkt.acknowledge - self.expect_receive_ = not self.expect_receive_ - self._send_ack() + self.expect_receive_ = not self.expect_receive_ + self._send_ack() - return response_format.parse(pkt.message) + return response_format.parse(pkt.message) + except construct.ConstructError as e: + raise lifescan.MalformedCommand(str(e)) def get_meter_info(self): return common.MeterInfo( -- cgit v1.2.3