Ejemplo n.º 1
0
 def loadThemes(cls, pack_path, develop):
     session = Session()
     # Load all Themes
     logger.info(u"PACKS: Loading themes")
     themes_path = os.path.join(pack_path, 'themes')
     session.query(Theme).delete()
     if os.path.isdir(themes_path):
         for file in os.listdir(themes_path):
             if not file.startswith('.'):  # not hidden file
                 info = os.path.join(themes_path, file, "info.json")
                 if os.path.isfile(info):
                     theme_file = open(info, "r")
                     theme_json = json.load(theme_file)
                     theme_id = theme_json["identity"]["id"]
                     theme_name = unicode(theme_json["identity"]["name"])
                     theme_version = theme_json["identity"]["version"]
                     t = Theme(id=theme_id,
                               name=theme_name,
                               version=theme_version,
                               style=unicode(json.dumps(
                                   theme_json['style'])))
                     if 'description' in theme_json["identity"]:
                         t.description = theme_json["identity"][
                             "description"]
                     session.add(t)
     session.commit()
     session.close()
Ejemplo n.º 2
0
def updateDb(user, db):
    from domoweb.models import metadata, engine, Session, Section, SectionParam
    from sqlalchemy import create_engine, func
    from alembic.config import Config
    from alembic import command

    alembic_cfg = Config("alembic.ini")
    if not os.path.isfile(db):
        ok("Creating new database: %s" % db)
        metadata.create_all(engine)
        command.stamp(alembic_cfg, "head")
        uid = pwd.getpwnam(user).pw_uid
        os.chown(db, uid, -1)

        ok("Adding initial data")
        session = Session()
        s = Section(name=unicode('Root'),
                    description=unicode('Root dashboard'),
                    left=1,
                    right=2)
        session.add(s)
        p = SectionParam(section_id=1, key='GridWidgetSize', value='100')
        session.add(p)
        p = SectionParam(section_id=1, key='GridWidgetSpace', value='20')
        session.add(p)
        p = SectionParam(section_id=1, key='GridMode', value='3')
        session.add(p)
        session.commit()
    else:
        ok("Upgrading existing database")
        session = Session()
        sections = session.query(Section).all()
        for s in sections:
            p = session.query(SectionParam).filter_by(section_id=s.id).filter(
                SectionParam.key.in_([
                    'GridWidgetSize', 'GridWidgetSpace', 'GridColumns',
                    'GridRows'
                ])).count()
            if p == 0:
                ok("Updating section '%s'" % s.name)
                p = SectionParam(section_id=s.id,
                                 key='GridWidgetSize',
                                 value='100')
                session.add(p)
                p = SectionParam(section_id=s.id,
                                 key='GridWidgetSpace',
                                 value='20')
                session.add(p)
                p = SectionParam(section_id=s.id, key='GridMode', value='3')
                session.add(p)
                session.commit()

        command.upgrade(alembic_cfg, "head")
Ejemplo n.º 3
0
    def loadDevices(cls, develop):
        logger.info(u"MQ: Loading Devices info")
        Device.clean()
        msg = MQMessage()
        msg.set_action('client.list.get')
        res = cli.request('manager', msg.get(), timeout=10)
        if res is not None:
            _datac = res.get_data()
        else:
            _datac = {}
        session = Session()
        for client in _datac.itervalues(): 
            # for each plugin client, we request the list of devices
            if client["type"] == "plugin":
                msg = MQMessage()
                msg.set_action('device.get')
                msg.add_data('type', 'plugin')
                msg.add_data('name', client["name"])
                msg.add_data('host', client["host"])
                logger.info(u"MQ: Get devices list for client {0}-{1}.{2}".format("plugin", client["name"], client["host"]))
                res = cli.request('dbmgr', msg.get(), timeout=10)
                if res is not None:
                    _datad = res.get_data()
                else:
                    _datad = {}
                if 'devices' in _datad:
                    for device in _datad["devices"]:
                        logger.info(u"- {0}".format(device["name"]))
                        d = Device(id=device["id"], name=device["name"], type=device["device_type_id"], reference=device["reference"])
                        session.add(d)
                        if "commands" in device:
                            for ref, command  in device["commands"].iteritems():
                                c = Command(id=command["id"], name=command["name"], device_id=device["id"], reference=ref, return_confirmation=command["return_confirmation"])
                                session.add(c)
                                c.datatypes = ""
                                for param in command["parameters"]:
                                    p = CommandParam(command_id=c.id, key=param["key"], datatype_id=param["data_type"])
                                    session.add(p)
                                    c.datatypes += param["data_type"]
                                session.add(c)
                        if "sensors" in device:
                            for ref, sensor in device["sensors"].iteritems():
                                s = Sensor(id=sensor["id"], name=sensor["name"], device_id=device["id"], reference=ref, datatype_id=sensor["data_type"], last_value=sensor["last_value"], last_received=sensor["last_received"], timeout=sensor["timeout"])
                                session.add(s)

        session.commit()
        session.flush()
Ejemplo n.º 4
0
    def loadDatatypes(cls):
        session = Session()

        # get all datatypes
        logger.info("MQ: Loading Datatypes")
        msg = MQMessage()
        msg.set_action('datatype.get')
        res = cli.request('manager', msg.get(), timeout=10)
        if res is not None:
            _data = res.get_data()['datatypes']
        else:
            _data = {}

        session.query(DataType).delete()
        for type, params in _data.iteritems():
            r = DataType(id=type, parameters=json.dumps(params))
            session.add(r)
        session.commit()
        session.flush()
Ejemplo n.º 5
0
    def loadDatatypes(cls, develop):
        session = Session()

        # get all datatypes
        logger.info(u"MQ: Loading Datatypes")
        msg = MQMessage()
        msg.set_action('datatype.get')
        res = cli.request('manager', msg.get(), timeout=10)
        if res is not None:
            _data = res.get_data()['datatypes']
        else:
            _data = {}

        session.query(DataType).delete()
        for type, params in _data.iteritems():
            r = DataType(id=type, parameters=json.dumps(params))
            session.add(r)
        session.commit()
        session.flush()
Ejemplo n.º 6
0
def updateDb(user, db):
    from domoweb.models import metadata, engine, Session, Section, SectionParam
    from sqlalchemy import create_engine, func
    from alembic.config import Config
    from alembic import command

    alembic_cfg = Config("alembic.ini")
    if not os.path.isfile(db):
        ok("Creating new database: %s" % db)
        metadata.create_all(engine)
        command.stamp(alembic_cfg, "head")
        uid = pwd.getpwnam(user).pw_uid
        os.chown(db, uid, -1)

        ok("Adding initial data")
        session = Session()
        s = Section(name=unicode('Root'), description=unicode('Root dashboard'), left=1, right=2)
        session.add(s)
        p = SectionParam(section_id=1, key='GridWidgetSize', value='100')
        session.add(p)
        p = SectionParam(section_id=1, key='GridWidgetSpace', value='20')
        session.add(p)
        p = SectionParam(section_id=1, key='GridMode', value='3')
        session.add(p)
        session.commit()
    else:
        ok("Upgrading existing database")
        session = Session()
        sections = session.query(Section).all()
        for s in sections:
            p = session.query(SectionParam).filter_by(section_id=s.id).filter(SectionParam.key.in_(['GridWidgetSize', 'GridWidgetSpace', 'GridColumns', 'GridRows'])).count()
            if p == 0:
                ok("Updating section '%s'" % s.name)
                p = SectionParam(section_id=s.id, key='GridWidgetSize', value='100')
                session.add(p)
                p = SectionParam(section_id=s.id, key='GridWidgetSpace', value='20')
                session.add(p)
                p = SectionParam(section_id=s.id, key='GridMode', value='3')
                session.add(p)
                session.commit()

        command.upgrade(alembic_cfg, "head")
Ejemplo n.º 7
0
 def loadIconsets(cls, pack_path):
     session = Session()
     # Load all Iconsets
     logger.info("PACKS: Loading iconsets")
     iconsets_path = os.path.join(pack_path, 'iconsets', 'section')
     session.query(SectionIcon).delete()
     if os.path.isdir(iconsets_path):
         for file in os.listdir(iconsets_path):
             if not file.startswith('.'): # not hidden file
                 info = os.path.join(iconsets_path, file, "info.json")
                 if os.path.isfile(info):
                     iconset_file = open(info, "r")
                     iconset_json = json.load(iconset_file)
                     iconset_id = iconset_json["identity"]["id"]
                     iconset_name = unicode(iconset_json["identity"]["name"])
                     for icon in iconset_json["icons"]:
                         id = iconset_id + '-' + icon["id"]
                         i = SectionIcon(id=id, iconset_id=iconset_id, iconset_name=iconset_name, icon_id=icon["id"], label=unicode(icon["label"]))
                         session.add(i)
     session.commit()
     session.close()
Ejemplo n.º 8
0
def updateDb(user, db):
    from domoweb.models import metadata, engine, Session, Section
    from sqlalchemy import create_engine
    from alembic.config import Config
    from alembic import command

    alembic_cfg = Config("alembic.ini")
    if not os.path.isfile(db):
        ok("Creating new database: %s" % db)
        metadata.create_all(engine)
        command.stamp(alembic_cfg, "head")
        uid = pwd.getpwnam(user).pw_uid
        os.chown(db, uid, -1)
        
        ok("Adding initial data")
        session = Session()
        s = Section(name=unicode('Root'), description=unicode('Root dashboard'), left=1, right=2)
        session.add(s)
        session.commit()
    else:
        ok("Upgrading existing database")
        command.upgrade(alembic_cfg, "head")
Ejemplo n.º 9
0
 def loadThemes(cls, pack_path, develop):
     session = Session()
     # Load all Themes
     logger.info(u"PACKS: Loading themes")
     themes_path = os.path.join(pack_path, 'themes')
     session.query(Theme).delete()
     if os.path.isdir(themes_path):
         for file in os.listdir(themes_path):
             if not file.startswith('.'): # not hidden file
                 info = os.path.join(themes_path, file, "info.json")
                 if os.path.isfile(info):
                     theme_file = open(info, "r")
                     theme_json = json.load(theme_file)
                     theme_id = theme_json["identity"]["id"]
                     theme_name = unicode(theme_json["identity"]["name"])
                     theme_version = theme_json["identity"]["version"]
                     t = Theme(id=theme_id, name=theme_name, version=theme_version, style=unicode(json.dumps(theme_json['style'])))
                     if 'description' in theme_json["identity"]:
                         t.description = theme_json["identity"]["description"]
                     session.add(t)
     session.commit()
     session.close()
Ejemplo n.º 10
0
    def loadDevices(cls, develop):
        logger.info(u"MQ: Loading Devices info")
        Device.clean()
        msg = MQMessage()
        msg.set_action('client.list.get')
        res = cli.request('manager', msg.get(), timeout=10)
        if res is not None:
            _datac = res.get_data()
        else:
            _datac = {}
        session = Session()
        for client in _datac.itervalues():
            # for each plugin client, we request the list of devices
            if client["type"] == "plugin":
                msg = MQMessage()
                msg.set_action('device.get')
                msg.add_data('type', 'plugin')
                msg.add_data('name', client["name"])
                msg.add_data('host', client["host"])
                logger.info(
                    u"MQ: Get devices list for client {0}-{1}.{2}".format(
                        "plugin", client["name"], client["host"]))
                res = cli.request('dbmgr', msg.get(), timeout=10)
                if res is not None:
                    _datad = res.get_data()
                else:
                    _datad = {}
                if 'devices' in _datad:
                    for device in _datad["devices"]:
                        logger.info(u"- {0}".format(device["name"]))
                        d = Device(id=device["id"],
                                   name=device["name"],
                                   type=device["device_type_id"],
                                   reference=device["reference"])
                        session.add(d)
                        if "commands" in device:
                            for ref, command in device["commands"].iteritems():
                                c = Command(id=command["id"],
                                            name=command["name"],
                                            device_id=device["id"],
                                            reference=ref,
                                            return_confirmation=command[
                                                "return_confirmation"])
                                session.add(c)
                                c.datatypes = ""
                                for param in command["parameters"]:
                                    p = CommandParam(
                                        command_id=c.id,
                                        key=param["key"],
                                        datatype_id=param["data_type"])
                                    session.add(p)
                                    c.datatypes += param["data_type"]
                                session.add(c)
                        if "sensors" in device:
                            for ref, sensor in device["sensors"].iteritems():
                                s = Sensor(
                                    id=sensor["id"],
                                    name=sensor["name"],
                                    device_id=device["id"],
                                    reference=ref,
                                    datatype_id=sensor["data_type"],
                                    last_value=sensor["last_value"],
                                    last_received=sensor["last_received"],
                                    timeout=sensor["timeout"])
                                session.add(s)

        session.commit()
        session.flush()