From f24a6ef27c7f2fddd4366f58e5db381baf2af0cf Mon Sep 17 00:00:00 2001 From: Manuel Alabor Date: Wed, 20 Jan 2016 21:23:27 +0100 Subject: [PATCH] feat(action): Provide executeAction The new function executeAction allows to execute an action of an activities control group. --- index.js | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index c377cd7..0e59e52 100644 --- a/index.js +++ b/index.js @@ -47,7 +47,8 @@ function createClientForHub (hub) { client.on('stateDigest', function (stateDigest) { debug('got state digest. reemit it') self.emit('stateDigest', { - hub: hub, stateDigest: stateDigest + hub: hub, + stateDigest: stateDigest }) }) @@ -61,52 +62,65 @@ Universe.prototype.getDiscoveredHubs = function getDiscoveredHubs () { return q.when(this._discoveredHubs) } -Universe.prototype.getActivitiesForHubWithUuid = function getActivitiesForHubWithUuid (uuid) { - return this.getClientForHubWithUuid(uuid) +Universe.prototype.getActivitiesForHubWithUuid = function getActivitiesForHubWithUuid (hubUuid) { + debug('get activities for hub with uuid ' + hubUuid) + return this.getClientForHubWithUuid(hubUuid) .then(function (client) { return client.getActivities() }) } -Universe.prototype.getCurrentActivityForHub = function getCurrentActivityForHub (uuid) { - return this.getClientForHubWithUuid(uuid) +Universe.prototype.getCurrentActivityForHub = function getCurrentActivityForHub (hubUuid) { + debug('get current activity for hub with uuid ' + hubUuid) + return this.getClientForHubWithUuid(hubUuid) .then(function (client) { return client.getCurrentActivity() }) } Universe.prototype.startActivityForHub = function startActivityForHub (hubUuid, activityId) { + debug('start activity ' + activityId + ' for hub ' + hubUuid) return this.getClientForHubWithUuid(hubUuid) .then(function (client) { return client.startActivity(activityId) }) } -Universe.prototype.getClientForHubWithUuid = function getClientForHubWithUuid (uuid) { - debug('getClientForHubWithUuid(' + uuid + ')') +Universe.prototype.executeAction = function executeAction (hubUuid, action) { + debug('execute action ' + action + ' for hub ' + hubUuid) + var encodedAction = 'action=' + action.replace(/\:/g, '::') + ':status=press' + + return this.getClientForHubWithUuid(hubUuid) + .then(function (client) { + return client.send('holdAction', encodedAction) + }) +} + +Universe.prototype.getClientForHubWithUuid = function getClientForHubWithUuid (hubUuid) { + debug('get client for hub ' + hubUuid) var self = this return this.getDiscoveredHubs() .then(function (hubs) { - hubs = hubs.filter(function (hub) { return (hub.uuid === uuid) }) + hubs = hubs.filter(function (hub) { return (hub.uuid === hubUuid) }) if (hubs.length > 0) { return self.getClientForHub(hubs[0]) } else { - throw new Error('no hub with uuid ' + uuid + ' discovered yet') + throw new Error('no hub with uuid ' + hubUuid + ' discovered yet') } }) } Universe.prototype.getClientForHub = function getClientForHub (hub) { - debug('getClientForHub(' + hub.uuid + ')') + debug('lookup client for hub ' + hub.uuid) if (!this._clients[hub.uuid]) { - debug('request new client for hub with uuid ' + hub.uuid) + debug('request new client for hub ' + hub.uuid) return createClientForHub.call(this, hub) } else { - debug('return existing client for hub with uuid ' + hub.uuid) + debug('return existing client for hub' + hub.uuid) return q.when(this._clients[hub.uuid]) } }