-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
211 lines (177 loc) · 6.32 KB
/
app.js
File metadata and controls
211 lines (177 loc) · 6.32 KB
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
/*
* @file: app.js
* @author: Matt Russell
* @description: Express app which coordinates the study. Responsibilities are:
* Serving html files:
* 1) For a 'backend' client to receive participants' webcam data over peerjs
* This would be loaded by a member of the study team; files are password protected
* 2) For the 'frontend' client (participant) to run the study
* Receiving metadata from qualtrics and coordinating it with the 'backend' client html page.
* Saving the metadata and webcam data over scp2.
* @notes: Config variables near the top need to be filled in with Heroku secrets or the like.
*/
const express = require('express')
const app = express()
const http = require('http').Server(app)
const io = require('socket.io')(http, {serveClient:false})
const cors = require('cors')
const path = require('path')
const _uuid = require('uuid')
const open = require('open')
const port = process.env.PORT || 8001
const Client = require('scp2').Client
const secure = require('express-force-https')
app.use(express.urlencoded( { extended: true } ) )
app.use(express.json())
app.use(cors())
app.use(secure)
app.use(express.static(path.join(__dirname, 'public')))
http.listen(port)
/* =========================================
* CONFIG VARIABLES
* =========================================
*/
// scp2 server address to send data files
const HOST = '';
const USER = '';
const PASS = '';
const save_path_client = new Client( {
host: HOST,
username: USER,
password: PASS
});
// basic auth on the server_index.html page
const SERVER_AUTH_USER = '';
const SERVER_AUTH_PASS = '';
const LOCAL_SAVE_PATH = '';
// secret key for the uuid generation
const UUID_NAMESPACE = '';
// participants object to store metadata
const participants = {};
const get_uuid = (name) => _uuid.v5(name, UUID_NAMESPACE);
/*
* @function: 'connection'
* @description: incoming socket.io connection event.
* @notes: This is a connection initiatied by the participant's machine.
* 'register_user' will be the first message sent by the client, at which
* point we create the uuid for the participant and save that participant's socket.
*/
io.on('connection', socket => {
socket.on('register_user', (args) => {
console.log("registering user - ARGS: " + args)
let uuid = get_uuid(args.name.toLowerCase());
participants[uuid] = { socket: socket, done: false, uuid: uuid };
socket.emit('uuid', uuid);
});
})
/*
* @function backendAuth
* @description: Middleware function to authenticate the backend client
*/
const backendAuth = (req, res, next) => {
const reject = () => {
res.setHeader('www-authenticate', 'Basic');
res.sendStatus(401);
}
const authorization = req.headers.authorization;
if (!authorization) {
return reject();
}
const [username, password] = Buffer.from(authorization.replace('Basic ', ''), 'base64').toString().split(':')
if (! (username === SERVER_AUTH_USER && password === SERVER_AUTH_PASS)) {
return reject();
}
next();
};
/*
* Static endpoints. '/private' folder is password protected.
*/
app.use('/private', backendAuth);
app.use('/private', express.static('private'));
app.use(express.static('public', { index: 'index.html' }));
/*
* @function: save_data
* @description: Save participant's metadata as a JSON file
* @params: pdata - participant's metadata
* @returns: none
* @notes: make a deep copy of the data in order to drop the non-JSON serializable socket
*/
function save_data(pdata) {
let savedata = {};
for ([key, val] of Object.entries(pdata)) {
if (key != 'socket') {
savedata[key] = val;
}
}
savedata['stop_time'] = Date.now();
save_path_client.write(
{
destination: LOCAL_SAVE_PATH + pdata.uuid + ".json",
content: Buffer.from(JSON.stringify(savedata))
},
function(err) {
if (err) {
console.log("error saving timestamps " + err);
}
}
);
}
/*
* =========================================
* QUALTRICS ENDPOINTS
* =========================================
*/
/*
* @endpoint: /get_uuid
* @method: POST
* @description: given a participant's name, lookup their uuid.
* @returns: sends the participant's uuid back to the client, or 'failed' if the name is not found
*/
app.post('/get_uuid', (req, res) => {
const uuid = get_uuid(req.body.name.toLowerCase());
res.send( { uuid: Object.keys(participants).includes(uuid) ? uuid : 'failed' } );
return;
})
/*
* @endpoint: /qualtrics
* @method: POST
* @description: sends to us which video the participant watched.
* @returns: "done" so that Qualtrics knows to continue.
* @effects: Save the metadata.
*/
app.post('/qualtrics', (req, res) => {
let location = req.body.location
let video = req.body.video_name
let uuid = req.body.uuid
console.log("received info from qualtrics")
console.log("location: " + location)
console.log("video: " + video)
console.log("uuid: " + uuid)
if (participants[uuid].done) {
res.send("done")
return
}
if (!(video in participants[uuid])) {
participants[uuid][video] = {
'start_video': false,
'stop_video': false,
'start_emotion': false,
'stop_emotion': false,
'start_quiz': false,
'stop_quiz': false
}
}
// qualtrics sends each request 3x, only process the first
if (participants[uuid][video][location] == false) {
// note the time
participants[uuid][video][location] = Date.now();
// send metadata to the backend
participants[uuid].socket.emit(location, video);
save_data(participants[uuid]);
// study is over, so save the json data
if (location == 'end_study') {
participants[uuid]['done'] == true;
}
}
res.send("done");
});