Merge pull request #12 from Azure/donzh-add-exception-handling

Donzh add exception handling
This commit is contained in:
donzh 2016-09-30 16:23:38 +08:00 коммит произвёл GitHub
Родитель f107bc803a 1aa52285ff
Коммит a54c50154a
4 изменённых файлов: 79 добавлений и 45 удалений

Просмотреть файл

@ -30,7 +30,7 @@ var VERSION = require('../package.json').version;
function main(argv) {
bi.trackEvent('command_line_arguments', {
argv: argv
info: argv.join(' ')
});
if (argv.length === 1) {
@ -204,6 +204,17 @@ function cmd_list_usb_uart() {
UsbUartTransport.beginDiscovery(onDeviceDiscovered);
}
bi.start();
main(process.argv.slice(1));
bi.flush();
var domain = require('domain').create();
domain.on('error', function(err){
bi.trackEvent('unknown_error', {
error: err.message
});
bi.flush();
console.log(err);
})
domain.run(function(){
bi.start();
main(process.argv.slice(1));
bi.flush();
})

Просмотреть файл

@ -1,6 +1,7 @@
'use-strict';
var arp = require('node-arp');
var bi = require('az-iot-bi');
var dns = require('dns');
var mdns = require('mdns-js');
var vow = require('vow');
@ -11,6 +12,9 @@ function resolveIpAddressToHostNameAsync(address) {
return new vow.Promise(function(resolve, reject /*, notify */) {
dns.reverse(address, function(err, domains) {
if(err) {
bi.trackEvent('ethernet_error', {
error: err.message
});
reject(err);
return;
}
@ -23,6 +27,9 @@ function resolveIpAddressToMacAddressAsync(address) {
return new vow.Promise(function(resolve, reject /*, notify */) {
arp.getMAC(address, function(err, mac) {
if(err) {
bi.trackEvent('ethernet_error', {
error: err.message
});
reject(err);
return;
}

Просмотреть файл

@ -1,32 +1,41 @@
'use-strict';
var bi = require('az-iot-bi');
var serialPort = require('serialport');
var UsbLookup = require('./usb-lookup');
exports.beginDiscovery = function beginDiscovery(callback) {
serialPort.list(function (err, ports) {
if(err){
return;
}
ports.forEach(function(port) {
var pnpId = port.pnpId;
if(!pnpId && port.vendorId && port.productId){
pnpId = 'usb-VID_' + port.vendorId.split('0x')[1] + '&PID_' + port.productId.split('0x')[1];
return new Promise(function(resolve, reject) {
serialPort.list(function (err, ports) {
if(err){
bi.trackEvent('usb_error', {
error: err.message
});
reject(err);
return;
}
var deviceName = UsbLookup.resolveUsbName(pnpId);
var deviceType = UsbLookup.resolveDeviceType(pnpId);
var deviceNameOrManufacturer = deviceName ? deviceName : port.manufacturer;
(function(com_name, device_name_or_manufacturer, device_type) {
if(com_name && device_name_or_manufacturer) {
var record = {
com_name: com_name,
device_name_or_manufacturer: device_name_or_manufacturer,
device_type: device_type,
transport: 'usb-uart',
};
callback(record);
ports.forEach(function(port) {
var pnpId = port.pnpId;
if(!pnpId && port.vendorId && port.productId){
pnpId = 'usb-VID_' + port.vendorId.split('0x')[1] + '&PID_' + port.productId.split('0x')[1];
}
})(port.comName, deviceNameOrManufacturer, deviceType);
var deviceName = UsbLookup.resolveUsbName(pnpId);
var deviceType = UsbLookup.resolveDeviceType(pnpId);
var deviceNameOrManufacturer = deviceName ? deviceName : port.manufacturer;
(function(com_name, device_name_or_manufacturer, device_type) {
if(com_name && device_name_or_manufacturer) {
var record = {
com_name: com_name,
device_name_or_manufacturer: device_name_or_manufacturer,
device_type: device_type,
transport: 'usb-uart',
};
callback(record);
}
})(port.comName, deviceNameOrManufacturer, deviceType);
});
resolve(ports);
});
});
};

Просмотреть файл

@ -1,29 +1,36 @@
'use-strict';
var bi = require('az-iot-bi');
var wifiscanner = require('node-wifiscanner');
var OuiLookup = require('./oui-lookup');
exports.beginDiscovery = function beginDiscovery(callback) {
wifiscanner.scan(function(err, data) {
if (err) {
return;
}
for (var i = 0; i < data.length; i++) {
(function(ssid, mac_address, channel, signal_level) {
var device_type = OuiLookup.resolveMacToDeviceType(mac_address);
var record = {
channel: channel,
device_type: device_type,
mac_address: mac_address,
signal_level: signal_level,
ssid: ssid,
transport: 'wifi-ap',
};
callback(record);
})(data[i].ssid, data[i].mac, data[i].channel, data[i].signal_level);
}
});
return new Promise(function(resolve, reject) {
wifiscanner.scan(function(err, data) {
if (err) {
bi.trackEvent('wifi_error', {
error: err.message
});
reject('\nProbably no Wi-Fi module on this computer.\n' + err.message);
return;
}
for (var i = 0; i < data.length; i++) {
(function(ssid, mac_address, channel, signal_level) {
var device_type = OuiLookup.resolveMacToDeviceType(mac_address);
var record = {
channel: channel,
device_type: device_type,
mac_address: mac_address,
signal_level: signal_level,
ssid: ssid,
transport: 'wifi-ap',
};
callback(record);
})(data[i].ssid, data[i].mac, data[i].channel, data[i].signal_level);
}
resolve(data);
});
})
};
exports.endDiscovery = function endDiscovery(/* callback */) {