Skip to content
This repository has been archived by the owner on Nov 23, 2017. It is now read-only.

Commit

Permalink
Merge branch 'feature/browser-abstraction' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Klaus Großmann committed Nov 29, 2015
2 parents 5c4fb08 + c43ee8b commit 5e2210f
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 75 deletions.
41 changes: 5 additions & 36 deletions client/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ var spawn = require('child_process').spawn;
var exec = require('child_process').exec;
var async = require('async');
var client = new net.Socket();
var chromeCtrl = require('chrome-remote-interface');
var chromeInstance;
var conf = require('config');
var debug = require('debug')('client:app');
var resolveCluster = require('./lib/mdnsClusterResolver');
Expand All @@ -22,28 +20,17 @@ var macAddress = require('./lib/macAddressResolver');
var Protocol = require('../lib/protocol');
var ServicePool = require('./lib/servicePool');
var Client = require('../lib/client');
var browser = require('./lib/browser');

var clientState = new Client({
id: macAddress,
url: conf.client.browser.url
});

clientState.on('needToNavigate', function (url) {
if (chromeInstance) {
chromeInstance.Page.navigate({
url: url
}, function (err) {
if (err) {
debug(err);
}
else {
clientState.emit('change');
}
});
}
else {
debug('Cannot connect to Chrome');
}
browser.navigate(url, function () {
clientState.emit('change');
});
});
clientState.on('needToSwitch', function (state) {
gpio.write(conf.client.io.tv, state, function (err) {
Expand Down Expand Up @@ -197,27 +184,9 @@ async.waterfall([
},
// setup Chrome
function (next) {
var args = conf.client.browser.args.concat([conf.client.browser.url]);
var chrome = spawn(conf.client.browser.path, args);

chrome.on('error', function (err) {
browser.init(conf.client.browser, function (err) {
next(err);
});
chrome.on('close', function (code) {
console.log('child process exited with code ' + code);
});

// wait to let chrome start gracefully
setTimeout(next, conf.client.browser.startupGracePeriod);
},
// setup Chrome Protocol
function (next) {
chromeCtrl(function (chrome) {
chromeInstance = chrome;
next();
}).on('error', function () {
next('Init chrome failed');
});
},
// start mdns Browser
function (next) {
Expand Down
113 changes: 113 additions & 0 deletions client/lib/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
var async = require('async');
var noop = require('../../lib/noop');
var spawn = require('child_process').spawn;
var logger = require('../../lib/logger')('browser.log');
var chromeCtrl = require('chrome-remote-interface');
var chromeConnection;
var chromeInstance;

var params = {};
var currentUrl;
var initialized = false;

function initChromeInstance(cb) {
chromeInstance = spawn(params.path, params.args.concat([params.url]));

chromeInstance.on('error', function (err) {
cb(err);
});
chromeInstance.on('close', function (code) {
logger.warn('Chrome instance exited with code ' + code);
initChromeInstance();
});
chromeInstance.stdout.on('data', function (data) {
logger.info(data.toString());
});

chromeInstance.stderr.on('data', function (data) {
logger.error(data.toString());
});

// wait to let chrome start gracefully
setTimeout(cb, params.startupGracePeriod);
}

module.exports = {
isInitialized: function () {
return initialized;
},
init: function (options, cb) {
cb = cb || noop;

if (!initialized) {
params = options || {};
currentUrl = params.url;

async.waterfall([
function (next) {
initChromeInstance(function (err) {
next(err);
});
},
// setup Chrome Protocol
function (next) {
chromeCtrl(function (chrome) {
chromeConnection = chrome;
next();
}).on('error', function () {
next('Init chrome failed');
});
}
], function (err, result) {
if (!err) {
initialized = true;
}
else {
logger.error(err);
}

cb(err, result);
});
}
},
navigate: function (url, cb) {
cb = cb || noop;

if (chromeConnection) {
chromeConnection.Page.navigate({
url: url
}, function (err) {
if (err) {
logger.error(err);
}
else {
cb();
}
});
}
else {
logger.error('Cannot connect to Chrome');
}
}
};

function exitHandler(options, err) {
if (options.cleanup) {
console.log('clean');
}
if (err) {
console.log(err.stack);
}
if (options.exit) {
process.exit();
}
}

//do something when app is closing
process.on('exit', exitHandler);

//catches ctrl+c event
process.on('SIGINT', exitHandler);

//catches uncaught exceptions
process.on('uncaughtException', exitHandler);
2 changes: 1 addition & 1 deletion client/lib/macAddressResolver.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var os = require('os');
// determine a mac address we may use to identify ourselves
module.exports = (function (interfaces) {
var testInterfaces = ['eth0', 'en0', 'wlan0'],
var testInterfaces = ['eth0', 'eth1', 'en0', 'wlan0', 'wlan1'],
mac = undefined;

testInterfaces.forEach(function (name) {
Expand Down
28 changes: 28 additions & 0 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var path = require('path');
var mkdirp = require('mkdirp');
var rootLogDir = (function (logPath) {
mkdirp.sync(logPath);

return logPath;
})(path.join(process.cwd(), 'logs'));

var winston = require('winston');

/**
* @param {String} filename
* @return {winston.Logger}
*/
module.exports = function (filename) {
return new (winston.Logger)({
transports: [
new (winston.transports.Console)({
timestamp: true
}),
new (winston.transports.File)({
filename: path.join(rootLogDir, filename),
zippedArchive: true,
timestamp: true
})
]
});
};
8 changes: 3 additions & 5 deletions master/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var express = require('express');
var hbs = require('hbs');
//var browserify = require('browserify-middleware');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
Expand Down Expand Up @@ -54,9 +53,9 @@ app.use(cookieParser());
//app.use('/js', browserify(path.join(__dirname, 'public/js')));

if (process.env.NODE_ENV != 'production') {
var serve = require("staticr/serve");
app.use(serve(require("./static-routes/browserify")));
app.use(serve(require("./static-routes/less")));
var serve = require('staticr/serve');
app.use(serve(require('./static-routes/browserify')));
app.use(serve(require('./static-routes/less')));
}

app.use('/fonts', express.static(path.join(__dirname, '../node_modules/font-awesome/fonts')));
Expand All @@ -71,7 +70,6 @@ app.use(function (req, res, next) {
next(err);
});


// error handlers

// development error handler
Expand Down
26 changes: 16 additions & 10 deletions master/lib/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var Q = require('q');
var async = require('async');
var os = require('os');
var net = require('net');
var mdns = require('mdns');
Expand Down Expand Up @@ -124,21 +125,26 @@ module.exports = {
},
/**
* @param {String} url
* @param {String} clientId
* @param {String} groupId
* @param {Function} cb
*/
changePanicUrl: function (url, clientId, cb) {
changeGroupPanicUrl: function (url, groupId, cb) {
try {
var client = this.getClient(clientId, true);
client.panicUrl = url;
var group = this.getGroup(groupId);
var clients = clientPool.getClientsByGroupId(groupId);

if (client.up) {
protocol.requestSetPanicUrl(url, client.socket, cb);
}
else {
cb(null, client);
}
group.panicUrl = url;

async.each(clients, function (client, done) {
client.panicUrl = url;

if (client.up) {
protocol.requestSetPanicUrl(url, client.socket, done);
}
else {
done(null);
}
}, cb);
} catch (e) {
cb(e);
}
Expand Down
20 changes: 10 additions & 10 deletions master/lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@ function Socket(server) {
});
});

socket.on(socketEvents.CLIENT_CHANGEPANICURL, function (data) {
debug([socketEvents.CLIENT_CHANGEPANICURL, data]);

serverService.changePanicUrl(data.panicUrl, data.id, function (err) {
if (err) {
socketError({id: data.id, message: 'Error on changePanicUrl!'});
}
});
});

socket.on(socketEvents.CLIENT_CHANGEPANICSTATE, function (data) {
debug([socketEvents.CLIENT_CHANGEPANICSTATE, data]);

Expand Down Expand Up @@ -92,6 +82,16 @@ function Socket(server) {
});
});

socket.on(socketEvents.GROUP_CHANGEPANICURL, function (data) {
debug([socketEvents.GROUP_CHANGEPANICURL, data]);

serverService.changeGroupPanicUrl(data.panicUrl, data.id, function (err) {
if (err) {
socketError({id: data.id, message: 'Error on changeGroupPanicUrl!'});
}
});
});

socket.on(socketEvents.GROUP_CHANGESETTINGS, function (data) {
debug([socketEvents.GROUP_CHANGESETTINGS, data]);

Expand Down
2 changes: 2 additions & 0 deletions master/lib/socketEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module.exports = {
CLIENT_MOVED: 'client:moved',
GROUP_CHANGETITLE: 'group:changetitle',
GROUP_CHANGESCHEDULES: 'group:changeschedules',
GROUP_CHANGEPANICURL: 'group:changepanicurl',
GROUP_CHANGEPANICSTATE: 'group:changepanicstate',
GROUP_ADD: 'group:new',
ERROR: 'error'
};
12 changes: 3 additions & 9 deletions master/public/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ $.fn.findGroup = function () {
$(function () {
var $waiting = $('.clients-waiting');

window.addEventListener('hashchange', showClients, false);
$(window).on('hashchange', showClients);

socket.on('connect', function () {
socket.emit(socketEvents.CLIENTS_GET);
Expand Down Expand Up @@ -203,7 +203,7 @@ $(function () {
submitValues.id = item.value;
break;
case 'panicUrl':
panicUrl = item.value;
submitValues.panicUrl = item.value;
break;
case 'on':
submitValues.schedules.on.push(item.value);
Expand All @@ -215,13 +215,7 @@ $(function () {
}
});

$clients.each(function () {
var $client = $(this);
var data = {id: $client.attr('client')};
data['panicUrl'] = panicUrl;
socket.emit(socketEvents.CLIENT_CHANGEPANICURL, data);
});

socket.emit(socketEvents.GROUP_CHANGEPANICURL, submitValues);
socket.emit(socketEvents.GROUP_CHANGESCHEDULES, submitValues);
})
.on('click', '[data-action]', function (event) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"serve-favicon": "^2.2.1",
"sinon": "^1.15.4",
"socket.io": "^1.3.5",
"touch": "0.0.3"
"touch": "0.0.3",
"winston": "^1.0.1"
},
"devDependencies": {
"autoprefixer-core": "^5.2.1",
Expand Down
Loading

0 comments on commit 5e2210f

Please sign in to comment.