Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

/robotJoin - make a robot join the server! #1

Open
dimembermatt opened this issue Aug 12, 2020 · 0 comments
Open

/robotJoin - make a robot join the server! #1

dimembermatt opened this issue Aug 12, 2020 · 0 comments
Assignees
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@dimembermatt
Copy link
Contributor

Context

We already have a bit of skeleton code in index.js:

// open up the endpoint /robotJoin to a GET request
app.get('/robotJoin', (req, res) => {
    console.log("req query: ", req.query);

    let output = getDataFromFile();
    // if this is the first robot who connects, let's set him up as something random.
    if(output.num_connected == 0){
        output.num_connected ++;
        var ROBOT_ID = 72;
        var duck = `{"${ROBOT_ID}": {"robot_state": "rose","robot_eye_color": "photo","robot_expression": "light"}}`;
        output.robots.push(JSON.parse(duck));
        saveDataToFile(output);
    }
    console.log(output);

    res.status(200).send('Hello World You are Robot ' + output);
});

How it works

We see that we set up a GET endpoint at localhost:3000/robotJoin. We open up output.json and then parse through it, and then add a fake robot to the data and save it back up.

As an example, if we empty the contents of output.json and start up the server, we see that the program state has initialized the file to be:

{
    "robot_type" : "dancebot",
    "num_connected" : 0,
    "robots" : []
}

We can type in localhost:3000/robotJoin into the browser and see that the console has received the request:

Example app listening at http://localhost:3000
req query:  {}
... some other debugging output here

We can then check our output.json and see that it has been updated with fake robot data!

{
    "robot_type": "dancebot",
    "num_connected": 1,
    "robots": [
        {
            "72": {
                "robot_state": "rose",
                "robot_eye_color": "photo",
                "robot_expression": "light"
            }
        }
    ]
}

Note that this is after formatting the data to better view it (It's typically in a single line).

Alright, now what?

We can send more information to the server through our GET endpoint.
If we type in localhost:3000/robotJoin?id=0&status=alive into the browser, our req query turns into the following:

Example app listening at http://localhost:3000
req query:  { id: '0', status: 'alive' }

GET request parameters

We want to have robots join the server, and provide an initial state on start up.
Therefore, we should fashion a GET request that does the following:

  • provide an unique identifier for the robot
  • provide their status:
    • state of charge
    • current movement
    • current eye color
    • current expression

Our url might look like the following:
localhost:3000/robotJoin?id=Blinky&soc=85%&move=ankles ...(and so on)

A list of the current movements are:

  • Ankles
  • Demo1
  • Demo2
  • Hop
  • Walk
  • Wiggle

Eye colors and expressions haven't been determined yet.

After we send this data through the endpoint, they are available from the request query; we can access a parameter from req.query.PARAM. For example, we can access the parameter id with req.query.id.

Dealing with robots joining

Instead of the current code, where we check if nobody is connected and then build a fake robot, we should check for these parameters and build a robot for it in the data if it does not exist yet.

This means getting the file, checking through a list to see if a robot by the same ID exists, and if it doesn't, pushing a new robot with the url parameters.

We can get the list of robots currently in our database with the following:

let output = getDataFromFile();
let robots = output.robots;
let found = false;
for (let i = 0; i < robots.num_connected; i ++) {
   let robot = robots[i];
   let robotID robots.id;
   // check to see if the robot ID matches our request ID
   // if it is, set found to be true
}
// if we found the robot, do nothing, but if we didn't add it!
// we can use lines 29-30 in `index.js` as an example how:
// build a robot
var ROBOT_ID = 72;
var duck = `{"${ROBOT_ID}": {"robot_state": "rose","robot_eye_color": "photo","robot_expression": "light"}}`;
// then push it to the array
output.robots.push(JSON.parse(duck));

// once you're done, save it back to the file!

Finally, once you're able to save the robot to the file and add multiple robots, we should send back a success message. Make it fancy! Update line 35 in index.js for this.

Bonus

If you have this working, now make a /robotLeave endpoint!

@dimembermatt dimembermatt added good first issue Good for newcomers help wanted Extra attention is needed labels Aug 12, 2020
This was referenced Aug 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants