Distinguish devices better and asynchronously
This commit is contained in:
parent
fa10ee6785
commit
c2a079d9af
139
app/main.js
139
app/main.js
|
@ -8,6 +8,7 @@ const moment = require('moment')
|
||||||
const uuid = require('uuid')
|
const uuid = require('uuid')
|
||||||
const Q = require('q')
|
const Q = require('q')
|
||||||
const events = require('events')
|
const events = require('events')
|
||||||
|
const async = require('async')
|
||||||
const ee = new events.EventEmitter()
|
const ee = new events.EventEmitter()
|
||||||
const capture = require('./lib/capture-report.js')(ee)
|
const capture = require('./lib/capture-report.js')(ee)
|
||||||
|
|
||||||
|
@ -32,7 +33,7 @@ mcopy.cfgInit = function () {
|
||||||
mcopy.cfg = JSON.parse(fs.readFileSync(mcopy.cfgFile, 'utf8'))
|
mcopy.cfg = JSON.parse(fs.readFileSync(mcopy.cfgFile, 'utf8'))
|
||||||
}
|
}
|
||||||
mcopy.cfgStore = function () {
|
mcopy.cfgStore = function () {
|
||||||
var data = JSON.stringify(mcopy.cfg)
|
var data = JSON.stringify(mcopy.cfg, null, '\t')
|
||||||
fs.writeFileSync(mcopy.cfgFile, data, 'utf8')
|
fs.writeFileSync(mcopy.cfgFile, data, 'utf8')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,6 +66,7 @@ var distinguishDevice = function (device, callback) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return console.error(err)
|
return console.error(err)
|
||||||
}
|
}
|
||||||
|
log.info(`Verified ${device} as mcopy device`, 'SERIAL', true, true)
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
arduino.distinguish(distinguishCb);
|
arduino.distinguish(distinguishCb);
|
||||||
}, 1000);
|
}, 1000);
|
||||||
|
@ -73,6 +75,7 @@ var distinguishDevice = function (device, callback) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return console.error(err)
|
return console.error(err)
|
||||||
}
|
}
|
||||||
|
log.info(`Determined ${device} to be ${type}`, 'SERIAL', true, true)
|
||||||
if (callback) { callback(err, type); }
|
if (callback) { callback(err, type); }
|
||||||
}
|
}
|
||||||
arduino.connect('connect', device, true, connectCb)
|
arduino.connect('connect', device, true, connectCb)
|
||||||
|
@ -80,54 +83,112 @@ var distinguishDevice = function (device, callback) {
|
||||||
|
|
||||||
//Cases for 1 or 2 arduinos connected
|
//Cases for 1 or 2 arduinos connected
|
||||||
var distinguishDevices = function (devices) {
|
var distinguishDevices = function (devices) {
|
||||||
var distinguishOne = function (err, type) {
|
var connected = {
|
||||||
arduino.close(() => {
|
projector : false,
|
||||||
if (type === 'projector') {
|
camera : false,
|
||||||
arduino.connect('projector', devices[0], false, () => {
|
light : false
|
||||||
log.info('Connected to ' + devices[0] + ' as PROJECTOR', 'SERIAL', true, true)
|
|
||||||
});
|
|
||||||
if (devices.length === 1) {
|
|
||||||
arduino.fakeConnect('camera', () => {
|
|
||||||
log.info('Connected to fake CAMERA device', 'SERIAL', true, true)
|
|
||||||
devicesReady(devices[0], 'fake')
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
} else if (type === 'camera') {
|
var checklist = []
|
||||||
arduino.connect('camera', devices[0], false, () => {
|
var fakeProjector = function () {
|
||||||
log.info('Connected to ' + devices[0] + ' as CAMERA', 'SERIAL', true, true)
|
connected.projector = '/dev/fake'
|
||||||
});
|
|
||||||
if (devices.length === 1) {
|
|
||||||
arduino.fakeConnect('projector', () => {
|
arduino.fakeConnect('projector', () => {
|
||||||
log.info('Connected to fake PROJECTOR device', 'SERIAL', true, true)
|
log.info('Connected to fake PROJECTOR device', 'SERIAL', true, true)
|
||||||
devicesReady('fake', devices[0])
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
var fakeCamera = function () {
|
||||||
if (devices.length > 1) {
|
connected.camera = '/dev/fake'
|
||||||
distinguishDevice(devices[1], distinguishTwo)
|
arduino.fakeConnect('camera', () => {
|
||||||
}
|
log.info('Connected to fake CAMERA device', 'SERIAL', true, true)
|
||||||
})
|
})
|
||||||
},
|
}
|
||||||
distinguishTwo = function (err, type) {
|
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(() => {
|
arduino.close(() => {
|
||||||
if (type === 'projector') {
|
if (type === 'projector') {
|
||||||
arduino.connect('projector', devices[1], false, () => {
|
connected.projector = device
|
||||||
log.info('Connected to ' + devices[1] + ' as PROJECTOR', 'SERIAL', true, true)
|
arduino.connect('projector', device, false, () => {
|
||||||
devicesReady(devices[1], devices[0])
|
log.info(`Connected to ${device} as PROJECTOR`, 'SERIAL', true, true)
|
||||||
});
|
cb()
|
||||||
|
})
|
||||||
} else if (type === 'camera') {
|
} else if (type === 'camera') {
|
||||||
arduino.connect('camera', devices[1], false, () => {
|
connected.camera = device
|
||||||
log.info('Connected to ' + devices[1] + ' as CAMERA', 'SERIAL', true, true)
|
arduino.connect('camera', device, false, () => {
|
||||||
devicesReady(devices[0], devices[1])
|
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()
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
};
|
}
|
||||||
distinguishDevice(devices[0], distinguishOne)
|
|
||||||
|
checklist = devices.map(device => {
|
||||||
|
return next => {
|
||||||
|
distinguishDevice(device, (err, type) => {
|
||||||
|
if (err) {
|
||||||
|
console.error(err)
|
||||||
|
return next()
|
||||||
|
}
|
||||||
|
distinguishCb(device, type, next)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
async.series(checklist, () => {
|
||||||
|
//done checking devices
|
||||||
|
if (!connected.projector) {
|
||||||
|
fakeProjector()
|
||||||
|
}
|
||||||
|
if (!connected.camera) {
|
||||||
|
fakeCamera()
|
||||||
|
}
|
||||||
|
if (!connected.light) {
|
||||||
|
fakeLight()
|
||||||
|
}
|
||||||
|
devicesReady(connected.projector, connected.camera, connected.light)
|
||||||
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
var devicesReady = function (camera, projector) {
|
var devicesReady = function (camera, projector, light) {
|
||||||
mainWindow.webContents.send('ready', {camera: camera, projector: projector })
|
mainWindow.webContents.send('ready', {camera: camera, projector: projector, light: light })
|
||||||
};
|
};
|
||||||
|
|
||||||
var createMenu = function () {
|
var createMenu = function () {
|
||||||
|
@ -285,10 +346,10 @@ light.listen = function () {
|
||||||
};
|
};
|
||||||
light.set = function (rgb, id) {
|
light.set = function (rgb, id) {
|
||||||
var str = rgb.join(',');
|
var str = rgb.join(',');
|
||||||
arduino.send('projector', mcopy.cfg.arduino.cmd.light, (ms) => {
|
arduino.send('light', mcopy.cfg.arduino.cmd.light, (ms) => {
|
||||||
light.end(rgb, id, ms)
|
light.end(rgb, id, ms)
|
||||||
})
|
})
|
||||||
arduino.string('projector', str)
|
arduino.string('light', str)
|
||||||
};
|
};
|
||||||
light.end = function (rgb, id, ms) {
|
light.end = function (rgb, id, ms) {
|
||||||
log.info('Light set to ' + rgb.join(','), 'LIGHT', true, true)
|
log.info('Light set to ' + rgb.join(','), 'LIGHT', true, true)
|
||||||
|
|
Loading…
Reference in New Issue