From 7435b383fbbc44dc69e42ca7b1fb9ad0d9579d14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Elio=20Petten=C3=B2?= Date: Sun, 29 Mar 2020 17:30:59 +0100 Subject: Move driver_base to glucometerutils.driver instead. This makes it more like the other common modules, and allows expanding the interfaces. --- glucometerutils/driver.py | 67 +++++++++++++++++++++++++++++ glucometerutils/drivers/accuchek_reports.py | 5 +-- glucometerutils/drivers/fsoptium.py | 6 +-- glucometerutils/drivers/otultra2.py | 6 +-- glucometerutils/drivers/otultraeasy.py | 5 +-- glucometerutils/drivers/otverio2015.py | 6 +-- glucometerutils/drivers/otverioiq.py | 11 ++--- glucometerutils/drivers/sdcodefree.py | 6 +-- glucometerutils/drivers/td4277.py | 6 +-- glucometerutils/support/contourusb.py | 5 ++- glucometerutils/support/driver_base.py | 67 ----------------------------- glucometerutils/support/freestyle.py | 6 +-- 12 files changed, 95 insertions(+), 101 deletions(-) create mode 100644 glucometerutils/driver.py delete mode 100644 glucometerutils/support/driver_base.py diff --git a/glucometerutils/driver.py b/glucometerutils/driver.py new file mode 100644 index 0000000..d630031 --- /dev/null +++ b/glucometerutils/driver.py @@ -0,0 +1,67 @@ +# -*- coding: utf-8 -*- +# +# SPDX-FileCopyrightText: © 2020 The glucometerutils Authors +# SPDX-License-Identifier: MIT + +import abc +import datetime +from typing import Generator, Optional, Text + +from glucometerutils import common + + +class GlucometerDriver(abc.ABC): + def __init__(self, device_path: Optional[Text]) -> None: + pass + + def connect(self) -> None: + pass + + def disconnect(self) -> None: + pass + + @abc.abstractmethod + def get_meter_info(self) -> common.MeterInfo: + """Return the device information in structured form.""" + pass + + @abc.abstractmethod + def get_serial_number(self) -> str: + pass + + @abc.abstractmethod + def get_glucose_unit(self) -> common.Unit: + """Returns the glucose unit of the device.""" + pass + + @abc.abstractmethod + def get_datetime(self) -> datetime.datetime: + pass + + def set_datetime( + self, date: Optional[datetime.datetime] = None + ) -> datetime.datetime: + """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.datetime.now() + return self._set_device_datetime(date) + + @abc.abstractmethod + def _set_device_datetime(self, date: datetime.datetime) -> datetime.datetime: + pass + + @abc.abstractmethod + def zero_log(self) -> None: + pass + + @abc.abstractmethod + def get_readings(self) -> Generator[common.AnyReading, None, None]: + pass diff --git a/glucometerutils/drivers/accuchek_reports.py b/glucometerutils/drivers/accuchek_reports.py index 7fd9df9..0f2c983 100644 --- a/glucometerutils/drivers/accuchek_reports.py +++ b/glucometerutils/drivers/accuchek_reports.py @@ -21,8 +21,7 @@ import glob import os from typing import Dict, Generator, NoReturn, Optional -from glucometerutils import common, exceptions -from glucometerutils.support import driver_base +from glucometerutils import common, driver, exceptions _UNIT_MAP = { "mmol/l": common.Unit.MMOL_L, @@ -47,7 +46,7 @@ _TIME_FORMAT = "%H:%M" _DATETIME_FORMAT = " ".join((_DATE_FORMAT, _TIME_FORMAT)) -class Device(driver_base.GlucometerDriver): +class Device(driver.GlucometerDriver): def __init__(self, device: Optional[str]) -> None: if not device or not os.path.isdir(device): raise exceptions.CommandLineError( diff --git a/glucometerutils/drivers/fsoptium.py b/glucometerutils/drivers/fsoptium.py index 3eb921f..789cdfd 100644 --- a/glucometerutils/drivers/fsoptium.py +++ b/glucometerutils/drivers/fsoptium.py @@ -22,8 +22,8 @@ import logging import re from typing import Generator, NoReturn, Sequence -from glucometerutils import common, exceptions -from glucometerutils.support import driver_base, serial +from glucometerutils import common, driver, exceptions +from glucometerutils.support import serial _CLOCK_RE = re.compile( r"^Clock:\t(?P[A-Z][a-z]{2}) (?P[0-9]{2}) (?P[0-9]{4})\t" @@ -86,7 +86,7 @@ def _parse_clock(datestr: str) -> datetime.datetime: return datetime.datetime(year, month, day, hour, minute, second) -class Device(serial.SerialDevice, driver_base.GlucometerDriver): +class Device(serial.SerialDevice, driver.GlucometerDriver): BAUDRATE = 19200 DEFAULT_CABLE_ID = "1a61:3420" diff --git a/glucometerutils/drivers/otultra2.py b/glucometerutils/drivers/otultra2.py index 1e458e3..9b74da8 100644 --- a/glucometerutils/drivers/otultra2.py +++ b/glucometerutils/drivers/otultra2.py @@ -18,8 +18,8 @@ import datetime import re from typing import Generator -from glucometerutils import common, exceptions -from glucometerutils.support import driver_base, lifescan, serial +from glucometerutils import common, driver, exceptions +from glucometerutils.support import lifescan, serial # The following two hashes are taken directly from LifeScan's documentation _MEAL_CODES = { @@ -130,7 +130,7 @@ def _parse_datetime(response: str) -> datetime.datetime: return datetime.datetime(2000 + year, month, day, hour, minute, second) -class Device(serial.SerialDevice, driver_base.GlucometerDriver): +class Device(serial.SerialDevice, driver.GlucometerDriver): BAUDRATE = 9600 DEFAULT_CABLE_ID = "067b:2303" # Generic PL2303 cable. diff --git a/glucometerutils/drivers/otultraeasy.py b/glucometerutils/drivers/otultraeasy.py index 4b7d278..00f00b6 100644 --- a/glucometerutils/drivers/otultraeasy.py +++ b/glucometerutils/drivers/otultraeasy.py @@ -23,10 +23,9 @@ from typing import Any, Dict, Generator, Optional import construct -from glucometerutils import common +from glucometerutils import common, driver from glucometerutils.support import ( construct_extras, - driver_base, lifescan, lifescan_binary_protocol, serial, @@ -118,7 +117,7 @@ def _make_packet( ) -class Device(serial.SerialDevice, driver_base.GlucometerDriver): +class Device(serial.SerialDevice, driver.GlucometerDriver): BAUDRATE = 9600 DEFAULT_CABLE_ID = "067b:2303" # Generic PL2303 cable. TIMEOUT = 0.5 diff --git a/glucometerutils/drivers/otverio2015.py b/glucometerutils/drivers/otverio2015.py index fb85b6a..44d03b1 100644 --- a/glucometerutils/drivers/otverio2015.py +++ b/glucometerutils/drivers/otverio2015.py @@ -32,8 +32,8 @@ import construct from pyscsi.pyscsi.scsi import SCSI from pyscsi.pyscsi.scsi_device import SCSIDevice -from glucometerutils import common, exceptions -from glucometerutils.support import driver_base, lifescan, lifescan_binary_protocol +from glucometerutils import common, driver, exceptions +from glucometerutils.support import lifescan, lifescan_binary_protocol # This device uses SCSI blocks as registers. _REGISTER_SIZE = 512 @@ -107,7 +107,7 @@ _READ_RECORD_RESPONSE = construct.Struct( ) -class Device(driver_base.GlucometerDriver): +class Device(driver.GlucometerDriver): def __init__(self, device: Optional[str]) -> None: if not device: raise exceptions.CommandLineError( diff --git a/glucometerutils/drivers/otverioiq.py b/glucometerutils/drivers/otverioiq.py index 181c899..f7b87dc 100644 --- a/glucometerutils/drivers/otverioiq.py +++ b/glucometerutils/drivers/otverioiq.py @@ -23,13 +23,8 @@ from typing import Any, Dict, Generator, Optional import construct -from glucometerutils import common -from glucometerutils.support import ( - driver_base, - lifescan, - lifescan_binary_protocol, - serial, -) +from glucometerutils import common, driver +from glucometerutils.support import lifescan, lifescan_binary_protocol, serial _PACKET = lifescan_binary_protocol.LifeScanPacket(False) @@ -99,7 +94,7 @@ _READING_RESPONSE = construct.Struct( ) -class Device(serial.SerialDevice, driver_base.GlucometerDriver): +class Device(serial.SerialDevice, driver.GlucometerDriver): BAUDRATE = 38400 DEFAULT_CABLE_ID = "10c4:85a7" # Specific ID for embedded cp210x TIMEOUT = 0.5 diff --git a/glucometerutils/drivers/sdcodefree.py b/glucometerutils/drivers/sdcodefree.py index 93b7adb..35ff8c7 100644 --- a/glucometerutils/drivers/sdcodefree.py +++ b/glucometerutils/drivers/sdcodefree.py @@ -26,8 +26,8 @@ from typing import Generator, NoReturn import construct -from glucometerutils import common, exceptions -from glucometerutils.support import driver_base, serial +from glucometerutils import common, driver, exceptions +from glucometerutils.support import serial def xor_checksum(msg: bytes) -> int: @@ -84,7 +84,7 @@ _READING = construct.Struct( ) -class Device(serial.SerialDevice, driver_base.GlucometerDriver): +class Device(serial.SerialDevice, driver.GlucometerDriver): BAUDRATE = 38400 DEFAULT_CABLE_ID = "10c4:ea60" # Generic cable. TIMEOUT = 300 # We need to wait for data from the device. diff --git a/glucometerutils/drivers/td4277.py b/glucometerutils/drivers/td4277.py index 443f69c..7ee177d 100644 --- a/glucometerutils/drivers/td4277.py +++ b/glucometerutils/drivers/td4277.py @@ -23,8 +23,8 @@ from typing import Generator, NoReturn, Optional, Tuple import construct -from glucometerutils import common, exceptions -from glucometerutils.support import driver_base, serial +from glucometerutils import common, driver, exceptions +from glucometerutils.support import serial class Direction(enum.Enum): @@ -132,7 +132,7 @@ def _select_record(record_id: int) -> bytes: return _READING_SELECTION_STRUCT.build({"record_id": record_id}) -class Device(serial.SerialDevice, driver_base.GlucometerDriver): +class Device(serial.SerialDevice, driver.GlucometerDriver): BAUDRATE = 19200 TIMEOUT = 0.5 diff --git a/glucometerutils/support/contourusb.py b/glucometerutils/support/contourusb.py index 1b2f802..e27347e 100644 --- a/glucometerutils/support/contourusb.py +++ b/glucometerutils/support/contourusb.py @@ -17,7 +17,8 @@ import enum import re from typing import Dict, Generator, List, Optional, Tuple -from glucometerutils.support import driver_base, hiddevice +from glucometerutils import driver +from glucometerutils.support import hiddevice # regexr.com/4k6jb _HEADER_RECORD_RE = re.compile( @@ -70,7 +71,7 @@ class Mode(enum.Enum): COMMAND = enum.auto() -class ContourHidDevice(driver_base.GlucometerDriver): +class ContourHidDevice(driver.GlucometerDriver): """Base class implementing the ContourUSB HID common protocol. """ diff --git a/glucometerutils/support/driver_base.py b/glucometerutils/support/driver_base.py deleted file mode 100644 index d630031..0000000 --- a/glucometerutils/support/driver_base.py +++ /dev/null @@ -1,67 +0,0 @@ -# -*- coding: utf-8 -*- -# -# SPDX-FileCopyrightText: © 2020 The glucometerutils Authors -# SPDX-License-Identifier: MIT - -import abc -import datetime -from typing import Generator, Optional, Text - -from glucometerutils import common - - -class GlucometerDriver(abc.ABC): - def __init__(self, device_path: Optional[Text]) -> None: - pass - - def connect(self) -> None: - pass - - def disconnect(self) -> None: - pass - - @abc.abstractmethod - def get_meter_info(self) -> common.MeterInfo: - """Return the device information in structured form.""" - pass - - @abc.abstractmethod - def get_serial_number(self) -> str: - pass - - @abc.abstractmethod - def get_glucose_unit(self) -> common.Unit: - """Returns the glucose unit of the device.""" - pass - - @abc.abstractmethod - def get_datetime(self) -> datetime.datetime: - pass - - def set_datetime( - self, date: Optional[datetime.datetime] = None - ) -> datetime.datetime: - """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.datetime.now() - return self._set_device_datetime(date) - - @abc.abstractmethod - def _set_device_datetime(self, date: datetime.datetime) -> datetime.datetime: - pass - - @abc.abstractmethod - def zero_log(self) -> None: - pass - - @abc.abstractmethod - def get_readings(self) -> Generator[common.AnyReading, None, None]: - pass diff --git a/glucometerutils/support/freestyle.py b/glucometerutils/support/freestyle.py index b70d993..9cdbff9 100644 --- a/glucometerutils/support/freestyle.py +++ b/glucometerutils/support/freestyle.py @@ -17,8 +17,8 @@ from typing import AnyStr, Callable, Iterator, List, Optional, Tuple import construct -from glucometerutils import exceptions -from glucometerutils.support import driver_base, hiddevice +from glucometerutils import driver, exceptions +from glucometerutils.support import hiddevice _INIT_COMMAND = 0x01 _INIT_RESPONSE = 0x71 @@ -286,7 +286,7 @@ class FreeStyleHidSession: return csv.reader(records_str.split("\r\n")) -class FreeStyleHidDevice(driver_base.GlucometerDriver): +class FreeStyleHidDevice(driver.GlucometerDriver): """Base class implementing the FreeStyle HID common protocol. This class implements opening, initializing the connection and sending -- cgit v1.2.3