Beispiel #1
0
    def set_setting(self, setting, value):
        table = self.group
        column = "device_id"
        query = self.device_id
        dcolumn = ["settings"]

        ans = my_database.get_from_table(table=table,
                                         column=column,
                                         query=query,
                                         dcolumn=dcolumn)
        if ans is False:
            return None

        ans = ans[0][0]
        ans = literal_eval(ans)
        ans[setting] = value

        ans = str(ans)
        ans = my_database.update_table(table=table,
                                       scolumns=dcolumn,
                                       dcolumn=column,
                                       dvalue=query,
                                       value=ans)
        if ans is not False:
            return True
        return False
Beispiel #2
0
    def get_online_lamps(self):
        ans = self.get_online_devices("lampbody")
        tans = []
        # get names of lamps whose lampbodies are online
        for body in ans:
            temp = my_database.get_from_table(table="lamps", column="lampbody_id",\
                    query=body, dcolumn=["name"])
            temp = temp[0][0]
            tans.append(temp)

        return tans
Beispiel #3
0
    def get_pair(self):
        table = self.group
        column = "device_id"
        query = self.device_id
        dcolumn = ["current_pair"]

        ans = my_database.get_from_table(table=table,
                                         column=column,
                                         query=query,
                                         dcolumn=dcolumn)
        if ans is not False:
            return ans[0][0]
        return None
Beispiel #4
0
    def __init__(self, **kwargs):
        if my_database.create_table(table="lamps",
                                    columns=lamp_columns) is False:
            raise Exception("Unable to create lamp table")

        # check if we already have this lamp in database
        if my_database.is_in_table(table="lamps",
                                   column="name",
                                   query=kwargs["name"]):
            Lamp.debug("Lamp already in Database")
            self.name = kwargs.get("name")
            temp = my_database.get_from_table(table="lamps", column="name", query=self.name, \
                    dcolumn=["lampshade_id", "lampbody_id"])
            temp = temp[0]
            shade_id = temp[0]
            body_id = temp[1]

            self.lampbody = Lampbody(device_id=body_id)
            self.lampshade = Lampshade(device_id=shade_id)
            return
        Lamp.debug("Making new Lamp.")

        # ensure that we have all necessary arguments
        if not my_database.in_list(["name", "lampbody", "lampshade"],
                                   kwargs.keys()):
            raise Exception("Incomplete args")

        Lamp.debug("Initializing Lamp")
        self.name = kwargs["name"]
        self.lampbody = kwargs["lampbody"]
        self.lampshade = kwargs["lampshade"]

        lampb = self.lampbody.device_id
        lamps = self.lampshade.device_id
        settings = my_config.get("lamp", "default_settings")

        template = " (name, lampbody_id, lampshade_id, settings) VALUES (%s, %s, %s, %s) "
        values = [self.name, lampb, lamps, str(settings)]

        if my_database.insert_in_table(
                table="lamps", template=template, value=values) is False:
            raise Exception("Unable to create new lamp object")

        Lamp.debug("Created new lamp")

        # set pairs
        Lamp.debug("Setting pairs")
        self.lampbody.set_pair(self.lampshade.device_id)
        self.lampshade.set_pair(self.lampbody.device_id)
Beispiel #5
0
    def get_setting(self, setting):
        table = self.group
        column = "device_id"
        query = self.device_id
        dcolumn = ["settings"]

        ans = my_database.get_from_table(table=table,
                                         column=column,
                                         query=query,
                                         dcolumn=dcolumn)
        if ans is False:
            return None

        ans = ans[0][0]
        ans = literal_eval(ans)

        if setting in ans.keys():
            return ans[setting]
        return None