mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-08-21 12:55:22 +00:00
Add server (web/socket), create socket system, better helper loader.
- The Magic Mirror is now hosted via a express server, allowing you to load it from an external client (for debugging.) - It now includes a socket system to communicate between the node_helper and the client module. - node_helpers are now only loaded if the module is configured in the config.
This commit is contained in:
153
js/electron.js
153
js/electron.js
@@ -1,10 +1,15 @@
|
||||
'use strict';
|
||||
|
||||
//for searching modules
|
||||
//load modules
|
||||
const walk = require('walk');
|
||||
const fs = require('fs');
|
||||
const Server = require(__dirname + '/server.js');
|
||||
const spawn = require('child_process').spawn;
|
||||
|
||||
const electron = require('electron');
|
||||
|
||||
|
||||
// Config
|
||||
var config = {};
|
||||
// Module to control application life.
|
||||
const app = electron.app;
|
||||
// Module to create native browser window.
|
||||
@@ -15,79 +20,115 @@ const BrowserWindow = electron.BrowserWindow;
|
||||
let mainWindow;
|
||||
|
||||
function createWindow () {
|
||||
// Create the browser window.
|
||||
mainWindow = new BrowserWindow({width: 800, height: 600, fullscreen: true, "auto-hide-menu-bar": true, "node-integration": false});
|
||||
// Create the browser window.
|
||||
mainWindow = new BrowserWindow({width: 800, height: 600, fullscreen: true, "auto-hide-menu-bar": true, "node-integration": false});
|
||||
|
||||
// and load the index.html of the app.
|
||||
mainWindow.loadURL('file://' + __dirname + '../../index.html');
|
||||
// and load the index.html of the app.
|
||||
//mainWindow.loadURL('file://' + __dirname + '../../index.html');
|
||||
mainWindow.loadURL('http://localhost:' + config.port);
|
||||
|
||||
// Open the DevTools.
|
||||
//mainWindow.webContents.openDevTools();
|
||||
// Open the DevTools.
|
||||
//mainWindow.webContents.openDevTools();
|
||||
|
||||
// Emitted when the window is closed.
|
||||
mainWindow.on('closed', function() {
|
||||
// Dereference the window object, usually you would store windows
|
||||
// in an array if your app supports multi windows, this is the time
|
||||
// when you should delete the corresponding element.
|
||||
mainWindow = null;
|
||||
});
|
||||
// Emitted when the window is closed.
|
||||
mainWindow.on('closed', function() {
|
||||
// Dereference the window object, usually you would store windows
|
||||
// in an array if your app supports multi windows, this is the time
|
||||
// when you should delete the corresponding element.
|
||||
mainWindow = null;
|
||||
});
|
||||
}
|
||||
|
||||
//Walk module folder and get file names
|
||||
var module_loader = walk.walk(__dirname + '/../modules', { followLinks: false });
|
||||
function loadConfig (callback) {
|
||||
console.log("Loading config ...");
|
||||
var defaults = require(__dirname + '/defaults.js');
|
||||
var configFilename = __dirname + '/../config/config.js';
|
||||
|
||||
//for each file in modules
|
||||
module_loader.on('file', function(root, stat, next) {
|
||||
//if file is called node_helper.js load it
|
||||
if (stat.name == "node_helper.js"){
|
||||
var module = (root + '/' + stat.name).split("/");
|
||||
var moduleName = module[module.length-2];
|
||||
try {
|
||||
fs.accessSync(configFilename, fs.R_OK);
|
||||
var c = require(configFilename);
|
||||
var config = Object.assign(defaults, c);
|
||||
callback(config);
|
||||
} catch (e) {
|
||||
callback(defaults);
|
||||
}
|
||||
}
|
||||
|
||||
//start module as child
|
||||
var child = spawn('node', [root + '/' + stat.name])
|
||||
function loadModule(moduleName) {
|
||||
var helperPath = __dirname + '/../modules/' + moduleName + '/node_helper.js';
|
||||
|
||||
// Make sure the output is logged.
|
||||
child.stdout.on('data', function(data) {
|
||||
process.stdout.write(moduleName + ': ' + data);
|
||||
});
|
||||
try {
|
||||
fs.accessSync(helperPath, fs.R_OK);
|
||||
|
||||
child.stderr.on('data', function(data) {
|
||||
process.stdout.write(moduleName + ': ' + data);
|
||||
});
|
||||
|
||||
child.on('close', function(code) {
|
||||
console.log(moduleName + ' closing code: ' + code);
|
||||
});
|
||||
var child = spawn('node', [helperPath]);
|
||||
|
||||
// Make sure the output is logged.
|
||||
child.stdout.on('data', function(data) {
|
||||
process.stdout.write('[' + moduleName + '] ' + data);
|
||||
});
|
||||
|
||||
//Log module name
|
||||
|
||||
console.log("Started helper script for module " + moduleName + ".");
|
||||
}
|
||||
next();
|
||||
child.stderr.on('data', function(data) {
|
||||
process.stdout.write('[' + moduleName + '] ' + data);
|
||||
});
|
||||
|
||||
child.on('close', function(code) {
|
||||
console.log(moduleName + ' closing code: ' + code);
|
||||
});
|
||||
|
||||
//Log module name
|
||||
console.log("Started helper script for module: " + moduleName + ".");
|
||||
|
||||
} catch (e) {
|
||||
console.log("No helper found for module: " + moduleName + ".");
|
||||
}
|
||||
}
|
||||
|
||||
function loadModules(modules) {
|
||||
console.log("Loading module helpers ...");
|
||||
|
||||
for (var m in modules) {
|
||||
loadModule(modules[m]);
|
||||
}
|
||||
|
||||
console.log("All module helpers loaded.");
|
||||
}
|
||||
|
||||
loadConfig(function(c) {
|
||||
config = c;
|
||||
|
||||
var modules = [];
|
||||
|
||||
for (var m in config.modules) {
|
||||
var module = config.modules[m];
|
||||
if (modules.indexOf(module.module) === -1) {
|
||||
modules.push(module.module);
|
||||
}
|
||||
}
|
||||
|
||||
loadModules(modules);
|
||||
});
|
||||
|
||||
module_loader.on('end', function() {
|
||||
console.log("All helpers started.");
|
||||
});
|
||||
|
||||
// This method will be called when Electron has finished
|
||||
// initialization and is ready to create browser windows.
|
||||
app.on('ready', createWindow);
|
||||
app.on('ready', function() {
|
||||
var server = new Server(config, function() {
|
||||
createWindow();
|
||||
});
|
||||
});
|
||||
|
||||
// Quit when all windows are closed.
|
||||
app.on('window-all-closed', function () {
|
||||
// On OS X it is common for applications and their menu bar
|
||||
// to stay active until the user quits explicitly with Cmd + Q
|
||||
if (process.platform !== 'darwin') {
|
||||
app.quit();
|
||||
}
|
||||
// On OS X it is common for applications and their menu bar
|
||||
// to stay active until the user quits explicitly with Cmd + Q
|
||||
if (process.platform !== 'darwin') {
|
||||
app.quit();
|
||||
}
|
||||
});
|
||||
|
||||
app.on('activate', function () {
|
||||
// On OS X it's common to re-create a window in the app when the
|
||||
// dock icon is clicked and there are no other windows open.
|
||||
if (mainWindow === null) {
|
||||
createWindow();
|
||||
}
|
||||
// On OS X it's common to re-create a window in the app when the
|
||||
// dock icon is clicked and there are no other windows open.
|
||||
if (mainWindow === null) {
|
||||
createWindow();
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user