diff options
author | Diego Elio Pettenò <flameeyes@flameeyes.eu> | 2013-09-29 23:17:22 +0200 |
---|---|---|
committer | Diego Elio Pettenò <flameeyes@flameeyes.eu> | 2013-09-29 23:17:22 +0200 |
commit | 3f48beabb58be75147197ee55f37673808d4c176 (patch) | |
tree | 6e3c9ba0e182c26188c1102b98454b9b0110e466 | |
parent | Fix style for tests. (diff) | |
download | glucometerutils-3f48beabb58be75147197ee55f37673808d4c176.tar glucometerutils-3f48beabb58be75147197ee55f37673808d4c176.tar.gz glucometerutils-3f48beabb58be75147197ee55f37673808d4c176.tar.bz2 glucometerutils-3f48beabb58be75147197ee55f37673808d4c176.tar.lz glucometerutils-3f48beabb58be75147197ee55f37673808d4c176.tar.xz glucometerutils-3f48beabb58be75147197ee55f37673808d4c176.tar.zst glucometerutils-3f48beabb58be75147197ee55f37673808d4c176.zip |
-rwxr-xr-x | glucometer.py | 4 | ||||
-rw-r--r-- | glucometerutils/common.py | 19 | ||||
-rw-r--r-- | glucometerutils/drivers/otultra2.py | 21 |
3 files changed, 32 insertions, 12 deletions
diff --git a/glucometer.py b/glucometer.py index c2504f2..facd88b 100755 --- a/glucometer.py +++ b/glucometer.py @@ -45,8 +45,8 @@ def main(): device = driver.Device(args.device) if args.action == 'dump': - for reading in device.get_readings(args.unit): - print('%s,%f' % reading) + for reading in device.get_readings(): + print('%s,%f' % (reading.timestamp, reading.get_value_as(args.unit))) elif args.action == 'datetime': if args.set == 'now': print(device.set_datetime()) diff --git a/glucometerutils/common.py b/glucometerutils/common.py index 1f8d59d..a93ecf0 100644 --- a/glucometerutils/common.py +++ b/glucometerutils/common.py @@ -47,3 +47,22 @@ def convert_glucose_unit(value, from_unit, to_unit=None): return round(value / 18.0, 2) else: return round(value * 18.0, 0) + + +class Reading(object): + def __init__(self, timestamp, value, unit): + self.timestamp = timestamp + self.value = value + self.unit = unit + + def get_value_as(self, to_unit): + """Returns the reading value as the given unit. + + Args: + to_unit: either UNIT_MGDL or UNIT_MMOLL as wanted; if None, the + value as recorded will be returned. + """ + if to_unit is None: + return self.value + + return convert_glucose_unit(self.value, self.unit, to_unit) diff --git a/glucometerutils/drivers/otultra2.py b/glucometerutils/drivers/otultra2.py index 9770b66..4eb2eb1 100644 --- a/glucometerutils/drivers/otultra2.py +++ b/glucometerutils/drivers/otultra2.py @@ -210,10 +210,13 @@ class Device(object): match = self._GLUCOSE_UNIT_RE.match(response) return self._parse_glucose_unit(match.group(1)) - _DUMP_HEADER_RE = re.compile(r'P ([0-9]{3}),"[0-9A-Z]{9}","(MG/DL |MMOL/L)"') - _DUMP_LINE_RE = re.compile(r'P ("[A-Z]{3}","[0-9/]{8}","[0-9:]{8} "),"([ 0-9.]{6})",') + _DUMP_HEADER_RE = re.compile(r'P ([0-9]{3}),"[0-9A-Z]{9}","(?:MG/DL |MMOL/L)"') + _DUMP_LINE_RE = re.compile( + r'P (?P<datetime>"[A-Z]{3}","[0-9/]{8}","[0-9:]{8} "),' + r'"(?P<control>[C ]) (?P<value>[0-9]{3})(?P<parityerror>[\? ])",' + r'"(?P<meal>[NBA])","(?P<comment>0[0-9]|1[01])", 00') - def get_readings(self, unit=None): + def get_readings(self): """Iterates over the reading values stored in the glucometer. Args: @@ -235,8 +238,6 @@ class Device(object): if not match: raise exceptions.InvalidResponse(header) - if not unit: - unit = self._parse_glucose_unit(match.group(2)) count = int(match.group(1)) assert count == len(data) @@ -247,12 +248,12 @@ class Device(object): if not match: raise exceptions.InvalidResponse(line) - date = self._parse_datetime(match.group(1)) + line_data = match.groupdict() + + date = self._parse_datetime(line_data['datetime']) # OneTouch2 always returns the data in mg/dL even if the # glucometer is set to mmol/L. We need to convert it to the # requested unit here. - value = common.convert_glucose_unit(int(match.group(2)), - common.UNIT_MGDL, unit) - - yield (date, value) + yield common.Reading(date, int(line_data['value']), + common.UNIT_MGDL) |