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

166 lines
3.4 KiB
JavaScript
Raw Permalink Normal View History

2014-12-14 08:19:25 +08:00
"use strict";
2014-11-26 06:39:24 +08:00
var Cylon = require("../..");
2013-11-27 04:21:43 +08:00
// ensure you install the API plugin first:
// $ npm install cylon-api-http
2014-12-14 08:19:25 +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({
2014-12-14 08:19:25 +08:00
name: "pebble",
2013-11-27 04:21:43 +08:00
2014-11-26 08:01:31 +08:00
connections: {
2014-12-14 08:19:25 +08:00
pebble: { adaptor: "pebble" }
2014-11-26 08:01:31 +08:00
},
devices: {
2014-12-14 08:19:25 +08:00
pebble: { driver: "pebble" }
2014-11-26 08:01:31 +08:00
},
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-12-14 08:19:25 +08:00
work: function() {
console.log("Pebble connected");
2014-11-26 06:39:24 +08:00
}
});
2013-11-27 04:21:43 +08:00
2014-11-26 06:39:24 +08:00
Cylon.robot({
2014-12-14 08:19:25 +08:00
name: "salesforce",
2013-11-27 04:21:43 +08:00
2014-11-26 08:01:31 +08:00
connections: {
sfcon: {
2014-12-14 08:19:25 +08:00
adaptor: "force",
2014-11-26 08:01:31 +08:00
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-12-14 08:19:25 +08:00
redirectUri: "http://localhost:3000/oauth/_callback"
2014-11-26 08:01:31 +08:00
}
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: {
2014-12-14 08:19:25 +08:00
salesforce: { driver: "force" }
2014-11-26 08:01:31 +08:00
},
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) {
2014-12-14 08:19:25 +08:00
my.salesforce.on("start", function() {
my.salesforce.subscribe("/topic/SpheroMsgOutbound", function(data) {
2014-11-26 06:39:24 +08:00
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 = [
2014-12-14 08:19:25 +08:00
{ 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" }
2014-11-26 06:39:24 +08:00
];
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: {
2014-12-14 08:19:25 +08:00
sphero: { adaptor: "sphero", port: bot.port }
2014-11-26 08:01:31 +08:00
},
devices: {
2014-12-14 08:19:25 +08:00
sphero: { driver: "sphero" }
2014-11-26 08:01:31 +08:00
},
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-12-14 08:19:25 +08:00
my.sphero.on("collision", function() {
2014-11-26 06:39:24 +08:00
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
2015-04-15 12:49:12 +08:00
var sf = Cylon.robots.salesforce;
2014-11-26 06:39:24 +08:00
sf.devices.salesforce.push("SpheroController", "POST", data);
});
}
});
});
2013-11-27 04:21:43 +08:00
Cylon.start();