Skip to content

Instantly share code, notes, and snippets.

@KeanW
Last active January 8, 2016 07:59
Show Gist options
  • Save KeanW/eaef42b389f4735f2f38 to your computer and use it in GitHub Desktop.
Save KeanW/eaef42b389f4735f2f38 to your computer and use it in GitHub Desktop.
Node web-service to control Ollie and BB-8 robots using Cylon.js
// robots-server.js
var express = require('express');
var app = express();
var robots = require('./bb8-ollie-cylon-controller');
var port = process.env.PORT || 8080;
var router = express.Router();
router.route('/robots')
// Get all the robots
// (accessed at GET http://localhost:8080/api/robots)
.get(function(req, res) {
var cont = robots.controller();
res.send(cont.connectedRobots);
});
// Routes that end in /robots/:robot_id
router.route('/robots/:robot_id')
// Get the robot with this id
// (accessed at GET http://localhost:8080/api/robots/:robot_id)
.get(function(req, res) {
var cont = robots.controller();
cont.wake([req.params.robot_id]);
res.send(req.params.robot_id);
});
router.route('/robots/:robot_id/:command')
// Send a command to the robot with this id
// (accessed at GET http://localhost:8080/api/robots/:robot_id/:command)
.get(function(req, res) {
var robot = [req.params.robot_id];
var cont = robots.controller();
// For now we only support move commands
cont.moveInDirection(req.params.command, robot);
res.send(req.params.command);
});
// All routes prefixed with /api
app.use('/api', router);
// Start the server
app.listen(port);
console.log('Listening on port ' + port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment