-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
142 lines (123 loc) · 3.58 KB
/
server.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
console.log('Server-side code running');
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
var request = require('request');
const app = express();
const FRC_SEASON = "2019"
// serve files from the public directory
app.use(express.static('public'));
// connect to the db and start the express server
let db;
// Replace the URL below with the URL for your database
MongoClient.connect("mongodb://localhost", (err, client) => {
if (err) {
return console.log(err);
}
db = client.db("otr-scouting");
// start the express web server listening on 8080
app.listen(8081, () => {
console.log('listening on 8081');
});
loadEvents();
loadSchedules();
});
// serve the homepage
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
// add a document to the DB collection recording the click event
app.post('/clicked', (req, res) => {
const click = { clickTime: new Date() };
console.log(click);
db.collection('clicks').save(click, (err, result) => {
if (err) {
return console.log(err);
}
console.log('click added to db');
res.sendStatus(201);
});
});
// Handles adding scouting data to the database.
app.get('/:event/:match/:robot/:action/:position', (req, res) => {
var scoutPoint = {
pointtime: new Date(),
event: req.params['event'],
match: req.params['match'],
robot: req.params['robot'],
action: req.params['action'],
position: req.params['position']
}
db.collection(FRC_SEASON).insertOne(scoutPoint, function (err, res) {
console.log("Inserted: " + scoutPoint);
res.sendStatus(201);
});
});
// Handles reading event pages.
app.get('/data/:event/', (req, res) => {
});
app.get('/data', (req, res) => {
res.sendFile(__dirname + '/public/data.html');
});
app.get('/match', (req, res) => {
res.sendFile(__dirname + '/public/match.html');
});
// get the click data from the database
app.get('/clicks', (req, res) => {
db.collection('clicks').find().toArray((err, result) => {
if (err) return console.log(err);
res.send(result);
});
});
function getScoutedPoints(event, callback) {
var query = { event: event };
db.collection(FRC_SEASON).find(query).toArray(function (err, result) {
if (!err)
callback(result);
});
}
function loadEvents() {
teams = [1334, 1374];
teams.forEach(team => {
frcapi("events?teamNumber=" + team, (data) => {
data["Events"].forEach(event => {
db.collection("events").replaceOne(event, event, { upsert: true }, (err, res) => {
if (!err) {
console.log("Updated event: " + event['code']);
}
});
});
});
});
}
function frcapi(args, callback) {
username = "jamiesinn",
password = "31DA05E9-230F-4542-9EE6-5371962BE301",
url = "https://frc-api.firstinspires.org/v2.0/" + FRC_SEASON + "/" + args,
auth = "Basic " + new Buffer(username + ":" + password).toString("base64");
request(
{
url: url,
headers: {
"Authorization": auth,
"Accept": "application/json"
}
},
function (error, response, body) {
data = JSON.parse(body);
callback(data);
}
);
}
function loadSchedules() {
db.collection("events").find().toArray((err, result) => {
result.forEach(event => {
frcapi("schedule/" + event['code'] + "?tournamentLevel=qual", (data) => {
var matches = data['Schedule'];
matches.forEach(match => {
match['event'] = event['code'];
db.collection("matches").replaceOne(match, match, { upsert: true }, (err, res) => { });
});
});
});
});
}