示例#1
0
class NanoleafController(BaseController):
    """
    This class controls the Nanoleaf lights
    """
    receiving_events = False
    touch_event_dict = {
        0: 'push',
        2: 'up',
        3: 'down',
        4: 'left',
        5: 'right',
    }

    def __init__(self):
        super(NanoleafController, self).__init__()
        self.device = Nanoleaf(ip=config.globals.devices.nanoleaf.ip,
                               auth_token=os.environ["nanoleaf_token"])
        logger.info("Nanoleaf controller has been properly set-up")
        self.events = []

    def get_scene(self):
        return self.device.get_current_effect()

    def set_scene(self, scene):
        self.device.set_effect(scene)

    def power_off(self):
        self.device.power_off()

    def power_on(self):
        self.device.power_on()

    def add_touch_event_listener(self):
        self.receiving_events = True
        self.device.register_event(self.store_event(), event_types=[4])

    def store_event(self):
        def register_event(event):
            self.events.append(event)

        return register_event
class TestApp(App):
    def connect_nanoleaf(self, *args):
        try:
            self.nl = Nanoleaf('192.168.0.108')
            auth_token = 'authentication token'  #Real token was removed by authors for protecting security
            self.nl = Nanoleaf('192.168.0.108', auth_token)
        except Exception:
            content = GridLayout(cols=1)
            content.add_widget(Label(text='No valid nanoleaf device found'))
            content_admit = Button(text='OK')
            content.add_widget(content_admit)
            popup = Popup(title='Message', content=content, auto_dismiss=False)
            content_admit.bind(on_release=popup.dismiss)
            popup.open()

    def power_on(self, *args):
        try:
            self.nl.power_on()
        except AttributeError:
            content = GridLayout(cols=1)
            content.add_widget(
                Label(text='Please connect to nanoleaf at first'))
            content_admit = Button(text='OK')
            content.add_widget(content_admit)
            popup = Popup(title='Message', content=content, auto_dismiss=False)
            content_admit.bind(on_release=popup.dismiss)
            popup.open()

    def power_off(self, *args):
        try:
            self.nl.power_off()
        except AttributeError:
            content = GridLayout(cols=1)
            content.add_widget(
                Label(text='Please connect to nanoleaf at first'))
            content_admit = Button(text='OK')
            content.add_widget(content_admit)
            popup = Popup(title='Message', content=content, auto_dismiss=False)
            content_admit.bind(on_release=popup.dismiss)
            popup.open()

    def turn_up(self, *args):
        try:
            if self.nl.get_power() == True:
                self.nl.increment_brightness(10)
        except AttributeError:
            content = GridLayout(cols=1)
            content.add_widget(
                Label(text='Please connect to nanoleaf at first'))
            content_admit = Button(text='OK')
            content.add_widget(content_admit)
            popup = Popup(title='Message', content=content, auto_dismiss=False)
            content_admit.bind(on_release=popup.dismiss)
            popup.open()

    def turn_down(self, *args):
        try:
            if self.nl.get_power() == True:
                if self.nl.get_brightness() > 0:
                    self.nl.set_brightness(self.nl.get_brightness() - 10)
                else:
                    self.nl.set_brightness(0)
        except AttributeError:
            content = GridLayout(cols=1)
            content.add_widget(
                Label(text='Please connect to nanoleaf at first'))
            content_admit = Button(text='OK')
            content.add_widget(content_admit)
            popup = Popup(title='Message', content=content, auto_dismiss=False)
            content_admit.bind(on_release=popup.dismiss)
            popup.open()

    def set_color(self, *args):
        try:
            if self.nl.get_power() == True:
                content = GridLayout(cols=2)
                content_admit = Button(text='OK')
                content_cancel = Button(text='Cancel')
                self.content_input = TextInput(multiline=False)
                content.add_widget(Label(text='Please input color in R,G,B:'))
                content.add_widget(self.content_input)
                content.add_widget(content_admit)
                content.add_widget(content_cancel)
                popup = Popup(title='Message',
                              content=content,
                              auto_dismiss=False)
                content_cancel.bind(on_release=popup.dismiss)
                content_admit.bind(on_release=self.pressed)
                popup.open()
        except AttributeError:
            content = GridLayout(cols=1)
            content.add_widget(
                Label(text='Please connect to nanoleaf at first'))
            content_admit = Button(text='OK')
            content.add_widget(content_admit)
            popup = Popup(title='Message', content=content, auto_dismiss=False)
            content_admit.bind(on_release=popup.dismiss)
            popup.open()

    def pressed(self, instance):
        color = self.content_input.text
        try:
            if color == 'RED':
                self.nl.set_color(RED)
            elif color == 'ORANGE':
                self.nl.set_color(ORANGE)
            elif color == 'YELLOW':
                self.nl.set_color(YELLOW)
            elif color == 'BLUE':
                self.nl.set_color(BLUE)
            elif color == 'GREEN':
                self.nl.set_color(GREEN)
            elif color == 'WHITE':
                self.nl.set_color(WHITE)
            elif color == 'LIGHT_BLUE':
                self.nl.set_color(LIGHT_BLUE)
            elif color == 'PINK':
                self.nl.set_color(PINK)
            elif color == 'PURPLE':
                self.nl.set_color(PURPLE)
            elif type(int(color.split(',')[0])) == int and type(int(color.split(',')[1])) == int and type(
                    int(color.split(',')[2])) == int and color.count(',') == 2 and \
                    (0 <= int(color.split(',')[0]) <= 255) == True and (
                    0 <= int(color.split(',')[1]) <= 255) == True and (0 <= int(color.split(',')[2]) <= 255) == True:
                self.nl.set_color(
                    (int(color.split(',')[0]), int(color.split(',')[1]),
                     int(color.split(',')[2])))

        except ValueError:
            content = GridLayout(cols=1)
            content.add_widget(Label(text='Please input valid numbers'))
            content_cancel = Button(text='OK')
            content.add_widget(content_cancel)
            popup = Popup(title='Warning', content=content, auto_dismiss=False)
            content_cancel.bind(on_release=popup.dismiss)
            popup.open()
        except IndexError:
            content = GridLayout(cols=1)
            content.add_widget(Label(text='Please input valid numbers'))
            content_cancel = Button(text='OK')
            content.add_widget(content_cancel)
            popup = Popup(title='Warning', content=content, auto_dismiss=False)
            content_cancel.bind(on_release=popup.dismiss)
            popup.open()

    def flash(self, *args):
        try:
            self.nl.identify()
        except AttributeError:
            content = GridLayout(cols=1)
            content.add_widget(
                Label(text='Please connect to nanoleaf at first'))
            content_admit = Button(text='OK')
            content.add_widget(content_admit)
            popup = Popup(title='Message', content=content, auto_dismiss=False)
            content_admit.bind(on_release=popup.dismiss)
            popup.open()

    def random_color(self, *args):
        try:
            if self.nl.get_power() == True:
                R = random.randint(0, 255)
                G = random.randint(0, 255)
                B = random.randint(0, 255)
                self.nl.set_color((R, G, B))
        except AttributeError:
            content = GridLayout(cols=1)
            content.add_widget(
                Label(text='Please connect to nanoleaf at first'))
            content_admit = Button(text='OK')
            content.add_widget(content_admit)
            popup = Popup(title='Message', content=content, auto_dismiss=False)
            content_admit.bind(on_release=popup.dismiss)
            popup.open()

    def on_value2(self, brightness):
        try:
            self.brightnessValue = "%d" % brightness
            self.nl.set_brightness(int(self.brightnessValue))
        except AttributeError:
            content = GridLayout(cols=1)
            content.add_widget(
                Label(text='Please connect to nanoleaf at first'))
            content_admit = Button(text='OK')
            content.add_widget(content_admit)
            popup = Popup(title='Message', content=content, auto_dismiss=False)
            content_admit.bind(on_release=popup.dismiss)
            popup.open()

    def on_value1(self, hue):
        try:
            self.hueValue = "%d" % hue
            self.nl.set_hue(int(self.hueValue))
        except AttributeError:
            content = GridLayout(cols=1)
            content.add_widget(
                Label(text='Please connect to nanoleaf at first'))
            content_admit = Button(text='OK')
            content.add_widget(content_admit)
            popup = Popup(title='Message', content=content, auto_dismiss=False)
            content_admit.bind(on_release=popup.dismiss)
            popup.open()

    def weatherControl(self):
        # self.state=active
        try:
            if self.aV1 == True and self.nl.get_power() == True:
                url = 'http://api.openweathermap.org/data/2.5/weather?id=5946768&appid=authentication token'  #Real token was removed by authors for protecting security
                json_data = requests.get(url).json()
                condition = json_data['weather'][0]['main']
                '''content=GridLayout(cols=1)
                content_admit=Button(text='OK')
                content.add_widget(Label(text='Weather is: '+condition))
                content.add_widget(content_admit)
                popup = Popup(title='Message',content=content,auto_dismiss=False)
                content_admit.bind(on_release=popup.dismiss)
                popup.open()'''
                if (condition == "Clear"):
                    self.sunny()
                elif (condition == "Clouds"):
                    self.cloudy()
                elif (condition == "Snow"):
                    self.snowy()
                elif (condition == "Rain"):
                    self.rainy()
                else:
                    pass
                t = Timer(5, self.weatherControl)
                t.start()
            else:
                t = Timer(5, self.weatherControl)
                t.cancel()

        except AttributeError:
            content = GridLayout(cols=1)
            content.add_widget(
                Label(text='Please connect to nanoleaf at first'))
            content_admit = Button(text='OK')
            content.add_widget(content_admit)
            popup = Popup(title='Message', content=content, auto_dismiss=False)
            content_admit.bind(on_release=popup.dismiss)
            popup.open()

    def sunControl(self):
        try:
            if self.aV3 == True:
                t1 = datetime.datetime.now()
                if t1.hour >= 8 and t1.hour <= 16:
                    self.nl.power_off()

                elif t1.hour == 7:
                    if t1.minute >= 40:
                        self.nl.power_off()
                    else:
                        self.nl.power_on()

                elif t1.hour == 17:
                    if t1.minute <= 45:
                        self.nl.power_off()
                    else:
                        self.nl.power_on()

                else:
                    self.nl.power_on()
                t = Timer(5, self.sunControl)
                t.start()

            else:
                # self.nl.power_off()
                t = Timer(5, self.sunControl)
                t.cancel()

        except AttributeError:
            content = GridLayout(cols=1)
            content.add_widget(
                Label(text='Please connect to nanoleaf at first'))
            content_admit = Button(text='OK')
            content.add_widget(content_admit)
            popup = Popup(title='Message', content=content, auto_dismiss=False)
            content_admit.bind(on_release=popup.dismiss)
            popup.open()

    def activeValue1(self, active):
        self.aV1 = active
        self.weatherControl()

    def activeValue2(self, active):
        self.aV2 = active
        self.tempControl()

    def activeValue3(self, active):
        self.aV3 = active
        self.sunControl()

    def activeValue4(self, active):
        self.aV4 = active
        self.sunLab()

    def activeValue5(self, active):
        self.aV5 = active
        self.sunBed()

    def activeValue6(self, active):
        self.aV6 = active
        self.sunLiving()

    def activeValue7(self, active):
        self.aV7 = active
        self.sunWhite()

    def activeValue8(self, active):
        self.aV8 = active
        self.weatherLab()

    def activeValue9(self, active):
        self.aV9 = active
        self.weatherBed()

    def activeValue10(self, active):
        self.aV10 = active
        self.weatherLiving()

    def activeValue11(self, active):
        self.aV11 = active
        self.weatherWhite()

    def activeValue12(self, active):
        self.aV12 = active
        self.geofencinglab()

    def activeValue13(self, active):
        self.aV13 = active
        self.geofencingBed()

    def activeValue14(self, active):
        self.aV14 = active
        self.geofencingLiving()

    def activeValue15(self, active):
        self.aV15 = active
        self.geofencingWhite()

    def activeValue16(self, active):
        self.aV16 = active
        self.musiclab()

    def activeValue17(self, active):
        self.aV17 = active
        self.labscreen()

    def activeValue18(self, active):
        self.aV18 = active
        self.musicbed()

    def activeValue19(self, active):
        self.aV19 = active
        self.bedscreen()

    def activeValue20(self, active):
        self.aV20 = active
        self.musicliving()

    def activeValue22(self, active):
        self.aV22 = active
        self.musicwhite()

    def activeValue23(self, active):
        self.aV23 = active
        self.on_checkbox_active()

    def musicbed(self):
        if self.aV18 == True:
            import lightningstrike

    def musiclab(self):
        if self.aV16 == True:
            import lightningstrike

    def labscreen(self):
        if self.aV17 == True:
            import screen

    def bedscreen(self):
        if self.aV19 == True:
            import screen

    def musicliving(self):
        if self.aV20 == True:
            import lightningstrike

    def musicwhite(self):
        if self.aV22 == True:
            import lightningstrike

    def geofencinglab(self):
        if self.aV12 == True:
            from geofencing import src
            import app

    def geofencingBed(self):
        if self.aV13 == True:
            from geofencing import src
            import app

    def geofencingLiving(self):
        if self.aV14 == True:
            from geofencing import src
            import app

    def geofencingWhite(self):
        if self.aV15 == True:
            from geofencing import src
            import app

    def sunLab(self):
        if self.aV4 == True:
            t1 = datetime.datetime.now()
            if t1.hour >= 8 and t1.hour <= 16:
                self.b.set_light(2, 'on', False)

            elif t1.hour == 7:
                if t1.minute >= 40:
                    self.b.set_light(2, 'on', False)
                else:
                    self.b.set_light(2, 'on', True)

            elif t1.hour == 17:
                if t1.minute <= 45:
                    self.b.set_light(2, 'on', False)
                else:
                    self.b.set_light(2, 'on', True)

            else:
                self.b.set_light(2, 'on', True)
            t = Timer(5, self.sunLab)
            t.start()

        else:

            t = Timer(5, self.sunLab)
            t.cancel()

    def weatherLab(self):
        if self.aV8 == True:
            import weather
            t = Timer(5, self.weatherLab)
            t.start()

        else:
            from phue import Bridge
            b = Bridge('192.168.0.13')
            b.set_light(2, 'on', False)

            t = Timer(5, self.weatherLab)
            t.cancel()

    def weatherBed(self):
        if self.aV9 == True:
            import weather
            t = Timer(5, self.weatherBed)
            t.start()

        else:

            from phue import Bridge
            b = Bridge('192.168.0.13')
            b.set_light(1, 'on', False)

            t = Timer(5, self.weatherLab)
            t.cancel()

            t = Timer(5, self.weatherBed)
            t.cancel()

    def weatherLiving(self):
        if self.aV10 == True:
            import weather
            t = Timer(5, self.weatherLiving)
            t.start()

        else:

            t = Timer(5, self.weatherLiving)
            t.cancel()

    def weatherWhite(self):
        if self.aV11 == True:
            import weather
            t = Timer(5, self.weatherWhite)
            t.start()

        else:

            t = Timer(5, self.weatherWhite)
            t.cancel()

    def sunBed(self):
        if self.aV5 == True:
            t1 = datetime.datetime.now()
            if t1.hour >= 8 and t1.hour <= 16:
                self.b.set_light(1, 'on', False)

            elif t1.hour == 7:
                if t1.minute >= 40:
                    self.b.set_light(1, 'on', False)
                else:
                    self.b.set_light(1, 'on', True)

            elif t1.hour == 17:
                if t1.minute <= 45:
                    self.b.set_light(1, 'on', False)
                else:
                    self.b.set_light(1, 'on', True)

            else:
                self.b.set_light(1, 'on', True)
            t = Timer(5, self.sunBed)
            t.start()

        else:

            t = Timer(5, self.sunBed)
            t.cancel()

    def sunLiving(self):
        if self.aV6 == True:
            t1 = datetime.datetime.now()
            if t1.hour >= 8 and t1.hour <= 16:
                self.b.set_light(3, 'on', False)

            elif t1.hour == 7:
                if t1.minute >= 40:
                    self.b.set_light(3, 'on', False)
                else:
                    self.b.set_light(3, 'on', True)

            elif t1.hour == 17:
                if t1.minute <= 45:
                    self.b.set_light(3, 'on', False)
                else:
                    self.b.set_light(3, 'on', True)

            else:
                self.b.set_light(3, 'on', True)
            t = Timer(5, self.sunLiving)
            t.start()

        else:

            t = Timer(5, self.sunLiving)
            t.cancel()

    def sunWhite(self):
        if self.aV7 == True:
            t1 = datetime.datetime.now()
            if t1.hour >= 8 and t1.hour <= 16:
                self.b.set_light(4, 'on', False)

            elif t1.hour == 7:
                if t1.minute >= 40:
                    self.b.set_light(4, 'on', False)
                else:
                    self.b.set_light(4, 'on', True)

            elif t1.hour == 17:
                if t1.minute <= 45:
                    self.b.set_light(4, 'on', False)
                else:
                    self.b.set_light(4, 'on', True)

            else:
                self.b.set_light(4, 'on', True)
            t = Timer(5, self.sunWhite)
            t.start()

        else:

            t = Timer(5, self.sunWhite)
            t.cancel()

    def snowy(self):
        self.nl.set_color(WHITE)
        return

    def rainy(self):
        self.nl.set_color((30, 144, 255))
        return

    def sunny(self):
        self.nl.set_color((ORANGE))
        return

    def cloudy(self):
        self.nl.set_color((135, 206, 235))
        return

    def tempControl(self):
        try:
            if self.aV2 == True and self.nl.get_power() == True:
                url = 'http://api.openweathermap.org/data/2.5/weather?id=5946768&appid=authentication token'  #Real token was removed by authors for protecting security
                json_data = requests.get(url).json()
                temp = json_data['main']['temp']
                cel = float(temp) - 273.15
                '''content=GridLayout(cols=1)
                content_admit=Button(text='OK')
                content.add_widget(Label(text='Temperature is: '+str(cel)+'°C'))
                content.add_widget(content_admit)
                popup = Popup(title='Message',content=content,auto_dismiss=False)
                content_admit.bind(on_release=popup.dismiss)
                popup.open()'''
                if (temp >= 303.15):
                    self.extreme()
                elif (temp >= 293.15 and temp < 303.15):
                    self.hot()
                elif (temp >= 283.15 and temp < 293.15):
                    self.medium()
                elif (temp >= 273.15 and temp < 283.15):
                    self.chilly()
                elif (temp >= 263.15 and temp < 273.15):
                    self.cold()
                elif (temp >= 253.15 and temp < 263.15):
                    self.freezing()
                else:
                    pass
                t = Timer(5, self.tempControl)
                t.start()
            else:
                t = Timer(5, self.tempControl)
                t.cancel()
        except AttributeError:
            content = GridLayout(cols=1)
            content.add_widget(
                Label(text='Please connect to nanoleaf at first'))
            content_admit = Button(text='OK')
            content.add_widget(content_admit)
            popup = Popup(title='Message', content=content, auto_dismiss=False)
            content_admit.bind(on_release=popup.dismiss)
            popup.open()

    def extreme(self):
        self.nl.set_color(RED)
        return

    def hot(self):
        self.nl.set_color(ORANGE)
        return

    def medium(self):
        self.nl.set_color(YELLOW)
        return

    def chilly(self):
        self.nl.set_color(GREEN)
        return

    def cold(self):
        self.nl.set_color((0, 255, 255))
        return

    def freezing(self):
        self.nl.set_color(BLUE)
        return

    def on_value3(self, brightness):
        self.labValue = "%d" % brightness
        self.b.set_light(2, 'bri', int(self.labValue))

    def on_value4(self, brightness):
        self.bedValue = "%d" % brightness
        self.b.set_light(1, 'bri', int(self.bedValue))

    def on_value5(self, brightness):
        self.livingValue = "%d" % brightness
        self.b.set_light(3, 'bri', int(self.livingValue))

    def on_value6(self, brightness):
        self.whiteValue = "%d" % brightness
        self.b.set_light(4, 'bri', int(self.whiteValue))

    def lab_on(self, *args):
        try:
            self.b.set_light(2, 'on', True)
        except phue.PhueRequestTimeout:
            content = GridLayout(cols=1)
            content.add_widget(
                Label(text='Please connect to philips  at first'))
            content_admit = Button(text='OK')
            content.add_widget(content_admit)
            popup = Popup(title='Message', content=content, auto_dismiss=False)
            content_admit.bind(on_release=popup.dismiss)
            popup.open()

    def lab_off(self, *args):
        self.b.set_light(2, 'on', False)

    def lab_up(self, *args):
        brightness = self.b.get_light(2, 'bri')
        if self.b.get_light(2, 'on') == True and brightness < 238:
            self.b.set_light(2, 'bri', brightness + 17)

    def lab_down(self, *args):
        brightness = self.b.get_light(2, 'bri')
        if self.b.get_light(2, 'on') == True and brightness > 17:
            self.b.set_light(2, 'bri', brightness - 17)

    def bed_on(self, *args):
        self.b.set_light(1, 'on', True)

    def bed_off(self, *args):
        self.b.set_light(1, 'on', False)

    def bed_up(self, *args):
        brightness = self.b.get_light(1, 'bri')
        if self.b.get_light(1, 'on') == True and brightness < 238:
            self.b.set_light(1, 'bri', brightness + 17)

    def bed_down(self, *args):
        brightness = self.b.get_light(1, 'bri')
        if self.b.get_light(1, 'on') == True and brightness > 17:
            self.b.set_light(1, 'bri', brightness - 17)

    def living_on(self, *args):
        self.b.set_light(3, 'on', True)

    def living_off(self, *args):
        self.b.set_light(3, 'on', False)

    def living_up(self, *args):
        brightness = self.b.get_light(1, 'bri')
        if self.b.get_light(3, 'on') == True and brightness < 238:
            self.b.set_light(3, 'bri', brightness + 17)

    def living_down(self, *args):
        brightness = self.b.get_light(3, 'bri')
        if self.b.get_light(3, 'on') == True and brightness > 17:
            self.b.set_light(3, 'bri', brightness - 17)

    def white_on(self, *args):
        self.b.set_light(4, 'on', True)

    def white_off(self, *args):
        self.b.set_light(4, 'on', False)

    def white_up(self, *args):
        brightness = self.b.get_light(4, 'bri')
        if self.b.get_light(4, 'on') == True and brightness < 238:
            self.b.set_light(4, 'bri', brightness + 17)

    def white_down(self, *args):
        brightness = self.b.get_light(4, 'bri')
        if self.b.get_light(4, 'on') == True and brightness > 17:
            self.b.set_light(4, 'bri', brightness - 17)

    def connect_hue(self, *args):
        self.b = Bridge('192.168.0.13')

    def callback1(self, *args):
        self.disabled = False

    def build(self):
        return Builder.load_string(kv)