2014-03-28 00:31:56 +08:00
|
|
|
# Sales Force Sphero
|
2014-02-21 08:29:18 +08:00
|
|
|
|
|
|
|
First, let's import Cylon:
|
|
|
|
|
2014-03-28 00:31:56 +08:00
|
|
|
var Cylon = require('../..');
|
|
|
|
|
2014-11-26 06:39:24 +08:00
|
|
|
With that done, let's define the Robot we'll use to communicate with Salesforce:
|
2014-03-28 00:31:56 +08:00
|
|
|
|
2014-11-26 06:39:24 +08:00
|
|
|
Cylon.robot({
|
|
|
|
name: 'salesforce',
|
2014-02-21 08:29:18 +08:00
|
|
|
|
2014-11-26 06:39:24 +08:00
|
|
|
connection: {
|
2014-03-28 00:31:56 +08:00
|
|
|
name: 'sfcon',
|
|
|
|
adaptor: 'force',
|
|
|
|
sfuser: process.env.SF_USERNAME,
|
|
|
|
sfpass: process.env.SF_SECURITY_TOKEN,
|
|
|
|
orgCreds: {
|
|
|
|
clientId: process.env.SF_CLIENT_ID,
|
|
|
|
clientSecret: process.env.SF_CLIENT_SECRET,
|
2014-02-21 08:29:18 +08:00
|
|
|
redirectUri: 'http://localhost:3000/oauth/_callback'
|
2014-03-28 00:31:56 +08:00
|
|
|
}
|
2014-11-26 06:39:24 +08:00
|
|
|
},
|
2014-02-21 08:29:18 +08:00
|
|
|
|
2014-11-26 06:39:24 +08:00
|
|
|
device: { name: 'salesforce', driver: 'force' },
|
2014-02-21 08:29:18 +08:00
|
|
|
|
2014-11-26 06:39:24 +08:00
|
|
|
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;
|
2014-02-21 08:29:18 +08:00
|
|
|
|
2014-03-28 00:31:56 +08:00
|
|
|
console.log(msg);
|
2014-11-26 06:39:24 +08:00
|
|
|
|
|
|
|
var sphero = Cylon.robots[data.sobject.Sphero_Name__c];
|
|
|
|
sphero.react();
|
2014-03-28 00:31:56 +08:00
|
|
|
});
|
|
|
|
});
|
2014-11-26 06:39:24 +08:00
|
|
|
}
|
|
|
|
});
|
2014-03-28 00:31:56 +08:00
|
|
|
|
2014-11-26 06:39:24 +08:00
|
|
|
Next up, the shape our Sphero Robot will take:
|
2014-03-28 00:31:56 +08:00
|
|
|
|
2014-11-26 06:39:24 +08:00
|
|
|
Cylon.robot({
|
|
|
|
name: 'ROY',
|
|
|
|
connection: { name: 'sphero', adaptor: 'sphero' },
|
|
|
|
device: { name: 'sphero', driver: 'sphero' },
|
2014-03-28 00:31:56 +08:00
|
|
|
|
2014-11-26 06:39:24 +08:00
|
|
|
react: function() {
|
|
|
|
this.sphero.setRGB(0x00FF00);
|
|
|
|
this.sphero.roll(90, Math.floor(Math.random() * 360));
|
|
|
|
},
|
2014-03-28 00:31:56 +08:00
|
|
|
|
2014-11-26 06:39:24 +08:00
|
|
|
work: function(my) {
|
|
|
|
console.log('Setting up collision detection.');
|
|
|
|
my.sphero.detectCollisions();
|
2014-03-28 00:31:56 +08:00
|
|
|
|
2014-11-26 06:39:24 +08:00
|
|
|
my.sphero.stop();
|
|
|
|
my.sphero.setRGB(0x00FF00);
|
2014-03-28 00:31:56 +08:00
|
|
|
|
2014-11-26 06:39:24 +08:00
|
|
|
my.sphero.roll(90, Math.floor(Math.random() * 360));
|
2014-03-28 00:31:56 +08:00
|
|
|
|
2014-11-26 06:39:24 +08:00
|
|
|
my.sphero.on('collision', function() {
|
|
|
|
my.sphero.setRGB(0x0000FF, my);
|
|
|
|
my.sphero.stop();
|
|
|
|
|
|
|
|
var data = JSON.stringify({
|
|
|
|
spheroName: my.name,
|
|
|
|
bucks: "" + (my.totalBucks++)
|
2014-03-28 00:31:56 +08:00
|
|
|
});
|
2014-11-26 06:39:24 +08:00
|
|
|
|
|
|
|
var sf = Cylon.robots.salesforce;
|
2014-06-07 02:53:54 +08:00
|
|
|
sf.devices.salesforce.push('SpheroController', 'POST', data);
|
2014-03-28 00:31:56 +08:00
|
|
|
});
|
2014-11-26 06:39:24 +08:00
|
|
|
}
|
|
|
|
});
|
2014-02-21 08:29:18 +08:00
|
|
|
|
|
|
|
Now that our robot knows what work to do, and the work it will be doing that
|
|
|
|
hardware with, we can start it:
|
|
|
|
|
2014-03-28 00:31:56 +08:00
|
|
|
Cylon.start();
|