Skip to content

Commit

Permalink
Merge pull request #284 from zettajs/fix-issue-276
Browse files Browse the repository at this point in the history
VirtualDevice properly formats Actions before updating state
  • Loading branch information
AdamMagaluk committed Jan 20, 2016
2 parents f552c19 + a7088a1 commit 9f602c9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ var ZettaHttpServer = module.exports = function(zettaInstance, options) {
name = decodeURI(name);
self.zetta.log.emit('log', 'http_server', 'Websocket connection for peer "' + name + '" established.');

// Include ._env and ._loader on websocket, allows argo formatters to work used in virtual_device build actions.
var host = ws.upgradeReq.headers['host']
self.wireUpWebSocketForEvent(ws, host, '/servers/' + name);

if (self.peers[name] && self.peers[name].state !== PeerSocket.DISCONNECTED) {
// peer already connected or connecting
ws.close(4000, 'peer already connected');
Expand Down
5 changes: 5 additions & 0 deletions lib/virtual_device.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var util = require('util');
var ReadableStream = require('stream').Readable;
var EventEmitter = require('events').EventEmitter;
var rels = require('zetta-rels');
var buildDeviceActions = require('./api_formats/siren/device.siren').buildActions;

var VirtualStream = module.exports = function(topic, socket, options) {
ReadableStream.call(this, options);
Expand Down Expand Up @@ -41,6 +42,10 @@ var VirtualDevice = module.exports = function(entity, peerSocket) {
});

this._socket.on(logTopic, function(data) {
// Format data.actions to siren action format
data.actions = buildDeviceActions(data.properties.id, self._socket.ws._env, self._socket.ws._loader, data.transitions);
delete data.transitions;

self._update(data);
self._eventEmitter.emit(data.transition);
});
Expand Down
25 changes: 25 additions & 0 deletions test/test_virtual_device.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,31 @@ describe('Virtual Device', function() {
});
});

it('_update should always be called with data.actions in proper format', function(done) {
var called = 0;
var orig = vdevice._update;
vdevice._update = function(data) {
called++;
assert(Array.isArray(data.actions));
data.actions.forEach(function(action) {
assert(action.class);
assert(action.name);
assert(action.method);
assert(action.href);
assert(action.fields);
});
orig.apply(vdevice, arguments);

// _update is called twice on transitions. Once for the return of the transition http POST and again
// for the log topic update.
if (called === 2) {
done();
}
};

vdevice.call('change');
});

it('call should work without arguments', function(done) {
vdevice.call('change', function(err) {
assert.equal(err, null);
Expand Down

0 comments on commit 9f602c9

Please sign in to comment.