-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
154 lines (124 loc) · 4.32 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# -*-coding:utf-8 -*-
from ashbin.auth.views import auth
from ashbin.user.views import user
from ashbin.utils.listen_ws import ws_listening
from ashbin.extensions import login_manager
from ashbin.user.models import User
from ashbin.devices.models import Device
from ashbin.gdata.models import Data
from ashbin.garbage_cans.model import GarbageCan
from ashbin.extensions import csrf, redis_store, admin, db
from flask_admin.contrib.sqla import ModelView
from wtforms.fields import SelectField
from ashbin.devices.views import devices
from ashbin.garbage_cans.views import garbage_can
from ashbin.map.views import map
import websocket
from ashbin.extensions import socketio
from flask import session, request
from flask import Flask
from threading import Thread
from flask_socketio import emit, rooms, close_room,leave_room, join_room, disconnect
from flask_socketio import SocketIO
async_mode = None
if async_mode is None:
try:
import eventlet
async_mode = 'eventlet'
except ImportError:
pass
if async_mode is None:
try:
from gevent import monkey
async_mode = 'gevent'
except ImportError:
pass
if async_mode is None:
async_mode = 'threading'
print('async_mode is ' + async_mode)
# monkey patching is necessary because this application uses a background
# thread
if async_mode == 'eventlet':
import eventlet
eventlet.monkey_patch()
elif async_mode == 'gevent':
from gevent import monkey
monkey.patch_all()
def create_app(config=None):
"""Creates the app."""
app = Flask(__name__, template_folder='ashbin/templates', static_folder='ashbin/static')
# Use the default config and override it afterwards
app.config.from_object('ashbin.configs.default.DefaultConfig')
# Update the config
app.config.from_object(config)
configure_extensions(app)
configure_blueprint(app)
init_app(app)
app.debug = app.config['DEBUG']
return app
def configure_blueprint(app):
app.register_blueprint(auth)
app.register_blueprint(user, url_prefix=app.config['USER_URL_PREFIX'])
app.register_blueprint(devices, url_prefix=app.config['DEVICES_URL_PREFIX'])
app.register_blueprint(garbage_can, url_prefix=app.config['GARBAGE_CAN_URL_PREFIX'])
app.register_blueprint(map, url_prefix=app.config['MAP_URL_PREFIX'])
class MyView(ModelView):
can_create = False
column_list = ['username', 'telephone']
form_overrides = dict(status=SelectField)
form_args = dict(
# Pass the choices to the `SelectField`
status=dict(
choices=[(0, 'waiting'), (1, 'in_progress'), (2, 'finished')]
))
def __init__(self, session, **kwargs):
super(MyView, self).__init__(User, session, endpoint='users', **kwargs)
def configure_extensions(app):
db.init_app(app)
login_configure(app)
# Flask-WTF CSRF
csrf.init_app(app)
# redis
redis_store.init_app(app)
socketio.init_app(app, async_mode=async_mode)
def login_configure(app):
login_manager.init_app(app)
login_manager.login_view = app.config['LOGIN_VIEW']
@login_manager.user_loader
def load_user(user_id):
user_instance = User.query.filter_by(id=user_id).first()
if user_instance:
return user_instance
else:
return None
thread = None
def init_app(app):
# #
@app.before_first_request
def before_first_request():
try:
# listen = Listening()
# ws = websocket.WebSocket()
# ws.connect(LORIOT_URL)
global thread
if thread is None:
print('ws_socket')
thread = Thread(target=ws_listening)
thread.daemon = True
thread.start()
# ws_listening_thread = Thread(target=ws_listening)
# ws_listening_thread.start()
except Exception, e:
print e.message
raise e
@socketio.on('test', namespace='/device')
def test_message(message):
session['receive_count'] = session.get('receive_count', 0) + 1
print(u'接收到的信息:%s' % message['data'])
emit('my response',
{'data': message['data'], 'count': session['receive_count']})
app = create_app()
if __name__ == '__main__':
app.debug = True
socketio.run(app, host='183.230.40.230', port=8099)
# app.run()