-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
120 lines (104 loc) · 2.98 KB
/
main.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
import arrow
from flask import Flask, request, session, make_response
import nacl.utils
from nacl.public import Box
from nacl.encoding import HexEncoder
import config
import utils
import db_api
from security import Decryptor
import pandas as pd
from jinja2 import Environment, FileSystemLoader
env = Environment(
loader=FileSystemLoader('templates')
)
app = Flask(__name__)
app.secret_key = 'secret'
@app.route("/")
def index():
template = env.get_template('index.html')
out = template.render()
return out
@app.route("/echo")
def echo():
msg = request.args.get('msg', '')
return "echo message from server: {0:s}".format(msg)
@app.route("/post", methods=["POST"])
def post():
msg = request.form.get('msg', '')
return "posted message to server: {0:s}".format(msg)
@app.route("/nonce")
def send_nonce():
session['timestamp'] = arrow.utcnow().format(config.TIMESTAMP_FMT)
session['nonce'] = nacl.utils.random(Box.NONCE_SIZE)
out = make_response(HexEncoder.encode(session['nonce']))
return out
@app.route("/send_message", methods=["POST"])
def send_message():
raw = utils.read_chunked()
d = Decryptor()
msg = d.decrypt(raw)
if isinstance(msg, basestring):
return msg
else:
db_api.update_db(msg)
msg = 'db success'
if msg:
session.clear()
print msg
return msg
@app.route("/stream", methods=["POST"])
def stream():
raw = utils.read_chunked()
print raw
return make_response(raw)
@app.route("/show_db")
@db_api.dbwrap
def show_db(**kwargs):
conn = kwargs.get('conn')
cur = kwargs.get('cur')
cmd = '''
WITH t AS (SELECT *
FROM demo
ORDER BY timestamp DESC
LIMIT 8
)
SELECT timestamp, client, handler_id, temperature, humidity
FROM t
ORDER BY timestamp ASC
'''
cur.execute(cmd)
data = cur.fetchall()
df = pd.DataFrame(data, columns=['timestamp', 'client', 'handler_status', 'temp', 'humidity'])
table = df.to_html(classes='datagrid')
template = env.get_template('demo.html')
out = template.render(table=table)
return str(out)
@app.route("/xhr_show_db")
@db_api.dbwrap
def xhr_show_db(**kwargs):
conn = kwargs.get('conn')
cur = kwargs.get('cur')
cmd = '''
WITH t AS (SELECT *
FROM demo
ORDER BY timestamp DESC
LIMIT 8
)
SELECT timestamp, client, handler_id, temperature, humidity
FROM t
ORDER BY timestamp ASC
'''
cur.execute(cmd)
data = cur.fetchall()
df = pd.DataFrame(data, columns=['timestamp', 'client', 'handler_status', 'temp', 'humidity'])
out = df.to_html(classes='datagrid')
return out
@app.route('/init_demo')
def init_demo():
from db_api.db_init_demo import db_init
db_init()
return 'demo database has been reset'
import pprint
if __name__ == "__main__":
app.run(debug=config.DEBUG)