cylon/examples/arduinos_and_skynet/multiple_arduinos_one_skyne...

78 lines
1.3 KiB
JavaScript
Raw Normal View History

2014-12-14 08:19:25 +08:00
"use strict";
var Cylon = require("../..");
2014-11-26 01:45:20 +08:00
var arduinos = [
{
name: "arduino0",
port: "/dev/ttyACM0",
devices: {
2014-12-14 08:19:25 +08:00
led00: { driver: "led", pin: 13 }
2014-11-26 01:45:20 +08:00
}
},
2014-11-26 01:45:20 +08:00
{
name: "arduin01",
port: "/dev/ttyACM1",
devices: {
2014-12-14 08:19:25 +08:00
led10: { driver: "led", pin: 11 },
led11: { driver: "led", pin: 12 },
led12: { driver: "led", pin: 13 }
2014-11-26 01:45:20 +08:00
}
}
2014-11-26 01:45:20 +08:00
];
2014-11-26 01:45:20 +08:00
Cylon.robot({
name: "SkynetBot",
2014-11-26 08:01:31 +08:00
connections: {
skynet: {
2014-12-14 08:19:25 +08:00
adaptor: "skynet",
2014-11-26 08:01:31 +08:00
uuid: "96630051-a3dc-11e3-8442-5bf31d98c912",
token: "2s67o7ek98pycik98f43reqr90t6s9k9"
}
},
2014-11-26 01:45:20 +08:00
handler: function(data) {
if (data.payload == null) {
return;
}
console.log("Data: ", data);
2014-11-26 01:45:20 +08:00
for (var i in data.payload.robots) {
var robot = data.payload.robots[i],
bot = Cylon.robots[robot.name];
2014-11-26 01:45:20 +08:00
2014-12-14 08:19:25 +08:00
if (robot.cmd === "on") {
2014-11-26 01:45:20 +08:00
bot.devices[robot.device].turnOn();
} else {
bot.devices[robot.device].turnOff();
}
2014-11-26 01:45:20 +08:00
}
},
work: function(my) {
2014-12-14 08:19:25 +08:00
my.skynet.on("message", my.handler);
2014-11-26 01:45:20 +08:00
console.log("Skynet is listening");
}
2014-11-26 01:45:20 +08:00
});
2014-11-26 01:45:20 +08:00
arduinos.forEach(function(bot) {
Cylon.robot({
name: bot.name,
2014-11-26 08:01:31 +08:00
connections: {
2014-12-14 08:19:25 +08:00
arduino: { adaptor: "firmata", port: bot.port }
2014-11-26 08:01:31 +08:00
},
2014-11-26 01:45:20 +08:00
devices: bot.devices,
work: function(my) {
console.log(my.name + " is online");
}
});
});
Cylon.start();