This repository has been archived by the owner on May 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
195 lines (162 loc) · 6.11 KB
/
index.js
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
var app = require('express')();
var http = require('http').Server(app);
var socket = require('socket.io')(http);
var mongoose = require('mongoose');
var fs = require('fs');
var readline = require('readline');
var { google } = require('googleapis');
// Note: `$ sudo mongod` to run mongoDB database
// Connect server to mongoDB and use the recon database.
mongoose.connect('mongodb://localhost:27017/recon');
const Schema = mongoose.Schema;
// Define MongoDB Match Document Schema
var matchDocument = mongoose.Schema({
team: { type: Number },
match: { type: String },
color: { type: String },
position: { type: Number },
auton: {
auton: { type: Boolean },
passed_baseline: { type: Boolean },
placed_switch: { type: Boolean },
placed_opponents_switch: { type: Boolean },
placed_scale: { type: Boolean }
},
teleop: {
cubes_home_switch: { type: Number },
cubes_away_switch: { type: Number },
cubes_scale: { type: Number },
cubes_vault: { type: Number },
defense: { type: Number },
cubes_dropped: { type: Number },
fall: { type: Boolean }
},
end: {
climber: { type: Boolean },
climb_aid: { type: Number },
fouls: { type: Number },
score: { type: Number },
comments: { type: String }
}
});
// Create the moongoose model based on the schema defined. Target DB collection is matches.
// Use 'Matche' as mongoose looks for the plural of the DB collection. (Matche -> matches)
var MatchModel = mongoose.model('Matche', matchDocument);
// Read Google authentication from client_secret.json file
fs.readFile('client_secret.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
authorize(JSON.parse(content));
});
// Authorize the Google API using the credentials in client_secret.json.
function authorize(credentials) {
const {
client_secret,
client_id,
redirect_uris
} = credentials.installed;
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);
// Check if we have previously stored a token.
fs.readFile('credentials.json', (err, token) => {
if (err) return getNewToken(oAuth2Client);
oAuth2Client.setCredentials(JSON.parse(token));
runServer(oAuth2Client);
});
}
function getNewToken(oAuth2Client) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: ['https://www.googleapis.com/auth/spreadsheets'],
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({ input: process.stdin, output: process.stdout, });
rl.question('Enter the code from that page here: ', (code) => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
oAuth2Client.setCredentials(token);
// Store the token to disk for later program executions
fs.writeFile('credentials.json', JSON.stringify(token), (err) => {
if (err) console.error(err);
console.log('Token stored to: credentials.json');
});
});
});
}
function addMatchToSheets(auth, data) {
const sheets = google.sheets({ version: 'v4', auth });
data = [ data ]
sheets.spreadsheets.values.append({
spreadsheetId: '1KV9_zPn-bgrKnPpb4sILBTk9PhdUjN5nAKE8fUPQjiM',
range: 'A4:T',
valueInputOption: 'USER_ENTERED',
resource: {
values: data
}
}, (err, res) => {
if (err) {
throw err;
}
console.log(res.data);
}
);
}
function runServer(auth) {
// Run server on port 8080
socket.listen(8080, function() {
console.log('Listening on port 8080');
});
socket.on('connect', function(client) {
console.log('A client connected with ID', client.id)
client.on('submit_match', (match) => {
match = JSON.parse(match);
// Create new document based on Match document schema and save it to the mongoDB database.
let match_model = new MatchModel(match);
match_model.save(function (err) {
if (err) return console.error(err);
});
data = [
match.team,
undefined,
undefined,
match.match,
match.color + match.position,
match.auton.auton ? 1 : 0,
match.auton.passed_baseline ? 1 : 0,
match.auton.placed_switch ? 1 : 0,
match.auton.placed_scale ? 1 : 0,
match.auton.placed_opponents_switch ? 1 : 0,
match.teleop.cubes_home_switch,
match.teleop.cubes_vault,
match.teleop.cubes_scale,
match.teleop.cubes_away_switch,
match.teleop.cubes_dropped,
match.teleop.defense,
match.end.climber ? 1 : 0,
match.end.climb_aid,
match.teleop.fall ? 1 : 0,
undefined,
match.end.comments
]
addMatchToSheets(auth, data);
// Send all connected clients the new match.
socket.emit('submit_match', match);
console.log("Match submitted");
});
client.on('request_update', function() {
console.log("Update requested");
// Finds all documents in database that use the Match document schema and
// send each document (individually) to the client who requested the data.
MatchModel.find(async function (err, matches) {
if (err) return console.error(err);
for (match of matches) {
await socket.to(client.id).emit('submit_match', match);
}
})
});
client.on('reconnect', function(reconnect) {
console.log('A client reconnected with ID', reconnect.id);
});
client.on('disconnect', () => {
console.log('A client disconnected');
});
});
}