mcopy/app/main.js

114 lines
2.4 KiB
JavaScript
Raw Normal View History

var electron = require('electron'),
2016-04-11 14:49:57 +00:00
fs = require('fs'),
Menu = require('menu'),
ipcMain = require('electron').ipcMain,
app = electron.app,
BrowserWindow = electron.BrowserWindow,
uuid = require('node-uuid'),
winston = require('winston'),
moment = require('moment'),
2016-04-11 14:49:57 +00:00
mcopy = {};
2016-04-11 06:01:26 +00:00
mcopy.cfg = JSON.parse(fs.readFileSync('./cfg.json', 'utf8'));
mcopy.arduino = require('./lib/mcopy-arduino.js')(mcopy.cfg);
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-11 14:49:57 +00:00
createWindow();
2016-04-13 02:51:47 +00:00
log.init();
2016-04-13 04:06:19 +00:00
setTimeout(function () {
mcopy.arduino.init(function (err, device) {
if (err) {
log.info(err, 'SERIAL', false, true);
} else {
log.info('Found device ' + device, 'SERIAL', true, true);
mcopy.arduino.connect(function () {
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-11 06:01:26 +00:00
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-11 14:49:57 +00:00
mainWindow = new BrowserWindow({width: 800, height: 600});
mainWindow.loadURL('file://' + __dirname + '/index.html');
2016-04-12 17:57:59 +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-11 14:49:57 +00:00
if (process.platform !== 'darwin') {
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();
}
});
ipcMain.on('light', function(event, arg) {
2016-04-13 04:06:19 +00:00
light.set(arg);
2016-04-11 14:49:57 +00:00
event.returnValue = true;
});
2016-04-13 02:51:47 +00:00
2016-04-13 04:06:19 +00:00
var light = {};
light.set = function (rgb) {
2016-04-13 04:06:19 +00:00
'use strict';
var str = rgb.join(',');
2016-04-13 04:06:19 +00:00
mcopy.arduino.send(mcopy.cfg.arduino.cmd.light, function () {
log.info('Light set to ' + str, 'LIGHT', true, true);
});
mcopy.arduino.string(str);
};
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
};