From c055f1e8575e35e55cb540ecfb5a8c313988eb23 Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Tue, 25 Nov 2014 14:39:24 -0800 Subject: [PATCH] Example updates --- examples/crazyflie/crazyflie.js | 16 +- examples/digispark_blink/blink.js | 18 +- examples/digispark_blink/blink.markdown | 1 - examples/hello/hello.js | 1 + examples/hello/hello.markdown | 14 +- examples/keyboard/keyboard.js | 8 +- examples/keyboard/keyboard.markdown | 4 +- examples/leap_arduino/leap_arduino.js | 11 +- examples/leap_arduino/leap_arduino.markdown | 15 +- examples/master/master.js | 23 +- examples/salesforce/salesforce.js | 12 +- examples/salesforce/salesforce.markdown | 12 +- examples/sf-sphero/sf-sphero.js | 106 +++----- examples/sf-sphero/sf-sphero.markdown | 115 ++++---- examples/skynet/skynet-blink.js | 16 +- examples/skynet/skynet-blink.markdown | 16 +- examples/sphero-pebble-sf/sphero-pebble-sf.js | 247 ++++++++--------- .../sphero-pebble-sf.markdown | 253 ++++++++---------- 18 files changed, 382 insertions(+), 506 deletions(-) diff --git a/examples/crazyflie/crazyflie.js b/examples/crazyflie/crazyflie.js index 3df033d..d747c49 100644 --- a/examples/crazyflie/crazyflie.js +++ b/examples/crazyflie/crazyflie.js @@ -1,22 +1,14 @@ var Cylon = require('../..'); Cylon.robot({ - connection: { - name: 'crazyflie', - adaptor: 'crazyflie', - port: "radio://1/10/250KPS" - }, - - device: { - name: 'drone', - driver: 'crazyflie' - }, + connection: { name: 'crazyflie', adaptor: 'crazyflie', port: "radio://1/10/250KPS" }, + device: { name: 'drone', driver: 'crazyflie' }, work: function(my) { my.drone.on('start', function() { my.drone.takeoff(); - after(10..seconds(), function() { my.drone.land(); }); - after(15..seconds(), function() { my.drone.stop(); }); + after((10).seconds(), my.drone.land); + after((15).seconds(), my.drone.stop); }); } }).start(); diff --git a/examples/digispark_blink/blink.js b/examples/digispark_blink/blink.js index 15c3ca1..c8687c3 100644 --- a/examples/digispark_blink/blink.js +++ b/examples/digispark_blink/blink.js @@ -1,18 +1,10 @@ var Cylon = require('../..'); Cylon.robot({ - connection: { - name: 'digispark', - adaptor: 'digispark' - }, - device: { - name: 'led', - driver: 'led', - pin: 1 - }, + connection: { name: 'digispark', adaptor: 'digispark' }, + device: { name: 'led', driver: 'led', pin: 1 }, + work: function(my) { - return every(1..second(), function() { - return my.led.toggle(); - }); + every((1).second(), my.led.toggle); } -}).start(); \ No newline at end of file +}).start(); diff --git a/examples/digispark_blink/blink.markdown b/examples/digispark_blink/blink.markdown index e673793..a33be97 100644 --- a/examples/digispark_blink/blink.markdown +++ b/examples/digispark_blink/blink.markdown @@ -11,7 +11,6 @@ Now that we have Cylon imported, we can start defining our robot Let's define the connections and devices: connection: { name: 'digispark', adaptor: 'digispark' }, - device: { name: 'led', driver: 'led', pin: 1 }, Now that Cylon knows about the necessary hardware we're going to be using, we'll diff --git a/examples/hello/hello.js b/examples/hello/hello.js index 905f933..a5e805f 100644 --- a/examples/hello/hello.js +++ b/examples/hello/hello.js @@ -4,6 +4,7 @@ Cylon.api(); Cylon.robot({ name: 'test', + connection: { name: 'loopback', adaptor: 'loopback' }, device: { name: 'ping', driver: 'ping' }, diff --git a/examples/hello/hello.markdown b/examples/hello/hello.markdown index 96efd6f..325ad14 100644 --- a/examples/hello/hello.markdown +++ b/examples/hello/hello.markdown @@ -11,17 +11,21 @@ Let's start by importing Cylon: Now we can define our robot: Cylon.robot({ + name: 'test', + + connection: { name: 'loopback', adaptor: 'loopback' }, + device: { name: 'ping', driver: 'ping' }, For work, it's going to print a message to the console every second, and another -message after ten seconds have elapsed. +message after five seconds have elapsed. - work: function() { - every((1).second(), function() { + work: function(my) { + every((1).seconds(), function(){ console.log("Hello, human!") + console.log(my.ping.ping()); }); - // This will happen only one time at the 5th second - after((5).seconds(), function() { + after((5).seconds(), function(){ console.log("I've been at your command for 5 seconds now.") }); } diff --git a/examples/keyboard/keyboard.js b/examples/keyboard/keyboard.js index 30408db..45efcc8 100644 --- a/examples/keyboard/keyboard.js +++ b/examples/keyboard/keyboard.js @@ -2,11 +2,11 @@ var Cylon = require('../..'); Cylon.robot({ connection: { name: 'keyboard', adaptor: 'keyboard' }, - device: {name: 'keyboard', driver: 'keyboard'}, + device: { name: 'keyboard', driver: 'keyboard' }, work: function(my) { - my.keyboard.on('a', function(key) { - console.log("A PRESSED!"); + my.keyboard.on('a', function(key) { + console.log("a pressed!"); }); } -}).start(); \ No newline at end of file +}).start(); diff --git a/examples/keyboard/keyboard.markdown b/examples/keyboard/keyboard.markdown index af8db67..b3a25ef 100644 --- a/examples/keyboard/keyboard.markdown +++ b/examples/keyboard/keyboard.markdown @@ -20,7 +20,9 @@ When we tell this robot to work, it's going to listen to the 'a' key on the keyboard and let us know when it's been pressed. work: function(my) { - my.keyboard.on('a', function(key) { console.log("A PRESSED!") }); + my.keyboard.on('a', function(key) { + console.log("a pressed!") + }); } With that done, let's get started! diff --git a/examples/leap_arduino/leap_arduino.js b/examples/leap_arduino/leap_arduino.js index 8a0205e..93526a3 100644 --- a/examples/leap_arduino/leap_arduino.js +++ b/examples/leap_arduino/leap_arduino.js @@ -2,19 +2,16 @@ var Cylon = require('../..'); Cylon.robot({ connections: { + leap: { adaptor: 'leapmotion' }, arduino: { adaptor: 'firmata', port: '/dev/ttyACM0' } - leapmotion: { adaptor: 'leapmotion' }, }, - devices: { - led: { driver: 'led', pin: 13, connection: 'arduino' } - leapmotion: { driver: 'leapmotion', connection: 'leapmotion' }, - }, + devices: { name: 'led', driver: 'led', pin: 13, connection: 'arduino' }, work: function(my) { my.leapmotion.on('frame', function(frame) { - if (frame.hands.length > 0) { - my.led.turnOn(); + if (frame.hands.length > 0) { + my.led.turnOn(); } else { my.led.turnOff(); } diff --git a/examples/leap_arduino/leap_arduino.markdown b/examples/leap_arduino/leap_arduino.markdown index 0682a7f..42a8d29 100644 --- a/examples/leap_arduino/leap_arduino.markdown +++ b/examples/leap_arduino/leap_arduino.markdown @@ -11,21 +11,22 @@ Now that we have Cylon imported, we can start defining our robot Let's define the connections and devices: connections: { - leapmotion: { adaptor: 'leapmotion' }, + leap: { adaptor: 'leapmotion' }, arduino: { adaptor: 'firmata', port: '/dev/ttyACM0' } }, - devices: { - leapmotion: { driver: 'leapmotion', connection: 'leapmotion' }, - led: { driver: 'led', pin: 13, connection: 'arduino' } - }, + device: { name: 'led', driver: 'led', pin: 13, connection: 'arduino' }, Now that Cylon knows about the necessary hardware we're going to be using, we'll tell it what work we want to do: work: function(my) { - my.leapmotion.on('frame', function(frame) { - frame.hands.length > 0 ? my.led.turnOn() : my.led.turnOff(); + my.leap.on('frame', function(frame) { + if (frame.hands.length > 0) { + my.led.turnOn(); + } else { + my.led.turnOff(); + } }); } diff --git a/examples/master/master.js b/examples/master/master.js index be4852f..89dab9a 100644 --- a/examples/master/master.js +++ b/examples/master/master.js @@ -2,21 +2,14 @@ var Cylon = require('../..'); var bots = [ 'Huey', 'Dewey', 'Louie' ]; -var MinionBot = (function() { - function MinionBot() {} +bots.forEach(function(name) { + Cylon.robot({ + name: name, - MinionBot.prototype.work = function(my) { - console.log("Robot " + my.name + " is now working!"); - }; - - return MinionBot; - -})(); - -for (var i = 0; i < bots.length; i++) { - var robot = new MinionBot; - robot.name = bots[i]; - Cylon.robot(robot); -} + work: function(my) { + console.log("Robot " + my.name + " is now working!"); + } + }); +}); Cylon.start(); diff --git a/examples/salesforce/salesforce.js b/examples/salesforce/salesforce.js index 0238a19..3964a7f 100644 --- a/examples/salesforce/salesforce.js +++ b/examples/salesforce/salesforce.js @@ -15,10 +15,10 @@ Cylon.robot({ device: { name: 'salesforce', driver: 'force' }, - work: function(me) { - me.salesforce.on('start', function() { - me.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) { - var msg = "Sphero: " + data.sobject.Sphero_Name__c + ","; + work: function(my) { + my.salesforce.on('start', function() { + my.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) { + var msg = "Sphero: " + data.sobject.Sphero_Namy__c + ","; msg += "Bucks: " + data.sobject.Bucks__c + ","; msg += "SM_Id: " + data.sobject.Id; @@ -30,11 +30,11 @@ Cylon.robot({ every((2).seconds(), function() { var data = JSON.stringify({ - spheroName: "" + me.name, + spheroNamy: "" + my.namy, bucks: "" + i }); - me.salesforce.push('SpheroController', 'POST', data); + my.salesforce.push('SpheroController', 'POST', data); }); } }).start(); diff --git a/examples/salesforce/salesforce.markdown b/examples/salesforce/salesforce.markdown index 8806b74..bc91752 100644 --- a/examples/salesforce/salesforce.markdown +++ b/examples/salesforce/salesforce.markdown @@ -27,10 +27,10 @@ Let's define the connections and devices: Now that Cylon knows about the necessary hardware we're going to be using, we'll tell it what work we want to do: - work: function(me) { - me.salesforce.on('start', function() { - me.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) { - var msg = "Sphero: " + data.sobject.Sphero_Name__c + ","; + work: function(my) { + my.salesforce.on('start', function() { + my.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) { + var msg = "Sphero: " + data.sobject.Sphero_Namy__c + ","; msg += "Bucks: " + data.sobject.Bucks__c + ","; msg += "SM_Id: " + data.sobject.Id; @@ -42,11 +42,11 @@ tell it what work we want to do: every((2).seconds(), function() { var data = JSON.stringify({ - spheroName: "" + me.name, + spheroNamy: "" + my.namy, bucks: "" + i }); - me.salesforce.push('SpheroController', 'POST', data); + my.salesforce.push('SpheroController', 'POST', data); }); } diff --git a/examples/sf-sphero/sf-sphero.js b/examples/sf-sphero/sf-sphero.js index f9d62a4..8d4e16b 100644 --- a/examples/sf-sphero/sf-sphero.js +++ b/examples/sf-sphero/sf-sphero.js @@ -1,13 +1,9 @@ -var __bind = function(fn, me) { - return function() { return fn.apply(me, arguments); }; -}; - var Cylon = require('../..'); -var SalesforceRobot = (function() { - function SalesforceRobot() {} +Cylon.robot({ + name: 'salesforce', - SalesforceRobot.prototype.connection = { + connection: { name: 'sfcon', adaptor: 'force', sfuser: process.env.SF_USERNAME, @@ -17,74 +13,58 @@ var SalesforceRobot = (function() { clientSecret: process.env.SF_CLIENT_SECRET, redirectUri: 'http://localhost:3000/oauth/_callback' } - }; + }, - SalesforceRobot.prototype.device = { name: 'salesforce', driver: 'force' }; + device: { name: 'salesforce', driver: 'force' }, + + work: function(my) { + my.salesforce.on('start', function() { + my.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) { + var msg = "Sphero: " + data.sobject.Sphero_Name__c + ","; + msg += "Bucks: " + data.sobject.Bucks__c + ","; + msg += "SM_Id: " + data.sobject.Id; - SalesforceRobot.prototype.work = function(me) { - me.salesforce.on('start', function() { - me.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) { - var msg; - msg = "Sphero: " + data.sobject.Sphero_Name__c + ","; - msg += "Bucks: " + data.sobject.Bucks__c + ","; - msg += "SM_Id: " + data.sobject.Id; console.log(msg); - var spheroBot = Cylon.robots[data.sobject.Sphero_Name__c]; - spheroBot.react(spheroBot.devices.sphero); + + var sphero = Cylon.robots[data.sobject.Sphero_Name__c]; + sphero.react(); }); }); - }; - - return SalesforceRobot; - -})(); - -var SpheroRobot = (function() { - function SpheroRobot() { - this.react = __bind(this.react, this); } +}); - SpheroRobot.prototype.totalBucks = 0; +Cylon.robot({ + name: 'ROY', + connection: { name: 'sphero', adaptor: 'sphero' }, + device: { name: 'sphero', driver: 'sphero' }, - SpheroRobot.prototype.connection = { name: 'sphero', adaptor: 'sphero' }; - SpheroRobot.prototype.device = { name: 'sphero', driver: 'sphero' }; + react: function() { + this.sphero.setRGB(0x00FF00); + this.sphero.roll(90, Math.floor(Math.random() * 360)); + }, - SpheroRobot.prototype.react = function(robot) { - robot.setRGB(0x00FF00); - robot.roll(90, Math.floor(Math.random() * 360)); - }; + work: function(my) { + console.log('Setting up collision detection.'); + my.sphero.detectCollisions(); - SpheroRobot.prototype.work = function(me) { - me.sphero.on('connect', function() { - console.log('Setting up Collision Detection...'); - me.sphero.detectCollisions(); - me.sphero.stop(); - me.sphero.setRGB(0x00FF00); - me.sphero.roll(90, Math.floor(Math.random() * 360)); - }); - me.sphero.on('collision', function(data) { - me.sphero.setRGB(0x0000FF, me); - me.sphero.stop(); - data = JSON.stringify({ - spheroName: "" + me.name, - bucks: "" + (me.totalBucks++) + my.sphero.stop(); + my.sphero.setRGB(0x00FF00); + + my.sphero.roll(90, Math.floor(Math.random() * 360)); + + my.sphero.on('collision', function() { + my.sphero.setRGB(0x0000FF, my); + my.sphero.stop(); + + var data = JSON.stringify({ + spheroName: my.name, + bucks: "" + (my.totalBucks++) }); - var sf = Cylon.robots['salesforce']; + + var sf = Cylon.robots.salesforce; sf.devices.salesforce.push('SpheroController', 'POST', data); }); - }; - - return SpheroRobot; - -})(); - -var sfRobot = new SalesforceRobot(); -sfRobot.name = "salesforce"; -Cylon.robot(sfRobot); - -var spheroRobot = new SpheroRobot(); -spheroRobot.name = 'ROY'; -spheroRobot.connection.port = '/dev/rfcomm0'; -Cylon.robot(spheroRobot); + } +}); Cylon.start(); diff --git a/examples/sf-sphero/sf-sphero.markdown b/examples/sf-sphero/sf-sphero.markdown index 437f7b0..32fabd3 100644 --- a/examples/sf-sphero/sf-sphero.markdown +++ b/examples/sf-sphero/sf-sphero.markdown @@ -4,20 +4,12 @@ First, let's import Cylon: var Cylon = require('../..'); -Now we'll define a `bind` helper function we'll use later: +With that done, let's define the Robot we'll use to communicate with Salesforce: - var bind = function(fn, me) { - return function() { return fn.apply(me, arguments); }; - }; + Cylon.robot({ + name: 'salesforce', -Now that we have Cylon imported, we can start defining our robot - - var SalesforceRobot = (function() { - function SalesforceRobot() {} - -Let's define the connections and devices: - - SalesforceRobot.prototype.connection = { + connection: { name: 'sfcon', adaptor: 'force', sfuser: process.env.SF_USERNAME, @@ -27,78 +19,61 @@ Let's define the connections and devices: clientSecret: process.env.SF_CLIENT_SECRET, redirectUri: 'http://localhost:3000/oauth/_callback' } - }; + }, - SalesforceRobot.prototype.device = { name: 'salesforce', driver: 'force' }; + device: { name: 'salesforce', driver: 'force' }, -Now that Cylon knows about the necessary hardware we're going to be using, we'll -tell it what work we want to do: + work: function(my) { + my.salesforce.on('start', function() { + my.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) { + var msg = "Sphero: " + data.sobject.Sphero_Name__c + ","; + msg += "Bucks: " + data.sobject.Bucks__c + ","; + msg += "SM_Id: " + data.sobject.Id; - SalesforceRobot.prototype.work = function(me) { - me.salesforce.on('start', function() { - me.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) { - var msg; - msg = "Sphero: " + data.sobject.Sphero_Name__c + ","; - msg += "Bucks: " + data.sobject.Bucks__c + ","; - msg += "SM_Id: " + data.sobject.Id; console.log(msg); - var spheroBot = Cylon.robots[data.sobject.Sphero_Name__c]; - spheroBot.react(spheroBot.devices.sphero); + + var sphero = Cylon.robots[data.sobject.Sphero_Name__c]; + sphero.react(); }); }); - }; - - return SalesforceRobot; - - })(); - - var SpheroRobot = (function() { - function SpheroRobot() { - this.react = bind(this.react, this); } + }); - SpheroRobot.prototype.totalBucks = 0; +Next up, the shape our Sphero Robot will take: - SpheroRobot.prototype.connection = { name: 'sphero', adaptor: 'sphero' }; - SpheroRobot.prototype.device = { name: 'sphero', driver: 'sphero' }; + Cylon.robot({ + name: 'ROY', + connection: { name: 'sphero', adaptor: 'sphero' }, + device: { name: 'sphero', driver: 'sphero' }, - SpheroRobot.prototype.react = function(robot) { - robot.setRGB(0x00FF00); - robot.roll(90, Math.floor(Math.random() * 360)); - }; + react: function() { + this.sphero.setRGB(0x00FF00); + this.sphero.roll(90, Math.floor(Math.random() * 360)); + }, - SpheroRobot.prototype.work = function(me) { - me.sphero.on('connect', function() { - console.log('Setting up Collision Detection...'); - me.sphero.detectCollisions(); - me.sphero.stop(); - me.sphero.setRGB(0x00FF00); - me.sphero.roll(90, Math.floor(Math.random() * 360)); - }); - me.sphero.on('collision', function(data) { - me.sphero.setRGB(0x0000FF, me); - me.sphero.stop(); - data = JSON.stringify({ - spheroName: "" + me.name, - bucks: "" + (me.totalBucks++) + work: function(my) { + console.log('Setting up collision detection.'); + my.sphero.detectCollisions(); + + my.sphero.stop(); + my.sphero.setRGB(0x00FF00); + + my.sphero.roll(90, Math.floor(Math.random() * 360)); + + my.sphero.on('collision', function() { + my.sphero.setRGB(0x0000FF, my); + my.sphero.stop(); + + var data = JSON.stringify({ + spheroName: my.name, + bucks: "" + (my.totalBucks++) }); - var sf = Cylon.robots['salesforce']; + + var sf = Cylon.robots.salesforce; sf.devices.salesforce.push('SpheroController', 'POST', data); }); - }; - - return SpheroRobot; - - })(); - - var sfRobot = new SalesforceRobot(); - sfRobot.name = "salesforce"; - Cylon.robot(sfRobot); - - var spheroRobot = new SpheroRobot(); - spheroRobot.name = 'ROY'; - spheroRobot.connection.port = '/dev/rfcomm0'; - Cylon.robot(spheroRobot); + } + }); Now that our robot knows what work to do, and the work it will be doing that hardware with, we can start it: diff --git a/examples/skynet/skynet-blink.js b/examples/skynet/skynet-blink.js index 09c3500..37826c3 100644 --- a/examples/skynet/skynet-blink.js +++ b/examples/skynet/skynet-blink.js @@ -3,6 +3,7 @@ var Cylon = require('../..'); Cylon.robot({ connections: { arduino: { adaptor: 'firmata', port: '/dev/ttyACM0' }, + skynet: { adaptor: 'skynet', uuid: "96630051-a3dc-11e3-8442-5bf31d98c912", @@ -13,16 +14,17 @@ Cylon.robot({ device: { name: 'led13', driver: 'led', pin: 13, connection: 'arduino' }, work: function(my) { - console.log("Skynet is listening..."); + console.log("Skynet is listening"); my.skynet.on('message', function(data) { + data = JSON.parse(data); + console.log(data); - var data = JSON.parse(data); - if(data.message.red == 'on') { - my.led13.turnOn() - } - else if(data.message.red == 'off') { - my.led13.turnOff() + + if (data.message.red === 'on') { + my.led13.turnOn(); + } else { + my.led13.turnOff(); } }); diff --git a/examples/skynet/skynet-blink.markdown b/examples/skynet/skynet-blink.markdown index ee1e867..a5e3513 100644 --- a/examples/skynet/skynet-blink.markdown +++ b/examples/skynet/skynet-blink.markdown @@ -12,6 +12,7 @@ Let's define the connections and devices: connections: { arduino: { adaptor: 'firmata', port: '/dev/ttyACM0' }, + skynet: { adaptor: 'skynet', uuid: "96630051-a3dc-11e3-8442-5bf31d98c912", @@ -25,16 +26,17 @@ Now that Cylon knows about the necessary hardware we're going to be using, we'll tell it what work we want to do: work: function(my) { - console.log("Skynet is listening..."); + console.log("Skynet is listening"); my.skynet.on('message', function(data) { + data = JSON.parse(data); + console.log(data); - var data = JSON.parse(data); - if(data.message.red == 'on') { - my.led13.turnOn() - } - else if(data.message.red == 'off') { - my.led13.turnOff() + + if (data.message.red === 'on') { + my.led13.turnOn(); + } else { + my.led13.turnOff(); } }); } diff --git a/examples/sphero-pebble-sf/sphero-pebble-sf.js b/examples/sphero-pebble-sf/sphero-pebble-sf.js index 324ea12..f254691 100644 --- a/examples/sphero-pebble-sf/sphero-pebble-sf.js +++ b/examples/sphero-pebble-sf/sphero-pebble-sf.js @@ -1,37 +1,26 @@ -var __bind = function(fn, me) { - return function() { return fn.apply(me, arguments); }; -}; +var Cylon = require("../.."); -var Cylon = require('../..'); +Cylon.api({ host: '0.0.0.0', port: '8080' }); -Cylon.config({ - api: { host: '0.0.0.0', port: '8080' } +Cylon.robot({ + name: 'pebble', + + connection: { name: 'pebble', adaptor: 'pebble' }, + device: { name: 'pebble', driver: 'pebble' }, + + message: function(msg) { + this.message_queue().push(msg); + }, + + work: function(my) { + console.log('Pebble connected'); + } }); -Cylon.api(); +Cylon.robot({ + name: 'salesforce', -var PebbleRobot = (function() { - function PebbleRobot() { this.message = __bind(this.message, this); } - - PebbleRobot.prototype.connection = { name: 'pebble', adaptor: 'pebble' }; - PebbleRobot.prototype.device = { name: 'pebble', driver: 'pebble' }; - - PebbleRobot.prototype.message = function(robot, msg) { - robot.message_queue().push(msg); - }; - - PebbleRobot.prototype.work = function(me) { - me.pebble.on('connect', function() { console.log("Connected!"); }); - }; - - return PebbleRobot; - -})(); - -var SalesforceRobot = (function() { - function SalesforceRobot() {} - - SalesforceRobot.prototype.connection = { + connection: { name: 'sfcon', adaptor: 'force', sfuser: process.env.SF_USERNAME, @@ -41,124 +30,41 @@ var SalesforceRobot = (function() { clientSecret: process.env.SF_CLIENT_SECRET, redirectUri: 'http://localhost:3000/oauth/_callback' } - }; + }, - SalesforceRobot.prototype.device = { name: 'salesforce', driver: 'force' }; + device: { name: 'salesforce', driver: 'force' }, - SalesforceRobot.prototype.spheroReport = {}; + spheroReport: {}, - SalesforceRobot.prototype.work = function(me) { - me.salesforce.on('start', function() { - me.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) { - var bucks, key, msg, name, toPebble, val, _ref; + work: function(my) { + my.salesforce.on('start', function() { + my.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) { + var toPebble = "", + name = data.sobject.Sphero_Name__c, + bucks = data.sobject.Bucks__c; - name = data.sobject.Sphero_Name__c; - bucks = data.sobject.Bucks__c; - - msg = "Sphero: " + name + ","; + var msg = "Sphero: " + name + ","; msg += "data Bucks: " + bucks + ","; msg += "SM_Id: " + data.sobject.Id; console.log(msg); - var spheroBot = Cylon.robots[name]; - spheroBot.react(spheroBot.devices.sphero); + var sphero = Cylon.robots[name]; + sphero.react(); - me.spheroReport[name] = bucks; - toPebble = ""; + my.spheroReport[name] = bucks; - _ref = me.spheroReport; - - for (key in _ref) { - val = _ref[key]; - toPebble += "" + key + ": $" + val + "\n"; + for (var key in my.spheroReport) { + var val = my.spheroReport[key]; + toPebble += key + ": $" + val + "\n"; } - var pebbleBot = Cylon.robots['pebble']; - pebbleBot.message(pebbleBot.devices.pebble, toPebble); + var pebble = Cylon.robots.pebble; + pebble.message(toPebble); }); }); - }; - - return SalesforceRobot; - -})(); - -var SpheroRobot = (function() { - function SpheroRobot() {} - - SpheroRobot.prototype.totalBucks = 1; - - SpheroRobot.prototype.payingPower = true; - - SpheroRobot.prototype.connection = { name: 'sphero', adaptor: 'sphero' }; - SpheroRobot.prototype.device = { name: 'sphero', driver: 'sphero' }; - - SpheroRobot.prototype.react = function(device) { - device.setRGB(0x00FF00); - device.roll(90, Math.floor(Math.random() * 360)); - this.payingPower = true; - }; - - SpheroRobot.prototype.bankrupt = function() { - var _this = this; - every(3..seconds(), function() { - if (_this.payingPower && _this.totalBucks > 0) { - _this.totalBucks += -1; - if (_this.totalBucks === 0) { - _this.sphero.setRGB(0xFF000); - _this.sphero.stop(); - } - } - }); - }; - - SpheroRobot.prototype.changeDirection = function() { - var _this = this; - every((1).seconds(), function() { - if (_this.payingPower) { - _this.sphero.roll(90, Math.floor(Math.random() * 360)); - } - }); - }; - - SpheroRobot.prototype.work = function(me) { - me.sphero.on('connect', function() { - console.log('Setting up Collision Detection...'); - me.sphero.detectCollisions(); - me.sphero.stop(); - me.sphero.setRGB(0x00FF00); - me.sphero.roll(90, Math.floor(Math.random() * 360)); - me.bankrupt(); - me.changeDirection(); - }); - - me.sphero.on('collision', function(data) { - me.sphero.setRGB(0x0000FF); - me.sphero.stop(); - me.payingPower = false; - - data = JSON.stringify({ - spheroName: "" + me.name, - bucks: "" + (me.totalBucks++) - }); - - var sf = Cylon.robots['salesforce']; - sf.devices.salesforce.push("SpheroController", "POST", data); - }); - }; - - return SpheroRobot; - -})(); - -var salesforceRobot = new SalesforceRobot(); -salesforceRobot.name = "salesforce"; -Cylon.robot(salesforceRobot); - -var pebbleRobot = new PebbleRobot(); -pebbleRobot.name = "pebble"; -Cylon.robot(pebbleRobot); + } +}); var bots = [ { port: '/dev/tty.Sphero-ROY-AMP-SPP', name: 'ROY' }, @@ -166,14 +72,77 @@ var bots = [ { port: '/dev/tty.Sphero-RRY-AMP-SPP', name: 'RRY' } ]; -for (var i = 0; i < bots.length; i++) { - var bot = bots[i]; - var robot = new SpheroRobot; +bots.forEach(function(bot) { + Cylon.robot({ + name: bot.name, - robot.connection.port = bot.port; - robot.name = bot.name; + connection: { name: 'sphero', adaptor: 'sphero', port: bot.port }, + device: { name: 'sphero', driver: 'sphero' }, - Cylon.robot(robot); -} + totalBucks: 1, + payingPower: true, + + react: function() { + this.sphero.setRGB(0x00FF00); + this.sphero.roll(90, Math.floor(Math.random() * 360)); + + this.payingPower = true; + }, + + bankrupt: function() { + var my = this; + + every((3).seconds(), function() { + if (my.payingPower && my.totalBucks > 0) { + my.totalBucks += -1; + + if (my.totalBucks === 0) { + my.sphero.setRGB(0xFF000); + my.sphero.stop(); + } + } + }); + }, + + changeDirection: function() { + var my = this; + + every((1).seconds(), function() { + if (my.payingPower) { + my.sphero.roll(90, Math.floor(Math.random() * 360)); + } + }); + }, + + work: function(my) { + console.log("Setting up collision detection for " + my.name); + + my.sphero.detectCollisions(); + + my.sphero.stop(); + + my.sphero.setRGB(0x00FF00); + + my.sphero.roll(90, Math.floor(Math.random() * 360)); + + my.bankrupt(); + my.changeDirection(); + + my.sphero.on('collision', function() { + my.sphero.setRGB(0x0000FF); + my.sphero.stop(); + my.payingPower = false; + + var data = JSON.stringify({ + spheroName: my.name, + bucks: "" + (my.totalBucks++) + }); + + var sf = Cylon.robots['salesforce']; + sf.devices.salesforce.push("SpheroController", "POST", data); + }); + } + }); +}); Cylon.start(); diff --git a/examples/sphero-pebble-sf/sphero-pebble-sf.markdown b/examples/sphero-pebble-sf/sphero-pebble-sf.markdown index 9f92c64..61debd3 100644 --- a/examples/sphero-pebble-sf/sphero-pebble-sf.markdown +++ b/examples/sphero-pebble-sf/sphero-pebble-sf.markdown @@ -7,50 +7,39 @@ First, let's import Cylon: Next up, we'll configure the API Cylon will serve, telling it to serve on port `8080`. - Cylon.config({ - api: { host: '0.0.0.0', port: '8080' } - }); + Cylon.api({ host: '0.0.0.0', port: '8080' }); - Cylon.api(); - -We'll also setup a convenince function for some binding we'll need to do later: - - var bind = function(fn, me) { - return function() { return fn.apply(me, arguments); }; - }; Now that we have Cylon imported, we can start defining our Pebble robot: - var PebbleRobot = (function() { + Cylon.robot({ + name: 'pebble', Let's define the connections and devices: - PebbleRobot.prototype.connection = { name: 'pebble', adaptor: 'pebble' }; - PebbleRobot.prototype.device = { name: 'pebble', driver: 'pebble' }; - - PebbleRobot.prototype.message = function(robot, msg) { - robot.message_queue().push(msg); - }; + connection: { name: 'pebble', adaptor: 'pebble' }, + device: { name: 'pebble', driver: 'pebble' }, Now that Cylon knows about the necessary hardware we're going to be using, we'll tell it what work we want to do: - PebbleRobot.prototype.work = function(me) { - me.pebble.on('connect', function() { console.log("Connected!"); }); - }; + message: function(msg) { + this.message_queue().push(msg); + }, - return PebbleRobot; - - })(); + work: function(my) { + console.log('Pebble connected'); + } + }); Next, let's define our SalesForce robot: - var SalesforceRobot = (function() { - function SalesforceRobot() {} + Cylon.robot({ + name: 'salesforce', Let's define the connections and devices: - SalesforceRobot.prototype.connection = { + connection: { name: 'sfcon', adaptor: 'force', sfuser: process.env.SF_USERNAME, @@ -60,132 +49,45 @@ Let's define the connections and devices: clientSecret: process.env.SF_CLIENT_SECRET, redirectUri: 'http://localhost:3000/oauth/_callback' } - }; + }, - SalesforceRobot.prototype.device = { name: 'salesforce', driver: 'force' }; - - SalesforceRobot.prototype.spheroReport = {}; + device: { name: 'salesforce', driver: 'force' }, Tell it what work we want to do: - SalesforceRobot.prototype.work = function(me) { - me.salesforce.on('start', function() { - me.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) { - var bucks, key, msg, name, toPebble, val, _ref; + spheroReport: {}, - name = data.sobject.Sphero_Name__c; - bucks = data.sobject.Bucks__c; + work: function(my) { + my.salesforce.on('start', function() { + my.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) { + var toPebble = "", + name = data.sobject.Sphero_Name__c, + bucks = data.sobject.Bucks__c; - msg = "Sphero: " + name + ","; + var msg = "Sphero: " + name + ","; msg += "data Bucks: " + bucks + ","; msg += "SM_Id: " + data.sobject.Id; console.log(msg); - var spheroBot = Cylon.robots[name]; - spheroBot.react(spheroBot.devices.sphero); + var sphero = Cylon.robots[name]; + sphero.react(); - me.spheroReport[name] = bucks; - toPebble = ""; + my.spheroReport[name] = bucks; - _ref = me.spheroReport; - - for (key in _ref) { - val = _ref[key]; - toPebble += "" + key + ": $" + val + "\n"; + for (var key in my.spheroReport) { + var val = my.spheroReport[key]; + toPebble += key + ": $" + val + "\n"; } - var pebbleBot = Cylon.robots['pebble']; - pebbleBot.message(pebbleBot.devices.pebble, toPebble); + var pebble = Cylon.robots.pebble; + pebble.message(toPebble); }); }); - }; + } + }); - return SalesforceRobot; - - })(); - -Now, Let's define our Sphero robot - - var SpheroRobot = (function() { - function SpheroRobot() {} - - SpheroRobot.prototype.totalBucks = 1; - - SpheroRobot.prototype.payingPower = true; - - SpheroRobot.prototype.connection = { name: 'sphero', adaptor: 'sphero' }; - SpheroRobot.prototype.device = { name: 'sphero', driver: 'sphero' }; - - SpheroRobot.prototype.react = function(device) { - device.setRGB(0x00FF00); - device.roll(90, Math.floor(Math.random() * 360)); - this.payingPower = true; - }; - - SpheroRobot.prototype.bankrupt = function() { - var _this = this; - every(3..seconds(), function() { - if (_this.payingPower && _this.totalBucks > 0) { - _this.totalBucks += -1; - if (_this.totalBucks === 0) { - _this.sphero.setRGB(0xFF000); - _this.sphero.stop(); - } - } - }); - }; - - SpheroRobot.prototype.changeDirection = function() { - var _this = this; - every((1).seconds(), function() { - if (_this.payingPower) { - _this.sphero.roll(90, Math.floor(Math.random() * 360)); - } - }); - }; - -Tell it what work we want to do: - - SpheroRobot.prototype.work = function(me) { - me.sphero.on('connect', function() { - console.log('Setting up Collision Detection...'); - me.sphero.detectCollisions(); - me.sphero.stop(); - me.sphero.setRGB(0x00FF00); - me.sphero.roll(90, Math.floor(Math.random() * 360)); - me.bankrupt(); - me.changeDirection(); - }); - - me.sphero.on('collision', function(data) { - me.sphero.setRGB(0x0000FF); - me.sphero.stop(); - me.payingPower = false; - - data = JSON.stringify({ - spheroName: "" + me.name, - bucks: "" + (me.totalBucks++) - }); - - var sf = Cylon.robots['salesforce']; - sf.devices.salesforce.push("SpheroController", "POST", data); - }); - }; - - return SpheroRobot; - - })(); - -Now that we've defined all of our bots, let's tell Cylon about them: - - var salesforceRobot = new SalesforceRobot(); - salesforceRobot.name = "salesforce"; - Cylon.robot(salesforceRobot); - - var pebbleRobot = new PebbleRobot(); - pebbleRobot.name = "pebble"; - Cylon.robot(pebbleRobot); +Now, Let's define our Sphero robots: var bots = [ { port: '/dev/tty.Sphero-ROY-AMP-SPP', name: 'ROY' }, @@ -193,17 +95,82 @@ Now that we've defined all of our bots, let's tell Cylon about them: { port: '/dev/tty.Sphero-RRY-AMP-SPP', name: 'RRY' } ]; - for (var i = 0; i < bots.length; i++) { - var bot = bots[i]; - var robot = new SpheroRobot; + bots.forEach(function(bot) { + Cylon.robot({ + name: bot.name, - robot.connection.port = bot.port; - robot.name = bot.name; + connection: { name: 'sphero', adaptor: 'sphero', port: bot.port }, + device: { name: 'sphero', driver: 'sphero' }, - Cylon.robot(robot); - } + totalBucks: 1, + payingPower: true, -Now that Cylon knows about all our robots, and what they'll be doing, we can -start: + +Tell them what work we want to do: + + react: function() { + this.sphero.setRGB(0x00FF00); + this.sphero.roll(90, Math.floor(Math.random() * 360)); + + this.payingPower = true; + }, + + bankrupt: function() { + var my = this; + + every((3).seconds(), function() { + if (my.payingPower && my.totalBucks > 0) { + my.totalBucks += -1; + + if (my.totalBucks === 0) { + my.sphero.setRGB(0xFF000); + my.sphero.stop(); + } + } + }); + }, + + changeDirection: function() { + var my = this; + + every((1).seconds(), function() { + if (my.payingPower) { + my.sphero.roll(90, Math.floor(Math.random() * 360)); + } + }); + }, + + work: function(my) { + console.log("Setting up collision detection for " + my.name); + + my.sphero.detectCollisions(); + + my.sphero.stop(); + + my.sphero.setRGB(0x00FF00); + + my.sphero.roll(90, Math.floor(Math.random() * 360)); + + my.bankrupt(); + my.changeDirection(); + + my.sphero.on('collision', function() { + my.sphero.setRGB(0x0000FF); + my.sphero.stop(); + my.payingPower = false; + + var data = JSON.stringify({ + spheroName: my.name, + bucks: "" + (my.totalBucks++) + }); + + var sf = Cylon.robots['salesforce']; + sf.devices.salesforce.push("SpheroController", "POST", data); + }); + } + }); + }); + +Now that Cylon knows about all our robots, and what they'll be doing, we can start: Cylon.start();