This repository has been archived by the owner on Sep 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
139 lines (113 loc) · 3.98 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var debug = require('debug')('orchestra:jsapi')
var Discover = require('harmonyhubjs-discover')
var Client = require('harmonyhubjs-client')
var EventEmitter = require('events').EventEmitter
var q = require('q')
var util = require('util')
var Universe = function () {
var self = this
self._discover = new Discover(61991)
self._discoveredHubs = []
self._clients = {}
self._discover.on('update', function (hubs) {
debug('received update event from harmonyhubjs-discover. there are ' + hubs.length + ' hubs')
self._discoveredHubs = hubs
self.emit('discoveredHubs', hubs)
})
self._discover.on('online', function (hub) {
self.emit('hubOnline', hub)
})
self._discover.on('offline', function (hub) {
self.emit('hubOffline', hub)
})
self._discover.start()
EventEmitter.call(self)
}
util.inherits(Universe, EventEmitter)
function createClientForHub (hub) {
var self = this
return Client(hub.ip)
.then(function (client) {
debug('created new client for hub with uuid ' + hub.uuid)
client._xmppClient.on('offline', function () {
debug('client for hub ' + hub.uuid + ' went offline. re-establish.')
self._clients[hub.uuid] = undefined
return createClientForHub.call(self, hub)
})
client.on('stateDigest', function (stateDigest) {
debug('got state digest. reemit it')
self.emit('stateDigest', {
hub: hub,
stateDigest: stateDigest
})
})
self._clients[hub.uuid] = client
return client
})
}
Universe.prototype.getDiscoveredHubs = function getDiscoveredHubs () {
debug('return list of ' + this._discoveredHubs.length + ' discovered hubs')
return q.when(this._discoveredHubs)
}
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 (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.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 === hubUuid) })
if (hubs.length > 0) {
return self.getClientForHub(hubs[0])
} else {
throw new Error('no hub with uuid ' + hubUuid + ' discovered yet')
}
})
}
Universe.prototype.getClientForHub = function getClientForHub (hub) {
debug('lookup client for hub ' + hub.uuid)
if (!this._clients[hub.uuid]) {
debug('request new client for hub ' + hub.uuid)
return createClientForHub.call(this, hub)
} else {
debug('return existing client for hub' + hub.uuid)
return q.when(this._clients[hub.uuid])
}
}
Universe.prototype.shutdown = function shutdown () {
debug('Shutdown universe')
var self = this
self._discover.stop()
Object
.keys(self._clients)
.map(function (key) { return self._clients[key] })
.forEach(function (client) { client.close() })
}
module.exports = Universe