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('../..');
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();

View File

@ -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();

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:
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

View File

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

View File

@ -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.")
});
}

View File

@ -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!");
console.log("a pressed!");
});
}
}).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.
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!

View File

@ -2,14 +2,11 @@ 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) {

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:
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();
}
});
}

View File

@ -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();

View File

@ -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();

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
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);
});
}

View File

@ -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();

View File

@ -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:

View File

@ -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();
}
});

View File

@ -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();
}
});
}

View File

@ -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();

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
`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();