def get_glucose_unit(self) -> common.Unit: """Returns a constant representing the unit displayed by the meter. Returns: common.Unit.MG_DL: if the glucometer displays in mg/dL common.Unit.MMOL_L: if the glucometer displays in mmol/L Raises: exceptions.InvalidGlucoseUnit: if the unit is not recognized OneTouch meters will always dump data in mg/dL because that's their internal storage. They will then provide a separate method to read the unit used for display. This is not settable by the user in all modern meters. """ response = self._send_oneliner_command("DMSU?") match = self._GLUCOSE_UNIT_RE.match(response) if match is None: raise exceptions.InvalidGlucoseUnit(response) unit = match.group(1) if unit == "MG/DL ": return common.Unit.MG_DL if unit == "MMOL/L": return common.Unit.MMOL_L raise exceptions.InvalidGlucoseUnit(response)
def convert_glucose_unit(value, from_unit, to_unit=None): """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. Raises: exceptions.InvalidGlucoseUnit: If the parameters are incorrect. """ if from_unit not in VALID_UNITS: raise exceptions.InvalidGlucoseUnit(from_unit) if from_unit == to_unit: return value if to_unit is not None: if to_unit not in VALID_UNITS: raise exceptions.InvalidGlucoseUnit(to_unit) if from_unit is UNIT_MGDL: return round(value / 18.0, 2) else: return round(value * 18.0, 0)
def get_glucose_unit(self): unit_value = self._read_parameter(_PARAMETER_KEY_UNIT) if unit_value == b'\x00\x00\x00\x00': return common.UNIT_MGDL elif unit_value == b'\x01\x00\x00\x00': return common.UNIT_MMOLL else: raise exceptions.InvalidGlucoseUnit('%r' % unit_value)