Skip to content

Commit

Permalink
TBonify mock local server
Browse files Browse the repository at this point in the history
  • Loading branch information
tillberg committed Jul 2, 2013
1 parent 50d7ce3 commit cbd444a
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 158 deletions.
14 changes: 11 additions & 3 deletions dev/autorun.js
Expand Up @@ -26,11 +26,17 @@ var tasks = [
watch: /^local\//,
exec: function () {
info('Local: Starting...');
exec('node', ['arduino.js'], { cwd: './local', pipe: true, name: 'local' }, function (err) {
var env = { LOCAL_SERVER: 'mock' };
var cmd = [
'killall --quiet -SIGHUP --user ' + process.env.USER + ' petbot_local',
'sleep 0.5', // wait for the petbot process to shut down gracefully
'node localserver'
].join(';');
exec('sh', ['-c', cmd], { cwd: 'local/', pipe: true, name: 'local', env: env }, function (err) {
info('Local: Exited.');
});
},
execOnStart: true
execOnStart: 500
},
{
name: 'reset remote',
Expand Down Expand Up @@ -82,7 +88,9 @@ _.each(tasks, function(opts) {
return false;
};
if (opts.execOnStart) {
opts.exec();
setTimeout(function () {
opts.exec();
}, typeof opts.execOnStart === 'number' ? opts.execOnStart : 0);
}
});

Expand Down
127 changes: 74 additions & 53 deletions local/localserver.js
@@ -1,61 +1,82 @@
process.title = 'petbot_local';

var config = require('../common/config');
var client = require('socket.io-client');
var _ = require('underscore');

var LocalServer = function () {
this.socket = client.connect(config.HOST);
this.BOARD_LOW = 0;
this.BOARD_HIGH = 1;
this.pinMap = {};
};

LocalServer.prototype.handleSignal = function (data) {
var self = this;
if (data.name == 'killswitch') {
self.kill();
} else {
var level = (data.active) ? self.BOARD_HIGH : self.BOARD_LOW;
self.write(data.name, level);
var T = require('tbone').tbone;
var tbone = T;

var servers = tbone.collections.base.make({
lookupById: true
});

var localServer = tbone.make();
T('servers', servers);

localServer('drive', function () {
var driving = _.filter(_.pluck(T('servers') || {}, 'drive'), function (drive) {
return drive && (!!drive.right || !!drive.forward);
});
return driving.length === 0 ? {} : driving[0];
});
localServer('awake', function () {
return _.keys(T('servers')).length > 0;
});

var nextId = 1;
var socket = client.connect(config.HOST, { transports: ['xhr-polling'] });
socket.on('connect', function() {
var me = tbone.make();
me('id', nextId++);
servers.add(me);

console.log('connected to remote server');
socket.emit('clientId', {id: config.DEVICE_ID});

// Expect the server to say "keepDriving" every ~500 ms.
var driveTimeout;
function stopDrivingAfterTimeout() {
if (driveTimeout) {
clearTimeout(driveTimeout);
}
driveTimeout = setTimeout(function () {
me('drive', {});
}, 1000);
}
}

LocalServer.prototype.sleep = function () {
console.log('local server is idle');
}

LocalServer.prototype.wake = function () {
console.log('local server is awake');
}

LocalServer.prototype.write = function (direction, level) {
console.log('direction ' + direction + ' at level ' + level);
}

LocalServer.prototype.kill = function () {
var self = this;
console.log('KILLSWITCH ENGAGE');
_.each(pinMap, function (k, v) {
self.write(v.pin, self.BOARD_LOW)
});
}

LocalServer.prototype.run = function () {
var self = this;
self.socket.on('connect', function() {
console.log('connected to remote server');
self.socket.emit('clientId', {id: config.DEVICE_ID});

self.wake();

self.socket.on('direction', function(data) {
self.handleSignal(data);
});
socket.on('drive', function(data) {
me('drive', data);
stopDrivingAfterTimeout();
});
socket.on('keepDriving', function () {
stopDrivingAfterTimeout();
});
T(function () {
if (!socket.disconnected) {
socket.emit('pins', localServer('pins'));
}
});
T(function () {
if (!socket.disconnected) {
socket.emit('drive', localServer('drive'));
}
});

self.socket.on('disconnect', function() {
console.log('disconnected from remote server');
self.sleep();
});
socket.on('disconnect', function() {
console.log('disconnected from remote server');
servers.remove(me);
});
};
});

require('./' + process.env.LOCAL_SERVER + '.js')(localServer);

module.exports = LocalServer;
// Gracefully shutdown on SIGHUP
process.on('SIGHUP', function () {
try {
socket.disconnect(function() {
process.exit(0);
});
} catch(e) {
process.exit(0);
}
});
15 changes: 15 additions & 0 deletions local/mock.js
@@ -0,0 +1,15 @@
var T = require('tbone').tbone;
var tbone = T;

module.exports = function (me) {
T(function () {
var drive = me('drive');
me('pins.right', drive.right === 1 ? 5 : 0);
me('pins.left', drive.right === -1 ? 5 : 0);
me('pins.forward', drive.forward === 1 ? 5 : 0);
me('pins.backward', drive.forward === -1 ? 5 : 0);
});
T(function () {
me('pins.ready', !!me('awake'));
});
};
19 changes: 12 additions & 7 deletions public/index.html
Expand Up @@ -6,7 +6,7 @@
<script src="/lib/less-1.3.3.min.js"></script>
</head>
<body>
<div tbone="tmpl topBannerWrap"></div>
<div tbone="tmpl topBanner"></div>
<div tbone="tmpl stuff"></div>
<div class="intro">
<h1>PETBOT v0.0.1</h1>
Expand All @@ -25,15 +25,20 @@ <h1>PETBOT v0.0.1</h1>
</div>
<div tbone="view direction"></div>

<script name="topBannerWrap" type="text/tbone-tmpl">
<div class="top-banner <%=localStatus.status%>">
<%=localStatus.message%>
</div>
<script name="topBanner" type="text/tbone-tmpl">
<%=status.message%>
</script>

<script name="stuff" type="text/tbone-tmpl">
<%=browserId%>
<%=browsers%>
browser ID: <%=browserId%>
<br>
browsers: <%=browsers%>
<br>
bots: <%=bots%>
<br>
Online: <%=onlineStr%>
<br>
Bot Online: <%=botOnlineStr%>
</script>

<script src="/socket.io/socket.io.js"></script>
Expand Down
84 changes: 54 additions & 30 deletions public/static/bindings.js
@@ -1,38 +1,15 @@
var socket = io.connect();
var socket = io.connect('/', {
transports: ['xhr-polling'],
'reconnection delay': 100,
'max reconnection attempts': 10000
});
socket.emit('clientId', {id: 'browser'});
// var keys = {
// 37: {
// name: 'left',
// active: false,
// mode: 'steer'
// },
// 38: {
// name: 'forward',
// active: false,
// mode: 'move'
// },
// 39: {
// name: 'right',
// active: false,
// mode: 'steer'
// },
// 40: {
// name: 'back',
// active: false,
// mode: 'move'
// }
// };
var keys = {
37: 'left',
38: 'forward',
39: 'right',
40: 'backward'
};
T('localStatus', {
online: false,
status: 'connecting',
message: 'Attempting to connect to PETBOT...'
});
socket.on('yourId', function (data) {
T('browserId', data);
});
Expand All @@ -42,9 +19,52 @@ socket.on('localStatus', function (data) {
socket.on('browsers', function (data) {
T('browsers', JSON.stringify(data));
});
socket.on('bots', function (data) {
T('bots', JSON.stringify(data));
});
socket.on('drive', function (data) {
T('actualDrive', data);
});
socket.on('botOnline', function (data) {
T('botOnline', data);
});
socket.on('connect', function () {
T('online', true);
});
socket.on('reconnect', function () {
T('online', true);
});
socket.on('disconnect', function () {
T('online', false);
});

T('onlineStr', function () {
return T('online') + '';
});
T('botOnlineStr', function () {
return T('botOnline') + '';
});
T('status', function () {
if (T('online')) {
if (T('botOnline')) {
return {
name: 'online',
message: 'Connected.'
};
} else {
return {
name: 'connecting',
message: 'Connecting to petbot...'
};
}
} else {
return {
name: 'offline',
message: 'Connecting to server...'
};
}
});

socket.on('reloadui', function () {
console.log('Reloading...')
location.reload();
Expand All @@ -70,10 +90,14 @@ T(function () {
setTimeout(function () {
T.toggle('isDrivingTimer');
}, 500);
} else {

}
});
tbone.createView('topBanner', function () {
this.$el.removeClass('online');
this.$el.removeClass('connecting');
this.$el.removeClass('offline');
this.$el.addClass(T('status.name'));
});
tbone.createView('direction', function () {
var $el = this.$el;
var isOtherDriver = !!T('actualDrive.browserId') && T('browserId') !== T('actualDrive.browserId');
Expand Down
7 changes: 6 additions & 1 deletion public/static/style.less
Expand Up @@ -70,7 +70,7 @@ a, a:visited, a:active {
.backward.left:before {
content: '\2199';
}
.top-banner {
.topBanner {
width: 100%;
height: 40px;
display: block;
Expand All @@ -81,6 +81,11 @@ a, a:visited, a:active {

&.online {
background: #bada55;
overflow: hidden;
height: 0px;
padding: 0px;
transition: height 1s, padding 1s;
transition-delay: 1s;
}

&.offline {
Expand Down

0 comments on commit cbd444a

Please sign in to comment.