Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
3D Printer Farm Management with Salesforce1 - Code
var http = require('http');
var request = require('request');
var nforce = require('nforce');
var http = require('http'),
faye = require('faye');
var client;
//salesforce settings
var sfuser = "user@some.name";
var sfpass = "yourpasswordSECTURIYTOKEN";
var org = nforce.createConnection({
clientId: 'oauth-client-id',
clientSecret: '4816545411598672683',
redirectUri: 'http://localhost:3000/oauth/_callback'
});
var pushTopicName = '/topic/Printer2';
var orgOauth;
//octoprint settings
//var PrinterName = 'Printer1';
//var OctoPrintURL = 'http://octopi.local';
//var OctoPrintApiKey = '6F16F662DF8C460DB38D50549B534706'; //printer1
var PrinterName = 'Printer2';
var OctoPrintApiKey = '6437AE8028D9409180845EF7D26E5B01'; //printer2
var OctoPrintURL = 'http://octopi2.local';
var AckCounter = 0;
var lastMessageId;
console.log("Welcome to the 3D Printer Farm Interface interface");
function handleStreamingAPI() {
client = new faye.Client(orgOauth.instance_url + '/cometd/29.0');
client.setHeader("Authorization", "OAuth " + orgOauth.access_token);
var subscription = client.subscribe(pushTopicName, function(message) {
console.log(message.sobject.Message__c + "\n");
lastMessageId = message.sobject.Id;
handleOctoPrintCall(message.sobject.Message__c, message.sobject.Body__c);
//handleMessage(message.sobject.Address__c, message.sobject.Message__c);
});
console.log("Streaming API Connected...");
}
function heartbeat() {
AckCounter++;
console.log("Ready and waiting [" + AckCounter + "]");
setTimeout(function() { heartbeat(); }, 10000);
}
function handleOctoPrintCall(command, body) {
var options;
if (body == null) {
console.log("body is null");
options = {
url: OctoPrintURL + command,
headers: {
'X-Api-Key': OctoPrintApiKey
}
};
} else {
console.log("body isn't null");
options = {
url: OctoPrintURL + command,
method: "POST",
body: body,
headers: {
'X-Api-Key': OctoPrintApiKey,
'Content-Type': 'application/json'
}
};
}
request(options, callback);
}
function callback(error, response, body) {
console.log("callback from printer");
//console.log(error);
//console.log(response);
console.log(body);
console.log("end callback from printer");
if (!error && response.statusCode == 200) {
console.log(body);
sendPrinterResponse(body);
} else if (!error && response.statusCode == 204) {
console.log("Empty Body (204)");
sendPrinterResponse( JSON.stringify({ "result" : "OK" }) );
}
}
function sendPrinterResponse(body) {
var newBody = {
originalId : lastMessageId,
responseJson : body
};
console.log("sendPrinterResponse");
console.log(newBody);
var options = {
url: orgOauth.instance_url + '/services/apexrest/PrinterResponse',
method: "POST",
body: JSON.stringify(newBody),
headers: {
'Authorization': 'OAuth ' + orgOauth.access_token,
"Content-Type": 'application/json'
}
};
request(options, printerResponseCallback);
}
function printerResponseCallback(error, response, body) {
console.log('printerResponseCallback');
//console.log(error);
//console.log(response);
//console.log(body);
lastMessageId = '';
if (!error) {
console.log("success");
console.log(body);
} else {
console.log("error");
console.log(error);
}
}
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, resp) {
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(resp);
orgOauth = resp;
handleStreamingAPI();
heartbeat();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment