Skip to content

Instantly share code, notes, and snippets.

@ReidCarlberg
Created March 19, 2014 15:58
Show Gist options
  • Save ReidCarlberg/9644867 to your computer and use it in GitHub Desktop.
Save ReidCarlberg/9644867 to your computer and use it in GitHub Desktop.
var http = require('http');
var nforce = require('nforce');
var http = require('http'),
faye = require('faye');
var client;
var AckCounter = 0;
console.log("Welcome to the Drone interface");
function handleStreamingAPI(_oauth) {
client = new faye.Client(_oauth.instance_url + '/cometd/29.0');
client.setHeader("Authorization", "OAuth " + _oauth.access_token);
var subscription = client.subscribe('/topic/LAB_Drone_Messages', function(message) {
console.log(message.sobject.Message__c + "\n");
handleMessage(message.sobject.Address__c, message.sobject.Message__c);
});
console.log("Streaming API Connected...");
}
function handleMessage(address, message) {
console.log("address: " + address);
console.log("message: " + message);
if (address == 'D1') {
if (message == 'takeoff') {
console.log("in d1 takeoff");
handleRequest("http://10.0.0.2:1337/start");
} else if (message == 'land') {
console.log("in d1 land");
handleRequest("http://10.0.0.2:1337/stop");
}
} else if (address == "D2") {
if (message == 'takeoff') {
console.log("in d2 takeoff");
handleRequest("http://10.0.0.3:1337/start");
} else if (message == 'land') {
console.log("in d2 land");
handleRequest("http://10.0.0.3:1337/stop");
}
}
}
function handleRequest(url) {
http.get(url, function(res) {
console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
}
function heartbeat() {
AckCounter++;
console.log("Ready and waiting [" + AckCounter + "]");
setTimeout(function() { heartbeat(); }, 10000);
}
var sfuser = "[email protected]";
var sfpass = "passwordSecurityCode";
var org = nforce.createConnection({
clientId: 'clientID',
clientSecret: 'secret',
redirectUri: 'http://localhost:3000/oauth/_callback'
});
var displayResult = function(result) {
console.log(result);
};
var displayError = function(err) {
console.error(err);
};
console.log("Authenticating with Salesforce");
org.authenticate({ username: sfuser, password: sfpass}, function(err, oauth) {
if(err) {
console.error('unable to authenticate to sfdc');
console.log(err);
process.exit(code=0);
} else {
console.log("authenticated");
console.log("oauth");
console.log(oauth);
handleStreamingAPI(oauth);
heartbeat();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment