From 649a12b906f8310b46f0e0d88374446abd1632e1 Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 18 Feb 2020 18:14:56 +0100 Subject: factor out common driver methods --- glucometerutils/support/driver_base.py | 55 ++++++++++++++++++++++++++++++++++ glucometerutils/support/freestyle.py | 16 ++++------ 2 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 glucometerutils/support/driver_base.py (limited to 'glucometerutils/support') diff --git a/glucometerutils/support/driver_base.py b/glucometerutils/support/driver_base.py new file mode 100644 index 0000000..b7b3d0f --- /dev/null +++ b/glucometerutils/support/driver_base.py @@ -0,0 +1,55 @@ +from abc import ABC, abstractmethod +from datetime import datetime + + +class GlucometerDriver(ABC): + + def connect(self): + pass + + def disconnect(self): + pass + + @abstractmethod + def get_meter_info(self): + """Return the device information in structured form.""" + pass + + @abstractmethod + def get_serial_number(self): + pass + + @abstractmethod + def get_glucose_unit(self): + """Returns the glucose unit of the device.""" + pass + + @abstractmethod + def get_datetime(self): + pass + + def set_datetime(self, date=None): + """Sets the date and time of the glucometer. + + Args: + date: The value to set the date/time of the glucometer to. If none is + given, the current date and time of the computer is used. + + Returns: + A datetime object built according to the returned response. + """ + if not date: + date = datetime.now() + return self._set_device_datetime(date) + + @abstractmethod + def _set_device_datetime(self, date): + pass + + @abstractmethod + def zero_log(self): + pass + + @abstractmethod + def get_readings(self): + pass diff --git a/glucometerutils/support/freestyle.py b/glucometerutils/support/freestyle.py index ed8f7e9..1e8c097 100644 --- a/glucometerutils/support/freestyle.py +++ b/glucometerutils/support/freestyle.py @@ -12,12 +12,13 @@ import csv import datetime import logging import re +from abc import ABC from typing import AnyStr, Callable, Iterator, List, Optional, Text, Tuple import construct from glucometerutils import exceptions -from glucometerutils.support import hiddevice +from glucometerutils.support import hiddevice, driver_base _INIT_COMMAND = 0x01 _INIT_RESPONSE = 0x71 @@ -103,6 +104,7 @@ def _verify_checksum(message, expected_checksum_hex): if expected_checksum != calculated_checksum: raise exceptions.InvalidChecksum(expected_checksum, calculated_checksum) + def convert_ketone_unit(raw_value): """Convert raw ketone value as read in the device to its value in mmol/L. @@ -113,7 +115,8 @@ def convert_ketone_unit(raw_value): """ return raw_value / 18.0 -class FreeStyleHidDevice(hiddevice.HidDevice): + +class FreeStyleHidDevice(hiddevice.HidDevice, driver_base.GlucometerDriver, ABC): """Base class implementing the FreeStyle HID common protocol. This class implements opening, initializing the connection and sending @@ -295,12 +298,9 @@ class FreeStyleHidDevice(hiddevice.HidDevice): except ValueError: raise exceptions.InvalidDateTime() - def set_datetime(self, date=None): + def _set_device_datetime(self, date): # type: (datetime.datetime) -> datetime.datetime - """Sets the date and time of the device.""" - if not date: - date = datetime.datetime.now() # The format used by the FreeStyle devices is not composable based on # standard strftime() (namely it includes no leading zeros), so we need # to build it manually. @@ -314,10 +314,6 @@ class FreeStyleHidDevice(hiddevice.HidDevice): return self.get_datetime() - def zero_log(self): - """Not implemented, Abbott devices don't allow resetting memory.""" - raise NotImplementedError - def _get_multirecord(self, command): # type: (bytes) -> Iterator[List[Text]] """Queries for, and returns, "multirecords" results. -- cgit v1.2.3