Remove a second require of the electron module

This commit is contained in:
mmcw-dev 2017-12-20 01:20:37 -05:00
parent 88fe5cf9eb
commit df4eeb9d14
1 changed files with 168 additions and 195 deletions

View File

@ -1,7 +1,7 @@
'use strict' 'use strict'
const electron = require('electron') const electron = require('electron')
const { Menu, MenuItem, ipcMain, BrowserWindow, app } = require('electron') const { Menu, MenuItem, ipcMain, BrowserWindow, app } = electron
const fs = require('fs') const fs = require('fs')
const winston = require('winston') const winston = require('winston')
const moment = require('moment') const moment = require('moment')
@ -20,54 +20,50 @@ let projector
let camera let camera
let log = {} let log = {}
console.log(process.version) //console.log(process.version)
mcopy.cfg = {} mcopy.cfg = {}
mcopy.cfgFile = './data/cfg.json' mcopy.cfgFile = './data/cfg.json'
mcopy.cfgInit = function () { mcopy.cfgInit = function () {
if (!fs.existsSync(mcopy.cfgFile)) { if (!fs.existsSync(mcopy.cfgFile)) {
console.log('Using default configuration...'); console.log('Using default configuration...')
fs.writeFileSync(mcopy.cfgFile, fs.readFileSync('./data/cfg.json.default')); fs.writeFileSync(mcopy.cfgFile, fs.readFileSync('./data/cfg.json.default'))
} }
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); fs.writeFileSync(mcopy.cfgFile, data, 'utf8')
fs.writeFileSync(mcopy.cfgFile, data, 'utf8'); }
};
var enumerateDevices = function (err, devices) { var enumerateDevices = function (err, devices) {
if (err) { if (err) {
log.info(err, 'SERIAL', false, true); log.info(err, 'SERIAL', false, true)
arduino.fakeConnect('projector', function () { arduino.fakeConnect('projector', () => {
log.info('Connected to fake PROJECTOR device', 'SERIAL', true, true); log.info('Connected to fake PROJECTOR device', 'SERIAL', true, true)
}); })
arduino.fakeConnect('camera', function () { arduino.fakeConnect('camera', () => {
log.info('Connected to fake CAMERA device', 'SERIAL', true, true); log.info('Connected to fake CAMERA device', 'SERIAL', true, true)
}); })
devicesReady('fake', 'fake'); devicesReady('fake', 'fake')
} else { } else {
log.info('Found ' + devices.length + ' USB devices', 'SERIAL', true, true); log.info('Found ' + devices.length + ' USB devices', 'SERIAL', true, true)
distinguishDevices(devices); distinguishDevices(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) {
return console.error(err); return console.error(err)
} }
setTimeout(function () { setTimeout(function () {
arduino.verify(verifyCb); arduino.verify(verifyCb)
}, 2000); }, 2000);
}, },
verifyCb = function (err, success) { verifyCb = function (err, success) {
if (err) { if (err) {
return console.error(err); return console.error(err)
} }
setTimeout(function () { setTimeout(function () {
arduino.distinguish(distinguishCb); arduino.distinguish(distinguishCb);
@ -75,65 +71,63 @@ var distinguishDevice = function (device, callback) {
}, },
distinguishCb = function (err, type) { distinguishCb = function (err, type) {
if (err) { if (err) {
return console.error(err); return console.error(err)
} }
if (callback) { callback(err, type); } if (callback) { callback(err, type); }
} }
arduino.connect('connect', device, true, connectCb); arduino.connect('connect', device, true, connectCb)
}; };
//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 distinguishOne = function (err, type) {
arduino.close(function () { arduino.close(() => {
if (type === 'projector') { if (type === 'projector') {
arduino.connect('projector', devices[0], false, function () { arduino.connect('projector', devices[0], false, () => {
log.info('Connected to ' + devices[0] + ' as PROJECTOR', 'SERIAL', true, true); log.info('Connected to ' + devices[0] + ' as PROJECTOR', 'SERIAL', true, true)
}); });
if (devices.length === 1) { if (devices.length === 1) {
arduino.fakeConnect('camera', function () { arduino.fakeConnect('camera', () => {
log.info('Connected to fake CAMERA device', 'SERIAL', true, true); log.info('Connected to fake CAMERA device', 'SERIAL', true, true)
devicesReady(devices[0], 'fake'); devicesReady(devices[0], 'fake')
}); });
} }
} else if (type === 'camera') { } else if (type === 'camera') {
arduino.connect('camera', devices[0], false, function () { arduino.connect('camera', devices[0], false, () => {
log.info('Connected to ' + devices[0] + ' as CAMERA', 'SERIAL', true, true); log.info('Connected to ' + devices[0] + ' as CAMERA', 'SERIAL', true, true)
}); });
if (devices.length === 1) { if (devices.length === 1) {
arduino.fakeConnect('projector', function () { 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]); devicesReady('fake', devices[0])
}); })
} }
} }
if (devices.length > 1) { if (devices.length > 1) {
distinguishDevice(devices[1], distinguishTwo); distinguishDevice(devices[1], distinguishTwo)
} }
}); })
}, },
distinguishTwo = function (err, type) { distinguishTwo = function (err, type) {
arduino.close(function () { arduino.close(() => {
if (type === 'projector') { if (type === 'projector') {
arduino.connect('projector', devices[1], false, function () { arduino.connect('projector', devices[1], false, () => {
log.info('Connected to ' + devices[1] + ' as PROJECTOR', 'SERIAL', true, true); log.info('Connected to ' + devices[1] + ' as PROJECTOR', 'SERIAL', true, true)
devicesReady(devices[1], devices[0]); devicesReady(devices[1], devices[0])
}); });
} else if (type === 'camera') { } else if (type === 'camera') {
arduino.connect('camera', devices[1], false, function () { arduino.connect('camera', devices[1], false, () => {
log.info('Connected to ' + devices[1] + ' as CAMERA', 'SERIAL', true, true); log.info('Connected to ' + devices[1] + ' as CAMERA', 'SERIAL', true, true)
devicesReady(devices[0], devices[1]); devicesReady(devices[0], devices[1])
}); });
} }
}); });
}; };
distinguishDevice(devices[0], distinguishOne); distinguishDevice(devices[0], distinguishOne)
}; };
var devicesReady = function (camera, projector) { var devicesReady = function (camera, projector) {
mainWindow.webContents.send('ready', {camera: camera, projector: projector })
mainWindow.webContents.send('ready', {camera: camera, projector: projector });
}; };
var createMenu = function () { var createMenu = function () {
@ -258,262 +252,241 @@ var createMenu = function () {
label: 'Help', label: 'Help',
submenu: [] submenu: []
} }
]; ]
menu = Menu.buildFromTemplate(template); menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu); Menu.setApplicationMenu(menu)
}; }
var createWindow = function () { var createWindow = function () {
mainWindow = new BrowserWindow({ mainWindow = new BrowserWindow({
width: 800, width: 800,
height: 600, height: 600,
minWidth : 800, minWidth : 800,
minHeight : 600 minHeight : 600
}); })
mainWindow.loadURL('file://' + __dirname + '/index.html'); mainWindow.loadURL('file://' + __dirname + '/index.html')
//mainWindow.webContents.openDevTools(); //mainWindow.webContents.openDevTools()
mainWindow.on('closed', function() { mainWindow.on('closed', () => {
mainWindow = null; mainWindow = null
}); });
} }
var light = {}; var light = {}
light.init = function () { light.init = function () {
light.listen()
light.listen();
}; };
light.listen = function () { light.listen = function () {
ipcMain.on('light', (event, arg) => {
ipcMain.on('light', function(event, arg) { light.set(arg.rgb, arg.id)
light.set(arg.rgb, arg.id); event.returnValue = true
event.returnValue = true; })
});
}; };
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, function (ms) { arduino.send('projector', mcopy.cfg.arduino.cmd.light, (ms) => {
light.end(rgb, id, ms); light.end(rgb, id, ms)
}); })
arduino.string('projector', str); arduino.string('projector', 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); mainWindow.webContents.send('light', {rgb: rgb, id : id, ms: ms})
mainWindow.webContents.send('light', {rgb: rgb, id : id, ms: ms});
}; };
var proj = {}; var proj = {}
proj.state = { proj.state = {
dir : true //default dir dir : true //default dir
}; };
proj.init = function () { proj.init = function () {
proj.listen()
proj.listen(); }
};
proj.set = function (dir, id) { proj.set = function (dir, id) {
var cmd
var cmd;
if (dir) { if (dir) {
cmd = mcopy.cfg.arduino.cmd.proj_forward; cmd = mcopy.cfg.arduino.cmd.proj_forward
} else { } else {
cmd = mcopy.cfg.arduino.cmd.proj_backward; cmd = mcopy.cfg.arduino.cmd.proj_backward
} }
proj.state.dir = dir; proj.state.dir = dir
arduino.send('projector', cmd, function (ms) { arduino.send('projector', cmd, (ms) => {
proj.end(cmd, id, ms); proj.end(cmd, id, ms)
}); })
}; }
proj.move = function (frame, id) { proj.move = function (frame, id) {
arduino.send('projector', mcopy.cfg.arduino.cmd.projector, (ms) => {
arduino.send('projector', mcopy.cfg.arduino.cmd.projector, function (ms) { proj.end(mcopy.cfg.arduino.cmd.projector, id, ms)
proj.end(mcopy.cfg.arduino.cmd.projector, id, ms); })
}); }
};
proj.listen = function () { proj.listen = function () {
ipcMain.on('proj', (event, arg) => {
ipcMain.on('proj', function (event, arg) {
if (typeof arg.dir !== 'undefined') { if (typeof arg.dir !== 'undefined') {
proj.set(arg.dir, arg.id); proj.set(arg.dir, arg.id)
} else if (typeof arg.frame !== 'undefined') { } else if (typeof arg.frame !== 'undefined') {
proj.move(arg.frame, arg.id); proj.move(arg.frame, arg.id)
} }
event.returnValue = true; event.returnValue = true
}); })
}; }
proj.end = function (cmd, id, ms) { proj.end = function (cmd, id, ms) {
var message = ''
var message = '';
if (cmd === mcopy.cfg.arduino.cmd.proj_forward) { if (cmd === mcopy.cfg.arduino.cmd.proj_forward) {
message = 'Projector set to FORWARD'; message = 'Projector set to FORWARD'
} else if (cmd === mcopy.cfg.arduino.cmd.proj_backward) { } else if (cmd === mcopy.cfg.arduino.cmd.proj_backward) {
message = 'Projector set to BACKWARD'; message = 'Projector set to BACKWARD'
} else if (cmd === mcopy.cfg.arduino.cmd.projector) { } else if (cmd === mcopy.cfg.arduino.cmd.projector) {
message = 'Projector '; message = 'Projector '
if (proj.state.dir) { if (proj.state.dir) {
message += 'ADVANCED'; message += 'ADVANCED'
} else { } else {
message += 'REWOUND' message += 'REWOUND'
} }
message += ' 1 frame'; message += ' 1 frame'
} }
log.info(message, 'PROJECTOR', true, true); log.info(message, 'PROJECTOR', true, true)
mainWindow.webContents.send('proj', {cmd: cmd, id : id, ms: ms}); mainWindow.webContents.send('proj', {cmd: cmd, id : id, ms: ms})
}; }
var cam = {}; var cam = {}
cam.state = { cam.state = {
dir : true //default dir dir : true //default dir
}; }
cam.init = function () { cam.init = function () {
cam.listen()
cam.listen(); }
};
cam.set = function (dir, id) { cam.set = function (dir, id) {
var cmd
var cmd;
if (dir) { if (dir) {
cmd = mcopy.cfg.arduino.cmd.cam_forward; cmd = mcopy.cfg.arduino.cmd.cam_forward
} else { } else {
cmd = mcopy.cfg.arduino.cmd.cam_backward; cmd = mcopy.cfg.arduino.cmd.cam_backward
} }
cam.state.dir = dir; cam.state.dir = dir
arduino.send('camera', cmd, function (ms) { arduino.send('camera', cmd, (ms) => {
cam.end(cmd, id, ms); cam.end(cmd, id, ms)
}); })
}; }
cam.move = function (frame, id) { cam.move = function (frame, id) {
arduino.send('camera', mcopy.cfg.arduino.cmd.camera, (ms) => {
arduino.send('camera', mcopy.cfg.arduino.cmd.camera, function (ms) { cam.end(mcopy.cfg.arduino.cmd.camera, id, ms)
cam.end(mcopy.cfg.arduino.cmd.camera, id, ms); })
}); }
};
cam.listen = function () { cam.listen = function () {
ipcMain.on('cam', (event, arg) => {
ipcMain.on('cam', function (event, arg) {
if (typeof arg.dir !== 'undefined') { if (typeof arg.dir !== 'undefined') {
cam.set(arg.dir, arg.id); cam.set(arg.dir, arg.id)
} else if (typeof arg.frame !== 'undefined') { } else if (typeof arg.frame !== 'undefined') {
cam.move(arg.frame, arg.id); cam.move(arg.frame, arg.id)
} }
event.returnValue = true; event.returnValue = true
}); });
}; };
cam.end = function (cmd, id, ms) { cam.end = function (cmd, id, ms) {
var message = ''
var message = '';
if (cmd === mcopy.cfg.arduino.cmd.cam_forward) { if (cmd === mcopy.cfg.arduino.cmd.cam_forward) {
message = 'Camera set to FORWARD'; message = 'Camera set to FORWARD'
} else if (cmd === mcopy.cfg.arduino.cmd.cam_backward) { } else if (cmd === mcopy.cfg.arduino.cmd.cam_backward) {
message = 'Camera set to BACKWARD'; message = 'Camera set to BACKWARD'
} else if (cmd === mcopy.cfg.arduino.cmd.camera) { } else if (cmd === mcopy.cfg.arduino.cmd.camera) {
message = 'Camera '; message = 'Camera '
if (cam.state.dir) { if (cam.state.dir) {
message += 'ADVANCED'; message += 'ADVANCED'
} else { } else {
message += 'REWOUND' message += 'REWOUND'
} }
message += ' 1 frame'; message += ' 1 frame'
} }
log.info(message, 'CAMERA', true, true); log.info(message, 'CAMERA', true, true)
mainWindow.webContents.send('cam', {cmd: cmd, id : id, ms: ms}); mainWindow.webContents.send('cam', {cmd: cmd, id : id, ms: ms})
}; };
log.time = 'MM/DD/YY-HH:mm:ss'; log.time = 'MM/DD/YY-HH:mm:ss'
log.transport = new (winston.Logger)({ log.transport = new (winston.Logger)({
transports: [ transports: [
new (winston.transports.Console)(), new (winston.transports.Console)(),
new (winston.transports.File)({ filename: './logs/mcopy.log' }) new (winston.transports.File)({ filename: './logs/mcopy.log' })
] ]
}); })
log.init = function () { log.init = function () {
log.listen()
log.listen();
}; };
log.display = function (obj) { log.display = function (obj) {
mainWindow.webContents.send('log', obj)
mainWindow.webContents.send('log', obj);
}; };
log.listen = function () { log.listen = function () {
ipcMain.on('log', (event, arg) => {
ipcMain.on('log', function (event, arg) { log.transport.info('renderer', arg)
log.transport.info('renderer', arg); event.returnValue = true
event.returnValue = true; })
});
}; };
log.info = function (action, service, status, display) { log.info = function (action, service, status, display) {
var obj = { var obj = {
time : moment().format(log.time), time : moment().format(log.time),
action : action, action : action,
service : service, service : service,
status : status status : status
}; }
log.transport.info('main', obj); log.transport.info('main', obj)
if (display) { if (display) {
log.display(obj); log.display(obj)
} }
}; };
var transfer = {}; var transfer = {}
transfer.init = function () { transfer.init = function () {
transfer.listen()
transfer.listen();
}; };
transfer.listen = function () { transfer.listen = function () {
ipcMain.on('transfer', (event, arg) => {
ipcMain.on('transfer', function (event, arg) {
var res = ''; var res = '';
//also turn on and off //also turn on and off
if (arg.action === 'enable') { if (arg.action === 'enable') {
capture.active = true; capture.active = true
res = capture.active; res = capture.active
} else if (arg.action === 'disable') { } else if (arg.action === 'disable') {
capture.active = false; capture.active = false
res = capture.active; res = capture.active
} else if (arg.action === 'start') { } else if (arg.action === 'start') {
capture.start(); capture.start()
} else if (arg.action === 'end') { } else if (arg.action === 'end') {
res = capture.end(); res = capture.end()
} }
event.returnValue = res; event.returnValue = res
}); })
}; }
var init = function () { var init = function () {
mcopy.cfgInit(); mcopy.cfgInit()
createWindow(); createWindow()
//createMenu(); //createMenu()
log.init(); log.init()
light.init(); light.init()
proj.init(); proj.init()
cam.init(); cam.init()
transfer.init(); transfer.init()
capture.init(); capture.init()
arduino = require('./lib/mcopy-arduino.js')(mcopy.cfg, ee); arduino = require('./lib/mcopy-arduino.js')(mcopy.cfg, ee)
mscript = require('./lib/mscript.js'); mscript = require('./lib/mscript.js')
setTimeout(function () { setTimeout( () => {
arduino.enumerate(enumerateDevices); arduino.enumerate(enumerateDevices)
}, 1000); }, 1000)
}; }
app.on('ready', init); app.on('ready', init)
app.on('window-all-closed', function () { app.on('window-all-closed', () => {
//if (process.platform !== 'darwin') { //if (process.platform !== 'darwin') {
app.quit(); app.quit();
//} //}
}); });
app.on('activate', function () { app.on('activate', () => {
if (mainWindow === null) { if (mainWindow === null) {
createWindow(); createWindow();
} }