From 3e1ef26874dc7ec76069a0c9dd1f15fae9505bb0 Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 11 Apr 2019 11:44:25 +0200 Subject: add time adjustments to fslibre dump --- .gitignore | 2 ++ glucometerutils/common.py | 17 +++++++++++++++++ glucometerutils/drivers/fslibre.py | 35 ++++++++++++++++++++++++++--------- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index a9e5af7..11c62a9 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ /dist/ __pycache__/ build +.idea/ +*venv/ \ No newline at end of file diff --git a/glucometerutils/common.py b/glucometerutils/common.py index ad56b8b..f549087 100644 --- a/glucometerutils/common.py +++ b/glucometerutils/common.py @@ -28,6 +28,7 @@ class Meal(enum.Enum): class MeasurementMethod(enum.Enum): BLOOD_SAMPLE = 'blood sample' CGM = 'CGM' # Continuous Glucose Monitoring + TIME = 'time' def convert_glucose_unit(value, from_unit, to_unit): @@ -97,6 +98,22 @@ class KetoneReading: self.timestamp, self.value, MeasurementMethod.BLOOD_SAMPLE, self.comment) +@attr.s +class TimeAdjustment: + timestamp = attr.ib() # type: datetime.datetime + old_timestamp = attr.ib() # type: datetime.datetime + measure_method = attr.ib( + default=MeasurementMethod.TIME, + validator=attr.validators.in_( + MeasurementMethod)) # type: MeasurementMethod + + def as_csv(self, unit): + del unit + return '"%s","","%s","%s"' % ( + self.timestamp, self.measure_method.value, self.old_timestamp + ) + + @attr.s class MeterInfo: """General information about the meter. diff --git a/glucometerutils/drivers/fslibre.py b/glucometerutils/drivers/fslibre.py index ecf6dd6..7d3e496 100644 --- a/glucometerutils/drivers/fslibre.py +++ b/glucometerutils/drivers/fslibre.py @@ -57,6 +57,15 @@ _ARRESULT_TYPE2_ENTRY_MAP = ( (28, 'errors'), ) +_ARRESULT_TYPE5_ENTRY_MAP = ( + (9, 'old_month'), + (10, 'old_day'), + (11, 'old_year'), + (12, 'old_hour'), + (13, 'old_minute'), + (14, 'old_second'), +) + # Fields only valid when rapid-acting-flag is "1" _ARRESULT_RAPID_INSULIN_ENTRY_MAP = ( (43, 'double-rapid-acting-insulin'), @@ -77,19 +86,19 @@ def _parse_record(record, entry_map): return {} -def _extract_timestamp(parsed_record): +def _extract_timestamp(parsed_record, prefix=''): """Extract the timestamp from a parsed record. This leverages the fact that all the records have the same base structure. """ return datetime.datetime( - parsed_record['year'] + 2000, - parsed_record['month'], - parsed_record['day'], - parsed_record['hour'], - parsed_record['minute'], - parsed_record['second']) + parsed_record[prefix + 'year'] + 2000, + parsed_record[prefix + 'month'], + parsed_record[prefix + 'day'], + parsed_record[prefix + 'hour'], + parsed_record[prefix + 'minute'], + parsed_record[prefix + 'second']) def _convert_ketone_unit(raw_value): @@ -110,10 +119,18 @@ def _parse_arresult(record): parsed_record = _parse_record(record, _BASE_ENTRY_MAP) # There are other record types, but we don't currently need to expose these. - if not parsed_record or parsed_record['type'] != 2: + if not parsed_record: + return None + elif parsed_record['type'] == 2: + parsed_record.update(_parse_record(record, _ARRESULT_TYPE2_ENTRY_MAP)) + elif parsed_record['type'] == 5: + parsed_record.update(_parse_record(record, _ARRESULT_TYPE5_ENTRY_MAP)) + return common.TimeAdjustment( + _extract_timestamp(parsed_record), + _extract_timestamp(parsed_record, 'old_')) + else: return None - parsed_record.update(_parse_record(record, _ARRESULT_TYPE2_ENTRY_MAP)) # Check right away if we have rapid insulin if parsed_record['rapid-acting-flag']: -- cgit v1.2.3