From 783ae018c61526f1b5e88f8d82e87c2955ba82bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Elio=20Petten=C3=B2?= Date: Mon, 1 Jul 2019 20:49:29 +0100 Subject: Introduce an explicit InvalidDateTime exception for meters with unset time. At least the FreeStyle Precision Neo appears to be able to report an invalid date/time (255/255/255 255:255), probably when the RTC is lost (see issue #58). This returns a more precise output than N/A, and should suggest to confirm the date and time setting instead. --- glucometerutils/exceptions.py | 8 ++++++++ glucometerutils/glucometer.py | 6 +++--- glucometerutils/support/freestyle.py | 8 +++++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/glucometerutils/exceptions.py b/glucometerutils/exceptions.py index 415c97f..1cc8cfd 100644 --- a/glucometerutils/exceptions.py +++ b/glucometerutils/exceptions.py @@ -46,3 +46,11 @@ class InvalidGlucoseUnit(Error): def __init__(self, unit): super(InvalidGlucoseUnit, self).__init__( 'Invalid glucose unit received:\n%s' % unit) + + +class InvalidDateTime(Error): + """The device has an invalid date/time setting.""" + + def __init__(self): + super(InvalidDateTime, self).__init__( + 'Invalid date and time for device') diff --git a/glucometerutils/glucometer.py b/glucometerutils/glucometer.py index 485b32b..14999a7 100755 --- a/glucometerutils/glucometer.py +++ b/glucometerutils/glucometer.py @@ -86,9 +86,9 @@ def main(): if args.action == 'info': try: time_str = device.get_datetime() - except ValueError: - time_str = 'N/A' #handles value error exception for when time format found isn't expected - except NotImplementedError: + except exceptions.InvalidDateTime: + time_str = 'INVALID' + except NotImplementedError, ValueError: # Catch any leftover ValueError time_str = 'N/A' print("{device_info}Time: {time}".format( device_info=str(device_info), time=time_str)) diff --git a/glucometerutils/support/freestyle.py b/glucometerutils/support/freestyle.py index d18a81f..475d6f5 100644 --- a/glucometerutils/support/freestyle.py +++ b/glucometerutils/support/freestyle.py @@ -200,7 +200,13 @@ class FreeStyleHidDevice(hiddevice.HidDevice): month, day, year = (int(x) for x in date.split(',')) hour, minute = (int(x) for x in time.split(',')) - return datetime.datetime(year + 2000, month, day, hour, minute) + # At least Precision Neo devices can have an invalid date (bad RTC?), + # and report 255 for each field, which is not valid for + # datetime.datetime(). + try: + return datetime.datetime(year + 2000, month, day, hour, minute) + except ValueError: + raise exceptions.InvalidDateTime() def set_datetime(self, date=datetime.datetime.now()): # type: (datetime.datetime) -> datetime.datetime -- cgit v1.2.3