-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
214 lines (188 loc) · 7.1 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const express = require('express')
const app = express()
const port = 3000
const fs = require('fs')
app.use(express.static("public"));
// default storage state
var obj = `{
"robot_type" : "dancebot",
"num_connected" : 0,
"robots" : [],
"offRobots" : []
}`;
// Write the storage default into 'Output.txt' .
fs.writeFile('public/output.json', obj, (err) => {
// In case of a error throw err.
if (err) throw err;
})
// open up the endpoint /robotJoin to a GET request
app.get('/robotJoin', (req, res) => {
console.log("\n\nreq query: ", req.query);
var exists = false;
let output = getDataFromFile();
console.log("output: ", output);
var robocharge = "100%";
var robomove = "stop";
var roboecol = "blue";
var roboexp = "happy";
if(req.query['charge']!=null){
robocharge = req.query['charge'];
}
if(req.query['move']!=null){
robomove = req.query['move'];
}
// if(req.query['eyecol']!=null){
// roboecol = req.query['eyecol'];
// }
if(req.query['expression']!=null){
roboexp = req.query['expression'];
}
if(req.query['id'] != null){
var botlist = output.robots;
for(i = 0; i < output.num_connected; i++){
console.log(botlist[i]);
let robot = botlist[i];
console.log(robot['id']);
if(robot['id'] === req.query['id']){
console.log('successful find!');
exists = true;
}
}
for(i = 0; i < output.offRobots.length; i++){
if(output.offRobots[i].id == req.query["id"]){ // Remove robot from off list if it's on it.
output.offRobots.splice(i, 1);
break;
}
}
}
// if robot is not specified, set him up with default settings and add id
if(req.query['id'] == null){
output.num_connected ++;
var auto = 'auto_add_';
var ROBOT_ID = auto.concat(output.num_connected.toString());
var duck = `{"id":"${ROBOT_ID}","charge":"${robocharge}","move":"${robomove}","expression":"${roboexp}"}`;
// var duck = `{"id":"${ROBOT_ID}","charge":"${robocharge}","move":"${robomove}","eyecol":"${roboecol}","expression":"${roboexp}"}`;
output.robots.push(JSON.parse(duck));
saveDataToFile(output);
}
else if(!exists){
output.num_connected ++;
var ROBOT_ID = req.query['id'];
var duck = `{"id":"${ROBOT_ID}","charge":"${robocharge}","move":"${robomove}","expression":"${roboexp}"}`;
// var duck = `{"id":"${ROBOT_ID}","charge":"${robocharge}","move":"${robomove}","eyecol":"${roboecol}","expression":"${roboexp}"}`;
output.robots.push(JSON.parse(duck));
saveDataToFile(output);
}
console.log(output);
res.status(200).send('Number of Bots Added: ' + output.num_connected);
});
app.get("/robotUpdate", (req, res) => {
// Find particular robot (either on or off) if id specified. Otherwise, return entire JSON output.
let fileData = getDataFromFile();
let output = {"id": req.query["id"], charge: '100%', move: 'stop', expression: 'happy', "status": "off"}; // Default starting point for all robots.
if(req.query['id'] != null){
let exists = false;
for(let i = 0; i < fileData.num_connected; i++){
if(fileData.robots[i]['id'] === req.query['id']){
// Found robot and it's on.
output = fileData.robots[i];
output["status"] = "on";
exists = true;
break;
}
}
if(!exists){
// Look to see if it was turned off. Robots that were turned on then off are saved so they can pick up right where they left off. Robots that were never turned on are not since they'll start from the default.
for(let j = 0; j < fileData.offRobots.length; j++){
if(fileData.offRobots[j]['id'] === req.query['id']){
// Found robot and it's off.
output = fileData.offRobots[j];
output["status"] = "off";
break;
}
}
}
}
console.log("robotUpdate returning ", output);
res.send(output);
})
app.get('/robotLeave', (req, res) => {
console.log("\n\nreq query: ", req.query);
let output = getDataFromFile();
console.log("output: ", output);
if(req.query['id'] != null){
var botlist = output.robots;
for(i = 0; i < output.num_connected; i++){
console.log(botlist[i]);
let robot = botlist[i];
console.log(robot['id']);
if(robot['id'] === req.query['id']){
console.log('successful find!');
output.robots.splice(i, 1);
output.num_connected--;
output.offRobots.push(robot)
saveDataToFile(output);
}
}
}
res.status(200).send('Successfully removed bot: ' + req.query['id'])
console.log(output);
});
app.get('/userMove', (req, res) => {
console.log("\n\nreq query: ", req.query);
var exists = false;
let output = getDataFromFile();
console.log("output: ", output);
if(req.query['id'] != null && req.query['move'] != null){
var botlist = output.robots;
for(i = 0; i < output.num_connected; i++){
console.log(botlist[i]);
let robot = botlist[i];
console.log(robot['id']);
if(robot['id'] === (req.query['id'])){
console.log('successful find!');
robot['move'] = req.query['move'];
saveDataToFile(output);
}
}
}
res.status(200).send('Successfully changed movement of bot: ' + req.query['id'])
console.log(output);
});
app.get('/userExpress', (req, res) => {
console.log("\n\nreq query: ", req.query);
var exists = false;
let output = getDataFromFile();
console.log("output: ", output);
if(req.query['id'] != null && req.query['expression'] != null){
var botlist = output.robots;
for(i = 0; i < output.num_connected; i++){
console.log(botlist[i]);
let robot = botlist[i];
console.log(robot['id']);
if(robot['id'] === (req.query['id'])){
console.log('successful find!');
robot['expression'] = req.query['expression'];
saveDataToFile(output);
}
}
}
res.status(200).send('Successfully changed expression of bot: ' + req.query['id'])
console.log(output);
});
// listen at port 3000
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));
// getDataFromFile returns a JSON object from a file.
function getDataFromFile() {
// read the output.json file and parse the data
let rawdata = fs.readFileSync('public/output.json');
let output = JSON.parse(rawdata);
console.log("Output.json has ", output);
return output;
}
// saveDataToFile saves a JSON object to a file.
function saveDataToFile(data) {
fs.writeFile('public/output.json', JSON.stringify(data), (err) => {
if (err) throw err;
})
}