forked from hcilabusf/language_learning_app_hci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
160 lines (123 loc) · 4.81 KB
/
main.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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import flask
import time
import uuid
import threading
from pages import PAGES, Page
from survey import SURVEY
from simple_server import SocketServer
from flask import flash, redirect, render_template, request, session, url_for
app = flask.Flask(__name__)
app.config['DEBUG'] = True
app.config['SESSION_TYPE'] = 'filesystem'
app.debug = True
app.secret_key = 'dfasdfasdfasdf'
trialNum = 0
canSendMarker = False
sockett = None # socket variable created
#SOCKET_SERVER_IP = "127.0.0.1"
#SOCKET_SERVER_IP = "192.168.1.100"
SOCKET_SERVER_IP = "0.0.0.0" # Might require change
SOCKET_SERVER_PORT = 8080 # Might require change
SERVER = None
def send_data(class_label):
global trialNum
print("sending!")
milliSec = int(round(time.time() * 1000)) # Get current time in milliseconds
data = "{};{};{};\n".format(trialNum, class_label, milliSec)
trialNum += 1 # increment trial number
SERVER.broadcast(data)
""" Runs the server in a separate thread
:param host: Server ip address
:param port: Server port
"""
def open_server(host, port):
global sockett
connected = openSocket()
if connected:
print("Connected sockett")
t1 = threading.Thread(target=run_server, args=(SOCKET_SERVER_IP, SOCKET_SERVER_PORT))
t1.start()
""" Initiate the server
:param host: Server ip address
:param port: Server port
"""
def run_server(host, port):
print("Opening server")
global SERVER
SERVER = SocketServer(SOCKET_SERVER_IP, SOCKET_SERVER_PORT)
SERVER.accept_connections()
@app.route('/question/')
def question_start():
send_data('baselinestart')
session['uid'] = uuid.uuid1()
time.sleep(3)
send_data('baselineend')
time.sleep(0.5)
send_data('easystart')
return redirect(url_for('question', page=0))
@app.route('/question/<int:page>', methods=['GET', 'POST'])
def question(page):
global canSendMarker
page_str = str(page)
# this is called after I press next on the survey
if canSendMarker:
if page == 10: # I will start with hard tasks
#sendData('hardstart')
send_data('hardstart')
canSendMarker = False # prevent from sending markers on wrong answers
if page >= len(PAGES):
session.pop('error_count')
session.pop('uid')
return render_template("cong.html")
# Adding condition for method!=POST so, we only send marker when page loads,
# not twice, for when page loads, and when the user submits an answer.
if PAGES[page].marker_data!='' and request.method!='POST':
sendData(PAGES[page].marker_data)
if request.method == 'POST':
expect = PAGES[page].answer
if expect == '':
return redirect(url_for('question', page=page + 1))
got = request.form['answer']
if got != expect:
if PAGES[page].is_test:
return redirect(url_for('question', page=page+1))
else:
session['error_count'][page_str] += 1
flash('Wrong!')
return redirect(url_for('question', page=page))
else: # means the answer is correct
canSendMarker = True
if canSendMarker:
if page == 9: # I finished the easy and will start survey so I will send easyend before starting the survey
send_data('easyend')
# I didn't change canSendMarker to false here because I need to send hard start when the new page 10 is loaded
if page == len(PAGES) - 1: # I finished the hard and will start survey so I will send hardend before starting the survey
send_data('hardend')
if PAGES[page].show_survey != 0:
return redirect(url_for('survey', survey_num=PAGES[page].show_survey, next_page=page + 1))
else:
flash('Correct!')
session['error_count'][page_str] = 0
return redirect(url_for('question', page=page + 1))
if 'error_count' not in session:
session['error_count'] = {}
if page_str not in session['error_count']:
session['error_count'][page_str] = 0
return render_template('question.html', page=PAGES[page], current_page=page_str)
@app.route('/')
def hello_world():
open_server(SOCKET_SERVER_IP, SOCKET_SERVER_PORT)
return render_template("welcomePage.html")
@app.route('/survey/<int:survey_num>')
def survey(survey_num):
next_page = request.args.get('next_page')
message = ''
if survey_num == 1:
message = 'end easy test, begin survey'
if survey_num == 2:
message = 'end med test, begin survey'
if survey_num == 3:
message = 'end hard test, begin survey'
return render_template('survey.html', survey_num=survey_num, next_page=next_page, url=SURVEY[survey_num - 1])