Merge pull request #131 from hybridgroup/cli-commands-refactor
Cli commands refactor
This commit is contained in:
commit
26008f3ee1
18
bin/cylon
18
bin/cylon
|
@ -1,9 +1,10 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
require('../lib/cli/cli-commands');
|
||||||
|
|
||||||
var Program = require('commander'),
|
var Program = require('commander'),
|
||||||
Pkg = require('../package.json'),
|
Pkg = require('../package.json'),
|
||||||
globule = require('globule'),
|
globule = require('globule');
|
||||||
CliCommands = require('../lib/cli/cli-commands');
|
|
||||||
|
|
||||||
Program
|
Program
|
||||||
.version(Pkg.version)
|
.version(Pkg.version)
|
||||||
|
@ -18,28 +19,31 @@ Program
|
||||||
.command("scan <type>")
|
.command("scan <type>")
|
||||||
.description("Scans serial, bluetooth or usb (pass param <serial> | <bluetooth> | <usb>) for connected devices")
|
.description("Scans serial, bluetooth or usb (pass param <serial> | <bluetooth> | <usb>) for connected devices")
|
||||||
.action(function(type) {
|
.action(function(type) {
|
||||||
CliCommands.scan(type);
|
var scan = new Cylon.CLI.Scan();
|
||||||
|
scan.search(type);
|
||||||
});
|
});
|
||||||
|
|
||||||
Program
|
Program
|
||||||
.command("bluetooth <subcmd> [address] [option]")
|
.command("bluetooth <subcmd> [address] [option]")
|
||||||
.description("Scan, pair, unpair bluetooth devices to/from the PC, establishes serial to bluetooth connection. Try 'cylon bluetooth help'")
|
.description("Scan, pair, unpair bluetooth devices to/from the PC, establishes serial to bluetooth connection. Try 'cylon bluetooth help'")
|
||||||
.action(function(subcmd, address, option){
|
.action(function(subcmd, address, option){
|
||||||
|
var bluetooth = new Cylon.CLI.Bluetooth();
|
||||||
|
|
||||||
switch(subcmd){
|
switch(subcmd){
|
||||||
case 'pair':
|
case 'pair':
|
||||||
option = (option === null || option === undefined) ? 'hci0' : option;
|
option = (option === null || option === undefined) ? 'hci0' : option;
|
||||||
CliCommands.bluetooth.pair(option, address);
|
bluetooth.pair(option, address);
|
||||||
break;
|
break;
|
||||||
case 'unpair':
|
case 'unpair':
|
||||||
option = (option === null || option === undefined) ? 'hci0' : option;
|
option = (option === null || option === undefined) ? 'hci0' : option;
|
||||||
CliCommands.bluetooth.unpair(option, address);
|
bluetooth.unpair(option, address);
|
||||||
break;
|
break;
|
||||||
case 'connect':
|
case 'connect':
|
||||||
option = (option === null || option === undefined) ? '/dev/rfcomm0' : option;
|
option = (option === null || option === undefined) ? '/dev/rfcomm0' : option;
|
||||||
CliCommands.bluetooth.connect(option, address);
|
bluetooth.connect(option, address);
|
||||||
break;
|
break;
|
||||||
case 'scan':
|
case 'scan':
|
||||||
CliCommands.bluetooth.scan();
|
bluetooth.scan();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.log("cylon bluetooth argument not recognized, try:\n");
|
console.log("cylon bluetooth argument not recognized, try:\n");
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
|
})();
|
||||||
|
});
|
|
@ -1,17 +1,17 @@
|
||||||
require("./process");
|
var os = require('os'),
|
||||||
|
namespace = require('node-namespace');
|
||||||
|
|
||||||
var bluetooth,
|
namespace("Cylon.CLI", function() {
|
||||||
scan = require("./scan"),
|
this.Bluetooth = (function() {
|
||||||
os = require('os');
|
function Bluetooth() {
|
||||||
|
this.process = new Cylon.Process;
|
||||||
|
this.platform = os.platform();
|
||||||
|
}
|
||||||
|
|
||||||
bluetooth = {
|
Bluetooth.prototype.pair = function(hciX, address) {
|
||||||
pair: function(hciX, address) {
|
switch (this.platform) {
|
||||||
var process = new Cylon.Process,
|
|
||||||
platform = os.platform();
|
|
||||||
|
|
||||||
switch (platform) {
|
|
||||||
case 'linux':
|
case 'linux':
|
||||||
process.spawn('bluez-simple-agent', [hciX, address]);
|
this.process.spawn('bluez-simple-agent', [hciX, address]);
|
||||||
break;
|
break;
|
||||||
case 'darwin':
|
case 'darwin':
|
||||||
console.log("OS X manages Bluetooth pairing itself.");
|
console.log("OS X manages Bluetooth pairing itself.");
|
||||||
|
@ -19,15 +19,12 @@ bluetooth = {
|
||||||
default:
|
default:
|
||||||
console.log("OS not yet supported.");
|
console.log("OS not yet supported.");
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
|
|
||||||
unpair: function(hciX, address) {
|
Bluetooth.prototype.unpair = function(hciX, address) {
|
||||||
var process = new Cylon.Process,
|
switch (this.platform) {
|
||||||
platform = os.platform();
|
|
||||||
|
|
||||||
switch (platform) {
|
|
||||||
case 'linux':
|
case 'linux':
|
||||||
process.spawn('bluez-simple-agent', [hciX, address, 'remove']);
|
this.process.spawn('bluez-simple-agent', [hciX, address, 'remove']);
|
||||||
break;
|
break;
|
||||||
case 'darwin':
|
case 'darwin':
|
||||||
console.log("OS X manages Bluetooth unpairing itself.");
|
console.log("OS X manages Bluetooth unpairing itself.");
|
||||||
|
@ -35,15 +32,12 @@ bluetooth = {
|
||||||
default:
|
default:
|
||||||
console.log("OS not yet supported.");
|
console.log("OS not yet supported.");
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
|
|
||||||
connect: function(dev, address) {
|
Bluetooth.prototype.connect = function(dev, address) {
|
||||||
var process = new Cylon.Process,
|
switch (this.platform) {
|
||||||
platform = os.platform();
|
|
||||||
|
|
||||||
switch (platform) {
|
|
||||||
case 'linux':
|
case 'linux':
|
||||||
process.spawn('sudo', ['rfcomm', 'connect', dev, address, '1']);
|
this.process.spawn('sudo', ['rfcomm', 'connect', dev, address, '1']);
|
||||||
break;
|
break;
|
||||||
case 'darwin':
|
case 'darwin':
|
||||||
console.log("OS X manages binding itself.");
|
console.log("OS X manages binding itself.");
|
||||||
|
@ -51,11 +45,14 @@ bluetooth = {
|
||||||
default:
|
default:
|
||||||
console.log("OS not yet supported");
|
console.log("OS not yet supported");
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
|
|
||||||
scan: function() {
|
Bluetooth.prototype.scan = function() {
|
||||||
scan('bluetooth');
|
var scan = new Cylon.CLI.Scan();
|
||||||
}
|
scan.bluetooth();
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = bluetooth;
|
return Bluetooth;
|
||||||
|
|
||||||
|
})();
|
||||||
|
});
|
||||||
|
|
|
@ -1,17 +1,7 @@
|
||||||
var bluetooth, cliCommands, connectToSerial, scan;
|
// Make Cylon.CLI.Arduino available for other modules
|
||||||
|
// executing commands
|
||||||
|
|
||||||
var cliCommands,
|
require("./process");
|
||||||
scan = require('./scan'),
|
require('./arduino');
|
||||||
bluetooth = require('./bluetooth'),
|
require('./scan');
|
||||||
|
require('./bluetooth');
|
||||||
cliCommands = {
|
|
||||||
scan: scan,
|
|
||||||
bluetooth: {
|
|
||||||
pair: bluetooth.pair,
|
|
||||||
unpair: bluetooth.unpair,
|
|
||||||
scan: bluetooth.scan,
|
|
||||||
connect: bluetooth.connect
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = cliCommands;
|
|
||||||
|
|
|
@ -44,5 +44,3 @@ namespace("Cylon", function() {
|
||||||
|
|
||||||
})();
|
})();
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = Cylon.Process;
|
|
||||||
|
|
|
@ -1,23 +1,37 @@
|
||||||
require("./process");
|
var os = require('os'),
|
||||||
|
namespace = require('node-namespace');
|
||||||
|
|
||||||
var scan,
|
namespace('Cylon.CLI', function() {
|
||||||
os = require('os');
|
this.Scan = (function() {
|
||||||
|
function Scan() {
|
||||||
|
this.process = new Cylon.Process;
|
||||||
|
this.platform = os.platform();
|
||||||
|
}
|
||||||
|
|
||||||
scan = function(type) {
|
Scan.prototype.serial = function(){
|
||||||
var process = new Cylon.Process,
|
this.search('serial');
|
||||||
platform = os.platform();
|
};
|
||||||
|
|
||||||
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':
|
case 'linux':
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'serial':
|
case 'serial':
|
||||||
process.exec("dmesg | grep tty");
|
this.process.exec("dmesg | grep tty");
|
||||||
break;
|
break;
|
||||||
case 'bluetooth':
|
case 'bluetooth':
|
||||||
process.exec("hcitool scan");
|
this.process.exec("hcitool scan");
|
||||||
break;
|
break;
|
||||||
case 'usb':
|
case 'usb':
|
||||||
process.exec("lsusb");
|
this.process.exec("lsusb");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.log("Device type not yet supported.");
|
console.log("Device type not yet supported.");
|
||||||
|
@ -25,11 +39,13 @@ scan = function(type) {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'darwin':
|
case 'darwin':
|
||||||
process.exec("ls /dev/{tty,cu}.*");
|
this.process.exec("ls /dev/{tty,cu}.*");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.log("OS not yet supported.");
|
console.log("OS not yet supported.");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = scan;
|
return Scan;
|
||||||
|
})();
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in New Issue