Example updates

This commit is contained in:
Andrew Stewart 2014-11-25 14:39:24 -08:00
parent 0541c225d8
commit c055f1e857
18 changed files with 382 additions and 506 deletions

View File

@ -1,22 +1,14 @@
var Cylon = require('../..'); var Cylon = require('../..');
Cylon.robot({ Cylon.robot({
connection: { connection: { name: 'crazyflie', adaptor: 'crazyflie', port: "radio://1/10/250KPS" },
name: 'crazyflie', device: { name: 'drone', driver: 'crazyflie' },
adaptor: 'crazyflie',
port: "radio://1/10/250KPS"
},
device: {
name: 'drone',
driver: 'crazyflie'
},
work: function(my) { work: function(my) {
my.drone.on('start', function() { my.drone.on('start', function() {
my.drone.takeoff(); my.drone.takeoff();
after(10..seconds(), function() { my.drone.land(); }); after((10).seconds(), my.drone.land);
after(15..seconds(), function() { my.drone.stop(); }); after((15).seconds(), my.drone.stop);
}); });
} }
}).start(); }).start();

View File

@ -1,18 +1,10 @@
var Cylon = require('../..'); var Cylon = require('../..');
Cylon.robot({ Cylon.robot({
connection: { connection: { name: 'digispark', adaptor: 'digispark' },
name: 'digispark', device: { name: 'led', driver: 'led', pin: 1 },
adaptor: 'digispark'
},
device: {
name: 'led',
driver: 'led',
pin: 1
},
work: function(my) { work: function(my) {
return every(1..second(), function() { every((1).second(), my.led.toggle);
return my.led.toggle();
});
} }
}).start(); }).start();

View File

@ -11,7 +11,6 @@ Now that we have Cylon imported, we can start defining our robot
Let's define the connections and devices: Let's define the connections and devices:
connection: { name: 'digispark', adaptor: 'digispark' }, connection: { name: 'digispark', adaptor: 'digispark' },
device: { name: 'led', driver: 'led', pin: 1 }, device: { name: 'led', driver: 'led', pin: 1 },
Now that Cylon knows about the necessary hardware we're going to be using, we'll Now that Cylon knows about the necessary hardware we're going to be using, we'll

View File

@ -4,6 +4,7 @@ Cylon.api();
Cylon.robot({ Cylon.robot({
name: 'test', name: 'test',
connection: { name: 'loopback', adaptor: 'loopback' }, connection: { name: 'loopback', adaptor: 'loopback' },
device: { name: 'ping', driver: 'ping' }, device: { name: 'ping', driver: 'ping' },

View File

@ -11,17 +11,21 @@ Let's start by importing Cylon:
Now we can define our robot: Now we can define our robot:
Cylon.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 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() { work: function(my) {
every((1).second(), function() { every((1).seconds(), function(){
console.log("Hello, human!") 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.") console.log("I've been at your command for 5 seconds now.")
}); });
} }

View File

@ -2,11 +2,11 @@ var Cylon = require('../..');
Cylon.robot({ Cylon.robot({
connection: { name: 'keyboard', adaptor: 'keyboard' }, connection: { name: 'keyboard', adaptor: 'keyboard' },
device: {name: 'keyboard', driver: 'keyboard'}, device: { name: 'keyboard', driver: 'keyboard' },
work: function(my) { work: function(my) {
my.keyboard.on('a', function(key) { my.keyboard.on('a', function(key) {
console.log("A PRESSED!"); console.log("a pressed!");
}); });
} }
}).start(); }).start();

View File

@ -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. keyboard and let us know when it's been pressed.
work: function(my) { 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! With that done, let's get started!

View File

@ -2,14 +2,11 @@ var Cylon = require('../..');
Cylon.robot({ Cylon.robot({
connections: { connections: {
leap: { adaptor: 'leapmotion' },
arduino: { adaptor: 'firmata', port: '/dev/ttyACM0' } arduino: { adaptor: 'firmata', port: '/dev/ttyACM0' }
leapmotion: { adaptor: 'leapmotion' },
}, },
devices: { devices: { name: 'led', driver: 'led', pin: 13, connection: 'arduino' },
led: { driver: 'led', pin: 13, connection: 'arduino' }
leapmotion: { driver: 'leapmotion', connection: 'leapmotion' },
},
work: function(my) { work: function(my) {
my.leapmotion.on('frame', function(frame) { my.leapmotion.on('frame', function(frame) {

View File

@ -11,21 +11,22 @@ Now that we have Cylon imported, we can start defining our robot
Let's define the connections and devices: Let's define the connections and devices:
connections: { connections: {
leapmotion: { adaptor: 'leapmotion' }, leap: { adaptor: 'leapmotion' },
arduino: { adaptor: 'firmata', port: '/dev/ttyACM0' } arduino: { adaptor: 'firmata', port: '/dev/ttyACM0' }
}, },
devices: { device: { name: 'led', driver: 'led', pin: 13, connection: 'arduino' },
leapmotion: { driver: 'leapmotion', connection: 'leapmotion' },
led: { driver: 'led', pin: 13, connection: 'arduino' }
},
Now that Cylon knows about the necessary hardware we're going to be using, we'll Now that Cylon knows about the necessary hardware we're going to be using, we'll
tell it what work we want to do: tell it what work we want to do:
work: function(my) { work: function(my) {
my.leapmotion.on('frame', function(frame) { my.leap.on('frame', function(frame) {
frame.hands.length > 0 ? my.led.turnOn() : my.led.turnOff(); if (frame.hands.length > 0) {
my.led.turnOn();
} else {
my.led.turnOff();
}
}); });
} }

View File

@ -2,21 +2,14 @@ var Cylon = require('../..');
var bots = [ 'Huey', 'Dewey', 'Louie' ]; var bots = [ 'Huey', 'Dewey', 'Louie' ];
var MinionBot = (function() { bots.forEach(function(name) {
function MinionBot() {} Cylon.robot({
name: name,
MinionBot.prototype.work = function(my) { work: function(my) {
console.log("Robot " + my.name + " is now working!"); 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);
}
Cylon.start(); Cylon.start();

View File

@ -15,10 +15,10 @@ Cylon.robot({
device: { name: 'salesforce', driver: 'force' }, device: { name: 'salesforce', driver: 'force' },
work: function(me) { work: function(my) {
me.salesforce.on('start', function() { my.salesforce.on('start', function() {
me.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) { my.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) {
var msg = "Sphero: " + data.sobject.Sphero_Name__c + ","; var msg = "Sphero: " + data.sobject.Sphero_Namy__c + ",";
msg += "Bucks: " + data.sobject.Bucks__c + ","; msg += "Bucks: " + data.sobject.Bucks__c + ",";
msg += "SM_Id: " + data.sobject.Id; msg += "SM_Id: " + data.sobject.Id;
@ -30,11 +30,11 @@ Cylon.robot({
every((2).seconds(), function() { every((2).seconds(), function() {
var data = JSON.stringify({ var data = JSON.stringify({
spheroName: "" + me.name, spheroNamy: "" + my.namy,
bucks: "" + i bucks: "" + i
}); });
me.salesforce.push('SpheroController', 'POST', data); my.salesforce.push('SpheroController', 'POST', data);
}); });
} }
}).start(); }).start();

View File

@ -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 Now that Cylon knows about the necessary hardware we're going to be using, we'll
tell it what work we want to do: tell it what work we want to do:
work: function(me) { work: function(my) {
me.salesforce.on('start', function() { my.salesforce.on('start', function() {
me.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) { my.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) {
var msg = "Sphero: " + data.sobject.Sphero_Name__c + ","; var msg = "Sphero: " + data.sobject.Sphero_Namy__c + ",";
msg += "Bucks: " + data.sobject.Bucks__c + ","; msg += "Bucks: " + data.sobject.Bucks__c + ",";
msg += "SM_Id: " + data.sobject.Id; msg += "SM_Id: " + data.sobject.Id;
@ -42,11 +42,11 @@ tell it what work we want to do:
every((2).seconds(), function() { every((2).seconds(), function() {
var data = JSON.stringify({ var data = JSON.stringify({
spheroName: "" + me.name, spheroNamy: "" + my.namy,
bucks: "" + i bucks: "" + i
}); });
me.salesforce.push('SpheroController', 'POST', data); my.salesforce.push('SpheroController', 'POST', data);
}); });
} }

View File

@ -1,13 +1,9 @@
var __bind = function(fn, me) {
return function() { return fn.apply(me, arguments); };
};
var Cylon = require('../..'); var Cylon = require('../..');
var SalesforceRobot = (function() { Cylon.robot({
function SalesforceRobot() {} name: 'salesforce',
SalesforceRobot.prototype.connection = { connection: {
name: 'sfcon', name: 'sfcon',
adaptor: 'force', adaptor: 'force',
sfuser: process.env.SF_USERNAME, sfuser: process.env.SF_USERNAME,
@ -17,74 +13,58 @@ var SalesforceRobot = (function() {
clientSecret: process.env.SF_CLIENT_SECRET, clientSecret: process.env.SF_CLIENT_SECRET,
redirectUri: 'http://localhost:3000/oauth/_callback' redirectUri: 'http://localhost:3000/oauth/_callback'
} }
}; },
SalesforceRobot.prototype.device = { name: 'salesforce', driver: 'force' }; device: { name: 'salesforce', driver: 'force' },
SalesforceRobot.prototype.work = function(me) { work: function(my) {
me.salesforce.on('start', function() { my.salesforce.on('start', function() {
me.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) { my.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) {
var msg; var msg = "Sphero: " + data.sobject.Sphero_Name__c + ",";
msg = "Sphero: " + data.sobject.Sphero_Name__c + ",";
msg += "Bucks: " + data.sobject.Bucks__c + ","; msg += "Bucks: " + data.sobject.Bucks__c + ",";
msg += "SM_Id: " + data.sobject.Id; msg += "SM_Id: " + data.sobject.Id;
console.log(msg); 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' }; react: function() {
SpheroRobot.prototype.device = { name: 'sphero', driver: 'sphero' }; this.sphero.setRGB(0x00FF00);
this.sphero.roll(90, Math.floor(Math.random() * 360));
},
SpheroRobot.prototype.react = function(robot) { work: function(my) {
robot.setRGB(0x00FF00); console.log('Setting up collision detection.');
robot.roll(90, Math.floor(Math.random() * 360)); my.sphero.detectCollisions();
};
SpheroRobot.prototype.work = function(me) { my.sphero.stop();
me.sphero.on('connect', function() { my.sphero.setRGB(0x00FF00);
console.log('Setting up Collision Detection...');
me.sphero.detectCollisions(); my.sphero.roll(90, Math.floor(Math.random() * 360));
me.sphero.stop();
me.sphero.setRGB(0x00FF00); my.sphero.on('collision', function() {
me.sphero.roll(90, Math.floor(Math.random() * 360)); my.sphero.setRGB(0x0000FF, my);
my.sphero.stop();
var data = JSON.stringify({
spheroName: my.name,
bucks: "" + (my.totalBucks++)
}); });
me.sphero.on('collision', function(data) {
me.sphero.setRGB(0x0000FF, me); var sf = Cylon.robots.salesforce;
me.sphero.stop();
data = JSON.stringify({
spheroName: "" + me.name,
bucks: "" + (me.totalBucks++)
});
var sf = Cylon.robots['salesforce'];
sf.devices.salesforce.push('SpheroController', 'POST', data); 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(); Cylon.start();

View File

@ -4,20 +4,12 @@ First, let's import Cylon:
var Cylon = require('../..'); 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) { Cylon.robot({
return function() { return fn.apply(me, arguments); }; name: 'salesforce',
};
Now that we have Cylon imported, we can start defining our robot connection: {
var SalesforceRobot = (function() {
function SalesforceRobot() {}
Let's define the connections and devices:
SalesforceRobot.prototype.connection = {
name: 'sfcon', name: 'sfcon',
adaptor: 'force', adaptor: 'force',
sfuser: process.env.SF_USERNAME, sfuser: process.env.SF_USERNAME,
@ -27,78 +19,61 @@ Let's define the connections and devices:
clientSecret: process.env.SF_CLIENT_SECRET, clientSecret: process.env.SF_CLIENT_SECRET,
redirectUri: 'http://localhost:3000/oauth/_callback' 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 work: function(my) {
tell it what work we want to do: my.salesforce.on('start', function() {
my.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) {
SalesforceRobot.prototype.work = function(me) { var msg = "Sphero: " + data.sobject.Sphero_Name__c + ",";
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 += "Bucks: " + data.sobject.Bucks__c + ",";
msg += "SM_Id: " + data.sobject.Id; msg += "SM_Id: " + data.sobject.Id;
console.log(msg); 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;
SpheroRobot.prototype.connection = { name: 'sphero', adaptor: 'sphero' };
SpheroRobot.prototype.device = { name: 'sphero', driver: 'sphero' };
SpheroRobot.prototype.react = function(robot) {
robot.setRGB(0x00FF00);
robot.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); Next up, the shape our Sphero Robot will take:
me.sphero.stop();
data = JSON.stringify({ Cylon.robot({
spheroName: "" + me.name, name: 'ROY',
bucks: "" + (me.totalBucks++) connection: { name: 'sphero', adaptor: 'sphero' },
device: { name: 'sphero', driver: 'sphero' },
react: function() {
this.sphero.setRGB(0x00FF00);
this.sphero.roll(90, Math.floor(Math.random() * 360));
},
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); 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 Now that our robot knows what work to do, and the work it will be doing that
hardware with, we can start it: hardware with, we can start it:

View File

@ -3,6 +3,7 @@ var Cylon = require('../..');
Cylon.robot({ Cylon.robot({
connections: { connections: {
arduino: { adaptor: 'firmata', port: '/dev/ttyACM0' }, arduino: { adaptor: 'firmata', port: '/dev/ttyACM0' },
skynet: { skynet: {
adaptor: 'skynet', adaptor: 'skynet',
uuid: "96630051-a3dc-11e3-8442-5bf31d98c912", uuid: "96630051-a3dc-11e3-8442-5bf31d98c912",
@ -13,16 +14,17 @@ Cylon.robot({
device: { name: 'led13', driver: 'led', pin: 13, connection: 'arduino' }, device: { name: 'led13', driver: 'led', pin: 13, connection: 'arduino' },
work: function(my) { work: function(my) {
console.log("Skynet is listening..."); console.log("Skynet is listening");
my.skynet.on('message', function(data) { my.skynet.on('message', function(data) {
data = JSON.parse(data);
console.log(data); console.log(data);
var data = JSON.parse(data);
if(data.message.red == 'on') { if (data.message.red === 'on') {
my.led13.turnOn() my.led13.turnOn();
} } else {
else if(data.message.red == 'off') { my.led13.turnOff();
my.led13.turnOff()
} }
}); });

View File

@ -12,6 +12,7 @@ Let's define the connections and devices:
connections: { connections: {
arduino: { adaptor: 'firmata', port: '/dev/ttyACM0' }, arduino: { adaptor: 'firmata', port: '/dev/ttyACM0' },
skynet: { skynet: {
adaptor: 'skynet', adaptor: 'skynet',
uuid: "96630051-a3dc-11e3-8442-5bf31d98c912", 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: tell it what work we want to do:
work: function(my) { work: function(my) {
console.log("Skynet is listening..."); console.log("Skynet is listening");
my.skynet.on('message', function(data) { my.skynet.on('message', function(data) {
data = JSON.parse(data);
console.log(data); console.log(data);
var data = JSON.parse(data);
if(data.message.red == 'on') { if (data.message.red === 'on') {
my.led13.turnOn() my.led13.turnOn();
} } else {
else if(data.message.red == 'off') { my.led13.turnOff();
my.led13.turnOff()
} }
}); });
} }

View File

@ -1,37 +1,26 @@
var __bind = function(fn, me) { var Cylon = require("../..");
return function() { return fn.apply(me, arguments); };
};
var Cylon = require('../..'); Cylon.api({ host: '0.0.0.0', port: '8080' });
Cylon.config({ Cylon.robot({
api: { host: '0.0.0.0', port: '8080' } 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() { connection: {
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 = {
name: 'sfcon', name: 'sfcon',
adaptor: 'force', adaptor: 'force',
sfuser: process.env.SF_USERNAME, sfuser: process.env.SF_USERNAME,
@ -41,124 +30,41 @@ var SalesforceRobot = (function() {
clientSecret: process.env.SF_CLIENT_SECRET, clientSecret: process.env.SF_CLIENT_SECRET,
redirectUri: 'http://localhost:3000/oauth/_callback' 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) { work: function(my) {
me.salesforce.on('start', function() { my.salesforce.on('start', function() {
me.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) { my.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) {
var bucks, key, msg, name, toPebble, val, _ref; var toPebble = "",
name = data.sobject.Sphero_Name__c,
name = data.sobject.Sphero_Name__c;
bucks = data.sobject.Bucks__c; bucks = data.sobject.Bucks__c;
msg = "Sphero: " + name + ","; var msg = "Sphero: " + name + ",";
msg += "data Bucks: " + bucks + ","; msg += "data Bucks: " + bucks + ",";
msg += "SM_Id: " + data.sobject.Id; msg += "SM_Id: " + data.sobject.Id;
console.log(msg); console.log(msg);
var spheroBot = Cylon.robots[name]; var sphero = Cylon.robots[name];
spheroBot.react(spheroBot.devices.sphero); sphero.react();
me.spheroReport[name] = bucks; my.spheroReport[name] = bucks;
toPebble = "";
_ref = me.spheroReport; for (var key in my.spheroReport) {
var val = my.spheroReport[key];
for (key in _ref) { toPebble += key + ": $" + val + "\n";
val = _ref[key];
toPebble += "" + key + ": $" + val + "\n";
} }
var pebbleBot = Cylon.robots['pebble']; var pebble = Cylon.robots.pebble;
pebbleBot.message(pebbleBot.devices.pebble, toPebble); 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 = [ var bots = [
{ port: '/dev/tty.Sphero-ROY-AMP-SPP', name: 'ROY' }, { port: '/dev/tty.Sphero-ROY-AMP-SPP', name: 'ROY' },
@ -166,14 +72,77 @@ var bots = [
{ port: '/dev/tty.Sphero-RRY-AMP-SPP', name: 'RRY' } { port: '/dev/tty.Sphero-RRY-AMP-SPP', name: 'RRY' }
]; ];
for (var i = 0; i < bots.length; i++) { bots.forEach(function(bot) {
var bot = bots[i]; Cylon.robot({
var robot = new SpheroRobot; name: bot.name,
robot.connection.port = bot.port; connection: { name: 'sphero', adaptor: 'sphero', port: bot.port },
robot.name = bot.name; 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(); Cylon.start();

View File

@ -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 Next up, we'll configure the API Cylon will serve, telling it to serve on port
`8080`. `8080`.
Cylon.config({ Cylon.api({ host: '0.0.0.0', port: '8080' });
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: 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: Let's define the connections and devices:
PebbleRobot.prototype.connection = { name: 'pebble', adaptor: 'pebble' }; connection: { name: 'pebble', adaptor: 'pebble' },
PebbleRobot.prototype.device = { name: 'pebble', driver: 'pebble' }; device: { name: 'pebble', driver: 'pebble' },
PebbleRobot.prototype.message = function(robot, msg) {
robot.message_queue().push(msg);
};
Now that Cylon knows about the necessary hardware we're going to be using, we'll Now that Cylon knows about the necessary hardware we're going to be using, we'll
tell it what work we want to do: tell it what work we want to do:
PebbleRobot.prototype.work = function(me) { message: function(msg) {
me.pebble.on('connect', function() { console.log("Connected!"); }); this.message_queue().push(msg);
}; },
return PebbleRobot; work: function(my) {
console.log('Pebble connected');
})(); }
});
Next, let's define our SalesForce robot: Next, let's define our SalesForce robot:
var SalesforceRobot = (function() { Cylon.robot({
function SalesforceRobot() {} name: 'salesforce',
Let's define the connections and devices: Let's define the connections and devices:
SalesforceRobot.prototype.connection = { connection: {
name: 'sfcon', name: 'sfcon',
adaptor: 'force', adaptor: 'force',
sfuser: process.env.SF_USERNAME, sfuser: process.env.SF_USERNAME,
@ -60,132 +49,45 @@ Let's define the connections and devices:
clientSecret: process.env.SF_CLIENT_SECRET, clientSecret: process.env.SF_CLIENT_SECRET,
redirectUri: 'http://localhost:3000/oauth/_callback' redirectUri: 'http://localhost:3000/oauth/_callback'
} }
}; },
SalesforceRobot.prototype.device = { name: 'salesforce', driver: 'force' }; device: { name: 'salesforce', driver: 'force' },
SalesforceRobot.prototype.spheroReport = {};
Tell it what work we want to do: Tell it what work we want to do:
SalesforceRobot.prototype.work = function(me) { spheroReport: {},
me.salesforce.on('start', function() {
me.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) {
var bucks, key, msg, name, toPebble, val, _ref;
name = data.sobject.Sphero_Name__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; bucks = data.sobject.Bucks__c;
msg = "Sphero: " + name + ","; var msg = "Sphero: " + name + ",";
msg += "data Bucks: " + bucks + ","; msg += "data Bucks: " + bucks + ",";
msg += "SM_Id: " + data.sobject.Id; msg += "SM_Id: " + data.sobject.Id;
console.log(msg); console.log(msg);
var spheroBot = Cylon.robots[name]; var sphero = Cylon.robots[name];
spheroBot.react(spheroBot.devices.sphero); sphero.react();
me.spheroReport[name] = bucks; my.spheroReport[name] = bucks;
toPebble = "";
_ref = me.spheroReport; for (var key in my.spheroReport) {
var val = my.spheroReport[key];
for (key in _ref) { toPebble += key + ": $" + val + "\n";
val = _ref[key];
toPebble += "" + key + ": $" + val + "\n";
} }
var pebbleBot = Cylon.robots['pebble']; var pebble = Cylon.robots.pebble;
pebbleBot.message(pebbleBot.devices.pebble, toPebble); 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() { Now, Let's define our Sphero robots:
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);
var bots = [ var bots = [
{ port: '/dev/tty.Sphero-ROY-AMP-SPP', name: 'ROY' }, { 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' } { port: '/dev/tty.Sphero-RRY-AMP-SPP', name: 'RRY' }
]; ];
for (var i = 0; i < bots.length; i++) { bots.forEach(function(bot) {
var bot = bots[i]; Cylon.robot({
var robot = new SpheroRobot; name: bot.name,
robot.connection.port = bot.port; connection: { name: 'sphero', adaptor: 'sphero', port: bot.port },
robot.name = bot.name; device: { name: 'sphero', driver: 'sphero' },
Cylon.robot(robot); totalBucks: 1,
payingPower: true,
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();
} }
}
});
},
Now that Cylon knows about all our robots, and what they'll be doing, we can changeDirection: function() {
start: 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(); Cylon.start();