Exemplo n.º 1
0
    def __init__(self, **kwargs):
        # create table if it doesnt exist already
        if my_database.create_table(table="lampshades",
                                    columns=lampshade_columns) is False:
            raise Exception("Unable to create table - lampshades")

        self.device_id = kwargs["device_id"]
        self.group = "lampshades"  # to be used by parent class to access Database Table

        # check if device already in database or not
        if not my_database.is_in_table(
                table="lampshades", column="device_id", query=self.device_id):

            # create entry for new lampshade
            table = "lampshades"
            template = " (device_id, settings) VALUES (%s, %s) "
            self.settings = my_config.get("lampshade", "default_settings")
            values = [self.device_id, str(self.settings)]

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

            Lampshade.debug("Created new lampshade object: " + self.device_id)

        else:  # is the device is already in our database
            Lampshade.debug("Device already in database: " + self.device_id)
Exemplo n.º 2
0
    def __init__(self):
        base = os.path.expanduser("~")
        self.parent_dir = my_config.get("filemanager", "parent_directory")
        self.parent_dir = os.path.join(base, self.parent_dir)
        self.image_dir = os.path.join(
            self.parent_dir, my_config.get("filemanager", "image_directory"))
        self.bin_dir = os.path.join(
            self.parent_dir, my_config.get("filemanager", "bin_directory"))

        File_Manager.debug("Initializing file manager")
        if not os.path.isdir(self.parent_dir):
            os.mkdir(self.parent_dir)
        if not os.path.isdir(self.image_dir):
            os.mkdir(self.image_dir)
        if not os.path.isdir(self.bin_dir):
            os.mkdir(self.bin_dir)
Exemplo n.º 3
0
    def send_command(self, command):
        topic = my_config.get("mqtt", "device_topic_base")
        topic = "{}/{}".format(topic, self.device_id)

        my_mqtt.publish(topic, command)
        ans = my_responses.wait_reply(self.device_id)
        if ans == None:
            raise Exception("No reply from " + self.device_id)
        return ans
Exemplo n.º 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)
Exemplo n.º 5
0
        return self.run_command(command=cmd, params=value)


    @staticmethod
    def in_list(items, dlist):         # Function to check if a items are in a list
        for item in items:
            if item not in dlist:
                return False

        return True


    @staticmethod
    def debug(*args):
        logger.debug("Database", *args)


    @staticmethod
    def error(*args):
        logger.error("Database", *args)



# Database object
mysql_host = my_config.get("database", "host")
mysql_user = my_config.get("database", "user")
mysql_password = my_config.get("database", "password")
mysql_database = my_config.get("database", "database")

my_database = Database(mysql_host, mysql_user, mysql_password, mysql_database)
Exemplo n.º 6
0
 def __init__(self):
     self.new_width = my_config.get("image", "new_width")
     self.new_height = my_config.get("image", "new_height")
     self.fixed_length = 60