From c72d80c6a1c4ff762132e025c97b957b044f0e3f Mon Sep 17 00:00:00 2001 From: mmcwilliams Date: Wed, 31 Jan 2018 21:46:03 -0500 Subject: [PATCH] Sort discovered devices into a list where previously connected ones are sorted to the top of the array. --- app/main.js | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/app/main.js b/app/main.js index 2a36926..59b52d5 100644 --- a/app/main.js +++ b/app/main.js @@ -42,10 +42,35 @@ var enumerateDevices = function (err, devices) { }, 1000) } else { log.info('Found ' + devices.length + ' USB devices', 'SERIAL', true, true) + console.dir(devices) + devices = favorDevices(devices) + console.dir(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 connectCb = function (err, device) { if (err) { @@ -361,17 +386,17 @@ cam.listen = function () { event.returnValue = true }) ipcMain.on('intval', (event, arg) => { - console.dir(arg) if (arg.connect) { cam.intval = new Intval(arg.url) - /*cam.intval.connect((err, ms, state) => { + cam.intval.connect((err, ms, state) => { if (err) { + mainWindow.webContents.send('intval', { connected : false }) log.info(`Cannot connect to ${arg.url}`, 'INTVAL', true, true) cam.intval = null } else { log.info() } - })*/ + }) } else if (arg.disconnect) { cam.intval = null }