Ejemplo n.º 1
0
    def update_details(self):
        self.details_container.clear_widgets()
        self.settings_widgets = []
        connected_color = [.5, .5, .5, 1]
        connected_text = "not connected"
        if self.device.connected:
            connected_color = [0, 1, 0, 1]
            connected_text = "connected"
        status_row = BoxLayout()
        connected_button = Button(
            text=connected_text,
            background_color=connected_color,
            height=30,
            size_hint_y=None,
        )
        connected_button.bind(
            on_press=lambda widget: self.app.update_devices())
        remove_button = Button(text="clear", height=30, size_hint_y=None)
        remove_button.bind(
            on_press=lambda widget: [self.parent.remove_widget(self)])
        status_row.add_widget(connected_button)
        status_row.add_widget(remove_button)
        self.details_container.add_widget(status_row)

        # device information
        for k, v in self.device.details.items():
            row = BoxLayout(height=30, size_hint_y=None)
            key = Label(text=str(k))
            value = Label(text=str(v))
            row.add_widget(key)
            row.add_widget(value)
            self.details_container.add_widget(row)

        # add adjustable values from the db
        # the db material is generated from xml
        # see enn-db and reference.xml
        try:
            # incorrect keys will be stored/reloaded from xml
            if "scripts" not in self.device.details:
                self.device.details["scripts"] = redis_conn.hget(
                    "device:script_lookup", self.device.details["name"])

            reference = redis_conn.hgetall("scripts:{}".format(
                self.device.details["scripts"]))
            for attribute, _ in reference.items():
                row = BoxLayout(height=30, size_hint_y=None)
                key = Label(text=str(attribute))
                value = TextInput(multiline=False)
                try:
                    value.text = self.device.settings[attribute]
                except KeyError as ex:
                    print(ex)
                    pass
                value.bind(
                    on_text_validate=lambda widget, attribute=attribute: self.
                    set_device_setting(attribute, widget.text, widget))
                # store to get set_device_setting before preview
                value.attribute = attribute
                self.settings_widgets.append(value)
                row.add_widget(key)
                row.add_widget(value)
                self.details_container.add_widget(row)
        except Exception as ex:
            print(ex)

        # preview
        self.view_call_input = TextInput(text=self.default_view_call,
                                         multiline=False,
                                         height=30,
                                         size_hint_y=None)
        self.view_call_input.bind(
            on_text_validate=lambda widget: self.check_call())
        preview_button = Button(
            text="preview",
            background_color=connected_color,
            height=60,
            size_hint_y=None,
        )
        preview_button.bind(on_press=lambda widget: self.preview())

        get_state_button = Button(text="get state",
                                  height=30,
                                  size_hint_y=None)
        set_state_button = Button(text="set state",
                                  height=30,
                                  size_hint_y=None)
        get_state_button.bind(on_press=lambda widget: self.get_state())
        set_state_button.bind(on_press=lambda widget: self.set_state())
        get_set_state_row = BoxLayout(height=30, size_hint_y=None)
        load_state_from_row = BoxLayout(height=30, size_hint_y=None)
        load_state_from_button = Button(text="load state from",
                                        height=30,
                                        size_hint_y=None)
        load_state_from_input = TextInput(hint_text="db key",
                                          multiline=False,
                                          height=30,
                                          size_hint_y=None)
        load_state_from_button.bind(
            on_press=lambda widget, state_source=load_state_from_input: self.
            load_state(load_state_from_input.text))
        get_set_state_row.add_widget(get_state_button)
        get_set_state_row.add_widget(set_state_button)
        self.details_container.add_widget(get_set_state_row)
        load_state_from_row.add_widget(load_state_from_button)
        load_state_from_row.add_widget(load_state_from_input)
        self.details_container.add_widget(load_state_from_row)
        self.details_container.add_widget(self.view_call_input)
        self.details_container.add_widget(preview_button)
        self.update_conditions()