def __get_sensor_temperature(self, serial): """ Private method used to retrieve temperature from real sensor """ logging.debug("Reading temperature from DS18B20 sensor " "with Serial [{0}]".format(serial)) ds18b20Sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, serial) logging.debug("Instantiated DS18B20 temperature sensor " "with Serial [{0}]".format(serial)) if DEFAULT_TEMPERATURE_UNIT == TEMPERATURE_ENUM.degC: temperature = ds18b20Sensor.get_temperature(W1ThermSensor.DEGREES_C) elif DEFAULT_TEMPERATURE_UNIT == TEMPERATURE_ENUM.degK: degF_temperature = ds18b20Sensor.get_temperature(W1ThermSensor.DEGREES_F) # sensor only supports Celsius and Fahrenheit. # We should therefore use pint to convert to Kelvin logging.debug( "Kevin unit is not supporte by sensor " "with serial [{0}]. Using pint to " "convert [{1}] to degK".format(serial, degF_temperature) ) temperature = convert_unit( UNIT_TYPES_ENUM.temperature, TEMPERATURE_ENUM.degF, temperature, TEMPERATURE_ENUM.degK ) elif DEFAULT_TEMPERATURE_UNIT == TEMPERATURE_ENUM.degF: temperature = ds18b20Sensor.get_temperature(W1ThermSensor.DEGREES_F) else: # should never get here (sanity only). raise Exception("Invalid [{0}]".format(DEFAULT_TEMPERATURE_UNIT)) return temperature
def convert(from_value, from_unit, to_unit): """convert length units. Parameters ---------- from_value: str (required) numeric value from_unit: str (required) length unit to_unit: str (required) length unit Returns ------- float: the converted value """ if not is_number(from_value): raise IllegalArgumentException( ("Parameter from_value=[{0}] not valid. " "A numeric value must be provided.").format(from_value)) if is_blank(from_unit): raise IllegalArgumentException( ("Parameter from_unit=[{0}] not valid. A unit be provided.") .format(from_unit)) if is_blank(to_unit): raise IllegalArgumentException( ("Parameter to_unit=[{0}] not valid. A unit be provided.") .format(to_unit)) if from_unit == to_unit: raise IllegalArgumentException( ("from_unit=[{0}] and to_unit=[{1}] units cannot be equal") .format(from_unit, to_unit)) # pint temperature units need to be lower-cased or degC, degF, degK from_unit = from_unit.lower().strip() if from_unit == "degc": from_unit = "degC" elif from_unit == "degf": from_unit = "degF" elif from_unit == "degk": from_unit = "degK" # pint temperature units need to be lower-cased or degC, degF, degK to_unit = to_unit.lower().strip() if to_unit == "degc": to_unit = "degC" elif to_unit == "degf": to_unit = "degF" elif to_unit == "degk": to_unit = "degK" result = convert_unit(UNIT_TYPES_ENUM.temperature, from_unit, from_value, to_unit) return result
def convert(from_value, from_unit, to_unit): """convert length units. Parameters ---------- from_value: float (required) numeric value from_unit: str (required) length unit to_unit: str (required) length unit Returns ------- float: the converted value """ if not is_number(from_value): raise IllegalArgumentException( ("Parameter from_value=[{0}] not valid. " "A numeric value must be provided.").format(from_value)) if is_blank(from_unit): raise IllegalArgumentException( ("Parameter from_unit=[{0}] not valid. A unit be provided.") .format(from_unit)) if is_blank(to_unit): raise IllegalArgumentException( ("Parameter to_unit=[{0}] not valid. A unit be provided.") .format(to_unit)) if from_unit == to_unit: raise IllegalArgumentException( ("from_unit=[{0}] and to_unit=[{1}] units cannot be equal") .format(from_unit, to_unit)) # pint unit registry only accepts lower case letters for length units from_unit = from_unit.lower().strip() to_unit = to_unit.lower().strip() result = convert_unit(UNIT_TYPES_ENUM.length, from_unit, from_value, to_unit) return result