Merge pull request #131 from hybridgroup/cli-commands-refactor

Cli commands refactor
This commit is contained in:
Andrew Stewart 2014-03-07 17:08:11 -08:00
commit 26008f3ee1
6 changed files with 159 additions and 105 deletions

View File

@ -1,9 +1,10 @@
#!/usr/bin/env node
require('../lib/cli/cli-commands');
var Program = require('commander'),
Pkg = require('../package.json'),
globule = require('globule'),
CliCommands = require('../lib/cli/cli-commands');
globule = require('globule');
Program
.version(Pkg.version)
@ -18,28 +19,31 @@ Program
.command("scan <type>")
.description("Scans serial, bluetooth or usb (pass param <serial> | <bluetooth> | <usb>) for connected devices")
.action(function(type) {
CliCommands.scan(type);
var scan = new Cylon.CLI.Scan();
scan.search(type);
});
Program
.command("bluetooth <subcmd> [address] [option]")
.description("Scan, pair, unpair bluetooth devices to/from the PC, establishes serial to bluetooth connection. Try 'cylon bluetooth help'")
.action(function(subcmd, address, option){
var bluetooth = new Cylon.CLI.Bluetooth();
switch(subcmd){
case 'pair':
option = (option === null || option === undefined) ? 'hci0' : option;
CliCommands.bluetooth.pair(option, address);
bluetooth.pair(option, address);
break;
case 'unpair':
option = (option === null || option === undefined) ? 'hci0' : option;
CliCommands.bluetooth.unpair(option, address);
bluetooth.unpair(option, address);
break;
case 'connect':
option = (option === null || option === undefined) ? '/dev/rfcomm0' : option;
CliCommands.bluetooth.connect(option, address);
bluetooth.connect(option, address);
break;
case 'scan':
CliCommands.bluetooth.scan();
bluetooth.scan();
break;
default:
console.log("cylon bluetooth argument not recognized, try:\n");

49
lib/cli/arduino.js Normal file
View File

@ -0,0 +1,49 @@
var os = require('os'),
namespace = require('node-namespace');
namespace("Cylon.CLI", function() {
this.Arduino = (function() {
function Arduino() {}
Arduino.prototype.upload = function(serialPort, hexFile) {
var cylonProcess = new Cylon.Process(),
part = '-patmega328p',
programmer = '-carduino',
baudrate = '-b115200',
hexFile = "-Uflash:w:" + hexFile + ":i",
port = serialPort.search(/[\/\:]/) >= 0 ? "-P" + serialPort : "-P/dev/" + serialPort;
switch (os.platform()) {
case 'linux':
cylonProcess.spawn('avrdude', [part, programmer, port, baudrate, '-D', hexFile]);
break;
case 'darwin':
cylonProcess.spawn('avrdude', [part, programmer, port, baudrate, '-D', hexFile]);
break;
default:
console.log('OS not yet supported...\n');
}
return true;
};
Arduino.prototype.install = function() {
var cylonProcess = new Cylon.Process();
switch (os.platform()) {
case 'linux':
console.log("Should be installing...");
cylonProcess.spawn('sudo', ['apt-get', 'install', 'avrdude']);
break;
case 'darwin':
cylonProcess.exec('brew install avrdude');
break;
default:
console.log('OS not yet supported...\n');
}
return true;
};
return Arduino;
})();
});

View File

@ -1,17 +1,17 @@
require("./process");
var os = require('os'),
namespace = require('node-namespace');
var bluetooth,
scan = require("./scan"),
os = require('os');
namespace("Cylon.CLI", function() {
this.Bluetooth = (function() {
function Bluetooth() {
this.process = new Cylon.Process;
this.platform = os.platform();
}
bluetooth = {
pair: function(hciX, address) {
var process = new Cylon.Process,
platform = os.platform();
switch (platform) {
Bluetooth.prototype.pair = function(hciX, address) {
switch (this.platform) {
case 'linux':
process.spawn('bluez-simple-agent', [hciX, address]);
this.process.spawn('bluez-simple-agent', [hciX, address]);
break;
case 'darwin':
console.log("OS X manages Bluetooth pairing itself.");
@ -19,15 +19,12 @@ bluetooth = {
default:
console.log("OS not yet supported.");
}
},
};
unpair: function(hciX, address) {
var process = new Cylon.Process,
platform = os.platform();
switch (platform) {
Bluetooth.prototype.unpair = function(hciX, address) {
switch (this.platform) {
case 'linux':
process.spawn('bluez-simple-agent', [hciX, address, 'remove']);
this.process.spawn('bluez-simple-agent', [hciX, address, 'remove']);
break;
case 'darwin':
console.log("OS X manages Bluetooth unpairing itself.");
@ -35,15 +32,12 @@ bluetooth = {
default:
console.log("OS not yet supported.");
}
},
};
connect: function(dev, address) {
var process = new Cylon.Process,
platform = os.platform();
switch (platform) {
Bluetooth.prototype.connect = function(dev, address) {
switch (this.platform) {
case 'linux':
process.spawn('sudo', ['rfcomm', 'connect', dev, address, '1']);
this.process.spawn('sudo', ['rfcomm', 'connect', dev, address, '1']);
break;
case 'darwin':
console.log("OS X manages binding itself.");
@ -51,11 +45,14 @@ bluetooth = {
default:
console.log("OS not yet supported");
}
},
};
scan: function() {
scan('bluetooth');
}
};
Bluetooth.prototype.scan = function() {
var scan = new Cylon.CLI.Scan();
scan.bluetooth();
};
module.exports = bluetooth;
return Bluetooth;
})();
});

View File

@ -1,17 +1,7 @@
var bluetooth, cliCommands, connectToSerial, scan;
// Make Cylon.CLI.Arduino available for other modules
// executing commands
var cliCommands,
scan = require('./scan'),
bluetooth = require('./bluetooth'),
cliCommands = {
scan: scan,
bluetooth: {
pair: bluetooth.pair,
unpair: bluetooth.unpair,
scan: bluetooth.scan,
connect: bluetooth.connect
}
};
module.exports = cliCommands;
require("./process");
require('./arduino');
require('./scan');
require('./bluetooth');

View File

@ -44,5 +44,3 @@ namespace("Cylon", function() {
})();
});
module.exports = Cylon.Process;

View File

@ -1,23 +1,37 @@
require("./process");
var os = require('os'),
namespace = require('node-namespace');
var scan,
os = require('os');
namespace('Cylon.CLI', function() {
this.Scan = (function() {
function Scan() {
this.process = new Cylon.Process;
this.platform = os.platform();
}
scan = function(type) {
var process = new Cylon.Process,
platform = os.platform();
Scan.prototype.serial = function(){
this.search('serial');
};
switch (platform) {
Scan.prototype.bluetooth = function(){
this.search('bluetooth');
};
Scan.prototype.usb = function(){
this.search('usb');
};
Scan.prototype.search = function(type) {
switch (this.platform) {
case 'linux':
switch (type) {
case 'serial':
process.exec("dmesg | grep tty");
this.process.exec("dmesg | grep tty");
break;
case 'bluetooth':
process.exec("hcitool scan");
this.process.exec("hcitool scan");
break;
case 'usb':
process.exec("lsusb");
this.process.exec("lsusb");
break;
default:
console.log("Device type not yet supported.");
@ -25,11 +39,13 @@ scan = function(type) {
}
break;
case 'darwin':
process.exec("ls /dev/{tty,cu}.*");
this.process.exec("ls /dev/{tty,cu}.*");
break;
default:
console.log("OS not yet supported.");
}
};
};
module.exports = scan;
return Scan;
})();
});