-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
160 lines (111 loc) · 4.01 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
import json
import time
from flask import (
Flask,
Response,
jsonify,
render_template,
request,
stream_with_context,
)
from db_method import DB_NAME, get_all_data, get_single_data, initdb, insert_sensor_data
app = Flask(__name__, template_folder="templates")
data = {"temperature": [], "humidity": []}
# -----------------前端頁面-----------
# 主頁
@app.route("/")
def index():
return render_template("index.html")
@app.get("/visualize-realtime")
def visualize():
return render_template("realtime-data.html")
@app.get("/visualize-specifiedtime")
def visualize2():
return render_template("specifiedtime-data.html")
@app.get("/visualize-camera_view")
def visualize3():
return render_template("camera_view.html")
# -----------------後端頁面-----------
# post 給esp32用
@app.post("/post_data")
def receive_data():
try:
content = request.get_json()
temperature = content["temperature"]
humidity = content["humidity"]
data["temperature"].append(temperature)
data["humidity"].append(humidity)
# Call function to initialize database if not already initialized
# initdb()
# Call function to insert sensor data into the database
insert_sensor_data(temperature, humidity)
print(f"Received data: temperature={temperature}, humidity={humidity}")
return jsonify({"success": True})
except Exception as e:
print(f"Error receiving data: {str(e)}")
return jsonify({"success": False, "error": str(e)})
@app.get("/get_data")
def get_data():
# Extract start_time and end_time parameters from the request URL
start_time_str = request.args.get("start_time")
end_time_str = request.args.get("end_time")
if not start_time_str or not end_time_str:
return (
jsonify({"error": "start_time and end_time parameters are required"}),
400,
)
# Call the function to retrieve temperature data between start_time and end_time
data_result = get_single_data(start_time_str, end_time_str, DB_NAME)
if data_result is None:
return jsonify({"error": "Failed to retrieve temperature data"}), 500
# Convert data to a list of dictionaries for JSON response
formatted_data = [
{"temperature": row[0], "humidity": row[1], "timestamp": row[2]}
for row in data_result
]
return jsonify(formatted_data)
@app.get("/get_all_data")
def get_all_data_route():
# Call the function to retrieve all temperature data
all_data = get_all_data(DB_NAME)
if all_data is None:
return jsonify({"error": "Failed to retrieve all the data"}), 500
# Convert data to a list of dictionaries for JSON response
formatted_data = [
{"temperature": row[0], "humidity": row[1], "timestamp": row[2]}
for row in all_data
]
return jsonify(formatted_data)
class camera_view:
def __init__(self):
self.a_camera_frame = {"image": "NC", "timestamp": ""}
def update_camera_frame(self, frame):
self.a_camera_frame = frame
def get_camera_frame(self):
return self.a_camera_frame
a_camera_view = camera_view()
@app.post("/post_camera_frame")
def receive_camera_frame():
try:
content = request.get_json()
a_camera_view.update_camera_frame(content)
except Exception as e:
print(f"Error receiving data: {str(e)}")
return jsonify({"success": False, "error": str(e)})
return "camera_frame updated successfully"
@app.route("/get_camera_stream")
def make_stream():
# 與相機網頁前端建立event-stream
@stream_with_context
def generate():
try:
while True:
yield "data:" + json.dumps(a_camera_view.get_camera_frame()) + "\n\n"
time.sleep(0.1)
except GeneratorExit:
print("closed")
# 用stream發給前端
return Response(generate(), mimetype="text/event-stream")
if __name__ == "__main__":
initdb(DB_NAME)
app.run(host="0.0.0.0", port=5000, debug=True)