# Path: braubuddy/thermometer/base.py
# class DeviceError(Exception):
#     """
#     Raised if there is a problem communicating with a thermometer device.
#     """
#     pass
# 
# Path: braubuddy/thermometer/base.py
# class IThermometer(object):
#     """
#     Interface for creating a thermometer for use with :mod:`braudbuddy`.
#     """
#     __metaclass__ = abc.ABCMeta
# 
#     def __init__(self):
#         pass
# 
#     @abc.abstractmethod
#     def get_temperature(self, units='celsius'):
#         """
#         Get thermometer temperature in celsius or farenheit.
# 
#         :param units: Temperature units, 'celsius' (default) or 'farenheit.'
#         :type units: :class:`str`
#         :returns: Thermometer temperature reading
#         :rtype: :class:`float`
#         :raises: :class:`braubuddy.thermometer.ReadError` if temperature can
#             not be read.
#         """
#         pass
# 
# Path: braubuddy/thermometer/ds18b20_gpio.py
# class DS18B20Thermometer(IThermometer):
#     """
#     DS18B20 GPIO anagolgue thermometer manufactured by `Maxim
#     Integrated Products <http://www.maximintegrated.com>`_.
# 
#     :raises: :class:`braubuddy.thermometer.DeviceError` if no DS18B20
#         thermometer devices discovered.
#     """
# 
#     def __init__(self):
#         try:
#             self._ds18b20_device = ds18b20.DS18B20()
#         except Exception as err:
#             msg = 'No DS18B20 devices discovered'
#             raise DeviceError(msg)
# 
#     def get_temperature(self, units='celsius'):
#         try:
#             return self._ds18b20_device.get_temperature(unit=UNIT_MAP[units])
#         except Exception as err:
#             raise ReadError(
#                 'Error reading device temperature: {0}'.format(err))
# 
# Path: braubuddy/thermometer/temper_usb.py
# class TEMPerThermometer(IThermometer):
#     """
#     TEMPer USB Thermometer manufactured by `RDing
#     <http://www.rding-china.com/>`_.
# 
#     :raises: :class:`braubuddy.thermometer.DeviceError` if no TEMPer USB
#         thermometer devices discovered.
#     """
# 
#     def __init__(self):
#         temper_devices = self._get_temper_devices()
#         if len(temper_devices) == 0:
#             msg = 'No TEMPer devices discovered'
#             raise DeviceError(msg)
#         # Use first device if multiple devices discovered
#         self._temper_device = temper_devices[0]
# 
#     def _get_temper_devices(self):
#         """
#         Internal method.
# 
#         Get attached TEMPer devices
# 
#         :returns: list of attached TEMPer devices
#         :rtype: :class:`list` of :class:`temperusb.TemperDevice`
#         """
#         th = temperusb.TemperHandler()
#         temper_devices = th.get_devices()
#         return temper_devices
# 
#     def get_temperature(self, units='celsius'):
#         try:
#             return self._temper_device.get_temperature(format=units)
#         except Exception as err:
#             raise ReadError(
#                 'Error reading device temperature: {0}'.format(err))
# 
# Path: braubuddy/thermometer/dummy.py
# class DummyThermometer(IThermometer):
#     """
#     A dummy thermometer which generates random temperature readings within
#     a defined range. Use for testing.
# 
#     :param `lower_bound`: Lower bound of returned temperature range
#     :type `lower_bound`: :class:`int`
#     :param `upper_bound`: Upper bound of returned temperature range
#     :type `upper_bound`: :class:`int`
#     """
# 
#     def __init__(self, lower_bound=20, upper_bound=30):
#         # Set random range bounds
#         self.lower_bound = lower_bound
#         self.upper_bound = upper_bound
#         super(DummyThermometer, self).__init__()
# 
#     def get_temperature(self, units='celsius'):
#         # Returns random temperature within defined range
#         current_temp = random.randrange(self.lower_bound, self.upper_bound)
#         if units == 'fahrenheit':
#             current_temp = utils.convert_temp_units(
#                 current_temp,
#                 units_from='celsius',
#                 units_to='fahrenheit'
#             )
#         return current_temp


from cherrypy import log
from braubuddy.thermometer import DeviceError
from braubuddy.thermometer import IThermometer
from braubuddy.thermometer import DS18B20Thermometer
from braubuddy.thermometer import TEMPerThermometer
from braubuddy.thermometer import DummyThermometer


class AutoThermometer(IThermometer):
    """
    Attempts to automatically discover a connected thermometer device.

        * If mutliple devices are connected, the first discovered device is
            returned.
        * If no devices are detected, a dummy device is returned.
    """

    def __new__(self):
        """
        Return an instance of the first subclass of
        :class:`braubuddy.thermometer.IThermometer` for which a device is
        discovered.
        """

        log('Auto-discovering Thermometer.')
        try:

# Path: braubuddy/thermometer/base.py
# class DeviceError(Exception):
#     """
#     Raised if there is a problem communicating with a thermometer device.
#     """
#     pass
# 
# Path: braubuddy/thermometer/base.py
# class IThermometer(object):
#     """
#     Interface for creating a thermometer for use with :mod:`braudbuddy`.
#     """
#     __metaclass__ = abc.ABCMeta
# 
#     def __init__(self):
#         pass
# 
#     @abc.abstractmethod
#     def get_temperature(self, units='celsius'):
#         """
#         Get thermometer temperature in celsius or farenheit.
# 
#         :param units: Temperature units, 'celsius' (default) or 'farenheit.'
#         :type units: :class:`str`
#         :returns: Thermometer temperature reading
#         :rtype: :class:`float`
#         :raises: :class:`braubuddy.thermometer.ReadError` if temperature can
#             not be read.
#         """
#         pass
# 
# Path: braubuddy/thermometer/ds18b20_gpio.py
# class DS18B20Thermometer(IThermometer):
#     """
#     DS18B20 GPIO anagolgue thermometer manufactured by `Maxim
#     Integrated Products <http://www.maximintegrated.com>`_.
# 
#     :raises: :class:`braubuddy.thermometer.DeviceError` if no DS18B20
#         thermometer devices discovered.
#     """
# 
#     def __init__(self):
#         try:
#             self._ds18b20_device = ds18b20.DS18B20()
#         except Exception as err:
#             msg = 'No DS18B20 devices discovered'
#             raise DeviceError(msg)
# 
#     def get_temperature(self, units='celsius'):
#         try:
#             return self._ds18b20_device.get_temperature(unit=UNIT_MAP[units])
#         except Exception as err:
#             raise ReadError(
#                 'Error reading device temperature: {0}'.format(err))
# 
# Path: braubuddy/thermometer/temper_usb.py
# class TEMPerThermometer(IThermometer):
#     """
#     TEMPer USB Thermometer manufactured by `RDing
#     <http://www.rding-china.com/>`_.
# 
#     :raises: :class:`braubuddy.thermometer.DeviceError` if no TEMPer USB
#         thermometer devices discovered.
#     """
# 
#     def __init__(self):
#         temper_devices = self._get_temper_devices()
#         if len(temper_devices) == 0:
#             msg = 'No TEMPer devices discovered'
#             raise DeviceError(msg)
#         # Use first device if multiple devices discovered
#         self._temper_device = temper_devices[0]
# 
#     def _get_temper_devices(self):
#         """
#         Internal method.
# 
#         Get attached TEMPer devices
# 
#         :returns: list of attached TEMPer devices
#         :rtype: :class:`list` of :class:`temperusb.TemperDevice`
#         """
#         th = temperusb.TemperHandler()
#         temper_devices = th.get_devices()
#         return temper_devices
# 
#     def get_temperature(self, units='celsius'):
#         try:
#             return self._temper_device.get_temperature(format=units)
#         except Exception as err:
#             raise ReadError(
#                 'Error reading device temperature: {0}'.format(err))
# 
# Path: braubuddy/thermometer/dummy.py
# class DummyThermometer(IThermometer):
#     """
#     A dummy thermometer which generates random temperature readings within
#     a defined range. Use for testing.
# 
#     :param `lower_bound`: Lower bound of returned temperature range
#     :type `lower_bound`: :class:`int`
#     :param `upper_bound`: Upper bound of returned temperature range
#     :type `upper_bound`: :class:`int`
#     """
# 
#     def __init__(self, lower_bound=20, upper_bound=30):
#         # Set random range bounds
#         self.lower_bound = lower_bound
#         self.upper_bound = upper_bound
#         super(DummyThermometer, self).__init__()
# 
#     def get_temperature(self, units='celsius'):
#         # Returns random temperature within defined range
#         current_temp = random.randrange(self.lower_bound, self.upper_bound)
#         if units == 'fahrenheit':
#             current_temp = utils.convert_temp_units(
#                 current_temp,
#                 units_from='celsius',
#                 units_to='fahrenheit'
#             )
#         return current_temp


# Path: braubuddy/thermometer/auto.py
from cherrypy import log
from braubuddy.thermometer import DeviceError
from braubuddy.thermometer import IThermometer
from braubuddy.thermometer import DS18B20Thermometer
from braubuddy.thermometer import TEMPerThermometer
from braubuddy.thermometer import DummyThermometer



class AutoThermometer(IThermometer):
    """
    Attempts to automatically discover a connected thermometer device.

        * If mutliple devices are connected, the first discovered device is
            returned.
        * If no devices are detected, a dummy device is returned.
    """

    def __new__(self):
        """
        Return an instance of the first subclass of
        :class:`braubuddy.thermometer.IThermometer` for which a device is
        discovered.
        """

        log('Auto-discovering Thermometer.')
        try:

            thermometer = DS18B20Thermometer()