From 74160047a81c952f3b0d85c8cd2753557d7b3c03 Mon Sep 17 00:00:00 2001 From: Edgar O Silva Date: Thu, 14 Nov 2013 10:34:29 -0600 Subject: [PATCH 01/10] Sf Client and S Example. --- dist/sf-client.js | 87 +++++++++++++++++++++++++++++++++++++++++ examples/sf-test.coffee | 29 ++++++++++++++ src/sf-client.coffee | 54 +++++++++++++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 dist/sf-client.js create mode 100644 examples/sf-test.coffee create mode 100644 src/sf-client.coffee diff --git a/dist/sf-client.js b/dist/sf-client.js new file mode 100644 index 0000000..f9431fc --- /dev/null +++ b/dist/sf-client.js @@ -0,0 +1,87 @@ +(function() { + 'use strict'; + var faye, http, namespace, nforce; + + nforce = require('nforce'); + + http = require('http'); + + faye = require('faye'); + + namespace = require('node-namespace'); + + namespace('Cylon.SF', function() { + return this.SFClient = (function() { + function SFClient(opts) { + this.client = null; + this.outboundMessages = []; + this.sfuser = opts.sfuser; + this.sfpass = opts.sfpass; + this.orgCreds = opts.orgCredentials; + this.org = nforce.createConnection(this.orgCreds); + } + + SFClient.prototype._processOutboundMessages = function() { + var msg, _i, _len, _ref, _results; + _ref = this.outboundMessages; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + msg = _ref[_i]; + _results.push(console.log(msg)); + } + return _results; + }; + + SFClient.prototype._handleStreamingAPI = function(outboundCB) { + var client, subscription; + client = new faye.Client(this.oauth.instance_url + '/cometd/28.0'); + client.setHeader("Authorization", "OAuth " + this.oauth.access_token); + subscription = client.subscribe('/topic/SpheroMsgOutbound', outboundCB); + return console.log("Streaming API Connected..."); + }; + + SFClient.prototype.authenticate = function(outboundCB) { + var _this = this; + return this.org.authenticate({ + username: this.sfuser, + password: this.sfpass + }, function(err, _oauth) { + var code; + if (err) { + console.error('unable to authenticate to sfdc'); + console.log(err); + return process.exit(code = 0); + } else { + console.log("authenticated"); + console.log("oauth"); + console.log(_oauth); + _this.oauth = _oauth; + _this._handleStreamingAPI(outboundCB); + return _this._processOutboundMessages(); + } + }); + }; + + SFClient.prototype.push = function(msg) { + var jsonString, + _this = this; + jsonString = msg; + return this.org.apexRest({ + uri: 'SpheroController', + method: 'POST', + body: jsonString + }, this.oauth, function(err, resp) { + if (err) { + return console.log(err); + } else { + return console.log(resp); + } + }); + }; + + return SFClient; + + })(); + }); + +}).call(this); diff --git a/examples/sf-test.coffee b/examples/sf-test.coffee new file mode 100644 index 0000000..581de3d --- /dev/null +++ b/examples/sf-test.coffee @@ -0,0 +1,29 @@ +require('../dist/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) diff --git a/src/sf-client.coffee b/src/sf-client.coffee new file mode 100644 index 0000000..69e450d --- /dev/null +++ b/src/sf-client.coffee @@ -0,0 +1,54 @@ +'use strict'; + +nforce = require('nforce') +http = require('http') +faye = require('faye') + +namespace = require('node-namespace') + +namespace 'Cylon.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 + console.log(msg) for msg in @outboundMessages + + _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") + console.log("oauth") + console.log(_oauth) + @oauth = _oauth + @_handleStreamingAPI(outboundCB) + @_processOutboundMessages() + ) + + push: (msg) -> + #jsonBody = JSON.parse(msg) + #jsonString = JSON.stringify(msg) + jsonString = msg + @org.apexRest({uri:'SpheroController', method: 'POST', body: jsonString}, @oauth, (err,resp) => + if(err) + console.log(err) + else + console.log(resp) + ) From 22fc963a44a9da91277d44e5075b50f666e6f46a Mon Sep 17 00:00:00 2001 From: Edgar O Silva Date: Thu, 14 Nov 2013 12:50:34 -0600 Subject: [PATCH 02/10] WIP on SF-Cylon connection --- examples/sf-sphero.coffee | 60 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 examples/sf-sphero.coffee diff --git a/examples/sf-sphero.coffee b/examples/sf-sphero.coffee new file mode 100644 index 0000000..f8978b2 --- /dev/null +++ b/examples/sf-sphero.coffee @@ -0,0 +1,60 @@ +require '../dist/sf-client' +Cylon = require('..') + +class SalesForceRobot + + connection: + name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0' + + device: + name: 'sphero', driver: 'sphero' + + setupSF: -> + new Cylon.SF.SFClient(sfuser: sfuser, sfpass: sfpass, orgCredentials: orgCreds) + + sfCreds: + { + sfuser: "edgarsilva@hybridgroup.com" + sfpass: "password1232YOQR0HQMpQ5f74msKqaPuCD6" + orgCredentials: { + clientId: '3MVG9A2kN3Bn17huqBLyrtmQ9Cgwc.FjKA4769ApTRhNNjgKEetcGv23W97cJQ3ER3VXxzyREIaD0Bp1Or8ou' + clientSecret: '6079348238616906521' + redirectUri: 'http://localhost:3000/oauth/_callback' + } + } + + work: (me) -> + color = 0x00FF00 + bitFilter = 0xFFFF00 + + me.sf = me.setupSF() + + mw.sf.authenticate((msg) => + @spheroRoll(0x00FF00) + ) + + me.sphero.on('connect', -> + Logger.info('Setting up Collision Detection...') + me.sphero.detectCollisions() + me.sphero.stop() + @spheroRoll(0x00FF00) + ) + + me.sphero.on 'collision', (data) -> + Logger.info 'collision:' + me.sphero.setRGB(0xFF0000) + me.sphero.stop() + console.log("Collision Data:") + console.log(data) + toSend = "{ \"identifier\" :\"#{ me.name }\", \"msg\": \"#{ data }\" }" + @sf.psh(toSend) + + spheroRoll: (color) -> + me.sphero.setRGB(color) + me.sphero.roll 90, Math.floor(Math.random() * 360) + +robot = new SalesForceRobot + +Cylon.robot robot + +Cylon.start() From b88e674178eb7a59004051c681893b93985e6c4e Mon Sep 17 00:00:00 2001 From: Edgar O Silva Date: Thu, 14 Nov 2013 12:59:57 -0600 Subject: [PATCH 03/10] WIP on Sphero SF --- dist/sf-client.js | 87 ------------------------------ {src => examples}/sf-client.coffee | 0 examples/sf-sphero.coffee | 2 +- examples/sf-test.coffee | 2 +- 4 files changed, 2 insertions(+), 89 deletions(-) delete mode 100644 dist/sf-client.js rename {src => examples}/sf-client.coffee (100%) diff --git a/dist/sf-client.js b/dist/sf-client.js deleted file mode 100644 index f9431fc..0000000 --- a/dist/sf-client.js +++ /dev/null @@ -1,87 +0,0 @@ -(function() { - 'use strict'; - var faye, http, namespace, nforce; - - nforce = require('nforce'); - - http = require('http'); - - faye = require('faye'); - - namespace = require('node-namespace'); - - namespace('Cylon.SF', function() { - return this.SFClient = (function() { - function SFClient(opts) { - this.client = null; - this.outboundMessages = []; - this.sfuser = opts.sfuser; - this.sfpass = opts.sfpass; - this.orgCreds = opts.orgCredentials; - this.org = nforce.createConnection(this.orgCreds); - } - - SFClient.prototype._processOutboundMessages = function() { - var msg, _i, _len, _ref, _results; - _ref = this.outboundMessages; - _results = []; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - msg = _ref[_i]; - _results.push(console.log(msg)); - } - return _results; - }; - - SFClient.prototype._handleStreamingAPI = function(outboundCB) { - var client, subscription; - client = new faye.Client(this.oauth.instance_url + '/cometd/28.0'); - client.setHeader("Authorization", "OAuth " + this.oauth.access_token); - subscription = client.subscribe('/topic/SpheroMsgOutbound', outboundCB); - return console.log("Streaming API Connected..."); - }; - - SFClient.prototype.authenticate = function(outboundCB) { - var _this = this; - return this.org.authenticate({ - username: this.sfuser, - password: this.sfpass - }, function(err, _oauth) { - var code; - if (err) { - console.error('unable to authenticate to sfdc'); - console.log(err); - return process.exit(code = 0); - } else { - console.log("authenticated"); - console.log("oauth"); - console.log(_oauth); - _this.oauth = _oauth; - _this._handleStreamingAPI(outboundCB); - return _this._processOutboundMessages(); - } - }); - }; - - SFClient.prototype.push = function(msg) { - var jsonString, - _this = this; - jsonString = msg; - return this.org.apexRest({ - uri: 'SpheroController', - method: 'POST', - body: jsonString - }, this.oauth, function(err, resp) { - if (err) { - return console.log(err); - } else { - return console.log(resp); - } - }); - }; - - return SFClient; - - })(); - }); - -}).call(this); diff --git a/src/sf-client.coffee b/examples/sf-client.coffee similarity index 100% rename from src/sf-client.coffee rename to examples/sf-client.coffee diff --git a/examples/sf-sphero.coffee b/examples/sf-sphero.coffee index f8978b2..dfe3237 100644 --- a/examples/sf-sphero.coffee +++ b/examples/sf-sphero.coffee @@ -1,4 +1,4 @@ -require '../dist/sf-client' +require './sf-client' Cylon = require('..') class SalesForceRobot diff --git a/examples/sf-test.coffee b/examples/sf-test.coffee index 581de3d..f21daea 100644 --- a/examples/sf-test.coffee +++ b/examples/sf-test.coffee @@ -1,4 +1,4 @@ -require('../dist/sf-client') +require('./sf-client') sfuser = "edgarsilva@hybridgroup.com" sfpass = "password1232YOQR0HQMpQ5f74msKqaPuCD6" From cb2ef05201737bd4d8ffdd8251b16634ded5ca63 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Thu, 14 Nov 2013 13:05:41 -0800 Subject: [PATCH 04/10] Happy references --- examples/sf-sphero.coffee | 2 +- examples/sf-test.coffee | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/sf-sphero.coffee b/examples/sf-sphero.coffee index dfe3237..0a7cd14 100644 --- a/examples/sf-sphero.coffee +++ b/examples/sf-sphero.coffee @@ -1,4 +1,4 @@ -require './sf-client' +require '../examples/sf-client' Cylon = require('..') class SalesForceRobot diff --git a/examples/sf-test.coffee b/examples/sf-test.coffee index f21daea..5f2a083 100644 --- a/examples/sf-test.coffee +++ b/examples/sf-test.coffee @@ -1,4 +1,4 @@ -require('./sf-client') +require('../examples/sf-client') sfuser = "edgarsilva@hybridgroup.com" sfpass = "password1232YOQR0HQMpQ5f74msKqaPuCD6" From 027a82c17eaed69fc475c74afbf48602a8ad2ece Mon Sep 17 00:00:00 2001 From: Edgar O Silva Date: Fri, 15 Nov 2013 12:00:48 -0600 Subject: [PATCH 05/10] Latest cylon example updates --- examples/sf-client.coffee | 2 +- examples/sf-sphero.coffee | 37 ++++++++++++++----------------------- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/examples/sf-client.coffee b/examples/sf-client.coffee index 69e450d..317a61d 100644 --- a/examples/sf-client.coffee +++ b/examples/sf-client.coffee @@ -6,7 +6,7 @@ faye = require('faye') namespace = require('node-namespace') -namespace 'Cylon.SF', -> +namespace 'SF', -> class @SFClient constructor: (opts) -> @client = null diff --git a/examples/sf-sphero.coffee b/examples/sf-sphero.coffee index 0a7cd14..07b1c91 100644 --- a/examples/sf-sphero.coffee +++ b/examples/sf-sphero.coffee @@ -1,8 +1,7 @@ require '../examples/sf-client' Cylon = require('..') -class SalesForceRobot - +Cylon.robot connection: name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0' @@ -10,7 +9,7 @@ class SalesForceRobot name: 'sphero', driver: 'sphero' setupSF: -> - new Cylon.SF.SFClient(sfuser: sfuser, sfpass: sfpass, orgCredentials: orgCreds) + new SF.SFClient(this.sfCreds) sfCreds: { @@ -24,37 +23,29 @@ class SalesForceRobot } work: (me) -> - color = 0x00FF00 - bitFilter = 0xFFFF00 - me.sf = me.setupSF() - mw.sf.authenticate((msg) => - @spheroRoll(0x00FF00) - ) - me.sphero.on('connect', -> Logger.info('Setting up Collision Detection...') me.sphero.detectCollisions() me.sphero.stop() - @spheroRoll(0x00FF00) + me.sphero.setRGB(0x00FF00) + me.sphero.roll 90, Math.floor(Math.random() * 360) ) me.sphero.on 'collision', (data) -> Logger.info 'collision:' - me.sphero.setRGB(0xFF0000) + me.sphero.setRGB(0xFF0000, me) me.sphero.stop() console.log("Collision Data:") - console.log(data) - toSend = "{ \"identifier\" :\"#{ me.name }\", \"msg\": \"#{ data }\" }" - @sf.psh(toSend) + console.log(data[0][0]) + toSend = "{ \"identifier\" :\"#{ me.name }\", \"msg\": \"#{ 'hello' }\" }" + me.sf.push(toSend) - spheroRoll: (color) -> - me.sphero.setRGB(color) - me.sphero.roll 90, Math.floor(Math.random() * 360) + me.sf.authenticate((msg) => + Logger.info 'SF Outbound Msg:' + me.sphero.setRGB(0x00FF00) + me.sphero.roll 90, Math.floor(Math.random() * 360) + ) -robot = new SalesForceRobot - -Cylon.robot robot - -Cylon.start() +.start() From 6f2c84b5167a7e43bfadb22607ff1a590a89f789 Mon Sep 17 00:00:00 2001 From: Edgar O Silva Date: Fri, 15 Nov 2013 16:51:49 -0600 Subject: [PATCH 06/10] Added example using salesforce driver and adaptor. --- examples/salesforce.coffee | 38 ++++++++++++++++++++++++++++++++++++++ examples/sf-client.coffee | 6 ++---- examples/sf-sphero.coffee | 27 ++++++++++++++++++++------- 3 files changed, 60 insertions(+), 11 deletions(-) create mode 100644 examples/salesforce.coffee diff --git a/examples/salesforce.coffee b/examples/salesforce.coffee new file mode 100644 index 0000000..1aeff54 --- /dev/null +++ b/examples/salesforce.coffee @@ -0,0 +1,38 @@ +Cylon = require('..') + +Cylon.robot + connection: + name: 'sfcon', adaptor: 'force' + + device: + name: 'salesforce', driver: 'force' + + work: (me) -> + + me.salesforce.on('authenticate', (oauth) -> + console.log('We are authenticated ===>') + console.log(oauth) + 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) + ) + + me.salesforce.on('subscribe', (subs) -> + console.log('Subscription ->') + console.log(subs) + ) + + me.salesforce.authenticate( + sfuser: "edgarsilva@hybridgroup.com" + sfpass: "password1232YOQR0HQMpQ5f74msKqaPuCD6" + orgCreds: { + clientId: '3MVG9A2kN3Bn17huqBLyrtmQ9Cgwc.FjKA4769ApTRhNNjgKEetcGv23W97cJQ3ER3VXxzyREIaD0Bp1Or8ou' + clientSecret: '6079348238616906521' + redirectUri: 'http://localhost:3000/oauth/_callback' + } + ) +.start() diff --git a/examples/sf-client.coffee b/examples/sf-client.coffee index 317a61d..e8eb342 100644 --- a/examples/sf-client.coffee +++ b/examples/sf-client.coffee @@ -1,7 +1,6 @@ 'use strict'; nforce = require('nforce') -http = require('http') faye = require('faye') namespace = require('node-namespace') @@ -18,7 +17,6 @@ namespace 'SF', -> _processOutboundMessages: () -> # Do work here - console.log(msg) for msg in @outboundMessages _handleStreamingAPI: (outboundCB) -> client = new faye.Client(@oauth.instance_url + '/cometd/28.0') @@ -35,8 +33,6 @@ namespace 'SF', -> process.exit(code=0) else console.log("authenticated") - console.log("oauth") - console.log(_oauth) @oauth = _oauth @_handleStreamingAPI(outboundCB) @_processOutboundMessages() @@ -46,6 +42,8 @@ namespace 'SF', -> #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) diff --git a/examples/sf-sphero.coffee b/examples/sf-sphero.coffee index 07b1c91..72c08c6 100644 --- a/examples/sf-sphero.coffee +++ b/examples/sf-sphero.coffee @@ -1,7 +1,12 @@ require '../examples/sf-client' Cylon = require('..') -Cylon.robot +bots = [ + { port: '/dev/rfcomm0', name: 'sphy-rgr' }, + { port: '/dev/rfcomm1', name: 'sphy-bpy' } +] + +class SpheroRobot connection: name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0' @@ -34,18 +39,26 @@ Cylon.robot ) me.sphero.on 'collision', (data) -> - Logger.info 'collision:' + #Logger.info 'collision:' me.sphero.setRGB(0xFF0000, me) me.sphero.stop() - console.log("Collision Data:") - console.log(data[0][0]) - toSend = "{ \"identifier\" :\"#{ me.name }\", \"msg\": \"#{ 'hello' }\" }" + #console.log("Collision Data:") + #console.log(data[0][0]) + toSend = "{ \"identifier\" :\"#{ me.name }\", \"msg\": \"#{ 'Collision detected' }\" }" me.sf.push(toSend) me.sf.authenticate((msg) => - Logger.info 'SF Outbound Msg:' + #Logger.info 'SF Outbound Msg:' + Logger.info "Sphero: #{ msg.sobject.Sphero_Name__c }, Msg Content: #{ msg.sobject.Content__c }, SM_Id: #{ msg.sobject.Id }" me.sphero.setRGB(0x00FF00) me.sphero.roll 90, Math.floor(Math.random() * 360) ) -.start() +for bot in bots + robot = new SpheroRobot + robot.connection.port = bot.port + robot.name = bot.name + console.log("Name: #{ robot.name }") + Cylon.robot robot + +Cylon.start() From 19d094bb161766983c5c960059647013e51f2a02 Mon Sep 17 00:00:00 2001 From: Edgar O Silva Date: Fri, 15 Nov 2013 18:44:24 -0600 Subject: [PATCH 07/10] Added adaptor extra params and updated salesforce example. --- dist/connection.js | 8 ++++---- dist/robot.js | 11 ++++++++--- examples/salesforce.coffee | 35 +++++++++++++---------------------- src/connection.coffee | 8 ++++---- src/robot.coffee | 6 +++--- 5 files changed, 32 insertions(+), 36 deletions(-) diff --git a/dist/connection.js b/dist/connection.js index 9372ba0..285eff7 100644 --- a/dist/connection.js +++ b/dist/connection.js @@ -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; diff --git a/dist/robot.js b/dist/robot.js index acdd834..39bf10c 100644 --- a/dist/robot.js +++ b/dist/robot.js @@ -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]; diff --git a/examples/salesforce.coffee b/examples/salesforce.coffee index 1aeff54..0303ee8 100644 --- a/examples/salesforce.coffee +++ b/examples/salesforce.coffee @@ -2,37 +2,28 @@ Cylon = require('..') Cylon.robot connection: - name: 'sfcon', adaptor: 'force' + 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('authenticate', (oauth) -> - console.log('We are authenticated ===>') - console.log(oauth) + 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) ) - me.salesforce.on('subscribe', (subs) -> - console.log('Subscription ->') - console.log(subs) - ) - - me.salesforce.authenticate( - sfuser: "edgarsilva@hybridgroup.com" - sfpass: "password1232YOQR0HQMpQ5f74msKqaPuCD6" - orgCreds: { - clientId: '3MVG9A2kN3Bn17huqBLyrtmQ9Cgwc.FjKA4769ApTRhNNjgKEetcGv23W97cJQ3ER3VXxzyREIaD0Bp1Or8ou' - clientSecret: '6079348238616906521' - redirectUri: 'http://localhost:3000/oauth/_callback' - } - ) + # push(apexPath, method, body) + toSend = "{ \"identifier\" :\"#{ me.name }\", \"msg\": \"#{ 'Salesforce Bot #2' }\" }" + me.salesforce.push('SpheroController', 'POST', toSend) .start() diff --git a/src/connection.coffee b/src/connection.coffee index cae5f64..5d263af 100644 --- a/src/connection.coffee +++ b/src/connection.coffee @@ -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 diff --git a/src/robot.coffee b/src/robot.coffee index e0e34de..48a58d4 100644 --- a/src/robot.coffee +++ b/src/robot.coffee @@ -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] From 6587cd146a9f8c840fd6b6345cba45f42630e887 Mon Sep 17 00:00:00 2001 From: Edgar O Silva Date: Fri, 15 Nov 2013 22:42:55 -0600 Subject: [PATCH 08/10] Sphero-pebble-salesforce example working. --- examples/multiple_devices.coffee | 0 examples/sf-sphero.coffee | 65 ++++++++++++----------- examples/sphero-pebble-sf.coffee | 90 ++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 30 deletions(-) create mode 100644 examples/multiple_devices.coffee create mode 100644 examples/sphero-pebble-sf.coffee diff --git a/examples/multiple_devices.coffee b/examples/multiple_devices.coffee new file mode 100644 index 0000000..e69de29 diff --git a/examples/sf-sphero.coffee b/examples/sf-sphero.coffee index 72c08c6..27bb499 100644 --- a/examples/sf-sphero.coffee +++ b/examples/sf-sphero.coffee @@ -1,11 +1,40 @@ -require '../examples/sf-client' 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' @@ -13,49 +42,25 @@ class SpheroRobot device: name: 'sphero', driver: 'sphero' - setupSF: -> - new SF.SFClient(this.sfCreds) - - sfCreds: - { - sfuser: "edgarsilva@hybridgroup.com" - sfpass: "password1232YOQR0HQMpQ5f74msKqaPuCD6" - orgCredentials: { - clientId: '3MVG9A2kN3Bn17huqBLyrtmQ9Cgwc.FjKA4769ApTRhNNjgKEetcGv23W97cJQ3ER3VXxzyREIaD0Bp1Or8ou' - clientSecret: '6079348238616906521' - redirectUri: 'http://localhost:3000/oauth/_callback' - } - } - work: (me) -> - me.sf = me.setupSF() - me.sphero.on('connect', -> + 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) -> - #Logger.info 'collision:' me.sphero.setRGB(0xFF0000, me) me.sphero.stop() - #console.log("Collision Data:") - #console.log(data[0][0]) toSend = "{ \"identifier\" :\"#{ me.name }\", \"msg\": \"#{ 'Collision detected' }\" }" - me.sf.push(toSend) - - me.sf.authenticate((msg) => - #Logger.info 'SF Outbound Msg:' - Logger.info "Sphero: #{ msg.sobject.Sphero_Name__c }, Msg Content: #{ msg.sobject.Content__c }, SM_Id: #{ msg.sobject.Id }" - me.sphero.setRGB(0x00FF00) - me.sphero.roll 90, Math.floor(Math.random() * 360) - ) + me.master.findRobot('salesforce', (err, sf) -> + sf.devices.salesforce.push('SpheroController', 'POST', toSend) + ) for bot in bots - robot = new SpheroRobot + 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 }") diff --git a/examples/sphero-pebble-sf.coffee b/examples/sphero-pebble-sf.coffee new file mode 100644 index 0000000..3e2772f --- /dev/null +++ b/examples/sphero-pebble-sf.coffee @@ -0,0 +1,90 @@ +Cylon = require('..') + +Cylon.api host: '0.0.0.0', port: '8080' + +bots = [ + { port: '/dev/rfcomm0', name: 'sphero-roy' }, + { port: '/dev/rfcomm1', name: 'sphero-gpg' } + { port: '', name: 'salesforce' }, + { port: '', name: 'pebble' } +] + +class PebbleRobot + connection: + name: 'pebble', adaptor: 'pebble' + device: + name: 'pebble', driver: 'pebble' + work: (me) -> + me.pebble.on('connect', -> + console.log('connected!') + ) + +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) + ) + me.master.findRobot('pebble', (error, robot) -> + robot.devices.pebble.message_queue().push(spheroName) + ) + ) + ) + +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 + switch bot.name + when 'salesforce' + robot = new SalesforceRobot + when 'pebble' + robot = new PebbleRobot + else + robot = new SpheroRobot + robot.connection.port = bot.port + robot.name = bot.name + console.log("Name: #{ robot.name }") + Cylon.robot robot + +Cylon.start() From 50b4ab3a061a1c8f21c9ce6bc73efd5e2a00bbf6 Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Sat, 16 Nov 2013 00:21:30 -0800 Subject: [PATCH 09/10] Update example --- examples/sphero-pebble-sf.coffee | 42 +++++++++++++++++++------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/examples/sphero-pebble-sf.coffee b/examples/sphero-pebble-sf.coffee index 3e2772f..08f3fe2 100644 --- a/examples/sphero-pebble-sf.coffee +++ b/examples/sphero-pebble-sf.coffee @@ -1,14 +1,14 @@ Cylon = require('..') - + Cylon.api host: '0.0.0.0', port: '8080' - + bots = [ - { port: '/dev/rfcomm0', name: 'sphero-roy' }, - { port: '/dev/rfcomm1', name: 'sphero-gpg' } + { port: '/dev/rfcomm0', name: 'ROY' }, + { port: '/dev/rfcomm1', name: 'GPG' } { port: '', name: 'salesforce' }, { port: '', name: 'pebble' } ] - + class PebbleRobot connection: name: 'pebble', adaptor: 'pebble' @@ -18,7 +18,7 @@ class PebbleRobot me.pebble.on('connect', -> console.log('connected!') ) - + class SalesforceRobot connection: name: 'sfcon', @@ -30,33 +30,41 @@ class SalesforceRobot clientSecret: '6079348238616906521', 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 - Logger.info "Sphero: #{ spheroName }, data Content: #{ data.sobject.Content__c }, SM_Id: #{ data.sobject.Id }" + counter = data.sobject.Content__c + Logger.info "Sphero: #{ spheroName }, data Content: #{ counter }, 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) ) - me.master.findRobot('pebble', (error, robot) -> - robot.devices.pebble.message_queue().push(spheroName) + me.spheroReport[spheroName] = counter + toPebble = "" + for key, val of me.spheroReport + toPebble += "#{key}: #{val}\n" + me.master.findRobot('pebble', (error, pebbleBot) -> + pebbleBot.devices.pebble.message_queue().push(toPebble) ) ) ) - + class SpheroRobot + total_collisions: 0 connection: - name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0' - + name: 'sphero', adaptor: 'sphero' + device: name: 'sphero', driver: 'sphero' + work: (me) -> me.sphero.on 'connect', -> @@ -65,15 +73,15 @@ class SpheroRobot 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' }\" }" + toSend = "{ \"identifier\" :\"#{ me.name }\", \"msg\": \"#{ me.total_collisions++ }\" }" me.master.findRobot('salesforce', (err, sf) -> sf.devices.salesforce.push('SpheroController', 'POST', toSend) ) - + for bot in bots switch bot.name when 'salesforce' From 23decd6b5a12707961e4b9403cc199dff314f53f Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Sat, 16 Nov 2013 14:04:22 -0800 Subject: [PATCH 10/10] Update example --- examples/sphero-pebble-sf.coffee | 53 +++++++++++++++++--------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/examples/sphero-pebble-sf.coffee b/examples/sphero-pebble-sf.coffee index 08f3fe2..01156be 100644 --- a/examples/sphero-pebble-sf.coffee +++ b/examples/sphero-pebble-sf.coffee @@ -2,18 +2,16 @@ Cylon = require('..') Cylon.api host: '0.0.0.0', port: '8080' -bots = [ - { port: '/dev/rfcomm0', name: 'ROY' }, - { port: '/dev/rfcomm1', name: 'GPG' } - { port: '', name: 'salesforce' }, - { port: '', name: 'pebble' } -] - 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!') @@ -23,14 +21,14 @@ class SalesforceRobot connection: name: 'sfcon', adaptor: 'force', - sfuser: "edgarsilva@hybridgroup.com", - sfpass: "password1232YOQR0HQMpQ5f74msKqaPuCD6", + sfuser: process.env.SF_USERNAME, + sfpass: process.env.SF_SECURITY_TOKEN, orgCreds: { - clientId: '3MVG9A2kN3Bn17huqBLyrtmQ9Cgwc.FjKA4769ApTRhNNjgKEetcGv23W97cJQ3ER3VXxzyREIaD0Bp1Or8ou', - clientSecret: '6079348238616906521', + clientId: process.env.SF_CLIENT_ID, + clientSecret: process.env.SF_CLIENT_SECRET, redirectUri: 'http://localhost:3000/oauth/_callback' } - + device: name: 'salesforce', driver: 'force' @@ -43,15 +41,14 @@ class SalesforceRobot counter = data.sobject.Content__c Logger.info "Sphero: #{ spheroName }, data Content: #{ counter }, 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) + spheroBot.react(spheroBot.devices.sphero) ) me.spheroReport[spheroName] = counter toPebble = "" for key, val of me.spheroReport - toPebble += "#{key}: #{val}\n" + toPebble += "#{key}: $#{val}\n" me.master.findRobot('pebble', (error, pebbleBot) -> - pebbleBot.devices.pebble.message_queue().push(toPebble) + pebbleBot.message(pebbleBot.devices.pebble, toPebble) ) ) ) @@ -64,6 +61,9 @@ class SpheroRobot device: name: 'sphero', driver: 'sphero' + react: (robot) => + robot.setRGB(0x00FF00) + robot.roll 90, Math.floor(Math.random() * 360) work: (me) -> @@ -81,18 +81,23 @@ class SpheroRobot 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 - switch bot.name - when 'salesforce' - robot = new SalesforceRobot - when 'pebble' - robot = new PebbleRobot - else - robot = new SpheroRobot + robot = new SpheroRobot robot.connection.port = bot.port robot.name = bot.name - console.log("Name: #{ robot.name }") Cylon.robot robot Cylon.start()