Refactoring cli commands to make them more friendly and pretty.

This commit is contained in:
Edgar O Silva 2014-03-07 15:57:08 -06:00
parent ea83c58097
commit 541c7402ef
6 changed files with 162 additions and 106 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");

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

@ -0,0 +1,50 @@
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;
})();
});
console.log("INSIDE NEW ARUDINO======>>");

View File

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

View File

@ -1,17 +1,8 @@
var bluetooth, cliCommands, connectToSerial, scan;
// Make Cylon.CLI.Arduino available for other modules
// executing commands
var os = require('os');
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,4 @@ namespace("Cylon", function() {
})();
});
module.exports = Cylon.Process;
console.log("INSIDE NEW PROCESS======>>");

View File

@ -1,35 +1,51 @@
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) {
case 'linux':
switch (type) {
case 'serial':
process.exec("dmesg | grep tty");
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':
this.process.exec("dmesg | grep tty");
break;
case 'bluetooth':
this.process.exec("hcitool scan");
break;
case 'usb':
this.process.exec("lsusb");
break;
default:
console.log("Device type not yet supported.");
break;
}
break;
case 'bluetooth':
process.exec("hcitool scan");
break;
case 'usb':
process.exec("lsusb");
case 'darwin':
this.process.exec("ls /dev/{tty,cu}.*");
break;
default:
console.log("Device type not yet supported.");
break;
console.log("OS not yet supported.");
}
break;
case 'darwin':
process.exec("ls /dev/{tty,cu}.*");
break;
default:
console.log("OS not yet supported.");
}
};
};
module.exports = scan;
return Scan;
})();
});