WIP on migrating cylon-cli to pure js.

This commit is contained in:
edgarsilva 2014-02-27 14:39:54 -06:00
parent 112c8d42ca
commit 655e7c5613
6 changed files with 141 additions and 157 deletions

View File

@ -2,7 +2,7 @@
var Program = require('commander'),
Pkg = require('../package.json'),
CliCommands = require('../dist/cli/cli-commands'),
CliCommands = require('../lib/cli/cli-commands'),
fs = require('fs');
Program
@ -36,7 +36,7 @@ Program
break;
case 'connect':
option = (option === null || option === undefined) ? '/dev/rfcomm0' : option;
CliCommands.connectToSerial(option, address);
CliCommands.bluetooth.connect(option, address);
break;
case 'scan':
CliCommands.bluetooth.scan();

View File

@ -1,44 +1,61 @@
(function() {
var bluetooth, os, scan;
require("./process");
require("./process");
var bluetooth,
scan = require("./scan"),
os = require('os');
scan = require("./scan");
bluetooth = {
pair: function(hciX, address) {
var process = new Cylon.Process,
platform = os.platform();
os = require('os');
bluetooth = {
pair: function(hciX, address) {
var platform, process;
process = new Cylon.Process;
platform = os.platform();
switch (platform) {
case 'linux':
return process.spawn('bluez-simple-agent', [hciX, address]);
case 'darwin':
return console.log("OS X manages Bluetooth pairing itself.");
default:
return console.log("OS not yet supported.");
}
},
unpair: function(hciX, address) {
var platform, process;
process = new Cylon.Process;
platform = os.platform();
switch (platform) {
case 'linux':
return process.spawn('bluez-simple-agent', [hciX, address, 'remove']);
case 'darwin':
return console.log("OS X manages Bluetooth unpairing itself.");
default:
return console.log("OS not yet supported.");
}
},
scan: function() {
return scan('bluetooth');
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.");
}
};
},
module.exports = bluetooth;
unpair: function(hciX, address) {
var process = new Cylon.Process,
platform = os.platform();
}).call(this);
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.");
}
},
connect: function(dev, address) {
var process = new Cylon.Process,
platform = os.platform();
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");
}
},
scan: function() {
scan('bluetooth');
}
};
module.exports = bluetooth;

View File

@ -1,22 +1,17 @@
(function() {
var bluetooth, cliCommands, connectToSerial, scan;
var bluetooth, cliCommands, connectToSerial, scan;
scan = require('./scan');
var cliCommands,
scan = require('./scan'),
bluetooth = require('./bluetooth'),
connectToSerial = require('./connect-to-serial');
cliCommands = {
scan: scan,
bluetooth: {
pair: bluetooth.pair,
unpair: bluetooth.unpair,
scan: bluetooth.scan,
connect: bluetooth.connect
}
};
bluetooth = require('./bluetooth');
cliCommands = {
scan: scan,
connectToSerial: connectToSerial,
bluetooth: {
pair: bluetooth.pair,
unpair: bluetooth.unpair,
scan: bluetooth.scan
}
};
module.exports = cliCommands;
}).call(this);
module.exports = cliCommands;

View File

@ -1,24 +0,0 @@
(function() {
var connectToSerial, os;
require("./process");
os = require('os');
connectToSerial = function(dev, address) {
var platform, process;
process = new Cylon.Process;
platform = os.platform();
switch (platform) {
case 'linux':
return process.spawn('sudo', ['rfcomm', 'connect', dev, address, '1']);
case 'darwin':
return console.log("OS X manages binding itself.");
default:
return console.log("OS not yet supported");
}
};
module.exports = connectToSerial;
}).call(this);

View File

@ -1,53 +1,48 @@
(function() {
var exec, namespace, spawn;
var spawn = require('child_process').spawn,
exec = require('child_process').exec,
namespace = require('node-namespace');
spawn = require('child_process').spawn;
namespace("Cylon", function() {
this.Process = (function() {
function Process() {}
exec = require('child_process').exec;
Process.prototype.exec = function(command) {
exec(command, function(err, stdout, stderr) {
if (stdout != null) {
console.log(stdout);
}
if (stderr != null) {
console.log(stderr);
}
if (err != null) {
console.log(err);
}
});
};
namespace = require('node-namespace');
Process.prototype.spawn = function(command, args) {
var cmd;
namespace("Cylon", function() {
return this.Process = (function() {
function Process() {}
cmd = spawn(command, args, {
stdio: 'inherit'
});
Process.prototype.exec = function(command) {
exec(command, function(err, stdout, stderr) {
if (stdout != null) {
console.log(stdout);
}
if (stderr != null) {
console.log(stderr);
}
if (err != null) {
return console.log(err);
}
});
return true;
};
cmd.on('close', function(code) {
if (code !== 0) {
return console.log("ps process exited with code " + code);
}
});
Process.prototype.spawn = function(command, args) {
var cmd;
cmd = spawn(command, args, {
stdio: 'inherit'
});
cmd.on('close', function(code) {
if (code !== 0) {
return console.log("ps process exited with code " + code);
}
});
return cmd.on('exit', function(code) {
if (code !== 0) {
return console.log("ps process exited with code " + code);
}
});
};
cmd.on('exit', function(code) {
if (code !== 0) {
return console.log("ps process exited with code " + code);
}
});
};
return Process;
return Process;
})();
});
})();
});
module.exports = Cylon.Process;
}).call(this);
module.exports = Cylon.Process;

View File

@ -1,34 +1,35 @@
(function() {
var os, scan;
require("./process");
require("./process");
var scan,
os = require('os');
os = require('os');
scan = function(type) {
var process = new Cylon.Process,
platform = os.platform();
scan = function(type) {
var platform, process;
process = new Cylon.Process;
platform = os.platform();
switch (platform) {
case 'linux':
switch (type) {
case 'serial':
return process.exec("dmesg | grep tty");
case 'bluetooth':
return process.exec("hcitool scan");
case 'usb':
return process.exec("lsusb");
default:
return console.log("Device type not yet supported.");
}
break;
case 'darwin':
return process.exec("ls /dev/{tty,cu}.*");
default:
return console.log("OS not yet supported.");
}
};
switch (platform) {
case 'linux':
switch (type) {
case 'serial':
process.exec("dmesg | grep tty");
break;
case 'bluetooth':
process.exec("hcitool scan");
break;
case 'usb':
process.exec("lsusb");
break;
default:
console.log("Device type not yet supported.");
break;
}
break;
case 'darwin':
process.exec("ls /dev/{tty,cu}.*");
break;
default:
console.log("OS not yet supported.");
}
};
module.exports = scan;
}).call(this);
module.exports = scan;