-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
276 lines (241 loc) · 9.9 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
from __future__ import print_function
from flask import Flask, render_template, session, request
from flask_socketio import SocketIO, emit, disconnect
from session.participant import Participant
import csv
from desktopmagic.screengrab_win32 import (getDisplayRects,getRectAsImage)
# Set this variable to "threading", "eventlet" or "gevent" to test the
# different async modes, or leave it set to None for the application to choose
# the best option based on installed packages.
async_mode = None
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, async_mode=async_mode)
activeUser = None
################################################
############### Routing Requests ###############
################################################
# Initial routing request
@app.route('/')
def index():
return render_template('index.html', async_mode=socketio.async_mode)
# Request to render info.html
@app.route('/info')
def info():
return render_template('info.html', async_mode=socketio.async_mode)
# Request to render demographics.html
@app.route('/demographics')
def demographics():
return render_template('demographics.html', async_mode=socketio.async_mode)
# Request to render calibration.html
@app.route('/calibration')
def calibration():
return render_template('calibration.html', async_mode=socketio.async_mode)
# Request to render instruction.html
@app.route('/instruction')
def instruction():
return render_template('instruction.html', async_mode=socketio.async_mode)
# Request to render conditionInstruction.html
@app.route('/conditionInstruction')
def conditionInstruction():
return render_template('conditionInstruction.html', async_mode=socketio.async_mode)
# Request to render startpoint.html
@app.route('/startpoint')
def startpoint():
return render_template('startpoint.html', async_mode=socketio.async_mode)
# Request to render advertisement.html
@app.route('/advertisement')
def advertisement():
return render_template('advertisement.html', async_mode=socketio.async_mode)
# Request to render confidence.html
@app.route('/confidence')
def confidence():
return render_template('confidence.html', async_mode=socketio.async_mode)
# Request to render evaluation.html
@app.route('/evaluation')
def evaluation():
return render_template('evaluation.html', async_mode=socketio.async_mode)
# Request to render postInterview.html
@app.route('/postInterview')
def postInterview():
return render_template('postInterview.html', async_mode=socketio.async_mode)
# Request to render postInterview.html
@app.route('/preInterview')
def preInterview():
return render_template('preInterview.html', async_mode=socketio.async_mode)
###############################################
############### Socket Messages ###############
###############################################
# Logs incoming messages
@socketio.on('message')
def test_message(message):
app.logger.info('Incoming message: %s',message)
# Client request to start a new survey session
# New session will be created and confirmation send to client
@socketio.on('start')
def start_study(message):
global activeUser
activeUser = Participant(message['participantID'],message['budget'])
app.logger.info('Participant %s has started a new session',message['participantID'])
emit('start')
# Client request the demographics questionaire
# Send acknowledgement
@socketio.on('demographics')
def go_demographics():
app.logger.info('Participant proceeds to demographics questionaire!')
emit('demographics')
# Client request to proceed to calibration
# Send acknowledgement
@socketio.on('preInterview')
def go_preInterview(message):
global activeUser
app.logger.info('Participant proceeds to preInterview!')
print("demographics completed")
print(message)
print(activeUser.participantID)
with open('./log/demographics.csv', 'a') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
filewriter.writerow([str(activeUser.participantID),str(activeUser.budget),
str(message['gender']),str(message['age']),str(message['visionCorrection']),str(message['subject']),str(message['lastSearch']),str(message['futureSearch'])])
emit('preInterview')
@socketio.on('calibration')
def go_calibration():
app.logger.info('Participant proceeds to initial calibration!')
emit('calibration')
# Client request to proceed to calibration
# Send acknowledgement
@socketio.on('instruction')
def go_instruction():
global activeUser
app.logger.info('Participant proceeds to instruction!')
if(activeUser.condition == 'FAMILIARIZATION'):
emit('instruction')
else:
emit('conditionInstruction')
# Client request to proceed with session
# Send acknowledgement
@socketio.on('startpoint')
def go_startpoint():
app.logger.info('Participant proceeds to startpoint!')
emit('startpoint')
# Automatic request to load new advertisement
# Send acknowledgement
@socketio.on('advertisement')
def go_advertisement():
app.logger.info('Participant receives advertisement!')
emit('advertisement')
#Site is requesting new dataset to show as advertisement
@socketio.on('adRequest')
def load_Page():
global activeUser
adID, dataset = activeUser.getNewDataSet()
print(activeUser.participantID)
print(activeUser.condition)
print(adID)
print(dataset)
if activeUser.adaptationDecision2Votes is None or activeUser.adaptationDecision3Stages is None:
emit('adRequest',{'id':adID,'data':dataset,'adaptation':'None', 'state':activeUser.condition})
else:
if activeUser.condition == 'COLORING':
emit('adRequest', {'id': adID, 'data': dataset, 'adaptation': activeUser.adaptationDecision3Stages, 'state':activeUser.condition})
elif activeUser.condition == 'FADING':
emit('adRequest',{'id': adID, 'data': dataset, 'adaptation': activeUser.adaptationDecision2Votes, 'state': activeUser.condition})
else:
emit('adRequest', {'id': adID, 'data': dataset, 'adaptation': 'None', 'state':activeUser.condition})
activeUser.startRecording()
# Client request to go to confidence questionnaire
# Send acknowledgement
@socketio.on('confidence')
def go_confidence():
global activeUser
print(activeUser.participantID)
activeUser.stopRecording()
app.logger.info('Participant proceeds to confidence questionnaire!')
emit('confidence')
# Client submits confidence answers and system checks next steps
# Send acknowledgement and instruction to next step
@socketio.on('confidenceResponse')
def next_ad(message):
global activeUser
decision = str(message['decision'])
confidence = str(message['confidence'])
print(activeUser.lastAd)
lastAd = str(activeUser.lastAd)
adCounter = str(activeUser.adCounter)
id = str(activeUser.participantID)
state = str(activeUser.condition)
dirName = './log/p' + str(id)
with open(dirName + '/confidence.csv', 'a') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
filewriter.writerow([id,adCounter, lastAd, state, decision, confidence])
activeUser.updateState()
if(activeUser.condition == 'FAMILIARIZATION'):
emit('startpoint')
elif(activeUser.condition == 'TRAINING'):
if (activeUser.adCounter == 4):
emit('calibration')
else:
emit('startpoint')
elif(activeUser.condition == 'DEFAULT'):
if (activeUser.adCounter == 14 or activeUser.adCounter == 21 or activeUser.adCounter == 28):
emit('calibration')
else:
emit('startpoint')
elif(activeUser.condition == 'COLORING'):
if (activeUser.adCounter == 14 or activeUser.adCounter == 21 or activeUser.adCounter == 28):
emit('calibration')
else:
emit('startpoint')
elif(activeUser.condition == 'FADING'):
if (activeUser.adCounter == 14 or activeUser.adCounter == 21 or activeUser.adCounter == 28):
emit('calibration')
else:
emit('startpoint')
elif(activeUser.condition == 'DONE'):
emit('evaluation')
# Client request to go to interview
# Send acknowledgement
@socketio.on('interview')
def go_interview(message):
global activeUser
with open('./log/evaluation.csv', 'a') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
filewriter.writerow([
str(activeUser.participantID),
str(message['horrible']),
str(message['difficult']),
str(message['frustrating']),
str(message['reading']),
str(message['coloring']),
str(message['fading']),
str(message['structure']),
str(message['content']),
str(message['terminology']),
str(message['handling']),
str(message['instruction']),
str(message['defaultDensity']),
str(message['coloringDensity']),
str(message['fadingDensity']),
str(message['understandable'])
])
app.logger.info('Participant proceeds to interview!')
emit('interview')
# Client received payment, study completed
# Send acknowledgement
@socketio.on('end')
def end_study():
global activeUser
activeUser = None
app.logger.info('Participant has finished the user study!')
emit('end')
# Client requests to take a screenshot
@socketio.on('screenshot')
def screenshot(message):
adID = message['adID']
for displayNumber, rect in enumerate(getDisplayRects(), 1):
if displayNumber == 1:
imDisplay = getRectAsImage(rect)
imDisplay.save("./log/p" + str(activeUser.participantID) + "/screenshots/screenshot" + "_" + str(activeUser.adCounter) + "_" + str(activeUser.condition) + "_" + str(activeUser.lastAd)+"_display%d.png" % (displayNumber,), format='png')
if __name__ == '__main__':
socketio.run(app, debug=True)
# app.run()