Convert remaining .litcoffee examples

This commit is contained in:
Andrew Stewart 2014-03-27 09:31:56 -07:00
parent 479388e7ad
commit 688df7a419
4 changed files with 163 additions and 108 deletions

View File

@ -12,46 +12,54 @@ http://localhost:8080/robots/frankie/commands/relax
First, let's make sure to load up Cylon:
Cylon = require '../..'
var Cylon = require('../..');
Now that we've got that, let's set up a custom API port:
Cylon.api host: '0.0.0.0', port: '8080'
Cylon.api({ host: '0.0.0.0', port: '8080' });
And with that done let's define our robot. We'll make a class to contain this
robot's logic:
class MyRobot
var MyRobot = (function() {
function MyRobot() {}
To let the API know what commands this robot has, we need to provide a `commands` array.
commands: ["relax"]
MyRobot.prototype.commands = ["relax"];
And with that done, we can now define the method. The result of this method will
be returned to the HTTP client as part of a JSON object.
relax: ->
"#{this.name} says relax"
MyRobot.prototype.relax = function() {
return "" + this.name + " says relax";
};
Since we don't really care what actual work this robot does, but need to keep it
busy, we'll just tell it to print it's name every second.
work: (me) ->
every 1.seconds(), ->
console.log me.name
MyRobot.prototype.work = function(me) {
every((1).seconds(), function() {
console.log(me.name);
});
};
return MyRobot;
})();
And with that all done, we can now instantiate our robot:
robot = new MyRobot
var robot = new MyRobot;
Now we can just give it a name and send it off to Cylon.
robot.name = "frankie"
Cylon.robot robot
robot.name = "frankie";
Cylon.robot(robot);
And now that all the pieces are in place, we can start up Cylon:
Cylon.start()
Cylon.start();
Now the robot will print it's name to the console, and Cylon will serve an API
to `localhost:8080`. Check it out!.

View File

@ -2,45 +2,55 @@
First, let's import Cylon:
Cylon = require '../..'
var Cylon = require('../..');
Now that we have Cylon imported, we can start defining our robot
Cylon.robot
Cylon.robot({
Let's define the connections and devices:
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
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' }
device: { name: 'salesforce', driver: 'force' },
Now that Cylon knows about the necessary hardware we're going to be using, we'll
tell it what work we want to do:
work: (me) ->
me.salesforce.on 'start', () ->
me.salesforce.subscribe '/topic/SpheroMsgOutbound', (data) ->
msg = "Sphero: #{data.sobject.Sphero_Name__c},"
msg += "Bucks: #{data.sobject.Bucks__c},"
msg += "SM_Id: #{data.sobject.Id}"
work: function(me) {
me.salesforce.on('start', function() {
me.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) {
var msg = "Sphero: " + data.sobject.Sphero_Name__c + ",";
msg += "Bucks: " + data.sobject.Bucks__c + ",";
msg += "SM_Id: " + data.sobject.Id;
console.log msg
console.log(msg);
});
});
i = 0
var i = 0;
every 2.seconds(), () ->
data = JSON.stringify { spheroName: "#{me.name}", bucks: "#{i}" }
me.salesforce.push 'SpheroController', 'POST', data
every((2).seconds(), function() {
var data = JSON.stringify({
spheroName: "" + me.name,
bucks: "" + i
});
me.salesforce.push('SpheroController', 'POST', data);
});
}
Now that our robot knows what work to do, and the work it will be doing that
hardware with, we can start it:
.start()
}).start();

View File

@ -1,82 +1,108 @@
# Sales Force Shpero
# Sales Force Sphero
First, let's import Cylon:
Cylon = require '../..'
var Cylon = require('../..');
Now we'll define a `bind` helper function we'll use later:
var bind = function(fn, me) {
return function() { return fn.apply(me, arguments); };
};
Now that we have Cylon imported, we can start defining our robot
class SalesforceRobot
var SalesforceRobot = (function() {
function SalesforceRobot() {}
Let's define the connections and devices:
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
SalesforceRobot.prototype.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' }
SalesforceRobot.prototype.device = { name: 'salesforce', driver: 'force' };
Now that Cylon knows about the necessary hardware we're going to be using, we'll
tell it what work we want to do:
work: (me) ->
me.salesforce.on 'start', () ->
me.salesforce.subscribe '/topic/SpheroMsgOutbound', (data) ->
msg = "Sphero: #{data.sobject.Sphero_Name__c},"
msg += "Bucks: #{data.sobject.Bucks__c},"
msg += "SM_Id: #{data.sobject.Id}"
SalesforceRobot.prototype.work = function(me) {
me.salesforce.on('start', function() {
me.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) {
var msg;
msg = "Sphero: " + data.sobject.Sphero_Name__c + ",";
msg += "Bucks: " + data.sobject.Bucks__c + ",";
msg += "SM_Id: " + data.sobject.Id;
console.log(msg);
me.master.findRobot(data.sobject.Sphero_Name__c, function(err, spheroBot) {
spheroBot.react(spheroBot.devices.sphero);
});
});
});
};
console.log msg
return SalesforceRobot;
me.master.findRobot data.sobject.Sphero_Name__c, (err, spheroBot) ->
spheroBot.react spheroBot.devices.sphero
})();
class SpheroRobot
totalBucks: 0
var SpheroRobot = (function() {
function SpheroRobot() {
this.react = bind(this.react, this);
}
connection: { name: 'sphero', adaptor: 'sphero' }
SpheroRobot.prototype.totalBucks = 0;
device: { name: 'sphero', driver: 'sphero' }
SpheroRobot.prototype.connection = { name: 'sphero', adaptor: 'sphero' };
SpheroRobot.prototype.device = { name: 'sphero', driver: 'sphero' };
react: (robot) =>
robot.setRGB 0x00FF00
robot.roll 90, Math.floor(Math.random() * 360)
SpheroRobot.prototype.react = function(robot) {
robot.setRGB(0x00FF00);
robot.roll(90, Math.floor(Math.random() * 360));
};
work: (me) ->
me.sphero.on 'connect', ->
console.log 'Setting up Collision Detection...'
me.sphero.detectCollisions()
me.sphero.stop()
me.sphero.setRGB 0x00FF00
me.sphero.roll 90, Math.floor(Math.random() * 360)
SpheroRobot.prototype.work = function(me) {
me.sphero.on('connect', function() {
console.log('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', function(data) {
me.sphero.setRGB(0x0000FF, me);
me.sphero.stop();
data = JSON.stringify({
spheroName: "" + me.name,
bucks: "" + (me.totalBucks++)
});
me.master.findRobot('salesforce', function(err, sf) {
sf.devices.salesforce.push('SpheroController', 'POST', data);
});
});
};
me.sphero.on 'collision', (data) ->
me.sphero.setRGB 0x0000FF, me
me.sphero.stop()
return SpheroRobot;
data = JSON.stringify
spheroName: "#{me.name}"
bucks: "#{me.totalBucks++}"
})();
me.master.findRobot 'salesforce', (err, sf) ->
sf.devices.salesforce.push 'SpheroController', 'POST', data
var sfRobot = new SalesforceRobot();
sfRobot.name = "salesforce";
Cylon.robot(sfRobot);
sfRobot = new SalesforceRobot()
sfRobot.name = "salesforce"
Cylon.robot sfRobot
spheroRobot = new SpheroRobot()
spheroRobot.name = 'ROY'
spheroRobot.connection.port = '/dev/rfcomm0'
Cylon.robot spheroRobot
var spheroRobot = new SpheroRobot();
spheroRobot.name = 'ROY';
spheroRobot.connection.port = '/dev/rfcomm0';
Cylon.robot(spheroRobot);
Now that our robot knows what work to do, and the work it will be doing that
hardware with, we can start it:
Cylon.start()
Cylon.start();

View File

@ -2,38 +2,49 @@
First, let's import Cylon:
Cylon = require '../..'
var Cylon = require('../..');
Now that we have Cylon imported, we can start defining our robot
Cylon.robot
Cylon.robot({
Let's define the connections and devices:
connections: [
{ name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' },
# { name: 'skynet', adaptor: 'skynet', uuid: "742401f1-87a4-11e3-834d-670dadc0ddbf", token: "xjq9h3yzhemf5hfrme8y08fh0sm50zfr" }
{ name: 'skynet', adaptor: 'skynet', host: 'localhost', portNumber: 3000, uuid: "18676eb1-9997-11e3-8c2a-f18a07a72684", token: "vbeo289aik65hfr1lns8vst273nmi" }
]
{
name: 'arduino',
adaptor: 'firmata',
port: '/dev/ttyACM0'
},
{
name: 'skynet',
adaptor: 'skynet',
uuid: "96630051-a3dc-11e3-8442-5bf31d98c912",
token: "2s67o7ek98pycik98f43reqr90t6s9k9"
}
],
device: { name: 'led', driver: 'led', pin: 13, connection: 'arduino' }
device: { name: 'led13', driver: 'led', pin: 13, connection: 'arduino' },
Now that Cylon knows about the necessary hardware we're going to be using, we'll
tell it what work we want to do:
work: (my) ->
Logger.info "connected..."
my.connections['skynet'].on 'message', (data) ->
console.log(data)
data = JSON.parse(data)
if data.message.red is 'on'
console.log("red on request received from skynet");
my.led.turnOn()
else if data.message.red is 'off'
console.log("red off request received from skynet");
my.led.turnOff()
work: function(my) {
Logger.info("Skynet is listening...");
my.skynet.on('message', function(data) {
Logger.info(data);
var data = JSON.parse(data);
if(data.message.red == 'on') {
my.led13.turnOn()
}
else if(data.message.red == 'off') {
my.led13.turnOff()
}
});
}
Now that our robot knows what work to do, and the work it will be doing that
hardware with, we can start it:
.start()
}).start():