cylon/examples/sphero-pebble-sf.coffee

121 lines
3.0 KiB
CoffeeScript
Raw Normal View History

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