Merge pull request #35 from hybridgroup/salesforce

Salesforce examples and changes for Dreamforce demos.
This commit is contained in:
Ron Evans 2013-11-16 20:01:55 -08:00
commit 77eaa44ead
10 changed files with 301 additions and 14 deletions

8
dist/connection.js vendored
View File

@ -38,7 +38,7 @@
this.robot = opts.robot;
this.name = opts.name;
this.connection_id = opts.id;
this.adaptor = this.requireAdaptor(opts.adaptor);
this.adaptor = this.requireAdaptor(opts);
this.port = new Cylon.Port(opts.port);
proxyFunctionsToObject(this.adaptor.commands(), this.adaptor, this.self);
}
@ -72,9 +72,9 @@
return this.adaptor.disconnect();
};
Connection.prototype.requireAdaptor = function(adaptorName) {
Logger.debug("Loading adaptor '" + adaptorName + "'");
return this.robot.requireAdaptor(adaptorName, this.self);
Connection.prototype.requireAdaptor = function(opts) {
Logger.debug("Loading adaptor '" + opts.adaptor + "'");
return this.robot.requireAdaptor(opts.adaptor, this.self, opts);
};
return Connection;

11
dist/robot.js vendored
View File

@ -182,19 +182,24 @@
return _results;
};
Robot.prototype.requireAdaptor = function(adaptorName, connection) {
Robot.prototype.requireAdaptor = function(adaptorName, connection, opts) {
if (opts == null) {
opts = {};
}
if (this.robot.adaptors[adaptorName] != null) {
if (typeof this.robot.adaptors[adaptorName] === 'string') {
this.robot.adaptors[adaptorName] = require(this.robot.adaptors[adaptorName]).adaptor({
name: adaptorName,
connection: connection
connection: connection,
extraParams: opts
});
}
} else {
require("cylon-" + adaptorName).register(this);
this.robot.adaptors[adaptorName] = require("cylon-" + adaptorName).adaptor({
name: adaptorName,
connection: connection
connection: connection,
extraParams: opts
});
}
return this.robot.adaptors[adaptorName];

View File

View File

@ -0,0 +1,29 @@
Cylon = require('..')
Cylon.robot
connection:
name: 'sfcon',
adaptor: 'force',
sfuser: "edgarsilva@hybridgroup.com",
sfpass: "password1232YOQR0HQMpQ5f74msKqaPuCD6",
orgCreds: {
clientId: '3MVG9A2kN3Bn17huqBLyrtmQ9Cgwc.FjKA4769ApTRhNNjgKEetcGv23W97cJQ3ER3VXxzyREIaD0Bp1Or8ou',
clientSecret: '6079348238616906521',
redirectUri: 'http://localhost:3000/oauth/_callback'
}
device:
name: 'salesforce', driver: 'force'
work: (me) ->
me.salesforce.on('start', () ->
me.salesforce.subscribe('/topic/SpheroMsgOutbound', (data) ->
Logger.info "Sphero: #{ data.sobject.Sphero_Name__c }, data Content: #{ data.sobject.Content__c }, SM_Id: #{ data.sobject.Id }"
)
)
# push(apexPath, method, body)
toSend = "{ \"identifier\" :\"#{ me.name }\", \"msg\": \"#{ 'Salesforce Bot #2' }\" }"
me.salesforce.push('SpheroController', 'POST', toSend)
.start()

52
examples/sf-client.coffee Normal file
View File

@ -0,0 +1,52 @@
'use strict';
nforce = require('nforce')
faye = require('faye')
namespace = require('node-namespace')
namespace 'SF', ->
class @SFClient
constructor: (opts) ->
@client = null
@outboundMessages = []
@sfuser = opts.sfuser
@sfpass = opts.sfpass
@orgCreds = opts.orgCredentials
@org = nforce.createConnection(@orgCreds)
_processOutboundMessages: () ->
# Do work here
_handleStreamingAPI: (outboundCB) ->
client = new faye.Client(@oauth.instance_url + '/cometd/28.0')
client.setHeader("Authorization", "OAuth #{ @oauth.access_token }")
subscription = client.subscribe('/topic/SpheroMsgOutbound', outboundCB)
console.log("Streaming API Connected...")
authenticate: (outboundCB) ->
@org.authenticate({ username: @sfuser, password: @sfpass}, (err, _oauth) =>
if(err)
console.error('unable to authenticate to sfdc')
console.log(err)
process.exit(code=0)
else
console.log("authenticated")
@oauth = _oauth
@_handleStreamingAPI(outboundCB)
@_processOutboundMessages()
)
push: (msg) ->
#jsonBody = JSON.parse(msg)
#jsonString = JSON.stringify(msg)
jsonString = msg
console.log("SpheroController post msg:")
console.log(msg)
@org.apexRest({uri:'SpheroController', method: 'POST', body: jsonString}, @oauth, (err,resp) =>
if(err)
console.log(err)
else
console.log(resp)
)

69
examples/sf-sphero.coffee Normal file
View File

@ -0,0 +1,69 @@
Cylon = require('..')
bots = [
{ port: '/dev/rfcomm0', name: 'sphy-rgr' },
{ port: '/dev/rfcomm1', name: 'sphy-bpy' }
{ name: 'salesforce' }
]
class SalesforceRobot
connection:
name: 'sfcon',
adaptor: 'force',
sfuser: "edgarsilva@hybridgroup.com",
sfpass: "password1232YOQR0HQMpQ5f74msKqaPuCD6",
orgCreds: {
clientId: '3MVG9A2kN3Bn17huqBLyrtmQ9Cgwc.FjKA4769ApTRhNNjgKEetcGv23W97cJQ3ER3VXxzyREIaD0Bp1Or8ou',
clientSecret: '6079348238616906521',
redirectUri: 'http://localhost:3000/oauth/_callback'
}
device:
name: 'salesforce', driver: 'force'
work: (me) ->
me.salesforce.on('start', () ->
me.salesforce.subscribe('/topic/SpheroMsgOutbound', (data) ->
spheroName = data.sobject.Sphero_Name__c
Logger.info "Sphero: #{ spheroName }, data Content: #{ data.sobject.Content__c }, SM_Id: #{ data.sobject.Id }"
me.master.findRobot(spheroName, (err, spheroBot) ->
spheroBot.devices.sphero.setRGB(0x00FF00)
spheroBot.devices.sphero.roll 90, Math.floor(Math.random() * 360)
)
)
)
class SpheroRobot
connection:
name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0'
device:
name: 'sphero', driver: 'sphero'
work: (me) ->
me.sphero.on 'connect', ->
Logger.info('Setting up Collision Detection...')
me.sphero.detectCollisions()
me.sphero.stop()
me.sphero.setRGB(0x00FF00)
me.sphero.roll 90, Math.floor(Math.random() * 360)
me.sphero.on 'collision', (data) ->
me.sphero.setRGB(0xFF0000, me)
me.sphero.stop()
toSend = "{ \"identifier\" :\"#{ me.name }\", \"msg\": \"#{ 'Collision detected' }\" }"
me.master.findRobot('salesforce', (err, sf) ->
sf.devices.salesforce.push('SpheroController', 'POST', toSend)
)
for bot in bots
robot = if ( bot.name == 'salesforce' ) then new SalesforceRobot else new SpheroRobot
robot.connection.port = bot.port
robot.name = bot.name
console.log("Name: #{ robot.name }")
Cylon.robot robot
Cylon.start()

29
examples/sf-test.coffee Normal file
View File

@ -0,0 +1,29 @@
require('../examples/sf-client')
sfuser = "edgarsilva@hybridgroup.com"
sfpass = "password1232YOQR0HQMpQ5f74msKqaPuCD6"
orgCreds = {
clientId: '3MVG9A2kN3Bn17huqBLyrtmQ9Cgwc.FjKA4769ApTRhNNjgKEetcGv23W97cJQ3ER3VXxzyREIaD0Bp1Or8ou'
clientSecret: '6079348238616906521'
redirectUri: 'http://localhost:3000/oauth/_callback'
}
sf = new Cylon.SF.SFClient(sfuser: sfuser, sfpass: sfpass, orgCredentials: orgCreds)
cc = 0
sf.authenticate((msg) ->
simpleMessageString = "^ Sphero Name: #{msg.sobject.Sphero_Name__c}, Msg Content:#{ msg.sobject.Content__c }, SM Id:#{ msg.sobject.Id }"
console.log("Printed from callback in client program.program")
console.log(simpleMessageString + "\n")
)
myId = null
message = "hello"
detail = "Some Stuff for details"
setInterval(() =>
cc++
myId = cc
toSend = "{ \"identifier\" :\"run3#{ myId }\", \"msg\": \"#{ message }\" }"
sf.push(toSend)
, 1000)

View File

@ -0,0 +1,103 @@
Cylon = require('..')
Cylon.api host: '0.0.0.0', port: '8080'
class PebbleRobot
connection:
name: 'pebble', adaptor: 'pebble'
device:
name: 'pebble', driver: 'pebble'
message: (robot, msg) =>
robot.message_queue().push(msg)
work: (me) ->
me.pebble.on('connect', ->
console.log('connected!')
)
class SalesforceRobot
connection:
name: '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'
}
device:
name: 'salesforce', driver: 'force'
spheroReport:{}
work: (me) ->
me.salesforce.on('start', () ->
me.salesforce.subscribe('/topic/SpheroMsgOutbound', (data) ->
spheroName = data.sobject.Sphero_Name__c
counter = data.sobject.Content__c
Logger.info "Sphero: #{ spheroName }, data Content: #{ counter }, SM_Id: #{ data.sobject.Id }"
me.master.findRobot(spheroName, (err, spheroBot) ->
spheroBot.react(spheroBot.devices.sphero)
)
me.spheroReport[spheroName] = counter
toPebble = ""
for key, val of me.spheroReport
toPebble += "#{key}: $#{val}\n"
me.master.findRobot('pebble', (error, pebbleBot) ->
pebbleBot.message(pebbleBot.devices.pebble, toPebble)
)
)
)
class SpheroRobot
total_collisions: 0
connection:
name: 'sphero', adaptor: 'sphero'
device:
name: 'sphero', driver: 'sphero'
react: (robot) =>
robot.setRGB(0x00FF00)
robot.roll 90, Math.floor(Math.random() * 360)
work: (me) ->
me.sphero.on 'connect', ->
Logger.info('Setting up Collision Detection...')
me.sphero.detectCollisions()
me.sphero.stop()
me.sphero.setRGB(0x00FF00)
me.sphero.roll 90, Math.floor(Math.random() * 360)
me.sphero.on 'collision', (data) ->
me.sphero.setRGB(0xFF0000, me)
me.sphero.stop()
toSend = "{ \"identifier\" :\"#{ me.name }\", \"msg\": \"#{ me.total_collisions++ }\" }"
me.master.findRobot('salesforce', (err, sf) ->
sf.devices.salesforce.push('SpheroController', 'POST', toSend)
)
sfRobot = new SalesforceRobot()
sfRobot.name = "salesforce"
Cylon.robot sfRobot
pebRobot = new PebbleRobot()
pebRobot.name = "pebble"
Cylon.robot pebRobot
bots = [
{ port: '/dev/rfcomm0', name: 'ROY' },
{ port: '/dev/rfcomm1', name: 'GPG'}
]
for bot in bots
robot = new SpheroRobot
robot.connection.port = bot.port
robot.name = bot.name
Cylon.robot robot
Cylon.start()

View File

@ -35,7 +35,7 @@ namespace 'Cylon', ->
@robot = opts.robot
@name = opts.name
@connection_id = opts.id
@adaptor = @requireAdaptor(opts.adaptor) # or 'loopback')
@adaptor = @requireAdaptor(opts) # or 'loopback')
@port = new Cylon.Port(opts.port)
proxyFunctionsToObject @adaptor.commands(), @adaptor, @self
@ -75,8 +75,8 @@ namespace 'Cylon', ->
# adaptorName - module name of adaptor to require
#
# Returns the set-up adaptor
requireAdaptor: (adaptorName) ->
Logger.debug "Loading adaptor '#{adaptorName}'"
@robot.requireAdaptor(adaptorName, @self)
requireAdaptor: (opts) ->
Logger.debug "Loading adaptor '#{opts.adaptor}'"
@robot.requireAdaptor(opts.adaptor, @self, opts)
module.exports = Cylon.Connection

View File

@ -168,13 +168,13 @@ namespace 'Cylon', ->
# connection - the Connection that requested the adaptor be required
#
# Returns the set-up adaptor
requireAdaptor: (adaptorName, connection) ->
requireAdaptor: (adaptorName, connection, opts = {}) ->
if @robot.adaptors[adaptorName]?
if typeof @robot.adaptors[adaptorName] is 'string'
@robot.adaptors[adaptorName] = require(@robot.adaptors[adaptorName]).adaptor(name: adaptorName, connection: connection)
@robot.adaptors[adaptorName] = require(@robot.adaptors[adaptorName]).adaptor(name: adaptorName, connection: connection, extraParams: opts)
else
require("cylon-#{adaptorName}").register(this)
@robot.adaptors[adaptorName] = require("cylon-#{adaptorName}").adaptor(name: adaptorName, connection: connection)
@robot.adaptors[adaptorName] = require("cylon-#{adaptorName}").adaptor(name: adaptorName, connection: connection, extraParams: opts)
return @robot.adaptors[adaptorName]