2013-11-16 12:42:55 +08:00
|
|
|
Cylon = require('..')
|
2013-11-17 10:15:09 +08:00
|
|
|
|
2013-11-16 12:42:55 +08:00
|
|
|
Cylon.api host: '0.0.0.0', port: '8080'
|
2013-11-17 10:15:09 +08:00
|
|
|
|
2013-11-16 12:42:55 +08:00
|
|
|
class PebbleRobot
|
|
|
|
connection:
|
|
|
|
name: 'pebble', adaptor: 'pebble'
|
2013-11-17 06:04:22 +08:00
|
|
|
|
2013-11-16 12:42:55 +08:00
|
|
|
device:
|
|
|
|
name: 'pebble', driver: 'pebble'
|
2013-11-17 06:04:22 +08:00
|
|
|
|
|
|
|
message: (robot, msg) =>
|
|
|
|
robot.message_queue().push(msg)
|
|
|
|
|
2013-11-16 12:42:55 +08:00
|
|
|
work: (me) ->
|
|
|
|
me.pebble.on('connect', ->
|
|
|
|
console.log('connected!')
|
|
|
|
)
|
2013-11-17 10:15:09 +08:00
|
|
|
|
2013-11-16 12:42:55 +08:00
|
|
|
class SalesforceRobot
|
|
|
|
connection:
|
|
|
|
name: 'sfcon',
|
|
|
|
adaptor: 'force',
|
2013-11-17 06:04:22 +08:00
|
|
|
sfuser: process.env.SF_USERNAME,
|
|
|
|
sfpass: process.env.SF_SECURITY_TOKEN,
|
2013-11-16 12:42:55 +08:00
|
|
|
orgCreds: {
|
2013-11-17 06:04:22 +08:00
|
|
|
clientId: process.env.SF_CLIENT_ID,
|
|
|
|
clientSecret: process.env.SF_CLIENT_SECRET,
|
2013-11-16 12:42:55 +08:00
|
|
|
redirectUri: 'http://localhost:3000/oauth/_callback'
|
|
|
|
}
|
2013-11-17 06:04:22 +08:00
|
|
|
|
2013-11-16 12:42:55 +08:00
|
|
|
device:
|
|
|
|
name: 'salesforce', driver: 'force'
|
2013-11-17 10:15:09 +08:00
|
|
|
|
2013-11-16 16:21:30 +08:00
|
|
|
spheroReport:{}
|
2013-11-16 12:42:55 +08:00
|
|
|
|
|
|
|
work: (me) ->
|
|
|
|
me.salesforce.on('start', () ->
|
|
|
|
me.salesforce.subscribe('/topic/SpheroMsgOutbound', (data) ->
|
|
|
|
spheroName = data.sobject.Sphero_Name__c
|
2013-11-18 12:52:54 +08:00
|
|
|
bucks = data.sobject.Bucks__c
|
|
|
|
Logger.info "Sphero: #{ spheroName }, data Bucks: #{ bucks }, SM_Id: #{ data.sobject.Id }"
|
2013-11-16 12:42:55 +08:00
|
|
|
me.master.findRobot(spheroName, (err, spheroBot) ->
|
2013-11-17 06:04:22 +08:00
|
|
|
spheroBot.react(spheroBot.devices.sphero)
|
2013-11-16 12:42:55 +08:00
|
|
|
)
|
2013-11-18 12:52:54 +08:00
|
|
|
me.spheroReport[spheroName] = bucks
|
2013-11-16 16:21:30 +08:00
|
|
|
toPebble = ""
|
|
|
|
for key, val of me.spheroReport
|
2013-11-17 06:04:22 +08:00
|
|
|
toPebble += "#{key}: $#{val}\n"
|
2013-11-16 16:21:30 +08:00
|
|
|
me.master.findRobot('pebble', (error, pebbleBot) ->
|
2013-11-17 06:04:22 +08:00
|
|
|
pebbleBot.message(pebbleBot.devices.pebble, toPebble)
|
2013-11-16 12:42:55 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2013-11-17 10:15:09 +08:00
|
|
|
|
2013-11-16 12:42:55 +08:00
|
|
|
class SpheroRobot
|
2013-11-18 13:04:20 +08:00
|
|
|
totalBucks: 1
|
2013-11-18 12:52:54 +08:00
|
|
|
payingPower: true
|
|
|
|
|
2013-11-16 12:42:55 +08:00
|
|
|
connection:
|
2013-11-16 16:21:30 +08:00
|
|
|
name: 'sphero', adaptor: 'sphero'
|
2013-11-17 10:15:09 +08:00
|
|
|
|
2013-11-16 12:42:55 +08:00
|
|
|
device:
|
|
|
|
name: 'sphero', driver: 'sphero'
|
|
|
|
|
2013-11-17 06:04:22 +08:00
|
|
|
react: (robot) =>
|
|
|
|
robot.setRGB(0x00FF00)
|
|
|
|
robot.roll 90, Math.floor(Math.random() * 360)
|
2013-11-18 12:52:54 +08:00
|
|
|
@payingPower = true
|
2013-11-17 10:15:09 +08:00
|
|
|
|
2013-11-18 13:37:46 +08:00
|
|
|
bankrupt: () ->
|
|
|
|
every 3.seconds(), () ->
|
2013-11-18 12:52:54 +08:00
|
|
|
me.totalBucks-- if payingPower and me.totalBucks > 0
|
2013-11-18 13:04:20 +08:00
|
|
|
if me.totalBucks == 0
|
2013-11-18 13:37:46 +08:00
|
|
|
me.sphero.setRGB(0xFF0000, me)
|
2013-11-18 13:04:20 +08:00
|
|
|
me.sphero.stop()
|
2013-11-16 12:42:55 +08:00
|
|
|
|
2013-11-18 13:37:46 +08:00
|
|
|
changeDirection: ()
|
|
|
|
every 1.seconds(), () ->
|
|
|
|
me.sphero.roll 90, Math.floor(Math.random() * 360) if @payingPower
|
|
|
|
|
|
|
|
work: (me) ->
|
|
|
|
|
2013-11-16 12:42:55 +08:00
|
|
|
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)
|
2013-11-18 13:37:46 +08:00
|
|
|
me.bankrupt()
|
|
|
|
me.changeDirection()
|
2013-11-17 10:15:09 +08:00
|
|
|
|
2013-11-16 12:42:55 +08:00
|
|
|
me.sphero.on 'collision', (data) ->
|
2013-11-18 13:04:20 +08:00
|
|
|
me.sphero.setRGB(0x0000FF, me)
|
2013-11-16 12:42:55 +08:00
|
|
|
me.sphero.stop()
|
2013-11-18 12:52:54 +08:00
|
|
|
me.payingPower = false
|
|
|
|
toSend = "{ \"spheroName\" :\"#{ me.name }\", \"bucks\": \"#{ me.totalBucks++ }\" }"
|
2013-11-16 12:42:55 +08:00
|
|
|
me.master.findRobot('salesforce', (err, sf) ->
|
|
|
|
sf.devices.salesforce.push('SpheroController', 'POST', toSend)
|
|
|
|
)
|
2013-11-17 06:04:22 +08:00
|
|
|
|
|
|
|
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'}
|
|
|
|
]
|
2013-11-17 10:15:09 +08:00
|
|
|
|
2013-11-16 12:42:55 +08:00
|
|
|
for bot in bots
|
2013-11-17 06:04:22 +08:00
|
|
|
robot = new SpheroRobot
|
2013-11-16 12:42:55 +08:00
|
|
|
robot.connection.port = bot.port
|
|
|
|
robot.name = bot.name
|
|
|
|
Cylon.robot robot
|
|
|
|
|
|
|
|
Cylon.start()
|