-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
245 lines (195 loc) · 8.99 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
from flask import Flask, render_template, request, json, send_from_directory
from models import db, TaskTime, MoodTime, AVRecordTime
from datetime import datetime
from conf import (
TIME_ON_PAGE_TASK, TIME_ON_PAGE_CALMING, TIME_ON_PAGE_TO_READ, TIME_ON_PAGE_MOOD, INDEX_BG_IMAGE,
CALMING_VIDEO_1, CALMING_VIDEO_2, CALMING_VIDEO_3, CALMING_VIDEO_4, GREEN_ZONE_VIDEO, BLUE_ZONE_VIDEO,
SESSION_NUMBER, CUSTOM_STATIC_PATH, TIME_ON_GAME, MATH_TASK_LEVEL
)
import subprocess
app = Flask(__name__, template_folder="./templates", static_folder='static')
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///saved-times.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
RECORD_AV_PROCESS = None
STARTING_TIME_AV_RECORD = None
ENDING_TIME_AV_RECORD = None
@app.route('/', methods=['GET'])
def index_page():
info = {}
timestamp = datetime.now()
adding_db_task_timestamp('index_page', timestamp)
return render_template('index.html', **info, image_bg=INDEX_BG_IMAGE)
def adding_db_task_timestamp(page, timestamp):
db_new_entry = TaskTime(page=page, timestamp=timestamp)
db.session.add(db_new_entry)
db.session.commit()
@app.route('/first_task', methods=['GET'])
def first_task():
timestamp = datetime.now()
adding_db_task_timestamp('first_task', timestamp)
time_on_page = json.dumps(TIME_ON_PAGE_TASK)
time_on_page_to_read = json.dumps(TIME_ON_PAGE_TO_READ)
return render_template('first_task.html', time_on_page=time_on_page, time_on_page_to_read=time_on_page_to_read,
green_video=GREEN_ZONE_VIDEO)
@app.route('/second_task', methods=['GET'])
def second_task():
timestamp = datetime.now()
adding_db_task_timestamp('second_task', timestamp)
time_on_page = json.dumps(TIME_ON_GAME)
time_on_page_to_read = json.dumps(TIME_ON_PAGE_TO_READ)
session_number = int(SESSION_NUMBER)
return render_template('second_task.html', time_on_page=time_on_page, time_on_page_to_read=time_on_page_to_read,
session_number=session_number)
@app.route('/third_task', methods=['GET'])
def third_task():
timestamp = datetime.now()
adding_db_task_timestamp('third_task', timestamp)
time_on_page = json.dumps(TIME_ON_PAGE_TASK)
time_on_page_to_read = json.dumps(TIME_ON_PAGE_TO_READ)
math_level_conf = str(MATH_TASK_LEVEL)
return render_template('third_task.html',
time_on_page=time_on_page,
time_on_page_to_read=time_on_page_to_read,
math_level_conf=math_level_conf)
@app.route('/forth_task', methods=['GET'])
def forth_task():
timestamp = datetime.now()
adding_db_task_timestamp('forth_task', timestamp)
time_on_page = json.dumps(TIME_ON_PAGE_TASK)
time_on_page_to_read = json.dumps(TIME_ON_PAGE_TO_READ)
return render_template('forth_task.html', time_on_page=time_on_page, time_on_page_to_read=time_on_page_to_read,
blue_video=BLUE_ZONE_VIDEO)
@app.route('/calming_content', methods=['GET'])
def calming_content():
timestamp = datetime.now()
adding_db_task_timestamp('calming_content_1', timestamp)
time_on_page = json.dumps(TIME_ON_PAGE_CALMING)
return render_template('calming_content.html', time_on_page=time_on_page, calming_video=CALMING_VIDEO_1)
@app.route('/calming_content_2', methods=['GET'])
def calming_content_2():
timestamp = datetime.now()
adding_db_task_timestamp('calming_content_2', timestamp)
time_on_page = json.dumps(TIME_ON_PAGE_CALMING)
return render_template('calming_content_2.html', time_on_page=time_on_page, calming_video=CALMING_VIDEO_2)
@app.route('/calming_content_3', methods=['GET'])
def calming_content_3():
timestamp = datetime.now()
adding_db_task_timestamp('calming_content_3', timestamp)
time_on_page = json.dumps(TIME_ON_PAGE_CALMING)
return render_template('calming_content_3.html', time_on_page=time_on_page, calming_video=CALMING_VIDEO_3)
@app.route('/calming_content_4', methods=['GET'])
def calming_content_4():
timestamp = datetime.now()
adding_db_task_timestamp('calming_content_4', timestamp)
time_on_page = json.dumps(TIME_ON_PAGE_CALMING)
return render_template('calming_content_4.html', time_on_page=time_on_page, calming_video=CALMING_VIDEO_4)
@app.route('/ending', methods=['GET'])
def ending():
# this is the timestamp the video record will end
timestamp = datetime.now()
adding_db_task_timestamp('ending_page', timestamp)
# global RECORD_AV_PROCESS
# RECORD_AV_PROCESS.communicate(b'q')
global ENDING_TIME_AV_RECORD
ENDING_TIME_AV_RECORD = timestamp
if STARTING_TIME_AV_RECORD is not None:
store_av_record_time(STARTING_TIME_AV_RECORD, ENDING_TIME_AV_RECORD)
# RECORD_AV_PROCESS.send_signal(signal.SIGINT)
return render_template('ending.html')
@app.route('/game', methods=['GET'])
def game():
timestamp = datetime.now()
adding_db_task_timestamp('game_page', timestamp)
time_on_page = json.dumps(TIME_ON_GAME)
time_on_page_to_read = json.dumps(TIME_ON_PAGE_TO_READ)
return render_template('game.html', time_on_page=time_on_page, time_on_page_to_read=time_on_page_to_read)
@app.route('/game_2', methods=['GET'])
def game_2():
timestamp = datetime.now()
adding_db_task_timestamp('game_page', timestamp)
time_passed = request.args.get("time_passed") if request.args.get("time_passed") is not None else 0
print(f'time passed: {time_passed}')
time_on_page = TIME_ON_GAME - (int(time_passed) * 1000)
time_on_page = json.dumps(time_on_page)
print(time_on_page)
time_on_page_to_read = json.dumps(TIME_ON_PAGE_TO_READ)
return render_template('game_2.html', time_on_page=time_on_page, time_on_page_to_read=time_on_page_to_read,
time_passed=time_passed)
@app.route('/data/<path:filename>')
def custom_static(filename):
return send_from_directory(CUSTOM_STATIC_PATH, filename)
@app.route('/end_game', methods=['GET'])
def end_game():
timestamp = datetime.now()
adding_db_task_timestamp('ending_game_page', timestamp)
time_on_page = json.dumps(TIME_ON_PAGE_TO_READ)
return render_template('end_game.html', time_on_page=time_on_page)
@app.route('/schedule', methods=['GET'])
def schedule():
# this is the timestamp the record video will start
timestamp = datetime.now()
adding_db_task_timestamp('schedule_page_initial', timestamp)
time_on_page = json.dumps(TIME_ON_PAGE_TO_READ)
# global RECORD_AV_PROCESS
# RECORD_AV_PROCESS = subprocess.Popen(['python', 'audio_video_record.py'], stdin=subprocess.PIPE)
global STARTING_TIME_AV_RECORD
STARTING_TIME_AV_RECORD = timestamp
return render_template('schedule.html', time_on_page=time_on_page)
@app.route('/schedule_after_1', methods=['GET'])
def schedule_after_1():
timestamp = datetime.now()
adding_db_task_timestamp('schedule_page_after_task_1', timestamp)
time_on_page = json.dumps(TIME_ON_PAGE_TO_READ)
return render_template('schedule_after_1.html', time_on_page=time_on_page)
@app.route('/schedule_after_2', methods=['GET'])
def schedule_after_2():
timestamp = datetime.now()
adding_db_task_timestamp('schedule_page_after_task_2', timestamp)
time_on_page = json.dumps(TIME_ON_PAGE_TO_READ)
return render_template('schedule_after_2.html', time_on_page=time_on_page)
@app.route('/schedule_after_3', methods=['GET'])
def schedule_after_3():
timestamp = datetime.now()
adding_db_task_timestamp('schedule_page_after_task_3', timestamp)
time_on_page = json.dumps(TIME_ON_PAGE_TO_READ)
return render_template('schedule_after_3.html', time_on_page=time_on_page)
@app.route('/schedule_ending', methods=['GET'])
def schedule_ending():
timestamp = datetime.now()
adding_db_task_timestamp('schedule_page_ending', timestamp)
time_on_page = json.dumps(TIME_ON_PAGE_TO_READ)
return render_template('schedule_ending.html', time_on_page=time_on_page)
@app.route('/mood', methods=['GET'])
def mood():
time_on_page = json.dumps(TIME_ON_PAGE_MOOD)
return render_template('mood.html', time_on_page=time_on_page)
@app.route('/store_mood', methods=['POST'])
def store_mood():
# print(request.is_json)
req_data = request.get_json()
# print(req_data)
mood = req_data['mood']
# print(mood)
task = req_data['task']
timestamp = datetime.now()
# test first to store in DB with given values.
# mood = 'happy'
# task = 'first_task'
# timestamp = datetime.now()
db_entry = MoodTime(mood=mood, task=task, timestamp=timestamp)
db.session.add(db_entry)
db.session.commit()
return 'mood saved on DB'
# return 'testing POST'
def store_av_record_time(start_time, end_time):
db_entry = AVRecordTime(timestamp_start=start_time, timestamp_end=end_time)
db.session.add(db_entry)
db.session.commit()
if __name__ == '__main__':
# comment the lines below
# app.app_context().push()
# db.drop_all()
# db.create_all()
# app.run(debug=True, host='0.0.0.0', port=90)
app.run(debug=False, host='0.0.0.0', port=5000)