-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
181 lines (146 loc) · 5.45 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# encoding: utf-8
import json
from flask import Flask, render_template, request, redirect, url_for, session, g
from sqlalchemy import or_
import config
from decorators import login_required
from exts import db, mqtt
from models import User, Question, Answer, Sensor
app = Flask(__name__)
app.config.from_object(config)
db.init_app(app)
mqtt.init_app(app)
mqtt.subscribe('sensor/temperature')
@mqtt.on_message()
def handle_mqtt_message(client, userdata, message):
if message.topic == u"sensor/temperature":
payload = message.payload.decode()
payload = float(payload)
print(payload)
@app.route("/")
def index():
questions = Question.query.order_by(db.desc(Question.create_time)).all()
return render_template("index.html", questions=questions)
@app.route("/login/", methods=["GET", "POST"])
def login():
if request.method == "GET":
return render_template("login.html")
else:
telephone = request.form.get("telephone")
password = request.form.get("password")
user = User.query.filter(User.telephone == telephone).first()
if user and user.check_password(password):
session["user_id"] = user.id
session.permanent = True
return redirect(url_for("index"))
else:
return u"手机号或者密码错误,请检查后再次登录"
@app.route("/logout/")
def logout():
session.clear()
return redirect(url_for("login"))
@app.route("/register/", methods=["GET", "POST"])
def register():
if request.method == "GET":
return render_template("register.html")
else:
telephone = request.form.get("telephone")
username = request.form.get("username")
password1 = request.form.get("password1")
password2 = request.form.get("password2")
user = User.query.filter(User.telephone == telephone).first()
if user:
return u"该手机号码已经被注册,请更换手机号码!"
else:
if password1 == password2:
user = User(telephone=telephone, password=password1, username=username)
db.session.add(user)
db.session.commit()
return redirect(url_for("login"))
else:
return u"两次密码不相同,请核对后再填写!"
@app.route("/question/", methods=["GET", "POST"])
@login_required
def question():
if request.method == "GET":
return render_template("question.html")
else:
title = request.form.get("title")
content = request.form.get("content")
newquestion = Question(title=title, content=content)
newquestion.author = g.user
db.session.add(newquestion)
db.session.commit()
return redirect(url_for("index"))
@app.route("/detail/<question_id>/")
def detail(question_id):
ques = Question.query.filter(Question.id == question_id).first()
return render_template("detail.html", question=ques)
@app.route("/add_answer/", methods=["POST"])
@login_required
def add_answer():
comment = request.form.get("comment")
question_id = request.form.get("question_id")
answer = Answer(content=comment)
answer.author = g.user
ques = Question.query.filter(Question.id == question_id).first()
answer.question = ques
db.session.add(answer)
db.session.commit()
return redirect(url_for("detail", question_id=question_id))
@app.route("/search/")
def search():
q = request.args.get("q")
condition = or_(Question.content.contains(q), Question.title.contains(q))
questions = Question.query.filter(condition).order_by(db.desc(Question.create_time))
return render_template("index.html", questions=questions)
# @login_required
@app.route("/monitor/")
def monitor():
sensors = Sensor.query.order_by(db.desc(Sensor.datetime)).limit(60)
data = {"time": [], "temp": []}
if sensors:
for sensor in sensors:
dt = sensor.datetime
temp = sensor.temperature
dt_str = "/".join([str(dt.year), str(dt.month), str(dt.day)]) + " " + ":".join(
[str(dt.hour), str(dt.minute), str(dt.second)])
data["time"].append(dt_str)
data["temp"].append(temp)
data["time"].reverse()
data["temp"].reverse()
return render_template("monitor.html", data=data)
@app.route("/monitor/<sensor>")
def get_sensor_data(sensor):
if sensor == "temperature":
sensors = Sensor.query.order_by(db.desc(Sensor.datetime)).limit(60)
data = {"time": [], "temp": []}
if sensors:
for sensor in sensors:
dt = sensor.datetime
temp = sensor.temperature
dt_str = "/".join([str(dt.year), str(dt.month), str(dt.day)]) + " " + ":".join(
[str(dt.hour), str(dt.minute), str(dt.second)])
data["time"].append(dt_str)
data["temp"].append(temp)
data["time"].reverse()
data["temp"].reverse()
return json.dumps(data)
@app.before_request
def my_before_request():
user_id = session.get("user_id")
if user_id:
user = User.query.filter(User.id == user_id).first()
if user:
g.user = user
@app.context_processor
def my_context_processor():
if hasattr(g, "user"):
return {"user": g.user}
else:
return {}
@app.errorhandler(404)
def page_not_found(error):
return u'您访问的页面不存在', 404
if __name__ == "__main__":
app.run(host="0.0.0.0")