WIP on multipoe arduino controlled by skynet.
This commit is contained in:
parent
9c5d1c3496
commit
4a3fdf7fec
|
@ -0,0 +1,62 @@
|
|||
var Cylon = require('../..');
|
||||
|
||||
SkynetBot = (function(){
|
||||
function SkynetBot(){
|
||||
this.connections = [
|
||||
{ name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' },
|
||||
{ name: 'skynet', adaptor: 'skynet'}
|
||||
];
|
||||
}
|
||||
|
||||
SkynetBot.prototype.port = function(serialPort){
|
||||
for (var i in this.connections){
|
||||
if (this.connections[i].adaptor == 'firmata')
|
||||
this.connections[i].port = serialPort;
|
||||
}
|
||||
};
|
||||
|
||||
SkynetBot.prototype.creds = function(uuid, token, portNumber){
|
||||
for (var i in this.connections){
|
||||
if (this.connections[i].adaptor == 'skynet'){
|
||||
this.connections[i].uuid = uuid;
|
||||
this.connections[i].token = token;
|
||||
this.connections[i].portNumber = portNumber;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
SkynetBot.prototype.device = { name: 'led13', driver: 'led', pin: 13, connection: 'arduino' };
|
||||
|
||||
SkynetBot.prototype.work = function(my) {
|
||||
|
||||
my.skynet.on('message', function(data) {
|
||||
if(data.led13 == 'on') {
|
||||
my.led13.turnOn()
|
||||
}
|
||||
else if(data.led13 == 'off') {
|
||||
my.led13.turnOff()
|
||||
}
|
||||
});
|
||||
|
||||
Logger.info("Skynet instance `" + my.name + "` is listening ...");
|
||||
};
|
||||
|
||||
return SkynetBot;
|
||||
})();
|
||||
|
||||
skynetBot1 = new SkynetBot();
|
||||
skynetBot1.name = 'skynet1';
|
||||
skynetBot1.port('/dev/ttyACM1');
|
||||
skynetBot1.creds("e8f942f1-a49c-11e3-9270-795e22e700d8","0lpxpyafz7z7u8frgvp44g8mbr7o80k9");
|
||||
console.log(skynetBot1.connections);
|
||||
Cylon.robot(skynetBot1);
|
||||
|
||||
skynetBot0 = new SkynetBot();
|
||||
skynetBot0.name = 'skynet0';
|
||||
//console.log(skynetBot0.connections);
|
||||
skynetBot0.creds("96630051-a3dc-11e3-8442-5bf31d98c912", "2s67o7ek98pycik98f43reqr90t6s9k9");
|
||||
console.log(skynetBot0.connections);
|
||||
console.log("===========================================");
|
||||
Cylon.robot(skynetBot0);
|
||||
|
||||
Cylon.start();
|
|
@ -0,0 +1,64 @@
|
|||
var Cylon = require('..'),
|
||||
arduino1, arduino2;
|
||||
|
||||
Arduino = (function(){
|
||||
function Arduino(){}
|
||||
|
||||
Arduino.prototype.connection = { name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' };
|
||||
|
||||
Arduino.prototype.work = function(my) {
|
||||
console.log("Name =====>");
|
||||
console.log(my.name);
|
||||
}
|
||||
|
||||
return Arduino;
|
||||
})();
|
||||
|
||||
skynet = {
|
||||
connections: [
|
||||
{ name: 'skynet',
|
||||
adaptor: 'skynet',
|
||||
uuid: "96630051-a3dc-11e3-8442-5bf31d98c912", token: "2s67o7ek98pycik98f43reqr90t6s9k9" }
|
||||
],
|
||||
|
||||
work: function(my) {
|
||||
Logger.info("Skynet is listening...");
|
||||
|
||||
my.skynet.on('message', function(data) {
|
||||
console.log(data);
|
||||
if (data.payload != null){
|
||||
var robot,
|
||||
robots = data.payload.robots;
|
||||
for(var index in robots){
|
||||
robot = robots[index];
|
||||
console.log(robot);
|
||||
my.master.findRobot(robot.name, function(err, bot){
|
||||
if (robot.cmd == 'on')
|
||||
bot.devices[robot.device].turnOn();
|
||||
else
|
||||
bot.devices[robot.device].turnOff();
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Cylon.robot(skynet);
|
||||
|
||||
arduino0 = new Arduino();
|
||||
console.log(arduino0);
|
||||
arduino0.name = 'arduino0';
|
||||
arduino0.device = {name: 'led00', driver: 'led', pin: 13};
|
||||
Cylon.robot(arduino0);
|
||||
|
||||
arduino1 = new Arduino();
|
||||
arduino1.name = 'arduino1'
|
||||
arduino1.connection.port = '/dev/ttyACM1';
|
||||
arduino1.devices = [
|
||||
{name: 'led10', driver: 'led', pin: 11},
|
||||
{name: 'led11', driver: 'led', pin: 12},
|
||||
{name: 'led12', driver: 'led', pin: 13}
|
||||
];
|
||||
Cylon.robot(arduino1);
|
||||
|
||||
Cylon.start();
|
|
@ -0,0 +1,64 @@
|
|||
var Cylon = require('..'),
|
||||
arduino1, arduino2;
|
||||
|
||||
Arduino = (function(){
|
||||
function Arduino(){}
|
||||
|
||||
Arduino.prototype.connection = { name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' };
|
||||
|
||||
Arduino.prototype.work = function(my) {
|
||||
console.log("Name =====>");
|
||||
console.log(my.name);
|
||||
}
|
||||
|
||||
return Arduino;
|
||||
})();
|
||||
|
||||
skynet = {
|
||||
connections: [
|
||||
{ name: 'skynet',
|
||||
adaptor: 'skynet',
|
||||
uuid: "e8f942f1-a49c-11e3-9270-795e22e700d8", token: "0lpxpyafz7z7u8frgvp44g8mbr7o80k9" }
|
||||
],
|
||||
|
||||
work: function(my) {
|
||||
Logger.info("Skynet is listening...");
|
||||
|
||||
my.skynet.on('message', function(data) {
|
||||
console.log(data);
|
||||
if (data.payload != null){
|
||||
var robot,
|
||||
robots = data.payload.robots;
|
||||
for(var index in robots){
|
||||
robot = robots[index];
|
||||
console.log(robot);
|
||||
my.master.findRobot(robot.name, function(err, bot){
|
||||
if (robot.cmd == 'on')
|
||||
bot.devices[robot.device].turnOn();
|
||||
else
|
||||
bot.devices[robot.device].turnOff();
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Cylon.robot(skynet);
|
||||
|
||||
arduino0 = new Arduino();
|
||||
console.log(arduino0);
|
||||
arduino0.name = 'arduino0';
|
||||
arduino0.device = {name: 'led00', driver: 'led', pin: 13};
|
||||
Cylon.robot(arduino0);
|
||||
|
||||
arduino1 = new Arduino();
|
||||
arduino1.name = 'arduino1'
|
||||
arduino1.connection.port = '/dev/ttyACM1';
|
||||
arduino1.devices = [
|
||||
{name: 'led10', driver: 'led', pin: 11},
|
||||
{name: 'led11', driver: 'led', pin: 12},
|
||||
{name: 'led12', driver: 'led', pin: 13}
|
||||
];
|
||||
Cylon.robot(arduino1);
|
||||
|
||||
Cylon.start();
|
|
@ -0,0 +1,113 @@
|
|||
var skynet = require('skynet');
|
||||
|
||||
var conn = skynet.createConnection({
|
||||
"uuid": "96630051-a3dc-11e3-8442-5bf31d98c912",
|
||||
"token": "2s67o7ek98pycik98f43reqr90t6s9k9"
|
||||
"protocol": "websocket", // or "websocket"
|
||||
"qos": 0, // MQTT Quality of Service (0=no confirmation, 1=confirmation, 2=N/A)
|
||||
"host": "localhost", // optional - defaults to http://skynet.im
|
||||
"port": 80 // optional - defaults to 80
|
||||
});
|
||||
|
||||
conn.on('notReady', function(data){
|
||||
console.log('UUID FAILED AUTHENTICATION!');
|
||||
console.log(data);
|
||||
});
|
||||
|
||||
conn.on('ready', function(data){
|
||||
console.log('UUID AUTHENTICATED!');
|
||||
console.log(data);
|
||||
|
||||
// Subscribe to device
|
||||
conn.subscribe({
|
||||
"uuid": "96630051-a3dc-11e3-8442-5bf31d98c912",
|
||||
"token": "2s67o7ek98pycik98f43reqr90t6s9k9"
|
||||
}, function (data) {
|
||||
console.log(data);
|
||||
});
|
||||
|
||||
// Subscribe to device
|
||||
//conn.unsubscribe({
|
||||
//"uuid": "f828ef20-29f7-11e3-9604-b360d462c699"
|
||||
//}, function (data) {
|
||||
//console.log(data);
|
||||
//});
|
||||
|
||||
// Send and receive messages
|
||||
conn.message({
|
||||
"devices": "*",
|
||||
"payload": {
|
||||
"skynet":"online"
|
||||
},
|
||||
"qos": 0
|
||||
});
|
||||
conn.message({
|
||||
"devices": "96630051-a3dc-11e3-8442-5bf31d98c912",
|
||||
"payload": {
|
||||
"skynet":"online 2"
|
||||
},
|
||||
"qos": 0
|
||||
});
|
||||
conn.message({
|
||||
"devices": "96630051-a3dc-11e3-8442-5bf31d98c912",
|
||||
"payload": {
|
||||
"skynet":"online 3"
|
||||
},
|
||||
"qos": 0
|
||||
});
|
||||
|
||||
conn.on('message', function(channel, message){
|
||||
console.log('message received', channel, message);
|
||||
});
|
||||
|
||||
|
||||
// Event triggered when device loses connection to skynet
|
||||
conn.on('disconnect', function(data){
|
||||
console.log('disconnected from skynet');
|
||||
});
|
||||
|
||||
// Register a device (note: you can leave off the token to have skynet generate one for you)
|
||||
//conn.register({
|
||||
//"token": "zh4p7as90pt1q0k98fzvwmc9rmjkyb9",
|
||||
//"type": "drone"
|
||||
//}, function (data) {
|
||||
//console.log(data);
|
||||
//});
|
||||
|
||||
// UnRegister a device
|
||||
//conn.unregister({
|
||||
//"uuid": "zh4p7as90pt1q0k98fzvwmc9rmjkyb9",
|
||||
//"token": "zh4p7as90pt1q0k98fzvwmc9rmjkyb9"
|
||||
//}, function (data) {
|
||||
//console.log(data);
|
||||
//});
|
||||
|
||||
|
||||
// Update device
|
||||
//conn.update({
|
||||
//"uuid":"ad698900-2546-11e3-87fb-c560cb0ca47b",
|
||||
//"token": "zh4p7as90pt1q0k98fzvwmc9rmjkyb9",
|
||||
//"armed":true
|
||||
//}, function (data) {
|
||||
//console.log(data);
|
||||
//});
|
||||
|
||||
// WhoAmI?
|
||||
conn.whoami({"uuid":"96630051-a3dc-11e3-8442-5bf31d98c912"}, function (data) {
|
||||
console.log(data);
|
||||
});
|
||||
|
||||
// Receive an array of device UUIDs based on user defined search criteria
|
||||
conn.devices({
|
||||
"type":"drone"
|
||||
}, function (data) {
|
||||
console.log(data);
|
||||
});
|
||||
|
||||
// Skynet status
|
||||
conn.status(function (data) {
|
||||
console.log(data);
|
||||
});
|
||||
|
||||
});
|
||||
|
|
@ -3,22 +3,25 @@ var Cylon = require('../..');
|
|||
Cylon.robot({
|
||||
connections: [
|
||||
{ name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' },
|
||||
{ name: 'skynet', adaptor: 'skynet', uuid: "742401f1-87a4-11e3-834d-670dadc0ddbf", token: "xjq9h3yzhemf5hfrme8y08fh0sm50zfr" }
|
||||
{ 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' },
|
||||
|
||||
work: function(my) {
|
||||
Logger.info("connected...");
|
||||
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.led.turnOn()
|
||||
my.led13.turnOn()
|
||||
}
|
||||
else if(data.message.red == 'off') {
|
||||
my.led.turnOff()
|
||||
my.led13.turnOff()
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}).start();
|
||||
|
|
|
@ -55,9 +55,12 @@ namespace("Cylon", function() {
|
|||
opts.sendUpdate = opts.sendUpdate || false;
|
||||
opts.targetEventName = opts.targetEventName || opts.eventName;
|
||||
|
||||
console.log(opts.eventName);
|
||||
|
||||
opts.source.on(opts.eventName, function() {
|
||||
var args = arguments.length >= 1 ? [].slice.call(arguments, 0) : [];
|
||||
args.unshift(opts.targetEventName);
|
||||
console.log(opts.eventName);
|
||||
opts.target.emit.apply(opts.target, args);
|
||||
|
||||
if (opts.sendUpdate) {
|
||||
|
|
Loading…
Reference in New Issue