Sort discovered devices into a list where previously connected ones are sorted to the top of the array.

This commit is contained in:
mmcwilliams 2018-01-31 21:46:03 -05:00
parent 66d9af42b6
commit c72d80c6a1
1 changed files with 28 additions and 3 deletions

View File

@ -42,10 +42,35 @@ var enumerateDevices = function (err, devices) {
}, 1000) }, 1000)
} else { } else {
log.info('Found ' + devices.length + ' USB devices', 'SERIAL', true, true) log.info('Found ' + devices.length + ' USB devices', 'SERIAL', true, true)
console.dir(devices)
devices = favorDevices(devices)
console.dir(devices)
distinguishDevices(devices) distinguishDevices(devices)
} }
} }
var favorDevices = function (devices) {
const past = mcopy.settings.devices.filter(dev => {
if (dev.arduino) {
return dev
}
}).map(dev => {
return dev.arduino
})
if (past.length === 0) {
return devices
}
devices.sort((a, b) => {
if (past.indexOf(a) !== -1 && past.indexOf(b) === -1) {
return 1
} else if (past.indexOf(a) === -1 && past.indexOf(b) !== -1) {
return -1
}
return 0
})
return devices
}
var distinguishDevice = function (device, callback) { var distinguishDevice = function (device, callback) {
var connectCb = function (err, device) { var connectCb = function (err, device) {
if (err) { if (err) {
@ -361,17 +386,17 @@ cam.listen = function () {
event.returnValue = true event.returnValue = true
}) })
ipcMain.on('intval', (event, arg) => { ipcMain.on('intval', (event, arg) => {
console.dir(arg)
if (arg.connect) { if (arg.connect) {
cam.intval = new Intval(arg.url) cam.intval = new Intval(arg.url)
/*cam.intval.connect((err, ms, state) => { cam.intval.connect((err, ms, state) => {
if (err) { if (err) {
mainWindow.webContents.send('intval', { connected : false })
log.info(`Cannot connect to ${arg.url}`, 'INTVAL', true, true) log.info(`Cannot connect to ${arg.url}`, 'INTVAL', true, true)
cam.intval = null cam.intval = null
} else { } else {
log.info() log.info()
} }
})*/ })
} else if (arg.disconnect) { } else if (arg.disconnect) {
cam.intval = null cam.intval = null
} }