mcopy/app/main.js

486 lines
11 KiB
JavaScript
Raw Normal View History

var electron = require('electron'),
2016-04-11 14:49:57 +00:00
fs = require('fs'),
Menu = require('menu'),
MenuItem = require('menu-item'),
2016-04-11 14:49:57 +00:00
ipcMain = require('electron').ipcMain,
app = electron.app,
BrowserWindow = electron.BrowserWindow,
uuid = require('node-uuid'),
winston = require('winston'),
moment = require('moment'),
2016-04-14 04:17:42 +00:00
Q = require('q'),
2016-04-18 03:02:22 +00:00
mcopy = {},
2016-04-18 04:19:09 +00:00
mainWindow,
mscript,
arduino,
projector,
camera;
2016-04-11 06:01:26 +00:00
mcopy.cfg = {};
mcopy.cfgFile = './data/cfg.json';
mcopy.cfgInit = function () {
2016-04-18 04:19:09 +00:00
'use strict';
if (!fs.existsSync(mcopy.cfgFile)) {
console.log('Using default configuration...');
fs.writeFileSync(mcopy.cfgFile, fs.readFileSync('./data/cfg.json.default'));
}
mcopy.cfg = JSON.parse(fs.readFileSync(mcopy.cfgFile, 'utf8'));
};
mcopy.cfgStore = function () {
2016-04-18 04:19:09 +00:00
'use strict';
var data = JSON.stringify(mcopy.cfg);
fs.writeFileSync(mcopy.cfgFile, data, 'utf8');
};
2016-04-11 14:49:57 +00:00
var init = function () {
2016-04-13 03:14:27 +00:00
'use strict';
mcopy.cfgInit();
2016-04-11 14:49:57 +00:00
createWindow();
//createMenu();
2016-04-13 02:51:47 +00:00
log.init();
light.init();
proj.init();
2016-04-18 03:02:22 +00:00
cam.init();
arduino = require('./lib/mcopy-arduino.js')(mcopy.cfg);
2016-04-18 04:19:09 +00:00
mscript = require('./lib/mscript.js');
2016-04-13 04:06:19 +00:00
setTimeout(function () {
arduino.enumerate(enumerateDevices);
}, 1000);
};
var enumerateDevices = function (err, devices) {
'use strict';
if (err) {
log.info(err, 'SERIAL', false, true);
arduino.fakeConnect('projector', function () {
log.info('Connected to fake PROJECTOR device', 'SERIAL', true, true);
});
arduino.fakeConnect('camera', function () {
log.info('Connected to fake CAMERA device', 'SERIAL', true, true);
});
devicesReady('fake', 'fake');
} else {
log.info('Found ' + devices.length + ' USB devices', 'SERIAL', true, true);
distinguishDevices(devices);
}
};
var distinguishDevice = function (device, callback) {
'use strict';
var connectCb = function (err, device) {
if (err) {
return console.error(err);
}
setTimeout(function () {
arduino.verify(verifyCb);
}, 2000);
},
verifyCb = function (err, success) {
if (err) {
return console.error(err);
}
setTimeout(function () {
arduino.distinguish(distinguishCb);
}, 1000);
},
distinguishCb = function (err, type) {
if (err) {
return console.error(err);
}
if (callback) { callback(err, type); }
}
arduino.connect('connect', device, true, connectCb);
};
//Cases for 1 or 2 arduinos connected
var distinguishDevices = function (devices) {
'use strict';
var distinguishOne = function (err, type) {
arduino.close(function () {
if (type === 'projector') {
arduino.connect('projector', devices[0], false, function () {
log.info('Connected to ' + devices[0] + ' as PROJECTOR', 'SERIAL', true, true);
2016-04-14 04:17:42 +00:00
});
if (devices.length === 1) {
arduino.fakeConnect('camera', function () {
log.info('Connected to fake CAMERA device', 'SERIAL', true, true);
devicesReady(devices[0], 'fake');
});
}
} else if (type === 'camera') {
arduino.connect('camera', devices[0], false, function () {
log.info('Connected to ' + devices[0] + ' as CAMERA', 'SERIAL', true, true);
2016-04-13 04:06:19 +00:00
});
if (devices.length === 1) {
arduino.fakeConnect('projector', function () {
log.info('Connected to fake PROJECTOR device', 'SERIAL', true, true);
devicesReady('fake', devices[0]);
});
}
2016-04-13 04:06:19 +00:00
}
2016-04-19 01:52:38 +00:00
if (devices.length > 1) {
distinguishDevice(devices[1], distinguishTwo);
}
2016-04-13 04:06:19 +00:00
});
},
distinguishTwo = function (err, type) {
2016-04-19 01:52:38 +00:00
arduino.close(function () {
if (type === 'projector') {
arduino.connect('projector', devices[1], false, function () {
log.info('Connected to ' + devices[1] + ' as PROJECTOR', 'SERIAL', true, true);
devicesReady(devices[1], devices[0]);
2016-04-19 01:52:38 +00:00
});
} else if (type === 'camera') {
arduino.connect('camera', devices[1], false, function () {
log.info('Connected to ' + devices[1] + ' as CAMERA', 'SERIAL', true, true);
devicesReady(devices[0], devices[1]);
2016-04-19 01:52:38 +00:00
});
}
});
};
distinguishDevice(devices[0], distinguishOne);
2016-04-11 14:49:57 +00:00
};
2016-04-11 06:01:26 +00:00
var devicesReady = function (camera, projector) {
'use strict';
mainWindow.webContents.send('ready', {camera: camera, projector: projector });
};
2016-04-11 14:49:57 +00:00
var createMenu = function () {
var template = [
{
label: 'mcopy',
submenu: [
{
label: 'About mcopy',
selector: 'orderFrontStandardAboutPanel:'
},
{
type: 'separator'
},
{
label: 'Services',
submenu: []
},
{
type: 'separator'
},
{
label: 'Hide mcopy',
accelerator: 'Command+H',
selector: 'hide:'
},
{
label: 'Hide Others',
accelerator: 'Command+Shift+H',
selector: 'hideOtherApplications:'
},
{
label: 'Show All',
selector: 'unhideAllApplications:'
},
{
type: 'separator'
},
{
label: 'Quit',
accelerator: 'Command+Q',
selector: 'terminate:'
},
]
},
{
label: 'Edit',
submenu: [
{
label: 'Undo',
accelerator: 'Command+Z',
selector: 'undo:'
},
{
label: 'Redo',
accelerator: 'Shift+Command+Z',
selector: 'redo:'
},
{
type: 'separator'
},
{
label: 'Cut',
accelerator: 'Command+X',
selector: 'cut:'
},
{
label: 'Copy',
accelerator: 'Command+C',
selector: 'copy:'
},
{
label: 'Paste',
accelerator: 'Command+V',
selector: 'paste:'
},
{
label: 'Select All',
accelerator: 'Command+A',
selector: 'selectAll:'
}
]
},
{
label: 'View',
submenu: [
{
label: 'Reload',
accelerator: 'Command+R',
click: function() { getCurrentWindow().reload(); }
},
{
label: 'Toggle DevTools',
accelerator: 'Alt+Command+I',
click: function() { getCurrentWindow().toggleDevTools(); }
},
]
},
{
label: 'Window',
submenu: [
{
label: 'Minimize',
accelerator: 'Command+M',
selector: 'performMiniaturize:'
},
{
label: 'Close',
accelerator: 'Command+W',
selector: 'performClose:'
},
{
type: 'separator'
},
{
label: 'Bring All to Front',
selector: 'arrangeInFront:'
}
]
},
{
label: 'Help',
submenu: []
}
];
2016-04-11 06:01:26 +00:00
menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
2016-04-11 14:49:57 +00:00
};
2016-04-11 06:01:26 +00:00
2016-04-11 14:49:57 +00:00
var createWindow = function () {
2016-04-13 03:14:27 +00:00
'use strict';
mainWindow = new BrowserWindow({
width: 800,
height: 600,
minWidth : 800,
minHeight : 600
});
2016-04-11 14:49:57 +00:00
mainWindow.loadURL('file://' + __dirname + '/index.html');
//mainWindow.webContents.openDevTools();
2016-04-11 14:49:57 +00:00
mainWindow.on('closed', function() {
mainWindow = null;
});
2016-04-11 06:01:26 +00:00
}
2016-04-11 14:49:57 +00:00
app.on('ready', init);
2016-04-11 06:01:26 +00:00
app.on('window-all-closed', function () {
//if (process.platform !== 'darwin') {
2016-04-11 14:49:57 +00:00
app.quit();
//}
2016-04-11 06:01:26 +00:00
});
app.on('activate', function () {
2016-04-11 14:49:57 +00:00
if (mainWindow === null) {
createWindow();
}
});
2016-04-13 04:06:19 +00:00
var light = {};
light.init = function () {
'use strict';
light.listen();
};
light.listen = function () {
2016-04-13 04:06:19 +00:00
'use strict';
ipcMain.on('light', function(event, arg) {
light.set(arg.rgb, arg.id);
event.returnValue = true;
});
};
light.set = function (rgb, id) {
'use strict';
var str = rgb.join(',');
arduino.send('projector', mcopy.cfg.arduino.cmd.light, function (ms) {
2016-04-18 04:19:09 +00:00
light.end(rgb, id, ms);
2016-04-13 04:06:19 +00:00
});
arduino.string('projector', str);
};
2016-04-18 04:19:09 +00:00
light.end = function (rgb, id, ms) {
'use strict';
log.info('Light set to ' + rgb.join(','), 'LIGHT', true, true);
2016-04-18 04:19:09 +00:00
mainWindow.webContents.send('light', {rgb: rgb, id : id, ms: ms});
2016-04-13 04:06:19 +00:00
};
var proj = {};
proj.state = {
dir : true //default dir
};
proj.init = function () {
'use strict';
proj.listen();
};
proj.set = function (dir, id) {
'use strict';
var cmd;
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, function (ms) {
2016-04-18 03:02:22 +00:00
proj.end(cmd, id, ms);
});
};
proj.move = function (frame, id) {
'use strict';
arduino.send('projector', mcopy.cfg.arduino.cmd.projector, function (ms) {
2016-04-18 03:02:22 +00:00
proj.end(mcopy.cfg.arduino.cmd.projector, id, ms);
});
};
proj.listen = function () {
'use strict';
ipcMain.on('proj', function (event, arg) {
if (typeof arg.dir !== 'undefined') {
proj.set(arg.dir, arg.id);
} else if (typeof arg.frame !== 'undefined') {
proj.move(arg.frame, arg.id);
}
event.returnValue = true;
});
};
2016-04-18 03:02:22 +00:00
proj.end = function (cmd, id, ms) {
'use strict';
var message = '';
if (cmd === mcopy.cfg.arduino.cmd.proj_forward) {
message = 'Projector set to FORWARD';
} else if (cmd === mcopy.cfg.arduino.cmd.proj_backward) {
message = 'Projector set to BACKWARD';
} else if (cmd === mcopy.cfg.arduino.cmd.projector) {
message = 'Projector ';
if (proj.state.dir) {
message += 'ADVANCED';
} else {
message += 'REWOUND'
}
message += ' 1 frame';
}
2016-04-18 04:19:09 +00:00
log.info(message, 'PROJECTOR', true, true);
2016-04-18 03:02:22 +00:00
mainWindow.webContents.send('proj', {cmd: cmd, id : id, ms: ms});
};
var cam = {};
cam.state = {
dir : true //default dir
};
cam.init = function () {
'use strict';
cam.listen();
};
cam.set = function (dir, id) {
'use strict';
var cmd;
if (dir) {
cmd = mcopy.cfg.arduino.cmd.cam_forward;
} else {
cmd = mcopy.cfg.arduino.cmd.cam_backward;
}
cam.state.dir = dir;
arduino.send('camera', cmd, function (ms) {
2016-04-18 03:02:22 +00:00
cam.end(cmd, id, ms);
});
};
cam.move = function (frame, id) {
'use strict';
arduino.send('camera', mcopy.cfg.arduino.cmd.camera, function (ms) {
2016-04-18 03:02:22 +00:00
cam.end(mcopy.cfg.arduino.cmd.camera, id, ms);
});
};
cam.listen = function () {
'use strict';
ipcMain.on('cam', function (event, arg) {
if (typeof arg.dir !== 'undefined') {
cam.set(arg.dir, arg.id);
} else if (typeof arg.frame !== 'undefined') {
cam.move(arg.frame, arg.id);
}
event.returnValue = true;
});
};
cam.end = function (cmd, id, ms) {
'use strict';
var message = '';
if (cmd === mcopy.cfg.arduino.cmd.cam_forward) {
message = 'Camera set to FORWARD';
} else if (cmd === mcopy.cfg.arduino.cmd.cam_backward) {
message = 'Camera set to BACKWARD';
} else if (cmd === mcopy.cfg.arduino.cmd.camera) {
message = 'Camera ';
if (cam.state.dir) {
message += 'ADVANCED';
} else {
message += 'REWOUND'
}
message += ' 1 frame';
}
2016-04-18 04:19:09 +00:00
log.info(message, 'CAMERA', true, true);
2016-04-18 03:02:22 +00:00
mainWindow.webContents.send('cam', {cmd: cmd, id : id, ms: ms});
};
2016-04-13 02:51:47 +00:00
var log = {};
log.time = 'MM/DD/YY-HH:mm:ss';
log.transport = new (winston.Logger)({
2016-04-13 02:55:51 +00:00
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: './logs/mcopy.log' })
]
});
2016-04-13 02:51:47 +00:00
log.init = function () {
'use strict';
log.listen();
};
log.display = function (obj) {
2016-04-13 02:51:47 +00:00
'use strict';
2016-04-13 04:06:19 +00:00
mainWindow.webContents.send('log', obj);
2016-04-13 02:51:47 +00:00
};
log.listen = function () {
'use strict';
ipcMain.on('log', function (event, arg) {
log.transport.info('renderer', arg);
2016-04-13 02:51:47 +00:00
event.returnValue = true;
});
};
log.info = function (action, service, status, display) {
'use strict';
var obj = {
time : moment().format(log.time),
action : action,
service : service,
status : status
};
log.transport.info('main', obj);
if (display) {
log.display(obj);
}
2016-04-13 02:51:47 +00:00
};