cylon/examples/sphero-pebble-sf.coffee

123 lines
3.2 KiB
CoffeeScript
Raw Normal View History

Cylon = require('..')
2013-11-18 17:02:38 +08:00
Cylon.api host: '0.0.0.0', port: '8080'
2013-11-18 17:02:38 +08:00
class PebbleRobot
connection:
name: 'pebble', adaptor: 'pebble'
2013-11-18 17:02:38 +08:00
device:
name: 'pebble', driver: 'pebble'
2013-11-18 17:02:38 +08:00
2013-11-17 06:04:22 +08:00
message: (robot, msg) =>
robot.message_queue().push(msg)
2013-11-18 17:02:38 +08:00
work: (me) ->
me.pebble.on('connect', ->
2013-11-18 17:02:38 +08:00
Logger.info 'connected!'
)
2013-11-18 17:02:38 +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-18 17:02:38 +08:00
device:
name: 'salesforce', driver: 'force'
2013-11-18 17:02:38 +08:00
2013-11-16 16:21:30 +08:00
spheroReport:{}
2013-11-18 17:02:38 +08:00
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-18 17:02:38 +08:00
class SpheroRobot
2013-11-18 13:04:20 +08:00
totalBucks: 1
payingPower: true
2013-11-18 17:02:38 +08:00
connection:
2013-11-16 16:21:30 +08:00
name: 'sphero', adaptor: 'sphero'
2013-11-18 17:02:38 +08:00
device:
name: 'sphero', driver: 'sphero'
2013-11-18 17:02:38 +08:00
react: (device) ->
device.setRGB(0x00FF00)
device.roll 90, Math.floor(Math.random() * 360)
this.payingPower = true
bankrupt: () ->
2013-11-18 17:02:38 +08:00
every 3.seconds(), () =>
if this.payingPower and this.totalBucks > 0
this.totalBucks += -1
if @totalBucks == 0
this.sphero.setRGB(0xFF000)
this.sphero.stop()
2013-11-18 13:38:37 +08:00
changeDirection: () ->
2013-11-18 17:02:38 +08:00
every 1.seconds(), () =>
this.sphero.roll 90, Math.floor(Math.random() * 360) if @payingPower
work: (me) ->
2013-11-18 17:02:38 +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)
me.bankrupt()
me.changeDirection()
2013-11-18 17:02:38 +08:00
me.sphero.on 'collision', (data) ->
2013-11-18 17:02:38 +08:00
me.sphero.setRGB(0x0000FF)
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-18 17:02:38 +08:00
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
2013-11-18 17:02:38 +08:00
2013-11-17 06:04:22 +08:00
bots = [
2013-11-18 17:02:38 +08:00
{ port: '/dev/tty.Sphero-ROY-AMP-SPP', name: 'ROY' },
{ port: '/dev/tty.Sphero-GBO-AMP-SPP', name: 'GBO'},
{ port: '/dev/tty.Sphero-RRY-AMP-SPP', name: 'RRY'}
2013-11-17 06:04:22 +08:00
]
2013-11-18 17:02:38 +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
2013-11-18 17:02:38 +08:00
Cylon.start()