mirror of
https://github.com/DeviceFarmer/stf.git
synced 2026-04-18 16:13:24 +02:00
Update STFService. Now both the agent and the service use the same wire proto.
This commit is contained in:
@@ -161,18 +161,60 @@ module.exports = syrup.serial()
|
||||
return callService(util.format("-a '%s'", apk.stopAction))
|
||||
}
|
||||
|
||||
function sendInputEvent(event) {
|
||||
agent.writer.write(new apk.agentProto.InputEvent(event).encodeNB())
|
||||
function keyEvent(data) {
|
||||
return runAgentCommand(
|
||||
apk.wire.RequestType.KEYEVENT
|
||||
, new apk.wire.KeyEventRequest(data)
|
||||
)
|
||||
}
|
||||
|
||||
function type(text) {
|
||||
return runAgentCommand(
|
||||
apk.wire.RequestType.TYPE
|
||||
, new apk.wire.TypeRequest(text)
|
||||
)
|
||||
}
|
||||
|
||||
function paste(text) {
|
||||
return setClipboard(text)
|
||||
.then(function() {
|
||||
keyEvent({
|
||||
event: apk.wire.KeyEvent.PRESS
|
||||
, keyCode: adb.Keycode.KEYCODE_V
|
||||
, ctrlKey: true
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function wake() {
|
||||
return runAgentCommand(
|
||||
apk.wire.RequestType.WAKE
|
||||
, new apk.wire.WakeRequest()
|
||||
)
|
||||
}
|
||||
|
||||
function freezeRotation(rotation) {
|
||||
return runAgentCommand(
|
||||
apk.wire.RequestType.SET_ROTATION
|
||||
, new apk.wire.SetRotationRequest(rotation, true)
|
||||
)
|
||||
}
|
||||
|
||||
function thawRotation() {
|
||||
return runAgentCommand(
|
||||
apk.wire.RequestType.SET_ROTATION
|
||||
, new apk.wire.SetRotationRequest(0, false)
|
||||
)
|
||||
}
|
||||
|
||||
function version() {
|
||||
return runServiceCommand(
|
||||
apk.serviceProto.RequestType.VERSION
|
||||
, new apk.serviceProto.VersionRequest()
|
||||
apk.wire.RequestType.VERSION
|
||||
, new apk.wire.VersionRequest()
|
||||
)
|
||||
.timeout(10000)
|
||||
.then(function(data) {
|
||||
var response = apk.serviceProto.VersionResponse.decode(data)
|
||||
var response = apk.wire.VersionResponse.decode(data)
|
||||
if (response.success) {
|
||||
return response.version
|
||||
}
|
||||
@@ -182,12 +224,12 @@ module.exports = syrup.serial()
|
||||
|
||||
function unlock() {
|
||||
return runServiceCommand(
|
||||
apk.serviceProto.RequestType.SET_KEYGUARD_STATE
|
||||
, new apk.serviceProto.SetKeyguardStateRequest(false)
|
||||
apk.wire.RequestType.SET_KEYGUARD_STATE
|
||||
, new apk.wire.SetKeyguardStateRequest(false)
|
||||
)
|
||||
.timeout(10000)
|
||||
.then(function(data) {
|
||||
var response = apk.serviceProto.SetKeyguardStateResponse.decode(data)
|
||||
var response = apk.wire.SetKeyguardStateResponse.decode(data)
|
||||
if (!response.success) {
|
||||
throw new Error('Unable to unlock device')
|
||||
}
|
||||
@@ -196,12 +238,12 @@ module.exports = syrup.serial()
|
||||
|
||||
function lock() {
|
||||
return runServiceCommand(
|
||||
apk.serviceProto.RequestType.SET_KEYGUARD_STATE
|
||||
, new apk.serviceProto.SetKeyguardStateRequest(true)
|
||||
apk.wire.RequestType.SET_KEYGUARD_STATE
|
||||
, new apk.wire.SetKeyguardStateRequest(true)
|
||||
)
|
||||
.timeout(10000)
|
||||
.then(function(data) {
|
||||
var response = apk.serviceProto.SetKeyguardStateResponse.decode(data)
|
||||
var response = apk.wire.SetKeyguardStateResponse.decode(data)
|
||||
if (!response.success) {
|
||||
throw new Error('Unable to lock device')
|
||||
}
|
||||
@@ -210,12 +252,12 @@ module.exports = syrup.serial()
|
||||
|
||||
function acquireWakeLock() {
|
||||
return runServiceCommand(
|
||||
apk.serviceProto.RequestType.SET_WAKE_LOCK
|
||||
, new apk.serviceProto.SetWakeLockRequest(true)
|
||||
apk.wire.RequestType.SET_WAKE_LOCK
|
||||
, new apk.wire.SetWakeLockRequest(true)
|
||||
)
|
||||
.timeout(10000)
|
||||
.then(function(data) {
|
||||
var response = apk.serviceProto.SetWakeLockResponse.decode(data)
|
||||
var response = apk.wire.SetWakeLockResponse.decode(data)
|
||||
if (!response.success) {
|
||||
throw new Error('Unable to acquire WakeLock')
|
||||
}
|
||||
@@ -224,12 +266,12 @@ module.exports = syrup.serial()
|
||||
|
||||
function releaseWakeLock() {
|
||||
return runServiceCommand(
|
||||
apk.serviceProto.RequestType.SET_WAKE_LOCK
|
||||
, new apk.serviceProto.SetWakeLockRequest(false)
|
||||
apk.wire.RequestType.SET_WAKE_LOCK
|
||||
, new apk.wire.SetWakeLockRequest(false)
|
||||
)
|
||||
.timeout(10000)
|
||||
.then(function(data) {
|
||||
var response = apk.serviceProto.SetWakeLockResponse.decode(data)
|
||||
var response = apk.wire.SetWakeLockResponse.decode(data)
|
||||
if (!response.success) {
|
||||
throw new Error('Unable to release WakeLock')
|
||||
}
|
||||
@@ -238,12 +280,12 @@ module.exports = syrup.serial()
|
||||
|
||||
function identity() {
|
||||
return runServiceCommand(
|
||||
apk.serviceProto.RequestType.IDENTIFY
|
||||
, new apk.serviceProto.IdentifyRequest(options.serial)
|
||||
apk.wire.RequestType.IDENTIFY
|
||||
, new apk.wire.IdentifyRequest(options.serial)
|
||||
)
|
||||
.timeout(10000)
|
||||
.then(function(data) {
|
||||
var response = apk.serviceProto.IdentifyResponse.decode(data)
|
||||
var response = apk.wire.IdentifyResponse.decode(data)
|
||||
if (!response.success) {
|
||||
throw new Error('Unable to identify device')
|
||||
}
|
||||
@@ -252,15 +294,15 @@ module.exports = syrup.serial()
|
||||
|
||||
function setClipboard(text) {
|
||||
return runServiceCommand(
|
||||
apk.serviceProto.RequestType.SET_CLIPBOARD
|
||||
, new apk.serviceProto.SetClipboardRequest(
|
||||
apk.serviceProto.ClipboardType.TEXT
|
||||
apk.wire.RequestType.SET_CLIPBOARD
|
||||
, new apk.wire.SetClipboardRequest(
|
||||
apk.wire.ClipboardType.TEXT
|
||||
, text
|
||||
)
|
||||
)
|
||||
.timeout(10000)
|
||||
.then(function(data) {
|
||||
var response = apk.serviceProto.SetClipboardResponse.decode(data)
|
||||
var response = apk.wire.SetClipboardResponse.decode(data)
|
||||
if (!response.success) {
|
||||
throw new Error('Unable to set clipboard')
|
||||
}
|
||||
@@ -269,17 +311,17 @@ module.exports = syrup.serial()
|
||||
|
||||
function getClipboard() {
|
||||
return runServiceCommand(
|
||||
apk.serviceProto.RequestType.GET_CLIPBOARD
|
||||
, new apk.serviceProto.GetClipboardRequest(
|
||||
apk.serviceProto.ClipboardType.TEXT
|
||||
apk.wire.RequestType.GET_CLIPBOARD
|
||||
, new apk.wire.GetClipboardRequest(
|
||||
apk.wire.ClipboardType.TEXT
|
||||
)
|
||||
)
|
||||
.timeout(10000)
|
||||
.then(function(data) {
|
||||
var response = apk.serviceProto.GetClipboardResponse.decode(data)
|
||||
var response = apk.wire.GetClipboardResponse.decode(data)
|
||||
if (response.success) {
|
||||
switch (response.type) {
|
||||
case apk.serviceProto.ClipboardType.TEXT:
|
||||
case apk.wire.ClipboardType.TEXT:
|
||||
return response.text
|
||||
}
|
||||
}
|
||||
@@ -289,12 +331,12 @@ module.exports = syrup.serial()
|
||||
|
||||
function getBrowsers() {
|
||||
return runServiceCommand(
|
||||
apk.serviceProto.RequestType.GET_BROWSERS
|
||||
, new apk.serviceProto.GetBrowsersRequest()
|
||||
apk.wire.RequestType.GET_BROWSERS
|
||||
, new apk.wire.GetBrowsersRequest()
|
||||
)
|
||||
.timeout(15000)
|
||||
.then(function(data) {
|
||||
var response = apk.serviceProto.GetBrowsersResponse.decode(data)
|
||||
var response = apk.wire.GetBrowsersResponse.decode(data)
|
||||
if (response.success) {
|
||||
delete response.success
|
||||
return response
|
||||
@@ -305,12 +347,12 @@ module.exports = syrup.serial()
|
||||
|
||||
function getProperties(properties) {
|
||||
return runServiceCommand(
|
||||
apk.serviceProto.RequestType.GET_PROPERTIES
|
||||
, new apk.serviceProto.GetPropertiesRequest(properties)
|
||||
apk.wire.RequestType.GET_PROPERTIES
|
||||
, new apk.wire.GetPropertiesRequest(properties)
|
||||
)
|
||||
.timeout(15000)
|
||||
.then(function(data) {
|
||||
var response = apk.serviceProto.GetPropertiesResponse.decode(data)
|
||||
var response = apk.wire.GetPropertiesResponse.decode(data)
|
||||
if (response.success) {
|
||||
var mapped = Object.create(null)
|
||||
response.properties.forEach(function(property) {
|
||||
@@ -324,7 +366,7 @@ module.exports = syrup.serial()
|
||||
|
||||
function runServiceCommand(type, cmd) {
|
||||
var resolver = Promise.defer()
|
||||
service.writer.write(new apk.serviceProto.RequestEnvelope(
|
||||
service.writer.write(new apk.wire.RequestEnvelope(
|
||||
type
|
||||
, cmd.encodeNB()
|
||||
).encodeNB())
|
||||
@@ -332,6 +374,13 @@ module.exports = syrup.serial()
|
||||
return resolver.promise
|
||||
}
|
||||
|
||||
function runAgentCommand(type, cmd) {
|
||||
agent.writer.write(new apk.wire.RequestEnvelope(
|
||||
type
|
||||
, cmd.encodeNB()
|
||||
).encodeNB())
|
||||
}
|
||||
|
||||
return openAgent()
|
||||
.then(openService)
|
||||
.then(function() {
|
||||
@@ -345,58 +394,47 @@ module.exports = syrup.serial()
|
||||
])
|
||||
})
|
||||
.on(wire.KeyDownMessage, function(channel, message) {
|
||||
sendInputEvent({
|
||||
action: apk.agentProto.InputAction.KEYDOWN
|
||||
keyEvent({
|
||||
event: apk.wire.KeyEvent.DOWN
|
||||
, keyCode: keyutil.unwire(message.keyCode)
|
||||
})
|
||||
})
|
||||
.on(wire.KeyUpMessage, function(channel, message) {
|
||||
sendInputEvent({
|
||||
action: apk.agentProto.InputAction.KEYUP
|
||||
keyEvent({
|
||||
event: apk.wire.KeyEvent.UP
|
||||
, keyCode: keyutil.unwire(message.keyCode)
|
||||
})
|
||||
})
|
||||
.on(wire.KeyPressMessage, function(channel, message) {
|
||||
sendInputEvent({
|
||||
action: apk.agentProto.InputAction.KEYPRESS
|
||||
keyEvent({
|
||||
event: apk.wire.KeyEvent.PRESS
|
||||
, keyCode: keyutil.unwire(message.keyCode)
|
||||
})
|
||||
})
|
||||
.on(wire.TypeMessage, function(channel, message) {
|
||||
sendInputEvent({
|
||||
action: apk.agentProto.InputAction.TYPE
|
||||
, keyCode: 0
|
||||
, text: message.text
|
||||
})
|
||||
type(message.text)
|
||||
})
|
||||
.on(wire.RotateMessage, function(channel, message) {
|
||||
sendInputEvent({
|
||||
action: message.rotation < 0
|
||||
? apk.agentProto.InputAction.THAW_ROTATION
|
||||
: apk.agentProto.InputAction.FREEZE_ROTATION
|
||||
, rotation: message.rotation
|
||||
})
|
||||
if (message.rotation >= 0) {
|
||||
freezeRotation(message.rotation)
|
||||
}
|
||||
else {
|
||||
thawRotation()
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
unlock: unlock
|
||||
, lock: lock
|
||||
, acquireWakeLock: acquireWakeLock
|
||||
, releaseWakeLock: releaseWakeLock
|
||||
, identity: identity
|
||||
, paste: function(text) {
|
||||
return setClipboard(text)
|
||||
.then(function() {
|
||||
sendInputEvent({
|
||||
action: apk.agentProto.InputAction.KEYPRESS
|
||||
, keyCode: adb.Keycode.KEYCODE_V
|
||||
, ctrlKey: true
|
||||
})
|
||||
})
|
||||
}
|
||||
acquireWakeLock: acquireWakeLock
|
||||
, copy: getClipboard
|
||||
, getBrowsers: getBrowsers
|
||||
, getProperties: getProperties
|
||||
, identity: identity
|
||||
, lock: lock
|
||||
, paste: paste
|
||||
, releaseWakeLock: releaseWakeLock
|
||||
, unlock: unlock
|
||||
, version: version
|
||||
, wake: wake
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user