Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

Commit

Permalink
feat(action): Provide executeAction
Browse files Browse the repository at this point in the history
The new function executeAction allows to execute an action of an activities control group.
  • Loading branch information
swissmanu committed Jan 20, 2016
1 parent 188ddc4 commit f24a6ef
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
})

Expand All @@ -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])
}
}
Expand Down

0 comments on commit f24a6ef

Please sign in to comment.