summaryrefslogtreecommitdiffstats
path: root/glucometerutils/common.py
blob: 8b413202947de428c34f9b57b32d085270c825a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# -*- coding: utf-8 -*-
#
# SPDX-License-Identifier: MIT
"""Common routines for data in glucometers."""

import datetime
import enum
import textwrap

try:
    from typing import Optional, Sequence
except ImportError:
    pass

import attr

class Unit(enum.Enum):
    MG_DL = 'mg/dL'
    MMOL_L = 'mmol/L'

# Constants for meal information
class Meal(enum.Enum):
    NONE = ''
    BEFORE = 'Before Meal'
    AFTER = 'After Meal'

# Constants for measure method
class MeasurementMethod(enum.Enum):
    BLOOD_SAMPLE = 'blood sample'
    CGM = 'CGM' # Continuous Glucose Monitoring
    TIME = 'time'


def convert_glucose_unit(value, from_unit, to_unit):
    # type: (float, Unit, Unit) -> float
    """Convert the given value of glucose level between units.

    Args:
      value: The value of glucose in the current unit
      from_unit: The unit value is currently expressed in
      to_unit: The unit to conver the value to: the other if empty.

    Returns:
      The converted representation of the blood glucose level.
    """
    from_unit = Unit(from_unit)
    to_unit = Unit(to_unit)

    if from_unit == to_unit:
        return value

    if from_unit == Unit.MG_DL:
        return round(value / 18.0, 2)

    return round(value * 18.0, 0)

@attr.s
class GlucoseReading:

    timestamp = attr.ib(type=datetime.datetime)
    value = attr.ib(type=float)
    meal = attr.ib(
        default=Meal.NONE, validator=attr.validators.in_(Meal),
        type=Meal)
    comment = attr.ib(default='', type=str)
    measure_method = attr.ib(
        default=MeasurementMethod.BLOOD_SAMPLE,
        validator=attr.validators.in_(MeasurementMethod),
        type=MeasurementMethod)
    extra_data = attr.ib(factory=dict)

    def get_value_as(self, to_unit):
        # type: (Unit) -> float
        """Returns the reading value as the given unit.

        Args:
          to_unit: (Unit) The unit to return the value to.
        """
        return convert_glucose_unit(self.value, Unit.MG_DL, to_unit)

    def as_csv(self, unit):
        # type: (Unit) -> str
        """Returns the reading as a formatted comma-separated value string."""
        return '"%s","%.2f","%s","%s","%s"' % (
            self.timestamp, self.get_value_as(unit), self.meal.value,
            self.measure_method.value, self.comment)

@attr.s
class KetoneReading:

    timestamp = attr.ib(type=datetime.datetime)
    value = attr.ib(type=float)
    comment = attr.ib(default='', type=str)
    extra_data = attr.ib(factory=dict)

    def as_csv(self, unit):
        """Returns the reading as a formatted comma-separated value string."""
        del unit  # Unused for Ketone readings.

        return '"%s","%.2f","%s","%s"' % (
            self.timestamp, self.value, MeasurementMethod.BLOOD_SAMPLE.value,
            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
    extra_data = attr.ib(factory=dict)

    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.

    Attributes:
      model: Human readable model name, chosen by the driver.
      serial_number: Serial number identified for the reader (or N/A if not
        available in the protocol.)
      version_info: List of strings with any version information available about
        the device. It can include hardware and software version.
      native_unit: One of the Unit values to identify the meter native unit.
    """
    model = attr.ib(type=str)
    serial_number = attr.ib(default='N/A', type=str)
    version_info = attr.ib(default=(), type=Sequence[str])
    native_unit = attr.ib(
        default=Unit.MG_DL, validator=attr.validators.in_(Unit),
        type=Unit)
    patient_name = attr.ib(default=None, type=Optional[str])

    def __str__(self):
        version_information_string = 'N/A'
        if self.version_info:
            version_information_string = '\n    '.join(
                self.version_info).strip()

        base_output = textwrap.dedent("""\
            {model}
            Serial Number: {serial_number}
            Version Information:
                {version_information_string}
            Native Unit: {native_unit}
        """).format(model=self.model, serial_number=self.serial_number,
                    version_information_string=version_information_string,
                    native_unit=self.native_unit.value)

        if self.patient_name != None:
            base_output += 'Patient Name: {patient_name}\n'.format(
                patient_name=self.patient_name)

        return base_output