diff --git a/examples/hello/hello.js b/examples/hello/hello.js index 4be89c5..905f933 100644 --- a/examples/hello/hello.js +++ b/examples/hello/hello.js @@ -1,12 +1,16 @@ var Cylon = require('../..'); +Cylon.api(); + Cylon.robot({ + name: 'test', connection: { name: 'loopback', adaptor: 'loopback' }, device: { name: 'ping', driver: 'ping' }, - work: function() { + work: function(my) { every((1).seconds(), function(){ console.log("Hello, human!") + console.log(my.ping.ping()); }); after((5).seconds(), function(){ diff --git a/lib/api/routes.js b/lib/api/routes.js index a266a9b..1d799a1 100644 --- a/lib/api/routes.js +++ b/lib/api/routes.js @@ -102,13 +102,12 @@ router.get("/robots/:robot/devices/:device/events/:event", load, function(req, r }); router.get("/robots/:robot/devices/:device/commands", load, function(req, res) { - res.json({ commands: req.device.toJSON().commands }); + res.json({ commands: Object.keys(req.device.commands) }); }); router.all("/robots/:robot/devices/:device/commands/:command", load, function(req, res) { - var command = req.params.command; - - var result = req.device[command].apply(req.device, req.commandParams); + var command = req.device.driver.commands[req.params.command]; + var result = command.apply(req.device, req.commandParams); res.json({ result: result }); }); diff --git a/lib/device.js b/lib/device.js index b9d567a..34accab 100644 --- a/lib/device.js +++ b/lib/device.js @@ -45,7 +45,7 @@ var Device = module.exports = function Device(opts) { } } - Utils.proxyFunctionsToObject(this.driver.commands, this.driver, this); + Utils.proxyFunctionsToObject(Object.keys(this.driver.commands), this.driver, this); }; Utils.subclass(Device, EventEmitter); @@ -86,7 +86,7 @@ Device.prototype.toJSON = function() { name: this.name, driver: this.driver.constructor.name || this.driver.name, connection: this.connection.name, - commands: this.driver.commands, + commands: Object.keys(this.driver.commands), details: this.details }; }; diff --git a/lib/test/ping.js b/lib/test/ping.js index 48b767b..133f575 100644 --- a/lib/test/ping.js +++ b/lib/test/ping.js @@ -15,7 +15,7 @@ var Ping = module.exports = function Ping() { Ping.__super__.constructor.apply(this, arguments); this.commands = { - ping: this.ping + ping: this.ping.bind(this) }; }; diff --git a/test/specs/device.spec.js b/test/specs/device.spec.js index 99673e4..300019c 100644 --- a/test/specs/device.spec.js +++ b/test/specs/device.spec.js @@ -130,7 +130,7 @@ describe("Device", function() { }); it("contains the device's driver commands", function() { - expect(json.commands).to.be.eql(driver.commands); + expect(json.commands).to.be.eql(Object.keys(driver.commands)); }); });