-
Notifications
You must be signed in to change notification settings - Fork 5
RestClient
RestClient is a small REST client that you can use to write JavaScript programs that control a firenodejs robot remotely via REST.
rest-demo.js shows how to use RestClient. In this demo, we do two things:
- Step 1. home FPD
- Step 2. move FPD to (x:50,y:50}
In the firenodejs directory, type the following bash command, substituting PI2 with the hostname or ip address of the firenodejs server:
node js/rest-demo.js -h PI2
Your FPD should move and you'll see something like the following:
START : rest-demo.js
INFO : Step 1. Homing...
HTTP : POST http://localhost:8080/firestep
HTTP : <= [{"hom":""},{"mpo":""}]
END : rest-demo.js
HTTP : => {"s":0,"r":{"mpo":{"1":0,"2":0,"3":0,"x":0,"y":0,"z":0,"xn":0,"yn":0,"zn":0}},"t":0.001}
INFO : FPD is at position x: 0 y: 0 z: 0
INFO : Step 2. Move to (50,50)...
HTTP : POST http://localhost:8080/firestep
HTTP : <= [{"mov":{"x":50,"y":50}},{"mpo":""}]
HTTP : => {"s":0,"r":{"mpo":{"1":1654,"2":-1025,"3":957,"x":49.999,"y":49.998,"z":-0.002,"xn":50,"yn":50,"zn":0}},"t":0.001}
INFO : FPD is at position x: 49.999 y: 49.998 z: -0.002
We first include everything we need and set up defaults:
var should = require("should");
var RestClient = require("./RestClient");
console.log("START\t: rest-demo.js");
var restOptions = {
protocol: "http://",
host: "localhost",
port: "8080",
};
Now we read command line arguments:
for (var i=1; i < process.argv.length; i++) {
var arg = process.argv[i];
if (arg === "-h") {
(++i).should.below(process.argv.length);
restOptions.host = process.argv[i];
}
}
We create a RestClient with our updated options:
var rest = new RestClient(restOptions);
And declare the actions we wish to perform:
function step1() {
console.log("Step 1. Homing...");
rest.post("/firestep", [{hom:""},{mpo:""}], function(data) { // Step 1 http response callback
console.log("FPD is at position x:", data.r.mpo.x, " y:", data.r.mpo.y, " z:", data.r.mpo.z);
step2(); // do AFTER step1 is done
});
}
function step2() {
console.log("Step 2. Move to (50,50)...");
rest.post("/firestep", [{mov:{x:50,y:50}},{mpo:""}], function(data) { // Step 2 http response callback
console.log("FPD is at position x:", data.r.mpo.x, " y:", data.r.mpo.y, " z:", data.r.mpo.z);
});
}
Now we kick off step1() and sit back till it's done. Notice that we do not call step2() directly--we have to WAIT until step1 is done:
step1();
// step2(); <= DANGER! we can't just put step2 here because it will run in PARALLEL!
console.log("END\t: rest-demo.js");
Using RestClient, you can orchestrate the behavior of one or more FPD(s), doing arbitrary calculations with the returned REST results.
Have fun!