Exemplo n.º 1
0
class GPIOController(object):

    gpio_off = GPIO.LOW
    gpio_on = GPIO.HIGH
    gpio_led_3_3_v = 22
    gpio_radio_bob_switch = 18
    gpio_hr3_switch = 23
    gpio_free_station_switch = 24

    def __init__(self):
        self.myLogger = MyLogger(self.__class__.__name__)
        self.myLogger.info('Init GPIOController')

        # call GPIOS by Numbers
        try:
            GPIO.setmode(GPIO.BCM)
            GPIO.setup(self.gpio_led_3_3_v, GPIO.OUT)
            GPIO.setup(self.gpio_radio_bob_switch, GPIO.IN)
            GPIO.setup(self.gpio_free_station_switch, GPIO.IN)
            GPIO.setup(self.gpio_hr3_switch, GPIO.IN)
        except:  # catch *all* exceptions
            e = sys.exc_info()[0]
            self.myLogger.error("Error: %s" % e)

    def call(self, gpio_number, gpio_command):
        self.myLogger.debug('Call GPIO %s with %x' %
                            (gpio_number, gpio_command))

        GPIO.output(gpio_number, gpio_command)

    def request(self, gpio_number):
        self.myLogger.debug('Request GPIO %s' % gpio_number)

        return GPIO.input(gpio_number)
Exemplo n.º 2
0
class RadioSwitch(object):

    radio_off = 'radio_off'
    selected_radiostation = 'selected_radiostation'
    hr3 = 'HR3'
    radioBob = 'RadioBob'
    airplay = 'AirPlay'
    selected_volume = 'selected_volume'

    def __init__(self):
        self.myLogger = MyLogger(self.__class__.__name__)
        self.myLogger.info('Init RadioSwitch')

        self.radiostationDict = {
            self.radioBob:
            Radiostation(
                'http://streams.radiobob.de/100/mp3-192/streams.radiobob.de/play.m3u',
                self.radioBob),
            self.hr3:
            Radiostation('http://metafiles.gl-systemhaus.de/hr/hr3_2.m3u',
                         self.hr3)
        }

    def get_radiostation(self, name):
        self.myLogger.debug("get_radiostation(%s)" % name)

        try:
            radiostation = self.radiostationDict[name]
        except KeyError:
            self.myLogger.error("Unknown radiostation: %s." % name)
            return

        return radiostation
Exemplo n.º 3
0
class SwitchWatcherProcess(object):
    def __init__(self):
        self.myLogger = MyLogger(self.__class__.__name__)
        self.myLogger.info('Init SwitchWatcherProcess')
        self.redisServer = redis.Redis(host='localhost', port=6379, db=0)
        self.gpioController = GPIOController()

    def process(self, stop_event):

        while not stop_event.is_set():
            time.sleep(1)

            try:
                gpio_radio_bob = self.gpioController.request(
                    GPIOController.gpio_radio_bob_switch)
                self.myLogger.debug("State of RadioBob switch: %s" %
                                    gpio_radio_bob)

                gpio_radio_hr3 = self.gpioController.request(
                    GPIOController.gpio_hr3_switch)
                self.myLogger.debug("State of HR3 switch: %s" % gpio_radio_hr3)

                gpio_free_station = self.gpioController.request(
                    GPIOController.gpio_free_station_switch)
                self.myLogger.debug("State of free station switch: %s" %
                                    gpio_free_station)

                if gpio_radio_bob == 1:
                    self.myLogger.debug("Switch to %s" % RadioSwitch.radioBob)
                    self.redisServer.set(RadioSwitch.selected_radiostation,
                                         RadioSwitch.radioBob)
                    continue

                if gpio_radio_hr3 == 1:
                    self.myLogger.debug("Switch to %s" % RadioSwitch.hr3)
                    self.redisServer.set(RadioSwitch.selected_radiostation,
                                         RadioSwitch.hr3)
                    continue

                if gpio_free_station == 1:
                    self.myLogger.debug("Switch to %s" % RadioSwitch.airplay)
                    self.redisServer.set(RadioSwitch.selected_radiostation,
                                         RadioSwitch.airplay)
                    continue

                self.myLogger.debug("No switch is pressed.")
                self.redisServer.set(RadioSwitch.selected_radiostation,
                                     RadioSwitch.radio_off)

            except:  # catch *all* exceptions
                e = sys.exc_info()[0]
                self.myLogger.error("Error: %s" % e)

        self.myLogger.info("Stop event is set. Stop %s" %
                           self.__class__.__name__)
Exemplo n.º 4
0
class LightProcess(object):
    def __init__(self):
        self.myLogger = MyLogger(self.__class__.__name__)
        self.myLogger.info('Init LightProcess')
        self.redisServer = redis.Redis(host='localhost', port=6379, db=0)
        self.gpioController = GPIOController()

    def process(self, stop_event):
        # init with start value
        current_radiostation_name = RadioSwitch.radio_off
        current_light_state = False
        self.gpioController.call(GPIOController.gpio_led_3_3_v,
                                 GPIOController.gpio_off)

        while not stop_event.is_set():
            time.sleep(1)

            try:
                self.myLogger.debug("Check selected radiostation")
                selected_radiostation_name = self.redisServer.get(
                    RadioSwitch.selected_radiostation)
                radiostation_name_decoded = selected_radiostation_name.decode(
                    'utf-8')

                turn_light_on = False
                if radiostation_name_decoded != RadioSwitch.radio_off:
                    turn_light_on = True

                if current_light_state == turn_light_on:
                    continue

                if turn_light_on:
                    self.myLogger.info("Switch on light and turn amp on")
                    self.gpioController.call(GPIOController.gpio_led_3_3_v,
                                             GPIOController.gpio_on)
                else:
                    self.myLogger.info("Switch off light and turn amp off")
                    self.gpioController.call(GPIOController.gpio_led_3_3_v,
                                             GPIOController.gpio_off)

                current_light_state = turn_light_on
                current_radiostation_name = radiostation_name_decoded

            except:  # catch *all* exceptions
                e = sys.exc_info()[0]
                self.myLogger.error("Error: %s" % e)

        self.myLogger.info("Stop event is set. Stop %s" %
                           self.__class__.__name__)
        self.gpioController.call(GPIOController.gpio_led_3_3_v,
                                 GPIOController.gpio_off)
Exemplo n.º 5
0
class DryModeProcess(object):
    def __init__(self):
        self.myLogger = MyLogger(self.__class__.__name__)
        self.myLogger.info('Init DryModeProcess')
        self.redisServer = redis.Redis(host='localhost', port=6379, db=0)

    def process(self, stop_event):
        count = 0

        # loop a couple of times for simulation
        while count < 5 and not stop_event.is_set():
            count += 1

            time.sleep(5)

            try:
                self.myLogger.info('Starte RadioBob')
                self.redisServer.set(RadioSwitch.selected_radiostation,
                                     RadioSwitch.radioBob)

                time.sleep(10)

                self.myLogger.info('Starte hr3')
                self.redisServer.set(RadioSwitch.selected_radiostation,
                                     RadioSwitch.hr3)

                time.sleep(10)

            except:  # catch *all* exceptions
                self.redisServer.set(RadioSwitch.selected_radiostation,
                                     RadioSwitch.radio_off)
                self.myLogger.debug(
                    "Set selected radiostation on default %s." %
                    RadioSwitch.radio_off)

                e = sys.exc_info()[0]
                self.myLogger.error("Error: %s" % e)

        self.myLogger.info("Stop event is set. Stop dry mode process.")
Exemplo n.º 6
0
class RadioProcess(object):
    def __init__(self):
        self.myLogger = MyLogger(self.__class__.__name__)
        self.myLogger.info('Init RadioProcess')
        self.radioSwitch = RadioSwitch()
        self.redisServer = redis.Redis(host='localhost', port=6379, db=0)

        # nur zum initialisieren. Todo: huebsch machen.
        self.current_radiostation = self.radioSwitch.get_radiostation(
            RadioSwitch.radioBob)

    def process(self, stop_event):
        current_radiostation_name = RadioSwitch.radio_off
        current_volume = 0

        while not stop_event.is_set():
            time.sleep(1)

            try:
                selected_radiostation_name = self.redisServer.get(
                    RadioSwitch.selected_radiostation).decode('utf-8')

                self.myLogger.debug('Selected Radiostation: %s.' %
                                    selected_radiostation_name)
                self.myLogger.debug('Current Radiostation: %s.' %
                                    current_radiostation_name)

                selected_volume = int(
                    self.redisServer.get(
                        RadioSwitch.selected_volume).decode('utf-8'))

                if current_volume != selected_volume:
                    self.myLogger.info('Change Volume: %s.' % selected_volume)
                    self.current_radiostation.set_volume(selected_volume)
                    current_volume = selected_volume

                if current_radiostation_name != selected_radiostation_name:

                    if selected_radiostation_name == RadioSwitch.radio_off or selected_radiostation_name == RadioSwitch.airplay:
                        self.myLogger.info("Stop Radio. Selected station %s." %
                                           selected_radiostation_name)
                        self.current_radiostation.stop()
                        self.myLogger.debug("Stop current radiostation %s ." %
                                            self.current_radiostation.name)
                    else:
                        self.myLogger.info("Change Radiostation to %s." %
                                           selected_radiostation_name)

                        self.myLogger.debug("Stop current radiostation %s ." %
                                            self.current_radiostation.name)
                        self.current_radiostation.stop()

                        self.current_radiostation = self.radioSwitch.get_radiostation(
                            selected_radiostation_name)

                        self.myLogger.debug("Play selected radiostation %s ." %
                                            self.current_radiostation.name)
                        self.current_radiostation.play()

                    current_radiostation_name = selected_radiostation_name

            except:  # catch *all* exceptions
                self.redisServer.set(RadioSwitch.selected_radiostation,
                                     RadioSwitch.radio_off)
                self.myLogger.debug(
                    "Set selected radiostation on default %s." %
                    RadioSwitch.radio_off)

                e = sys.exc_info()[0]
                self.myLogger.error("Error: %s" % e)

        self.myLogger.info("Stop event is set. Stop %s" %
                           self.__class__.__name__)
        self.current_radiostation.stop()
Exemplo n.º 7
0
class VolumeWatcherProcess(object):
    def __init__(self):
        self.myLogger = MyLogger(self.__class__.__name__)
        self.myLogger.info('Init VolumeWatcherProcess')
        self.redisServer = redis.Redis(host='localhost', port=6379, db=0)
        self.mcp3008Controller = MCP3008Controller()

    def process(self, stop_event):

        while not stop_event.is_set():
            time.sleep(1)

            try:
                value = self.mcp3008Controller.read(channel=7)
                self.myLogger.debug("voltage current: %.2f" % (value))

                if value > 4000:
                    self.redisServer.set(RadioSwitch.selected_volume, 100)
                    continue

                if value > 3000:
                    self.redisServer.set(RadioSwitch.selected_volume, 90)
                    continue

                if value > 2000:
                    self.redisServer.set(RadioSwitch.selected_volume, 80)
                    continue

                if value > 1500:
                    self.redisServer.set(RadioSwitch.selected_volume, 70)
                    continue

                if value > 800:
                    self.redisServer.set(RadioSwitch.selected_volume, 60)
                    continue

                if value > 500:
                    self.redisServer.set(RadioSwitch.selected_volume, 50)
                    continue

                if value > 300:
                    self.redisServer.set(RadioSwitch.selected_volume, 40)
                    continue

                if value > 100:
                    self.redisServer.set(RadioSwitch.selected_volume, 30)
                    continue

                if value > 50:
                    self.redisServer.set(RadioSwitch.selected_volume, 20)
                    continue

                if value > 10:
                    self.redisServer.set(RadioSwitch.selected_volume, 10)
                    continue

                if value < 10:
                    self.redisServer.set(RadioSwitch.selected_volume, 0)
                    continue

            except:  # catch *all* exceptions
                e = sys.exc_info()[0]
                self.myLogger.error("Error: %s" % e)

        self.myLogger.info("Stop event is set. Stop %s" %
                           self.__class__.__name__)