2016-04-12 03:05:05 +00:00
|
|
|
var electron = require('electron'),
|
2016-04-11 14:49:57 +00:00
|
|
|
fs = require('fs'),
|
|
|
|
Menu = require('menu'),
|
2016-04-17 21:55:23 +00:00
|
|
|
MenuItem = require('menu-item'),
|
|
|
|
notifier = require('node-notifier'),
|
2016-04-11 14:49:57 +00:00
|
|
|
ipcMain = require('electron').ipcMain,
|
|
|
|
app = electron.app,
|
|
|
|
BrowserWindow = electron.BrowserWindow,
|
|
|
|
uuid = require('node-uuid'),
|
2016-04-13 03:08:56 +00:00
|
|
|
winston = require('winston'),
|
|
|
|
moment = require('moment'),
|
2016-04-14 04:17:42 +00:00
|
|
|
Q = require('q'),
|
2016-04-11 14:49:57 +00:00
|
|
|
mcopy = {};
|
2016-04-11 06:01:26 +00:00
|
|
|
|
2016-04-15 22:16:14 +00:00
|
|
|
mcopy.cfg = {};
|
|
|
|
mcopy.cfgFile = './data/cfg.json';
|
|
|
|
mcopy.cfgInit = function () {
|
|
|
|
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 () {
|
|
|
|
var data = JSON.stringify(mcopy.cfg);
|
|
|
|
fs.writeFileSync(mcopy.cfgFile, data, 'utf8');
|
|
|
|
};
|
|
|
|
|
|
|
|
var arduino;
|
2016-04-12 03:05:05 +00:00
|
|
|
|
|
|
|
var mainWindow;
|
2016-04-11 06:01:26 +00:00
|
|
|
|
2016-04-11 14:49:57 +00:00
|
|
|
var init = function () {
|
2016-04-13 03:14:27 +00:00
|
|
|
'use strict';
|
2016-04-15 22:16:14 +00:00
|
|
|
mcopy.cfgInit();
|
2016-04-11 14:49:57 +00:00
|
|
|
createWindow();
|
2016-04-17 21:55:23 +00:00
|
|
|
//createMenu();
|
2016-04-13 02:51:47 +00:00
|
|
|
log.init();
|
2016-04-17 21:55:23 +00:00
|
|
|
light.init();
|
2016-04-15 22:16:14 +00:00
|
|
|
arduino = require('./lib/mcopy-arduino.js')(mcopy.cfg);
|
2016-04-13 04:06:19 +00:00
|
|
|
setTimeout(function () {
|
2016-04-15 22:17:59 +00:00
|
|
|
arduino.init(function (err, device) {
|
2016-04-13 04:06:19 +00:00
|
|
|
if (err) {
|
|
|
|
log.info(err, 'SERIAL', false, true);
|
2016-04-15 22:17:59 +00:00
|
|
|
arduino.fakeConnect(function () {
|
2016-04-14 04:17:42 +00:00
|
|
|
log.info('Connected to fake USB device', 'SERIAL', true, true);
|
|
|
|
});
|
2016-04-13 04:06:19 +00:00
|
|
|
} else {
|
|
|
|
log.info('Found device ' + device, 'SERIAL', true, true);
|
2016-04-15 22:17:59 +00:00
|
|
|
arduino.connect(function () {
|
2016-04-13 04:06:19 +00:00
|
|
|
log.info('Connected to device ' + device, 'SERIAL', true, true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 1000);
|
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 createMenu = function () {
|
2016-04-17 21:55:23 +00:00
|
|
|
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
|
|
|
|
2016-04-17 21:55:23 +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';
|
2016-04-17 21:55:23 +00:00
|
|
|
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');
|
2016-04-17 21:55:23 +00:00
|
|
|
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 () {
|
2016-04-17 21:55:23 +00:00
|
|
|
//if (process.platform !== 'darwin') {
|
2016-04-11 14:49:57 +00:00
|
|
|
app.quit();
|
2016-04-17 21:55:23 +00:00
|
|
|
//}
|
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 = {};
|
2016-04-17 21:55:23 +00:00
|
|
|
light.init = function () {
|
|
|
|
'use strict';
|
|
|
|
light.listen();
|
|
|
|
};
|
|
|
|
light.listen = function () {
|
2016-04-13 04:06:19 +00:00
|
|
|
'use strict';
|
2016-04-17 21:55:23 +00:00
|
|
|
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(',');
|
2016-04-15 22:16:14 +00:00
|
|
|
arduino.send(mcopy.cfg.arduino.cmd.light, function () {
|
2016-04-17 21:55:23 +00:00
|
|
|
light.end(rgb, id);
|
2016-04-13 04:06:19 +00:00
|
|
|
});
|
2016-04-15 22:16:14 +00:00
|
|
|
arduino.string(str);
|
2016-04-17 21:55:23 +00:00
|
|
|
};
|
|
|
|
light.end = function (rgb, id) {
|
|
|
|
'use strict';
|
|
|
|
log.info('Light set to ' + rgb.join(','), 'LIGHT', true, true);
|
|
|
|
mainWindow.webContents.send('light', {rgb: rgb, id : id});
|
2016-04-13 04:06:19 +00:00
|
|
|
};
|
|
|
|
|
2016-04-13 02:51:47 +00:00
|
|
|
var log = {};
|
2016-04-13 03:08:56 +00:00
|
|
|
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();
|
|
|
|
};
|
2016-04-13 03:08:56 +00:00
|
|
|
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) {
|
2016-04-13 03:08:56 +00:00
|
|
|
log.transport.info('renderer', arg);
|
2016-04-13 02:51:47 +00:00
|
|
|
event.returnValue = true;
|
|
|
|
});
|
2016-04-13 03:08:56 +00:00
|
|
|
};
|
|
|
|
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
|
|
|
};
|