예제 #1
0
파일: server.py 프로젝트: hujiang001/iot-X
    def run():
        #加锁,只能启动一个服务
        if ServerClass._lock:
            log.logError("server is already running..")
            return
        ServerClass._lock = True
        #初始化数据库
        database.db_init()
        #添加超级用户
        privilegeM.priv_add_superuser()
        #初始化权限管理模块
        privilegeM.priv_init()

        #解析命令行, 有了这行,还可以看到日志...
        #options.parse_command_line()
        settings = {
                    'debug': False,
                    'gzip': True,
                    'autoescape': None,
                    'xsrf_cookies': False,
                    'cookie_secret':"61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
                    'template_path':os.path.join(os.path.dirname(__file__), "playground"),
                    'static_path':os.path.join(os.path.dirname(__file__), "playground/main/static"),
                    'login_url':'/playground/login'
                }
        application = web.Application(
            [
                (HTTP_RES['root'], handlers.rootHandle),
                (HTTP_RES['user'], handlers.usersHandle),
                (HTTP_RES['userOne'], handlers.userOneHandle),
                (HTTP_RES['device'], handlers.devicesHandle),
                (HTTP_RES['deviceOne'], handlers.deviceOneHandle),
                (HTTP_RES['sensor'], handlers.sensorsHandle),
                (HTTP_RES['sensorOne'], handlers.sensorOneHandle),
                (HTTP_RES['dataSet'], handlers.datasetHandle),
                (HTTP_RES['commandSet'], handlers.commandsetHandle),
                (HTTP_RES['commandSetOne'], handlers.commandsetOneHandle),
                (HTTP_RES['deviceAuth'], handlers.deviceauthHandle),
                (HTTP_RES['accessKey'], handlers.accessKeyHandle),
                (HTTP_RES['userLogin'], handlers.userLoginHandle),
                (HTTP_RES['userLogout'], handlers.userLogoutHandle),
                ##playgruod below
                (r'/playground', playground.rootHandle),
                (r'/playground/login', playground.loginHandle),
            ], **settings)
        ServerClass.server = HTTPServer(application)
        ServerClass.server.listen(configure.server_port,configure.server_ip)
        try:
            ioloop.IOLoop.instance().start()
        except Exception,e:
            log.logFatal("start server fail, exception:"+e.message)
            ServerClass._lock = False
예제 #2
0
def db_init():
    """firstly, this functon should be called before use database.
    it can be called repeatedly, because we use "IF NOT EXISTS" to
    create a table.
    """
    str_sql_init = """
    CREATE TABLE IF NOT EXISTS [USER] (
      [ID] INTEGER PRIMARY KEY AUTOINCREMENT,
      [NAME] CHAR(60) NOT NULL UNIQUE,
      [PWD] CHAR(60),
      [REG_TIME] TIME,
      [LAST_LOGIN_TIME] TIME,
      [STATE] CHAR(10) NOT NULL,
      [USER_DEF_AREA] TEXT
    );
    CREATE TABLE IF NOT EXISTS [DEVICE] (
      [ID] INTEGER PRIMARY KEY AUTOINCREMENT,
      [NAME] CHAR(60) NOT NULL,
      [DESCRIPTION] TEXT,
      [REG_TIME] TIME,
      [LOCAL] CHAR(32),
      [LATITUDE] FLOAT,
      [LONGITUDE] FLOAT,
      [USER_DEF_AREA] TEXT,
      [KEY] CHAR(65)
    );
    CREATE TABLE IF NOT EXISTS [SENSOR] (
      [ID] INTEGER PRIMARY KEY AUTOINCREMENT,
      [NAME] CHAR(60) NOT NULL,
      [DESCRIPTION] TEXT,
      [REG_TIME] TIME,
      [DEVICE_ID] INT,
      [USER_DEF_AREA] TEXT
    );
    CREATE TABLE IF NOT EXISTS [DEVICEAUTH] (
      [USER_ID] INT NOT NULL,
      [DEVICE_ID] INT NOT NULL,
      [IS_OWNER] INT NOT NULL,
      PRIMARY KEY (USER_ID,DEVICE_ID)
    );
    CREATE TABLE IF NOT EXISTS [DATASET] (
      [DEVICE_ID] INT NOT NULL,
      [SENSOR_ID] INT NOT NULL,
      [CREATE_TIME] TIME NOT NULL,
      [LAST_UPDATE_TIME] TIME NOT NULL,
      [KEY] CHAR(60),
      [VALUE]
    );
    CREATE TABLE IF NOT EXISTS [COMMANDSET] (
      [DEVICE_ID] INT NOT NULL,
      [SENSOR_ID] INT NOT NULL,
      [COMMAND] CHAR(40) NOT NULL,
      [VALUE] TEXT,
      [CREATE_TIME] TIME NOT NULL,
      [LAST_UPDATE_TIME] TIME NOT NULL,
      PRIMARY KEY (DEVICE_ID,SENSOR_ID,COMMAND)
    );
    CREATE TABLE IF NOT EXISTS [PRIVILEGE] (
      [PRIVILEGE_MASTER] CHAR(32) NOT NULL,
      [PRIVILEGE_MASTER_ID] INT NOT NULL,
      [PRIVILEGE_MASTER_ROLE] INT NOT NULL,
      [OBJ_TYPE] CHAR(32) NOT NULL,
      [OBJ_ID] INT NOT NULL,
      [OPERATION_LIST] TEXT,
      [CREATE_TIME] TIME NOT NULL,
      [LAST_UPDATE_TIME] TIME NOT NULL,
      PRIMARY KEY (PRIVILEGE_MASTER,PRIVILEGE_MASTER_ID,PRIVILEGE_MASTER_ROLE,OBJ_TYPE,OBJ_ID)
    );
    CREATE TABLE IF NOT EXISTS [ACCESSKEY] (
      [KEY] CHAR(64) PRIMARY KEY,
      [ALLOC_USER] INT NOT NULL,
      [CREATE_TIME] TIME NOT NULL,
      [ACCESS_DEVICES] CHAR[64]
    );
    """
    myconn = db_getConn()

    try:
        myconn.executescript(str_sql_init)
        myconn.commit()
    except Exception,e:
        log.logFatal("init database fail,exception:" + e.message )
        util.safeExit()
        return