Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VirtualDevice properly formats Actions before updating state #284

Merged
merged 2 commits into from
Jan 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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