Execute robot commands from api

This commit is contained in:
Adrian Zankich 2013-11-25 14:28:13 -08:00
parent 0bf4d110a5
commit b46d4242ea
5 changed files with 65 additions and 1 deletions

26
dist/api.js vendored
View File

@ -52,6 +52,32 @@
return res.json(err ? err : robot.data());
});
});
this.server.get("/robots/:robotname/commands", function(req, res) {
return master.findRobot(req.params.robotname, function(err, robot) {
return res.json(err ? err : robot.data().commands);
});
});
this.server.all("/robots/:robotname/commands/:commandname", function(req, res) {
var key, params, value, _ref;
params = [];
if (typeof req.body === 'object') {
_ref = req.body;
for (key in _ref) {
value = _ref[key];
params.push(value);
}
}
return master.findRobot(req.params.robotname, function(err, robot) {
var result;
if (err) {
return res.json(err);
}
result = robot[req.params.commandname].apply(robot, params);
return res.json({
result: result
});
});
});
this.server.get("/robots/:robotname/devices", function(req, res) {
return master.findRobot(req.params.robotname, function(err, robot) {
return res.json(err ? err : robot.data().devices);

4
dist/robot.js vendored
View File

@ -52,6 +52,7 @@
this.devices = {};
this.adaptors = {};
this.drivers = {};
this.commands = [];
this.registerAdaptor("./loopback", "loopback");
this.registerDriver("./ping", "ping");
this.initConnections(opts.connection || opts.connections);
@ -95,7 +96,8 @@
_results.push(device.data());
}
return _results;
}).call(this)
}).call(this),
commands: this.commands
};
};

View File

@ -0,0 +1,20 @@
Cylon = require '..'
Cylon.api host: '0.0.0.0', port: '8080'
class MyRobot
commands:
["relax"]
relax: ->
return "#{this.name} says relax"
work: (me) ->
every 1.seconds(), ->
Logger.info me.name
robot = new MyRobot
robot.name = "frankie"
Cylon.robot robot
Cylon.start()

View File

@ -39,6 +39,20 @@ namespace 'Api', ->
master.findRobot req.params.robotname, (err, robot) ->
res.json if err then err else robot.data()
@server.get "/robots/:robotname/commands", (req, res) ->
master.findRobot req.params.robotname, (err, robot) ->
res.json if err then err else robot.data().commands
@server.all "/robots/:robotname/commands/:commandname", (req, res) ->
params = []
if typeof req.body is 'object'
params.push(value) for key, value of req.body
master.findRobot req.params.robotname, (err, robot) ->
if err then return res.json err
result = robot[req.params.commandname](params...)
res.json result: result
@server.get "/robots/:robotname/devices", (req, res) ->
master.findRobot req.params.robotname, (err, robot) ->
res.json if err then err else robot.data().devices

View File

@ -55,6 +55,7 @@ namespace 'Cylon', ->
@devices = {}
@adaptors = {}
@drivers = {}
@commands = []
@registerAdaptor "./loopback", "loopback"
@registerDriver "./ping", "ping"
@ -81,6 +82,7 @@ namespace 'Cylon', ->
name: @name
connections: (connection.data() for n, connection of @connections)
devices: (device.data() for n, device of @devices)
commands: @commands
}
# Public: Initializes all connections for the robot