From 7eab7675cbd75266f51d8fbd920e7bb50d454caa Mon Sep 17 00:00:00 2001 From: mmcw-dev Date: Sat, 3 Mar 2018 00:27:49 -0500 Subject: [PATCH] Async/Await refactor in intval lib. FakeConnect needs repair. --- app/lib/arduino/index.js | 103 ++++++++++++++++++---------------- app/lib/intval/index.js | 47 ++++++++-------- app/main.js | 116 +++++++++++++++++++++++---------------- 3 files changed, 150 insertions(+), 116 deletions(-) diff --git a/app/lib/arduino/index.js b/app/lib/arduino/index.js index 2726f97..5d4bdb1 100644 --- a/app/lib/arduino/index.js +++ b/app/lib/arduino/index.js @@ -18,12 +18,15 @@ async function delay (ms) { async function send (device, cmd) { return new Promise ((resolve, reject) => { - mcopy.arduino.serial[device].write(cmd, (err, results) => { + mcopy.arduino.queue[cmd] = (ms) => { + return resolve(ms) + } + return mcopy.arduino.serial[device].write(cmd, (err, results) => { if (err) { //console.error(err) return reject(err) } - return resolve(results) + // }) }) } @@ -123,7 +126,6 @@ mcopy.arduino.send = async function (serial, cmd, res) { return false } mcopy.arduino.lock = true - mcopy.arduino.queue[cmd] = res await delay(mcopy.cfg.arduino.serialDelay) try { results = await send(device, cmd) @@ -154,18 +156,20 @@ mcopy.arduino.string = async function (serial, str) { } //respond with same char over serial when done -mcopy.arduino.end = function (data) { - var end = new Date().getTime(), - ms = end - mcopy.arduino.timer; +mcopy.arduino.end = async function (data) { + const end = new Date().getTime() + const ms = end - mcopy.arduino.timer + let complete if (mcopy.arduino.queue[data] !== undefined) { mcopy.arduino.lock = false; //console.log('Command ' + data + ' took ' + ms + 'ms'); - mcopy.arduino.queue[data](ms); //execute callback + complete = mcopy.arduino.queue[data](ms) //execute callback eventEmitter.emit('arduino_end', data) delete mcopy.arduino.queue[data] } else { //console.log('Received stray "' + data + '"'); //silent to user } + return complete }; mcopy.arduino.alias = function (serial, device) { console.log(`Making "${serial}" an alias of ${device}`) @@ -189,16 +193,16 @@ mcopy.arduino.connect = async function (serial, device, confirm) { } console.log(`Opened connection with ${mcopy.arduino.path[serial]} as ${serial}`); if (!confirm) { - mcopy.arduino.serial[device].on('data', async data => { + mcopy.arduino.serial[device].on('data', async (data) => { let d = data.toString('utf8') d = d.replace(newlineRe, '').replace(returnRe, '') - mcopy.arduino.end(d) + return await mcopy.arduino.end(d) }) } else { - mcopy.arduino.serial[device].on('data', data => { + mcopy.arduino.serial[device].on('data', async (data) => { let d = data.toString('utf8') d = d.replace(newlineRe, '').replace(returnRe, '') - mcopy.arduino.confirmEnd(d) + return await mcopy.arduino.confirmEnd(d) }) } return resolve(mcopy.arduino.path[serial]) @@ -242,31 +246,35 @@ mcopy.arduino.verify = async function () { }) } -mcopy.arduino.distinguish = async function (callback) { - const device = mcopy.arduino.alias['connect'] - let writeSuccess - mcopy.arduino.confirmExec = function (err, data) { - if (data === mcopy.cfg.arduino.cmd.proj_identifier) { - callback(null, 'projector'); - } else if (data === mcopy.cfg.arduino.cmd.cam_identifier) { - callback(null, 'camera'); - } else if (data === mcopy.cfg.arduino.cmd.light_identifier) { - callback(null, 'light') - } else if (data === mcopy.cfg.arduino.cmd.proj_light_identifier) { - callback(null, 'projector,light') - } else if (data === mcopy.cfg.arduino.cmd.proj_cam_light_identifier) { - callback(null, 'projector,camera,light') - } else if (data === mcopy.cfg.arduino.cmd.proj_cam_identifier) { - callback(null, 'projector,camera') +mcopy.arduino.distinguish = async function () { + return new Promise(async (resolve, reject) => { + const device = mcopy.arduino.alias['connect'] + let writeSuccess + let type + mcopy.arduino.confirmExec = function (err, data) { + if (data === mcopy.cfg.arduino.cmd.proj_identifier) { + type = 'projector' + } else if (data === mcopy.cfg.arduino.cmd.cam_identifier) { + type = 'camera' + } else if (data === mcopy.cfg.arduino.cmd.light_identifier) { + type = 'light' + } else if (data === mcopy.cfg.arduino.cmd.proj_light_identifier) { + type = 'projector,light' + } else if (data === mcopy.cfg.arduino.cmd.proj_cam_light_identifier) { + type = 'projector,camera,light' + } else if (data === mcopy.cfg.arduino.cmd.proj_cam_identifier) { + type = 'projector,camera' + } + return resolve(type) } - } - await delay(mcopy.cfg.arduino.serialDelay) - try { - writeSuccess = await send(device, mcopy.cfg.arduino.cmd.mcopy_identifier) - } catch (e) { - return console.error(e) - } - return writeSuccess + await delay(mcopy.cfg.arduino.serialDelay) + try { + writeSuccess = await send(device, mcopy.cfg.arduino.cmd.mcopy_identifier) + } catch (e) { + console.error(e) + return reject(e) + } + }) } mcopy.arduino.close = async function (callback) { @@ -285,18 +293,19 @@ mcopy.arduino.fakeConnect = async function (serial) { const device = '/dev/fake' mcopy.arduino.alias[serial] = device mcopy.arduino.serial[device] = { - write : async function (cmd, res) { - return new Promise(async (resolve, reject) => { - var t = { - c : mcopy.cfg.arduino.cam.time + mcopy.cfg.arduino.cam.delay, - p : mcopy.cfg.arduino.proj.time + mcopy.cfg.arduino.proj.delay - }, - timeout = t[cmd]; - if (typeof timeout === 'undefined') timeout = 10; - mcopy.arduino.timer = +new Date(); - await delay(timeout) - return await mcopy.arduino.end(cmd) - }) + write : function (cmd, cb) { + const t = { + c : mcopy.cfg.arduino.cam.time + mcopy.cfg.arduino.cam.delay, + p : mcopy.cfg.arduino.proj.time + mcopy.cfg.arduino.proj.delay + } + let timeout = t[cmd] + let end + if (typeof timeout === 'undefined') timeout = 10 + mcopy.arduino.timer = +new Date() + return setTimeout(() => { + mcopy.arduino.end(cmd) + return cb() + }, timeout) }, string : async function (str) { //do nothing diff --git a/app/lib/intval/index.js b/app/lib/intval/index.js index 1e6d726..b1a0f9a 100644 --- a/app/lib/intval/index.js +++ b/app/lib/intval/index.js @@ -6,32 +6,35 @@ class Intval { constructor (url) { this._baseUrl = `http://${url}` } - move (cb) { - const timeStart = +new Date() - const url = `${this._baseUrl}/frame` - //console.log(url) - req(url, (err, res, body) => { - let ms = (+new Date()) - timeStart - if (err) { - console.error(err) - } - cb(ms) + async move () { + return new Promise ((resolve, reject) => { + const timeStart = +new Date() + const url = `${this._baseUrl}/frame` + //console.log(url) + return req(url, (err, res, body) => { + let ms = (+new Date()) - timeStart + if (err) { + return reject(err) + } + return resolve(ms) + }) }) } - setDir (dir, cb) { - const timeStart = +new Date() - const url = `${this._baseUrl}/dir?dir=${dir}` - //console.log(url) - req(url, (err, res, body) => { - let ms = (+new Date()) - timeStart - if (err) { - console.error(err) - } - cb(ms) + async setDir (dir) { + return new Promise ((resolve, reject) => { + const timeStart = +new Date() + const url = `${this._baseUrl}/dir?dir=${dir}` + //console.log(url) + return req(url, (err, res, body) => { + let ms = (+new Date()) - timeStart + if (err) { + return reject(err) + } + return resolve(ms) + }) }) - } - setExposure (exposure, cb) { + async setExposure (exposure, cb) { const timeStart = +new Date() const url = `${this._baseUrl}/exposure?exposure=${exposure}` //console.log(url) diff --git a/app/main.js b/app/main.js index 1a72810..713aab0 100644 --- a/app/main.js +++ b/app/main.js @@ -95,7 +95,7 @@ dev.favor = function (devices) { return devices } -dev.distinguish = async function (device, callback) { +dev.distinguish = async function (device) { let connectSuccess let verifySuccess let type @@ -249,6 +249,11 @@ dev.connectDevice = async function (device, type) { //Cases for 1 or 2 arduinos connected dev.all = async function (devices) { + let c = {} + let p = {} + let l = {} + let all + dev.connected = { projector : false, camera : false, @@ -257,32 +262,31 @@ dev.all = async function (devices) { let checklist = [] - /*await Promise.all(devices.map(async (device) => { - return new Promise( async (resolve, reject) => { + all = Promise.all(devices.map(async (device) => { + return new Promise(async (resolve, reject) => { let type let d + try { type = await dev.distinguish(device) } catch (err) { console.error(err) return reject(err) } + try { d = await dev.connectDevice(device, type) } catch (err) { console.error(err) return reject(err) } - return resolve(d) + + return d }) - })*/ + })) //done checking devices - let c = {} - let p = {} - let l = {} - if (!dev.connected.projector) { await dev.fakeProjector() } @@ -294,7 +298,6 @@ dev.all = async function (devices) { if (mcopy.settings.camera.intval) { c.intval = mcopy.settings.camera.intval - console.dir(mcopy.settings.camera) await delay(1000) cam.connectIntval(null, { connect : true, url : c.intval }) } @@ -305,7 +308,7 @@ dev.all = async function (devices) { l.arduino = dev.connected.light - dev.ready(p, c, l) + return dev.ready(p, c, l) } dev.remember = function (which, device, type) { @@ -337,6 +340,7 @@ dev.ready = function (projector, camera, light) { settings.update('projector', projector) settings.update('light', light) settings.save() + return true }; var createMenu = function () { @@ -370,16 +374,20 @@ light.listen = function () { event.returnValue = true }) } -light.set = function (rgb, id) { - var str = rgb.join(','); - arduino.send('light', mcopy.cfg.arduino.cmd.light, (ms) => { - light.end(rgb, id, ms) - }) - arduino.string('light', str) +light.set = async function (rgb, id) { + const str = rgb.join(','); + let ms + try { + ms = await arduino.send('light', mcopy.cfg.arduino.cmd.light) + } catch (err) { + console.error(err) + } + await arduino.string('light', str) + return await light.end(rgb, id, ms) } -light.end = function (rgb, id, ms) { +light.end = async function (rgb, id, ms) { log.info('Light set to ' + rgb.join(','), 'LIGHT', true, true) - mainWindow.webContents.send('light', {rgb: rgb, id : id, ms: ms}) + return await mainWindow.webContents.send('light', {rgb: rgb, id : id, ms: ms}) } proj.state = { @@ -388,22 +396,31 @@ proj.state = { proj.init = function () { proj.listen() } -proj.set = function (dir, id) { - var cmd +proj.set = async function (dir, id) { + let cmd + let ms if (dir) { cmd = mcopy.cfg.arduino.cmd.proj_forward } else { cmd = mcopy.cfg.arduino.cmd.proj_backward } proj.state.dir = dir - arduino.send('projector', cmd, (ms) => { - proj.end(cmd, id, ms) - }) + try { + ms = await arduino.send('projector', cmd) + } catch (err) { + console.error(err) + } + return await proj.end(cmd, id, ms) } -proj.move = function (frame, id) { - arduino.send('projector', mcopy.cfg.arduino.cmd.projector, (ms) => { - proj.end(mcopy.cfg.arduino.cmd.projector, id, ms) - }) +proj.move = async function (frame, id) { + const cmd = mcopy.cfg.arduino.cmd.projector + let ms + try { + ms = await arduino.send('projector', cmd) + } catch (err) { + console.error(err) + } + return await proj.end(mcopy.cfg.arduino.cmd.projector, id, ms) } proj.listen = function () { ipcMain.on('proj', (event, arg) => { @@ -415,8 +432,8 @@ proj.listen = function () { event.returnValue = true }) } -proj.end = function (cmd, id, ms) { - var message = '' +proj.end = async function (cmd, id, ms) { + let message = '' if (cmd === mcopy.cfg.arduino.cmd.proj_forward) { message = 'Projector set to FORWARD' } else if (cmd === mcopy.cfg.arduino.cmd.proj_backward) { @@ -431,7 +448,7 @@ proj.end = function (cmd, id, ms) { message += ' 1 frame' } log.info(message, 'PROJECTOR', true, true) - mainWindow.webContents.send('proj', {cmd: cmd, id : id, ms: ms}) + return await mainWindow.webContents.send('proj', {cmd: cmd, id : id, ms: ms}) } cam.intval = null @@ -441,8 +458,9 @@ cam.state = { cam.init = function () { cam.listen() } -cam.set = function (dir, id) { +cam.set = async function (dir, id) { let cmd + let ms if (dir) { cmd = mcopy.cfg.arduino.cmd.cam_forward } else { @@ -451,27 +469,31 @@ cam.set = function (dir, id) { cam.state.dir = dir if (cam.intval) { - cam.intval.setDir(dir, ms => { - cam.end(cmd, id, ms) - }) + try { + ms = await cam.intval.setDir(dir) + } catch (err) { + console.error(err) + } } else { - arduino.send('camera', cmd, ms => { - cam.end(cmd, id, ms) - }) + try { + ms = await arduino.send('camera', cmd) + } catch (err) { + console.error(err) + } } + return await cam.end(cmd, id, ms) } -cam.move = function (frame, id) { - let cmd = mcopy.cfg.arduino.cmd.camera +cam.move = async function (frame, id) { + const cmd = mcopy.cfg.arduino.cmd.camera + let ms if (cam.intval) { - cam.intval.move(ms => { - cam.end(cmd, id, ms) - }) + ms = await cam.intval.move() } else { - arduino.send('camera', cmd, ms => { - cam.end(cmd, id, ms) - }) + ms = await arduino.send('camera', cmd) } + console.log(ms) + return cam.end(cmd, id, ms) } cam.exposure = function (exposure, id) { @@ -515,7 +537,7 @@ cam.listen = function () { }) ipcMain.on('intval', cam.connectIntval) } -cam.end = function (cmd, id, ms) { +cam.end = async function (cmd, id, ms) { var message = '' if (cmd === mcopy.cfg.arduino.cmd.cam_forward) { message = 'Camera set to FORWARD'