main.py 2.34 KB
# coding: utf-8
from flask import Flask,Blueprint
import threading
from apps.api.http_serv import tx1_box
from apps.api.ws_serv import Server
import apps.gbvar.ws_serv_global  as sv_global
import apps.gbvar.database_global as database
import apps.gbvar.dict_properties as dict_p
import fdb
import sys
import os
from apps.commons.util import *

app = Flask(__name__, static_folder='apps/static')
# 获取本脚本所在的路径
pro_path = os.path.split(os.path.realpath(__file__))[0]

#配置文件路径
con_path = '/'.join((pro_path, 'special.properties'))
print con_path
dictProperties=Properties(con_path).getProperties()

# 创建路由
app.register_blueprint(tx1_box, url_prefix='/api/v1/tx1_box')


def start_ws(host,port):
    try:
        sv_global._init ()
        __sv = Server(host,port)
        sv_global.set_value(__sv)
    except Exception,e:
        print e.message
        return

def thread_start(__sv):
    __sv.listen_client ()

#系统初始化数据库
def init():
    database._init()
    dict_p._init()
    dict_p.set_value(dictProperties)
    ###===================database=============================
    con = fdb.connect ( host=dictProperties['tx1_host'], database=dictProperties['datasource_url'], user='sysdba',
                        password='masterkey',
                        charset='UTF8' )
    cur = con.cursor ()
    database.set_value(cur)

    '''newLanguages = [
        (1, 'Dylan')
    ]
    cur.executemany ( "insert into tb_traffic(id,name) VALUES(?,?)",
                      newLanguages
                      )
    con.commit ()
    sql_select = 'select * from tb_traffic'

    # 显示全部内容
    cur.execute ( sql_select )
    print(cur.fetchall ())

    cur.close ()
    con.close ()'''
    ####====================database===================

#启动服务
if __name__=='__main__':
    #init()
    _h=dictProperties['tx1_host']
    _p=int(dictProperties['tx1_ws_port'])
    start_ws(_h,_p)
    print sv_global.get_value()
    t1 = threading.Thread ( target=thread_start, name='start_ws_serv', args={sv_global.get_value (), } )
    t1.start ()
    print 'sv_global.get_value()=%s' % sv_global.get_value ()
    # 启动http
    app.run (
        host=dictProperties['tx1_host'],
        port=int(dictProperties['tx1_http_port'] ),debug=False,threaded=True)