2013-11-28 08:37:46 +08:00
|
|
|
# Robot Commands
|
|
|
|
|
|
|
|
This example demonstrates a feature of the Cylon API: running arbitrary commands
|
|
|
|
on robots over HTTP. To demonstrate this, we're going to make a basic robot,
|
|
|
|
with a custom command. This in and of itself is nothing to write home about, but
|
|
|
|
you'll be able to trigger the custom command by visiting this URL in your
|
|
|
|
browser:
|
|
|
|
|
|
|
|
```
|
|
|
|
http://localhost:8080/robots/frankie/commands/relax
|
|
|
|
```
|
|
|
|
|
|
|
|
First, let's make sure to load up Cylon:
|
|
|
|
|
2014-03-28 00:31:56 +08:00
|
|
|
var Cylon = require('../..');
|
2013-11-28 08:37:46 +08:00
|
|
|
|
2014-08-06 09:41:57 +08:00
|
|
|
Now that we've got that, let's set up the api:
|
2013-11-28 08:37:46 +08:00
|
|
|
|
2014-08-06 09:41:57 +08:00
|
|
|
Cylon.api();
|
2013-11-28 08:37:46 +08:00
|
|
|
|
2014-08-06 09:41:57 +08:00
|
|
|
And with that done let's define our robot:
|
2013-11-28 08:37:46 +08:00
|
|
|
|
2014-08-06 09:41:57 +08:00
|
|
|
Cylon.robot({
|
|
|
|
name: 'Frankie',
|
2013-11-28 08:37:46 +08:00
|
|
|
|
2014-08-06 09:41:57 +08:00
|
|
|
The result of this method will be returned to the HTTP client as part of a JSON
|
|
|
|
object.
|
2013-11-28 08:37:46 +08:00
|
|
|
|
2014-08-06 09:41:57 +08:00
|
|
|
sayRelax: function() {
|
|
|
|
return this.name + " says relax");
|
|
|
|
},
|
2013-11-28 08:37:46 +08:00
|
|
|
|
|
|
|
Since we don't really care what actual work this robot does, but need to keep it
|
2014-08-06 09:41:57 +08:00
|
|
|
busy, we'll just tell it to print it's name every five seconds.
|
2013-11-28 08:37:46 +08:00
|
|
|
|
2014-08-06 09:41:57 +08:00
|
|
|
work: function(my) {
|
|
|
|
every((5).seconds(), function() {
|
|
|
|
console.log(my.sayRelax());
|
2014-03-28 00:31:56 +08:00
|
|
|
});
|
2014-08-06 09:41:57 +08:00
|
|
|
},
|
2013-11-28 08:37:46 +08:00
|
|
|
|
2014-08-06 09:41:57 +08:00
|
|
|
We'll then set up the `commands` object, which tells the API which commands the
|
|
|
|
Robot has should be publically accessible:
|
2013-11-28 08:37:46 +08:00
|
|
|
|
2014-08-06 09:41:57 +08:00
|
|
|
commands: function() {
|
|
|
|
return {
|
|
|
|
say_relax: this.sayRelax
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
2013-11-28 08:37:46 +08:00
|
|
|
|
|
|
|
And now that all the pieces are in place, we can start up Cylon:
|
|
|
|
|
2014-03-28 00:31:56 +08:00
|
|
|
Cylon.start();
|
2013-11-28 08:37:46 +08:00
|
|
|
|
|
|
|
Now the robot will print it's name to the console, and Cylon will serve an API
|
|
|
|
to `localhost:8080`. Check it out!.
|