mirror of
https://github.com/openstf/stf
synced 2025-10-05 10:39:25 +02:00
Logcat works now, but still kind of wondering how the messages should arrive.
This commit is contained in:
parent
40b493649b
commit
8da5b61289
7 changed files with 240 additions and 107 deletions
|
@ -19,71 +19,76 @@ module.exports = syrup.serial()
|
|||
.define(function(options, identity, input, router, push, sub, channels) {
|
||||
var log = logger.createLogger('device:plugins:group')
|
||||
, currentGroup = null
|
||||
, emitter = new events.EventEmitter()
|
||||
, plugin = new events.EventEmitter()
|
||||
|
||||
function joinGroup(newGroup, timeout) {
|
||||
if (currentGroup) {
|
||||
return Promise.reject(new grouputil.AlreadyGroupedError())
|
||||
plugin.get = Promise.method(function() {
|
||||
if (!currentGroup) {
|
||||
throw new grouputil.NoGroupError()
|
||||
}
|
||||
|
||||
currentGroup = newGroup
|
||||
return currentGroup
|
||||
})
|
||||
|
||||
log.info('Now owned by "%s"', currentGroup.email)
|
||||
log.info('Subscribing to group channel "%s"', currentGroup.group)
|
||||
plugin.join = function(newGroup, timeout) {
|
||||
return plugin.get()
|
||||
.then(function() {
|
||||
throw new grouputil.AlreadyGroupedError()
|
||||
})
|
||||
.catch(grouputil.NoGroupError, function() {
|
||||
currentGroup = newGroup
|
||||
|
||||
channels.register(currentGroup.group, timeout)
|
||||
sub.subscribe(currentGroup.group)
|
||||
log.info('Now owned by "%s"', currentGroup.email)
|
||||
log.info('Subscribing to group channel "%s"', currentGroup.group)
|
||||
|
||||
push.send([
|
||||
wireutil.global
|
||||
, wireutil.envelope(new wire.JoinGroupMessage(
|
||||
options.serial
|
||||
, currentGroup
|
||||
))
|
||||
])
|
||||
channels.register(currentGroup.group, timeout)
|
||||
sub.subscribe(currentGroup.group)
|
||||
|
||||
push.send([
|
||||
wireutil.global
|
||||
, wireutil.envelope(new wire.JoinGroupMessage(
|
||||
options.serial
|
||||
, currentGroup
|
||||
))
|
||||
])
|
||||
|
||||
plugin.emit('join', currentGroup)
|
||||
|
||||
return currentGroup
|
||||
})
|
||||
}
|
||||
|
||||
plugin.leave = function() {
|
||||
return plugin.get()
|
||||
.then(function(group) {
|
||||
log.info('No longer owned by "%s"', group.email)
|
||||
log.info('Unsubscribing from group channel "%s"', group.group)
|
||||
|
||||
channels.unregister(group.group)
|
||||
sub.unsubscribe(group.group)
|
||||
|
||||
push.send([
|
||||
wireutil.global
|
||||
, wireutil.envelope(new wire.LeaveGroupMessage(
|
||||
options.serial
|
||||
, group
|
||||
))
|
||||
])
|
||||
|
||||
currentGroup = null
|
||||
plugin.emit('leave', group)
|
||||
|
||||
return group
|
||||
})
|
||||
}
|
||||
|
||||
plugin.on('join', function() {
|
||||
input.acquireWakeLock()
|
||||
input.unlock()
|
||||
})
|
||||
|
||||
emitter.emit('join', currentGroup)
|
||||
|
||||
return Promise.resolve(currentGroup)
|
||||
}
|
||||
|
||||
function leaveGroup() {
|
||||
if (!currentGroup) {
|
||||
return Promise.reject(new grouputil.NotGroupedError())
|
||||
}
|
||||
|
||||
log.info('No longer owned by "%s"', currentGroup.email)
|
||||
log.info('Unsubscribing from group channel "%s"', currentGroup.group)
|
||||
|
||||
channels.unregister(currentGroup.group)
|
||||
sub.unsubscribe(currentGroup.group)
|
||||
|
||||
push.send([
|
||||
wireutil.global
|
||||
, wireutil.envelope(new wire.LeaveGroupMessage(
|
||||
options.serial
|
||||
, currentGroup
|
||||
))
|
||||
])
|
||||
|
||||
plugin.on('leave', function() {
|
||||
input.releaseWakeLock()
|
||||
input.lock()
|
||||
|
||||
var oldGroup = currentGroup
|
||||
currentGroup = null
|
||||
|
||||
emitter.emit('leave', oldGroup)
|
||||
|
||||
return Promise.resolve(oldGroup)
|
||||
}
|
||||
|
||||
channels.on('timeout', function(channel) {
|
||||
if (currentGroup && channel === currentGroup.group) {
|
||||
leaveGroup()
|
||||
}
|
||||
})
|
||||
|
||||
router
|
||||
|
@ -91,7 +96,7 @@ module.exports = syrup.serial()
|
|||
var reply = wireutil.reply(options.serial)
|
||||
grouputil.match(identity, message.requirements)
|
||||
.then(function() {
|
||||
return joinGroup(message.owner)
|
||||
return plugin.join(message.owner, message.timeout)
|
||||
})
|
||||
.then(function() {
|
||||
push.send([
|
||||
|
@ -116,7 +121,7 @@ module.exports = syrup.serial()
|
|||
var reply = wireutil.reply(options.serial)
|
||||
grouputil.match(identity, message.requirements)
|
||||
.then(function() {
|
||||
return leaveGroup()
|
||||
return plugin.leave()
|
||||
})
|
||||
.then(function() {
|
||||
push.send([
|
||||
|
@ -124,7 +129,7 @@ module.exports = syrup.serial()
|
|||
, reply.okay()
|
||||
])
|
||||
})
|
||||
.catch(grouputil.NotGroupedError, function(err) {
|
||||
.catch(grouputil.NoGroupError, function(err) {
|
||||
push.send([
|
||||
channel
|
||||
, reply.fail(err.message)
|
||||
|
@ -132,15 +137,19 @@ module.exports = syrup.serial()
|
|||
})
|
||||
})
|
||||
|
||||
lifecycle.observe(function() {
|
||||
if (currentGroup) {
|
||||
leaveGroup()
|
||||
return Promise.delay(500)
|
||||
}
|
||||
else {
|
||||
return true
|
||||
channels.on('timeout', function(channel) {
|
||||
if (currentGroup && channel === currentGroup.group) {
|
||||
plugin.leave()
|
||||
}
|
||||
})
|
||||
|
||||
return emitter
|
||||
lifecycle.observe(function() {
|
||||
return plugin.leave()
|
||||
.delay(500) // Make sure that the message gets sent
|
||||
.catch(grouputil.NoGroupError, function() {
|
||||
return true
|
||||
})
|
||||
})
|
||||
|
||||
return plugin
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue