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

83 lines
1.7 KiB
JavaScript
Raw Normal View History

2014-12-14 08:19:25 +08:00
"use strict";
var Cylon = require("../..");
2014-11-26 06:39:24 +08:00
Cylon.robot({
2014-12-14 08:19:25 +08:00
name: "salesforce",
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
}
}
2014-11-26 06:39:24 +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
},
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 msg = "Sphero: " + data.sobject.Sphero_Name__c + ",";
2015-04-15 12:49:12 +08:00
msg += "Bucks: " + data.sobject.Bucks__c + ",";
msg += "SM_Id: " + data.sobject.Id;
console.log(msg);
2014-11-26 06:39:24 +08:00
var sphero = Cylon.robots[data.sobject.Sphero_Name__c];
sphero.react();
});
});
2014-11-26 06:39:24 +08:00
}
});
2014-11-26 06:39:24 +08:00
Cylon.robot({
2014-12-14 08:19:25 +08:00
name: "ROY",
2014-11-26 08:01:31 +08:00
connections: {
2014-12-14 08:19:25 +08:00
sphero: { adaptor: "sphero" }
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
},
2014-11-26 06:39:24 +08:00
react: function() {
this.sphero.setRGB(0x00FF00);
this.sphero.roll(90, Math.floor(Math.random() * 360));
},
2014-11-26 06:39:24 +08:00
work: function(my) {
2014-12-14 08:19:25 +08:00
console.log("Setting up collision detection.");
2014-11-26 06:39:24 +08:00
my.sphero.detectCollisions();
2014-11-26 06:39:24 +08:00
my.sphero.stop();
my.sphero.setRGB(0x00FF00);
2014-11-26 06:39:24 +08:00
my.sphero.roll(90, Math.floor(Math.random() * 360));
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);
my.sphero.stop();
2014-11-26 06:39:24 +08:00
var data = JSON.stringify({
spheroName: my.name,
bucks: "" + (my.totalBucks++)
});
2014-11-26 06:39:24 +08:00
var sf = Cylon.robots.salesforce;
2014-12-14 08:19:25 +08:00
sf.devices.salesforce.push("SpheroController", "POST", data);
});
2014-11-26 06:39:24 +08:00
}
});
Cylon.start();