-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
129 lines (99 loc) · 3.83 KB
/
server.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
from flask import Flask, render_template, request, jsonify
import os
import feedback
from datetime import datetime
import pandas as pd
app = Flask(__name__)
@app.route('/')
def index():
if not os.path.isdir("./static/data"):
os.mkdir("./static/data")
return render_template('index.html')
@app.route('/process_log', methods=['POST'])
def process_log():
data = request.json
user_id = data['user_id']
message = data['message']
add_log_entry(user_id, message)
return jsonify({'message': "Log received"})
@app.route('/process_user_id', methods=['POST'])
def process_user_id():
data = request.json
user_id = data['user_id']
condition = data['condition']
# make folder for user data
user_path = f"./static/data/{user_id}"
if not os.path.isdir(user_path):
os.mkdir(user_path)
# save condition
condition_path = "./static/data/conditions.csv"
if not os.path.isfile(condition_path):
with open(condition_path, "w") as f:
f.write("time,user_id,condition\n")
with open(condition_path, "a") as f:
f.write(f"{datetime.now()},{user_id},{condition}\n")
# start log file
add_log_entry(user_id, "Received user ID and condition, start log")
# create file for survey responses
with open(f"./static/data/{user_id}/survey_responses.csv", "w") as f:
f.write("trial,motivation,manageable,actionable,timely,reflection,outcome\n")
return jsonify({'message': "User ID processed"})
@app.route('/process_trial_start', methods=['POST'])
def process_trial_start():
data = request.json
user_id = data['user_id']
trial = data['trial']
# make folder for trial data
trial_path = f"./static/data/{user_id}/trial_{trial}"
if not os.path.isdir(trial_path):
os.mkdir(trial_path)
# start log file
add_log_entry(user_id, f"Trial {trial} started")
# create file for trial data
with open(f"./static/data/{user_id}/trial_{trial}/trajectory.csv", "w") as f:
f.write("x,y_js,vx,vy,phi,ux,uy_js\n")
return jsonify({'message': "Trial started"})
@app.route('/process_survey_responses', methods=['POST'])
def process_survey_responses():
data = request.json
user_id = data['user_id']
trial = data['trial']
motivation = data['motivation']
manageable = data['manageable']
actionable = data['actionable']
timely = data['timely']
reflection = data['reflection']
outcome = data['outcome']
# start log file
add_log_entry(user_id, f"Received survey responses")
with open(f"./static/data/{user_id}/survey_responses.csv", "a") as f:
f.write(f"{trial},{motivation},{manageable},{actionable},{timely},{reflection},{outcome}\n")
return jsonify({'message': "Survey responses received"})
@app.route('/process_trajectory', methods=['POST'])
async def process_trajectory():
data = request.json
user_id = data['user_id']
trial = data['trial']
log = data['log']
# start log file
add_log_entry(user_id, f"Received trajectory data")
with open(f"./static/data/{user_id}/trial_{trial}/trajectory.csv", "a") as f:
f.write(log)
# fix y positions
df = pd.read_csv(f"./static/data/{user_id}/trial_{trial}/trajectory.csv")
df['y_py'] = -(df['y_js'] - 600)
df['uy_py'] = -df['uy_js']
df.to_csv(f"./static/data/{user_id}/trial_{trial}/trajectory.csv", index=False)
return jsonify({'message': "Trajectory data received"})
@app.route('/wait_for_feedback/<userid>/<trial>')
async def wait_for_feedback(userid, trial):
await wait(userid, trial)
return "Complete"
def add_log_entry(user_id, entry):
with open(f"./static/data/{user_id}/log.txt", "a") as f:
f.write(f"{datetime.now()}: {entry}\n")
async def wait(userid, trial):
await feedback.main(userid, trial)
return "Complete"
if __name__ == '__main__':
app.run(debug=True)