cylon/examples/sphero-pebble-sf/sphero-pebble-sf.js

162 lines
3.3 KiB
JavaScript
Raw Normal View History

2014-11-26 06:39:24 +08:00
var Cylon = require("../..");
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
Cylon.api({ host: '0.0.0.0', port: '8080' });
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
Cylon.robot({
name: 'pebble',
2013-11-27 04:21:43 +08:00
2014-11-26 08:01:31 +08:00
connections: {
pebble: { adaptor: 'pebble' }
},
devices: {
pebble: { driver: 'pebble' }
},
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
message: function(msg) {
this.message_queue().push(msg);
},
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
work: function(my) {
console.log('Pebble connected');
}
});
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
Cylon.robot({
name: 'salesforce',
2013-11-27 04:21:43 +08:00
2014-11-26 08:01:31 +08:00
connections: {
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,
redirectUri: 'http://localhost:3000/oauth/_callback'
}
2013-11-27 04:21:43 +08:00
}
2014-11-26 06:39:24 +08:00
},
2013-11-27 04:21:43 +08:00
2014-11-26 08:01:31 +08:00
devices: {
salesforce: { driver: 'force' }
},
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
spheroReport: {},
2013-11-27 04:21:43 +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 toPebble = "",
name = data.sobject.Sphero_Name__c,
bucks = data.sobject.Bucks__c;
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
var msg = "Sphero: " + name + ",";
2013-11-27 04:21:43 +08:00
msg += "data Bucks: " + bucks + ",";
msg += "SM_Id: " + data.sobject.Id;
console.log(msg);
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
var sphero = Cylon.robots[name];
sphero.react();
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
my.spheroReport[name] = bucks;
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
for (var key in my.spheroReport) {
var val = my.spheroReport[key];
toPebble += key + ": $" + val + "\n";
2013-11-27 04:21:43 +08:00
}
2014-11-26 06:39:24 +08:00
var pebble = Cylon.robots.pebble;
pebble.message(toPebble);
2013-11-27 04:21:43 +08:00
});
});
2014-11-26 06:39:24 +08:00
}
});
var bots = [
{ port: '/dev/tty.Sphero-ROY-AMP-SPP', name: 'ROY' },
{ port: '/dev/tty.Sphero-GBO-AMP-SPP', name: 'GBO' },
{ port: '/dev/tty.Sphero-RRY-AMP-SPP', name: 'RRY' }
];
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
bots.forEach(function(bot) {
Cylon.robot({
name: bot.name,
2013-11-27 04:21:43 +08:00
2014-11-26 08:01:31 +08:00
connections: {
sphero: { adaptor: 'sphero', port: bot.port }
},
devices: {
sphero: { driver: 'sphero' }
},
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
totalBucks: 1,
payingPower: true,
2013-11-27 04:21:43 +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));
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
this.payingPower = true;
},
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
bankrupt: function() {
var my = this;
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
every((3).seconds(), function() {
if (my.payingPower && my.totalBucks > 0) {
my.totalBucks += -1;
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
if (my.totalBucks === 0) {
my.sphero.setRGB(0xFF000);
my.sphero.stop();
}
2013-11-27 04:21:43 +08:00
}
2014-11-26 06:39:24 +08:00
});
},
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
changeDirection: function() {
var my = this;
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
every((1).seconds(), function() {
if (my.payingPower) {
my.sphero.roll(90, Math.floor(Math.random() * 360));
}
2013-11-27 04:21:43 +08:00
});
2014-11-26 06:39:24 +08:00
},
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
work: function(my) {
console.log("Setting up collision detection for " + my.name);
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
my.sphero.detectCollisions();
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
my.sphero.stop();
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
my.sphero.setRGB(0x00FF00);
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
my.sphero.roll(90, Math.floor(Math.random() * 360));
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
my.bankrupt();
my.changeDirection();
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
my.sphero.on('collision', function() {
my.sphero.setRGB(0x0000FF);
my.sphero.stop();
my.payingPower = false;
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
var data = JSON.stringify({
spheroName: my.name,
bucks: "" + (my.totalBucks++)
});
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
var sf = Cylon.robots['salesforce'];
sf.devices.salesforce.push("SpheroController", "POST", data);
});
}
});
});
2013-11-27 04:21:43 +08:00
Cylon.start();