Async/Await refactor for arduino and sequence-related features
This commit is contained in:
parent
99c59a6c34
commit
c453e7ac43
|
@ -10,6 +10,58 @@ let eventEmitter
|
|||
|
||||
const mcopy = {}
|
||||
|
||||
async function delay (ms) {
|
||||
return new Promise(resolve => {
|
||||
return setTimeout(resolve, ms)
|
||||
})
|
||||
}
|
||||
|
||||
async function send (device, cmd) {
|
||||
return new Promise ((resolve, reject) => {
|
||||
mcopy.arduino.serial[device].write(cmd, (err, results) => {
|
||||
if (err) {
|
||||
//console.error(err)
|
||||
return reject(err)
|
||||
}
|
||||
return resolve(results)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async function write (device, str) {
|
||||
return new Promise ((resolve, reject) => {
|
||||
mcopy.arduino.serial[device].write(str, function (err, results) {
|
||||
if (err) {
|
||||
return reject(err)
|
||||
}
|
||||
//console.log('sent: ' + str)
|
||||
return resolve(results)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async function open (device) {
|
||||
return new Promise((resolve, reject) => {
|
||||
return mcopy.arduino.serial[device].open(error => {
|
||||
if (error) {
|
||||
return reject(error)
|
||||
}
|
||||
return resolve(true)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async function close (device) {
|
||||
return new Promise((resolve, reject) => {
|
||||
return mcopy.arduino.serial[device].close((err) => {
|
||||
if (err) {
|
||||
return reject(err)
|
||||
}
|
||||
return resolve(true)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/******
|
||||
Arduino handlers
|
||||
*******/
|
||||
|
@ -40,10 +92,13 @@ mcopy.arduino = {
|
|||
lock : false
|
||||
}
|
||||
|
||||
mcopy.arduino.enumerate = function (callback) {
|
||||
mcopy.arduino.enumerate = async function () {
|
||||
return new Promise( (resolve, reject) => {
|
||||
return SerialPort.list((err, ports) => {
|
||||
let matches = []
|
||||
SerialPort.list((err, ports) => {
|
||||
//console.dir(ports)
|
||||
if (err) {
|
||||
return reject(err)
|
||||
}
|
||||
ports.forEach(port => {
|
||||
if (mcopy.arduino.known.indexOf(port.comName) !== -1) {
|
||||
matches.push(port.comName)
|
||||
|
@ -52,44 +107,52 @@ mcopy.arduino.enumerate = function (callback) {
|
|||
}
|
||||
})
|
||||
if (matches.length === 0) {
|
||||
if (callback) { callback('No USB devices found'); }
|
||||
return reject('No USB devices found');
|
||||
} else if (matches.length > 0) {
|
||||
if (callback) { callback(null, matches); }
|
||||
return resolve(matches)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
//commands which respond to a sent char
|
||||
mcopy.arduino.send = function (serial, cmd, res) {
|
||||
mcopy.arduino.send = async function (serial, cmd, res) {
|
||||
const device = mcopy.arduino.alias[serial]
|
||||
if (!mcopy.arduino.lock) {
|
||||
let results
|
||||
if (mcopy.arduino.lock) {
|
||||
return false
|
||||
}
|
||||
mcopy.arduino.lock = true
|
||||
mcopy.arduino.queue[cmd] = res
|
||||
setTimeout(() => {
|
||||
mcopy.arduino.serial[device].write(cmd, (err, results) => {
|
||||
if (err) { console.log(err) }
|
||||
await delay(mcopy.cfg.arduino.serialDelay)
|
||||
try {
|
||||
results = await send(device, cmd)
|
||||
} catch (e) {
|
||||
return console.error(e)
|
||||
}
|
||||
mcopy.arduino.lock = false
|
||||
mcopy.arduino.timer = new Date().getTime()
|
||||
})
|
||||
}, mcopy.cfg.arduino.serialDelay)
|
||||
eventEmitter.emit('arduino_send', cmd)
|
||||
}
|
||||
};
|
||||
return await eventEmitter.emit('arduino_send', cmd)
|
||||
}
|
||||
|
||||
//send strings, after char triggers firmware to accept
|
||||
mcopy.arduino.string = function (serial, str) {
|
||||
mcopy.arduino.string = async function (serial, str) {
|
||||
const device = mcopy.arduino.alias[serial]
|
||||
setTimeout(function () {
|
||||
let writeSuccess
|
||||
await delay(mcopy.cfg.arduino.serialDelay)
|
||||
if (typeof mcopy.arduino.serial[device].fake !== 'undefined'
|
||||
&& mcopy.arduino.serial[device].fake) {
|
||||
mcopy.arduino.serial[device].string(str);
|
||||
return mcopy.arduino.serial[device].string(str)
|
||||
} else {
|
||||
mcopy.arduino.serial[device].write(str, function (err, results) {
|
||||
if (err) { console.log(err); }
|
||||
//console.log('sent: ' + str);
|
||||
});
|
||||
try {
|
||||
writeSuccess = await write(device, str)
|
||||
} catch (e) {
|
||||
return console.error(e)
|
||||
}
|
||||
}, mcopy.cfg.arduino.serialDelay);
|
||||
};
|
||||
return writeSuccess
|
||||
}
|
||||
}
|
||||
|
||||
//respond with same char over serial when done
|
||||
mcopy.arduino.end = function (data) {
|
||||
var end = new Date().getTime(),
|
||||
|
@ -98,32 +161,35 @@ mcopy.arduino.end = function (data) {
|
|||
mcopy.arduino.lock = false;
|
||||
//console.log('Command ' + data + ' took ' + ms + 'ms');
|
||||
mcopy.arduino.queue[data](ms); //execute callback
|
||||
eventEmitter.emit('arduino_end', data);
|
||||
delete mcopy.arduino.queue[data];
|
||||
eventEmitter.emit('arduino_end', data)
|
||||
delete mcopy.arduino.queue[data]
|
||||
} else {
|
||||
console.log('Received stray "' + data + '"'); //silent to user
|
||||
//console.log('Received stray "' + data + '"'); //silent to user
|
||||
}
|
||||
};
|
||||
mcopy.arduino.alias = function (serial, device) {
|
||||
console.log(`Making "${serial}" an alias of ${device}`)
|
||||
mcopy.arduino.alias[serial] = device
|
||||
}
|
||||
mcopy.arduino.connect = function (serial, device, confirm, callback) {
|
||||
mcopy.arduino.connect = async function (serial, device, confirm) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let connectSuccess
|
||||
mcopy.arduino.path[serial] = device;
|
||||
mcopy.arduino.alias[serial] = device;
|
||||
mcopy.arduino.serial[device] = new SerialPort(mcopy.arduino.path[serial], {
|
||||
autoOpen : false,
|
||||
baudRate: mcopy.cfg.arduino.baud,
|
||||
parser: parser
|
||||
});
|
||||
mcopy.arduino.serial[device].open(error => {
|
||||
if (error) {
|
||||
if (callback) { callback(error); }
|
||||
return console.log('failed to open: '+ error);
|
||||
} else {
|
||||
})
|
||||
try {
|
||||
connectSuccess = await open(device)
|
||||
} catch (e) {
|
||||
console.error('failed to open: ' + e)
|
||||
return reject(e)
|
||||
}
|
||||
console.log(`Opened connection with ${mcopy.arduino.path[serial]} as ${serial}`);
|
||||
if (!confirm) {
|
||||
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.end(d)
|
||||
|
@ -135,12 +201,10 @@ mcopy.arduino.connect = function (serial, device, confirm, callback) {
|
|||
mcopy.arduino.confirmEnd(d)
|
||||
})
|
||||
}
|
||||
if (callback) {
|
||||
callback(null, mcopy.arduino.path[serial])
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
return resolve(mcopy.arduino.path[serial])
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
mcopy.arduino.confirmExec = {};
|
||||
mcopy.arduino.confirmEnd = function (data) {
|
||||
|
@ -155,24 +219,32 @@ mcopy.arduino.confirmEnd = function (data) {
|
|||
mcopy.arduino.confirmExec(null, data);
|
||||
mcopy.arduino.confirmExec = {};
|
||||
}
|
||||
};
|
||||
mcopy.arduino.verify = function (callback) {
|
||||
}
|
||||
|
||||
mcopy.arduino.verify = async function () {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const device = mcopy.arduino.alias['connect']
|
||||
let writeSuccess
|
||||
mcopy.arduino.confirmExec = function (err, data) {
|
||||
if (data === mcopy.cfg.arduino.cmd.connect) {
|
||||
callback(null, true);
|
||||
return resolve(true)
|
||||
} else {
|
||||
return reject('Wrong data returned')
|
||||
}
|
||||
};
|
||||
setTimeout(function () {
|
||||
mcopy.arduino.serial[device].write(mcopy.cfg.arduino.cmd.connect, (err, results) => {
|
||||
if (err) {
|
||||
return console.log(err);
|
||||
}
|
||||
});
|
||||
}, mcopy.cfg.arduino.serialDelay);
|
||||
};
|
||||
mcopy.arduino.distinguish = function (callback) {
|
||||
await delay(mcopy.cfg.arduino.serialDelay)
|
||||
try {
|
||||
writeSuccess = await send(device, mcopy.cfg.arduino.cmd.connect)
|
||||
} catch (e) {
|
||||
return reject(e)
|
||||
}
|
||||
return resolve(writeSuccess)
|
||||
})
|
||||
}
|
||||
|
||||
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');
|
||||
|
@ -188,30 +260,33 @@ mcopy.arduino.distinguish = function (callback) {
|
|||
callback(null, 'projector,camera')
|
||||
}
|
||||
}
|
||||
setTimeout(function () {
|
||||
mcopy.arduino.serial[device].write(mcopy.cfg.arduino.cmd.mcopy_identifier, function (err, results) {
|
||||
if (err) {
|
||||
return console.log(err);
|
||||
await delay(mcopy.cfg.arduino.serialDelay)
|
||||
try {
|
||||
writeSuccess = await send(device, mcopy.cfg.arduino.cmd.mcopy_identifier)
|
||||
} catch (e) {
|
||||
return console.error(e)
|
||||
}
|
||||
});
|
||||
}, mcopy.cfg.arduino.serialDelay);
|
||||
return writeSuccess
|
||||
}
|
||||
|
||||
mcopy.arduino.close = function (callback) {
|
||||
let device = mcopy.arduino.alias['connect']
|
||||
mcopy.arduino.serial[device].close((err) => {
|
||||
if (callback) {
|
||||
callback(err)
|
||||
mcopy.arduino.close = async function (callback) {
|
||||
const device = mcopy.arduino.alias['connect']
|
||||
let closeSuccess
|
||||
try {
|
||||
closeSuccess = await close(device)
|
||||
} catch (e) {
|
||||
return console.error(e)
|
||||
}
|
||||
});
|
||||
return closeSuccess
|
||||
};
|
||||
|
||||
mcopy.arduino.fakeConnect = function (serial, callback) {
|
||||
mcopy.arduino.fakeConnect = async function (serial) {
|
||||
//console.log('Connecting to fake arduino...');
|
||||
const device = '/dev/fake'
|
||||
mcopy.arduino.alias[serial] = device
|
||||
mcopy.arduino.serial[device] = {
|
||||
write : function (cmd, res) {
|
||||
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
|
||||
|
@ -219,18 +294,18 @@ mcopy.arduino.fakeConnect = function (serial, callback) {
|
|||
timeout = t[cmd];
|
||||
if (typeof timeout === 'undefined') timeout = 10;
|
||||
mcopy.arduino.timer = +new Date();
|
||||
setTimeout(() => {
|
||||
mcopy.arduino.end(cmd)
|
||||
}, timeout)
|
||||
await delay(timeout)
|
||||
return await mcopy.arduino.end(cmd)
|
||||
})
|
||||
},
|
||||
string : function (str) {
|
||||
string : async function (str) {
|
||||
//do nothing
|
||||
return true
|
||||
},
|
||||
fake : true
|
||||
};
|
||||
//console.log('Connected to fake arduino! Not real! Doesn\'t exist!');
|
||||
if (callback) callback()
|
||||
return true
|
||||
}
|
||||
|
||||
if (typeof module !== 'undefined' && module.parent) {
|
||||
|
|
322
app/main.js
322
app/main.js
|
@ -36,6 +36,12 @@ let camera
|
|||
let server
|
||||
let menu
|
||||
|
||||
async function delay (ms) {
|
||||
return new Promise(resolve => {
|
||||
return setTimeout(resolve, ms)
|
||||
})
|
||||
}
|
||||
|
||||
//console.log(process.version)
|
||||
|
||||
mcopy.cfg = require('./data/cfg.json')
|
||||
|
@ -53,17 +59,18 @@ dev.listen = function () {
|
|||
})
|
||||
}
|
||||
|
||||
dev.enumerate = function (err, devices) {
|
||||
if (err) {
|
||||
dev.enumerate = async function () {
|
||||
let devices
|
||||
try{
|
||||
devices = await arduino.enumerate()
|
||||
} catch (err) {
|
||||
log.info(err, 'SERIAL', false, true)
|
||||
setTimeout(() =>{
|
||||
dev.all([])
|
||||
}, 1000)
|
||||
} else {
|
||||
await delay(1000)
|
||||
return dev.all([])
|
||||
}
|
||||
log.info(`Found ${devices.length} USB devices`, 'SERIAL', true, true)
|
||||
devices = dev.favor(devices)
|
||||
dev.all(devices)
|
||||
}
|
||||
return dev.all(devices)
|
||||
}
|
||||
|
||||
dev.favor = function (devices) {
|
||||
|
@ -88,162 +95,218 @@ dev.favor = function (devices) {
|
|||
return devices
|
||||
}
|
||||
|
||||
dev.distinguish = function (device, callback) {
|
||||
var connectCb = function (err, device) {
|
||||
if (err) {
|
||||
return console.error(err)
|
||||
dev.distinguish = async function (device, callback) {
|
||||
let connectSuccess
|
||||
let verifySuccess
|
||||
let type
|
||||
|
||||
try {
|
||||
connectSuccess = await arduino.connect('connect', device, true)
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return null
|
||||
}
|
||||
setTimeout(function () {
|
||||
arduino.verify(verifyCb)
|
||||
}, 2000);
|
||||
},
|
||||
verifyCb = function (err, success) {
|
||||
if (err) {
|
||||
return console.error(err)
|
||||
|
||||
await delay(2000)
|
||||
|
||||
try {
|
||||
verifySuccess = await arduino.verify()
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return null
|
||||
}
|
||||
|
||||
log.info(`Verified ${device} as mcopy device`, 'SERIAL', true, true)
|
||||
|
||||
setTimeout(function () {
|
||||
arduino.distinguish(distinguishCb);
|
||||
}, 1000);
|
||||
},
|
||||
distinguishCb = function (err, type) {
|
||||
if (err) {
|
||||
return console.error(err)
|
||||
}
|
||||
dev.remember('arduino', device, type)
|
||||
await delay(1000)
|
||||
|
||||
log.info(`Determined ${device} to be ${type}`, 'SERIAL', true, true)
|
||||
if (callback) { callback(err, type); }
|
||||
try {
|
||||
type = await arduino.distinguish()
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return null
|
||||
}
|
||||
arduino.connect('connect', device, true, connectCb)
|
||||
};
|
||||
|
||||
dev.remember('arduino', device, type)
|
||||
log.info(`Determined ${device} to be ${type}`, 'SERIAL', true, true)
|
||||
|
||||
return type
|
||||
}
|
||||
|
||||
dev.fakeProjector = async function () {
|
||||
dev.connected.projector = '/dev/fake'
|
||||
try {
|
||||
await arduino.fakeConnect('projector')
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
log.error(`Error connecting to fake PRONECTOR device`, 'SERIAL', true, true)
|
||||
return false
|
||||
}
|
||||
log.info('Connected to fake PROJECTOR device', 'SERIAL', true, true)
|
||||
return true
|
||||
}
|
||||
dev.fakeCamera = async function () {
|
||||
dev.connected.camera = '/dev/fake'
|
||||
try {
|
||||
await arduino.fakeConnect('camera')
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
log.error(`Error connecting to fake CAMERA device`, 'SERIAL', true, true)
|
||||
return false
|
||||
}
|
||||
log.info('Connected to fake CAMERA device', 'SERIAL', true, true)
|
||||
return true
|
||||
}
|
||||
dev.fakeLight = async function () {
|
||||
dev.connected.light = '/dev/fake'
|
||||
try {
|
||||
await arduino.fakeConnect('light')
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
log.error(`Error connecting to fake LIGHT device`, 'SERIAL', true, true)
|
||||
return false
|
||||
}
|
||||
log.info('Connected to fake LIGHT device', 'SERIAL', true, true)
|
||||
return true
|
||||
}
|
||||
|
||||
dev.connectDevice = async function (device, type) {
|
||||
let closeSuccess
|
||||
let connectSuccess
|
||||
try {
|
||||
closeSuccess = await arduino.close()
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return false
|
||||
}
|
||||
if (type === 'projector') {
|
||||
dev.connected.projector = device
|
||||
try {
|
||||
connectSuccess = await arduino.connect('projector', device, false)
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return false
|
||||
}
|
||||
log.info(`Connected to ${device} as PROJECTOR`, 'SERIAL', true, true)
|
||||
} else if (type === 'camera') {
|
||||
dev.connected.camera = device
|
||||
try {
|
||||
connectSuccess = await arduino.connect('camera', device, false)
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return false
|
||||
}
|
||||
log.info(`Connected to ${device} as CAMERA`, 'SERIAL', true, true)
|
||||
} else if (type === 'light') {
|
||||
dev.connected.light = device
|
||||
try {
|
||||
connectSuccess = await arduino.connect('light', device, false)
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return false
|
||||
}
|
||||
log.info(`Connected to ${device} as LIGHT`, 'SERIAL', true, true)
|
||||
} else if (type === 'projector,light') {
|
||||
dev.connected.projector = device
|
||||
dev.connected.light = device
|
||||
arduino.alias('light', device)
|
||||
try{
|
||||
connectSuccess = await arduino.connect('projector', device, false)
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return false
|
||||
}
|
||||
log.info(`Connected to ${device} as PROJECTOR + LIGHT`, 'SERIAL', true, true)
|
||||
|
||||
} else if (type === 'projector,camera,light') {
|
||||
dev.connected.projector = device
|
||||
dev.connected.camera = device
|
||||
dev.connected.light = device
|
||||
arduino.alias('camera', device)
|
||||
arduino.alias('light', device)
|
||||
try {
|
||||
connectSuccess = await arduino.connect('projector', device, false)
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return false
|
||||
}
|
||||
log.info(`Connected to ${device} as PROJECTOR + CAMERA + LIGHT`, 'SERIAL', true, true)
|
||||
|
||||
} else if (type === 'projector,camera') {
|
||||
dev.connected.projector = device
|
||||
dev.connected.camera = device
|
||||
arduino.alias('camera', device)
|
||||
try {
|
||||
connectSuccess = await arduino.connect('projector', device, false)
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return false
|
||||
}
|
||||
log.info(`Connected to ${device} as PROJECTOR`, 'SERIAL', true, true)
|
||||
}
|
||||
return connectSuccess
|
||||
}
|
||||
|
||||
//Cases for 1 or 2 arduinos connected
|
||||
dev.all = function (devices) {
|
||||
const connected = {
|
||||
dev.all = async function (devices) {
|
||||
dev.connected = {
|
||||
projector : false,
|
||||
camera : false,
|
||||
light : false
|
||||
}
|
||||
|
||||
let checklist = []
|
||||
var fakeProjector = function () {
|
||||
connected.projector = '/dev/fake'
|
||||
arduino.fakeConnect('projector', () => {
|
||||
log.info('Connected to fake PROJECTOR device', 'SERIAL', true, true)
|
||||
|
||||
})
|
||||
}
|
||||
var fakeCamera = function () {
|
||||
connected.camera = '/dev/fake'
|
||||
arduino.fakeConnect('camera', () => {
|
||||
log.info('Connected to fake CAMERA device', 'SERIAL', true, true)
|
||||
})
|
||||
}
|
||||
var fakeLight = function () {
|
||||
connected.light = '/dev/fake'
|
||||
arduino.fakeConnect('light', () => {
|
||||
log.info('Connected to fake LIGHT device', 'SERIAL', true, true)
|
||||
|
||||
})
|
||||
}
|
||||
var distinguishCb = function (device, type, cb) {
|
||||
arduino.close(() => {
|
||||
if (type === 'projector') {
|
||||
connected.projector = device
|
||||
arduino.connect('projector', device, false, () => {
|
||||
log.info(`Connected to ${device} as PROJECTOR`, 'SERIAL', true, true)
|
||||
cb()
|
||||
})
|
||||
} else if (type === 'camera') {
|
||||
connected.camera = device
|
||||
arduino.connect('camera', device, false, () => {
|
||||
log.info(`Connected to ${device} as CAMERA`, 'SERIAL', true, true)
|
||||
cb()
|
||||
})
|
||||
} else if (type === 'light') {
|
||||
connected.light = device
|
||||
arduino.connect('light', device, false, () => {
|
||||
log.info(`Connected to ${device} as LIGHT`, 'SERIAL', true, true)
|
||||
cb()
|
||||
})
|
||||
} else if (type === 'projector,light') {
|
||||
connected.projector = device
|
||||
connected.light = device
|
||||
arduino.connect('projector', device, false, () => {
|
||||
log.info(`Connected to ${device} as PROJECTOR + LIGHT`, 'SERIAL', true, true)
|
||||
cb()
|
||||
})
|
||||
arduino.alias('light', device)
|
||||
} else if (type === 'projector,camera,light') {
|
||||
connected.projector = device
|
||||
connected.camera = device
|
||||
connected.light = device
|
||||
arduino.connect('projector', device, false, () => {
|
||||
log.info(`Connected to ${device} as PROJECTOR + CAMERA + LIGHT`, 'SERIAL', true, true)
|
||||
cb()
|
||||
})
|
||||
arduino.alias('camera', device)
|
||||
arduino.alias('light', device)
|
||||
} else if (type === 'projector,camera') {
|
||||
connected.projector = device
|
||||
connected.camera = device
|
||||
arduino.connect('projector', device, false, () => {
|
||||
log.info(`Connected to ${device} as PROJECTOR`, 'SERIAL', true, true)
|
||||
cb()
|
||||
})
|
||||
arduino.alias('camera', device)
|
||||
} else {
|
||||
cb()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
checklist = devices.map(device => {
|
||||
return next => {
|
||||
dev.distinguish(device, (err, type) => {
|
||||
if (err) {
|
||||
/*await 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 next()
|
||||
return reject(err)
|
||||
}
|
||||
distinguishCb(device, type, next)
|
||||
})
|
||||
try {
|
||||
d = await dev.connectDevice(device, type)
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return reject(err)
|
||||
}
|
||||
return resolve(d)
|
||||
})
|
||||
})*/
|
||||
|
||||
async.series(checklist, () => {
|
||||
//done checking devices
|
||||
|
||||
let c = {}
|
||||
let p = {}
|
||||
let l = {}
|
||||
|
||||
if (!connected.projector) {
|
||||
fakeProjector()
|
||||
if (!dev.connected.projector) {
|
||||
await dev.fakeProjector()
|
||||
}
|
||||
p.arduino = connected.projector
|
||||
if (!connected.camera) {
|
||||
fakeCamera()
|
||||
p.arduino = dev.connected.projector
|
||||
if (!dev.connected.camera) {
|
||||
await dev.fakeCamera()
|
||||
}
|
||||
c.arduino = connected.camera
|
||||
c.arduino = dev.connected.camera
|
||||
|
||||
if (mcopy.settings.camera.intval) {
|
||||
c.intval = mcopy.settings.camera.intval
|
||||
console.dir(mcopy.settings.camera)
|
||||
setTimeout(() => {
|
||||
await delay(1000)
|
||||
cam.connectIntval(null, { connect : true, url : c.intval })
|
||||
}, 1000)
|
||||
}
|
||||
|
||||
if (!connected.light) {
|
||||
fakeLight()
|
||||
if (!dev.connected.light) {
|
||||
await dev.fakeLight()
|
||||
}
|
||||
l.arduino = connected.light
|
||||
|
||||
l.arduino = dev.connected.light
|
||||
|
||||
dev.ready(p, c, l)
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
dev.remember = function (which, device, type) {
|
||||
let deviceEntry
|
||||
|
@ -539,7 +602,7 @@ transfer.listen = function () {
|
|||
})
|
||||
}
|
||||
*/
|
||||
var init = function () {
|
||||
var init = async function () {
|
||||
|
||||
createWindow()
|
||||
createMenu()
|
||||
|
@ -560,9 +623,8 @@ var init = function () {
|
|||
settings.restore()
|
||||
mcopy.settings = settings.all()
|
||||
|
||||
setTimeout( () => {
|
||||
arduino.enumerate(dev.enumerate)
|
||||
}, 1000)
|
||||
await delay(1000)
|
||||
await dev.enumerate()
|
||||
}
|
||||
|
||||
app.on('ready', init)
|
||||
|
|
Loading…
Reference in New Issue