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: 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: 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 And with that done let's define our robot. We'll make a class to contain this
robot's logic: 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. 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 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. be returned to the HTTP client as part of a JSON object.
relax: -> MyRobot.prototype.relax = function() {
"#{this.name} says relax" return "" + this.name + " says relax";
};
Since we don't really care what actual work this robot does, but need to keep it 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. busy, we'll just tell it to print it's name every second.
work: (me) -> MyRobot.prototype.work = function(me) {
every 1.seconds(), -> every((1).seconds(), function() {
console.log me.name console.log(me.name);
});
};
return MyRobot;
})();
And with that all done, we can now instantiate our robot: 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. Now we can just give it a name and send it off to Cylon.
robot.name = "frankie" robot.name = "frankie";
Cylon.robot robot Cylon.robot(robot);
And now that all the pieces are in place, we can start up Cylon: 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 Now the robot will print it's name to the console, and Cylon will serve an API
to `localhost:8080`. Check it out!. to `localhost:8080`. Check it out!.

View File

@ -2,45 +2,55 @@
First, let's import Cylon: First, let's import Cylon:
Cylon = require '../..' var Cylon = require('../..');
Now that we have Cylon imported, we can start defining our robot Now that we have Cylon imported, we can start defining our robot
Cylon.robot Cylon.robot({
Let's define the connections and devices: Let's define the connections and devices:
connection: connection: {
name: 'sfcon' name: 'sfcon',
adaptor: 'force' adaptor: 'force',
sfuser: process.env.SF_USERNAME sfuser: process.env.SF_USERNAME,
sfpass: process.env.SF_SECURITY_TOKEN sfpass: process.env.SF_SECURITY_TOKEN,
orgCreds: orgCreds: {
clientId: process.env.SF_CLIENT_ID clientId: process.env.SF_CLIENT_ID,
clientSecret: process.env.SF_CLIENT_SECRET clientSecret: process.env.SF_CLIENT_SECRET,
redirectUri: 'http://localhost:3000/oauth/_callback' 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 Now that Cylon knows about the necessary hardware we're going to be using, we'll
tell it what work we want to do: tell it what work we want to do:
work: (me) -> work: function(me) {
me.salesforce.on 'start', () -> me.salesforce.on('start', function() {
me.salesforce.subscribe '/topic/SpheroMsgOutbound', (data) -> me.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) {
msg = "Sphero: #{data.sobject.Sphero_Name__c}," var msg = "Sphero: " + data.sobject.Sphero_Name__c + ",";
msg += "Bucks: #{data.sobject.Bucks__c}," msg += "Bucks: " + data.sobject.Bucks__c + ",";
msg += "SM_Id: #{data.sobject.Id}" msg += "SM_Id: " + data.sobject.Id;
console.log msg console.log(msg);
});
});
i = 0 var i = 0;
every 2.seconds(), () -> every((2).seconds(), function() {
data = JSON.stringify { spheroName: "#{me.name}", bucks: "#{i}" } var data = JSON.stringify({
me.salesforce.push 'SpheroController', 'POST', data 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 Now that our robot knows what work to do, and the work it will be doing that
hardware with, we can start it: 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: 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 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: Let's define the connections and devices:
connection: SalesforceRobot.prototype.connection = {
name: 'sfcon' name: 'sfcon',
adaptor: 'force' adaptor: 'force',
sfuser: process.env.SF_USERNAME sfuser: process.env.SF_USERNAME,
sfpass: process.env.SF_SECURITY_TOKEN sfpass: process.env.SF_SECURITY_TOKEN,
orgCreds: orgCreds: {
clientId: process.env.SF_CLIENT_ID clientId: process.env.SF_CLIENT_ID,
clientSecret: process.env.SF_CLIENT_SECRET clientSecret: process.env.SF_CLIENT_SECRET,
redirectUri: 'http://localhost:3000/oauth/_callback' 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 Now that Cylon knows about the necessary hardware we're going to be using, we'll
tell it what work we want to do: tell it what work we want to do:
work: (me) -> SalesforceRobot.prototype.work = function(me) {
me.salesforce.on 'start', () -> me.salesforce.on('start', function() {
me.salesforce.subscribe '/topic/SpheroMsgOutbound', (data) -> me.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) {
msg = "Sphero: #{data.sobject.Sphero_Name__c}," var msg;
msg += "Bucks: #{data.sobject.Bucks__c}," msg = "Sphero: " + data.sobject.Sphero_Name__c + ",";
msg += "SM_Id: #{data.sobject.Id}" 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 var SpheroRobot = (function() {
totalBucks: 0 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) => SpheroRobot.prototype.react = function(robot) {
robot.setRGB 0x00FF00 robot.setRGB(0x00FF00);
robot.roll 90, Math.floor(Math.random() * 360) robot.roll(90, Math.floor(Math.random() * 360));
};
work: (me) -> SpheroRobot.prototype.work = function(me) {
me.sphero.on 'connect', -> me.sphero.on('connect', function() {
console.log 'Setting up Collision Detection...' console.log('Setting up Collision Detection...');
me.sphero.detectCollisions() me.sphero.detectCollisions();
me.sphero.stop() me.sphero.stop();
me.sphero.setRGB 0x00FF00 me.sphero.setRGB(0x00FF00);
me.sphero.roll 90, Math.floor(Math.random() * 360) 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) -> return SpheroRobot;
me.sphero.setRGB 0x0000FF, me
me.sphero.stop()
data = JSON.stringify })();
spheroName: "#{me.name}"
bucks: "#{me.totalBucks++}"
me.master.findRobot 'salesforce', (err, sf) -> var sfRobot = new SalesforceRobot();
sf.devices.salesforce.push 'SpheroController', 'POST', data sfRobot.name = "salesforce";
Cylon.robot(sfRobot);
sfRobot = new SalesforceRobot() var spheroRobot = new SpheroRobot();
sfRobot.name = "salesforce" spheroRobot.name = 'ROY';
Cylon.robot sfRobot spheroRobot.connection.port = '/dev/rfcomm0';
Cylon.robot(spheroRobot);
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 Now that our robot knows what work to do, and the work it will be doing that
hardware with, we can start it: hardware with, we can start it:
Cylon.start() Cylon.start();

View File

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