前提条件
远程服务器STF已连接手机
官方脚本地址
stf.sh
#!/usr/bin/env bash
set -euxo pipefail
if [ "$DEVICE_SERIAL" == "" ]; then
echo "Please specify device serial using ENV['DEVICE_SERIAL']"
exit 1
fi
if [ "$STF_URL" == "" ]; then
echo "Please specify stf url using ENV['STF_URL']"
exit 1
fi
if [ "$STF_TOKEN" == "" ]; then
echo "Please specify stf token using using ENV['STF_TOKEN']"
exit 1
fi
function add_device
{
response=$(curl -X POST -H "Content-Type: application/json"
-H "Authorization: Bearer $STF_TOKEN"
--data "{"serial": "$DEVICE_SERIAL"}" $STF_URL/api/v1/user/devices)
success=$(echo "$response" | jq .success | tr -d '"')
description=$(echo "$response" | jq .description | tr -d '"')
if [ "$success" != "true" ]; then
echo "Failed because $description"
exit 1
fi
echo "Device $DEVICE_SERIAL added successfully"
}
function remote_connect
{
response=$(curl -X POST
-H "Authorization: Bearer $STF_TOKEN"
$STF_URL/api/v1/user/devices/$DEVICE_SERIAL/remoteConnect)
success=$(echo "$response" | jq .success | tr -d '"')
description=$(echo "$response" | jq .description | tr -d '"')
if [ "$success" != "true" ]; then
echo "Failed because $description"
exit 1
fi
remote_connect_url=$(echo "$response" | jq .remoteConnectUrl | tr -d '"')
adb connect $remote_connect_url
echo "Device $DEVICE_SERIAL remote connected successfully"
}
function remove_device
{
response=$(curl -X DELETE
-H "Authorization: Bearer $STF_TOKEN"
$STF_URL/api/v1/user/devices/$DEVICE_SERIAL)
success=$(echo "$response" | jq .success | tr -d '"')
description=$(echo "$response" | jq .description | tr -d '"')
if [ "$success" != "true" ]; then
echo "Failed because $description"
exit 1
fi
echo "Device $DEVICE_SERIAL removed successfully"
}
登录STF创建访问令牌
将地址复制到stf.sh脚本STF_URL的值
将生产的令牌复制到脚本STF_TOKEN的值
修改stf.sh
#!/usr/bin/env bash
set -euxo pipefail
# 默认设备serial序列号device
DEVICE_SERIAL=device
STF_URL=http://192.168.111.129:7100
STF_TOKEN=c6d87223ba024acaa6a9ab031d9cfb9ba108abf2d9ed40ca98e2f737e1714b25
#if [ "$DEVICE_SERIAL" == "" ]; then
# echo "Please specify device serial using ENV['DEVICE_SERIAL']"
# exit 1
#fi
#
#if [ "$STF_URL" == "" ]; then
# echo "Please specify stf url using ENV['STF_URL']"
# exit 1
#fi
#
#if [ "$STF_TOKEN" == "" ]; then
# echo "Please specify stf token using using ENV['STF_TOKEN']"
# exit 1
#fi
function add_device
{
response=$(curl -X POST -H "Content-Type: application/json"
-H "Authorization: Bearer $STF_TOKEN"
--data "{"serial": "$DEVICE_SERIAL"}" $STF_URL/api/v1/user/devices)
success=$(echo "$response" | jq .success | tr -d '"')
description=$(echo "$response" | jq .description | tr -d '"')
if [ "$success" != "true" ]; then
echo "Failed because $description"
exit 1
fi
echo "Device $DEVICE_SERIAL added successfully"
}
function remote_connect
{
response=$(curl -X POST
-H "Authorization: Bearer $STF_TOKEN"
$STF_URL/api/v1/user/devices/$DEVICE_SERIAL/remoteConnect)
success=$(echo "$response" | jq .success | tr -d '"')
description=$(echo "$response" | jq .description | tr -d '"')
if [ "$success" != "true" ]; then
echo "Failed because $description"
exit 1
fi
remote_connect_url=$(echo "$response" | jq .remoteConnectUrl | tr -d '"')
adb connect $remote_connect_url
echo "Device $DEVICE_SERIAL remote connected successfully"
}
function remove_device
{
response=$(curl -X DELETE
-H "Authorization: Bearer $STF_TOKEN"
$STF_URL/api/v1/user/devices/$DEVICE_SERIAL)
success=$(echo "$response" | jq .success | tr -d '"')
description=$(echo "$response" | jq .description | tr -d '"')
if [ "$success" != "true" ]; then
echo "Failed because $description"
exit 1
fi
echo "Device $DEVICE_SERIAL removed successfully"
}
执行脚本
[root@WzcWyt demo]# ll -rw-r--r-- 1 root root 2143 Nov 15 14:33 stf.sh [root@WzcWyt demo]# . stf.sh申请要用的设备,复制Serial序列号
申请要用的设备
[root@WzcWyt demo]# DEVICE_SERIAL=P7CDU18929008778
将远程设备添加到本地,这步需要到stf页面通过一下
[root@WzcWyt demo]# add_device [root@WzcWyt demo]# remote_connect
查看设备
[root@WzcWyt demo]# adb devices
下面就可以用这台设备做自动化了
删除远程设备
[root@WzcWyt demo]# remove_device方式2:使用Nodejs
stf-connect.js
var Swagger = require('swagger-client');
var SWAGGER_URL = 'https://stf.example.org/api/v1/swagger.json';
var AUTH_TOKEN = 'xx-xxxx-xx';
// Using Promise
var client = new Swagger({
url: SWAGGER_URL
, usePromise: true
, authorizations: {
accessTokenAuth: new Swagger.ApiKeyAuthorization('Authorization', 'Bearer ' + AUTH_TOKEN, 'header')
}
})
var serial = process.argv.slice(2)[0]
client.then(function(api) {
return api.devices.getDeviceBySerial({
serial: serial
, fields: 'serial,present,ready,using,owner'
}).then(function(res) {
// check if device can be added or not
var device = res.obj.device
if (!device.present || !device.ready || device.using || device.owner) {
console.log('Device is not available')
return
}
// add device to user
return api.user.addUserDevice({
device: {
serial: device.serial
, timeout: 900000
}
}).then(function(res) {
if (!res.obj.success) {
console.log('Could not add device')
return
}
// get remote connect url
return api.user.remoteConnectUserDeviceBySerial({
serial: device.serial
}).then(function(res) {
console.log(res.obj.remoteConnectUrl)
})
})
})
})
执行命令,添加设备
node stf-connect.js xxxx # $PROVIDR_IP:16829
stf-disconnect.js
var Swagger = require('swagger-client');
var SWAGGER_URL = 'https://stf.example.org/api/v1/swagger.json';
var AUTH_TOKEN = 'xx-xxxx-xx';
var client = new Swagger({
url: SWAGGER_URL
, usePromise: true
, authorizations: {
accessTokenAuth: new Swagger.ApiKeyAuthorization('Authorization', 'Bearer ' + AUTH_TOKEN, 'header')
}
})
var serial = process.argv.slice(2)[0]
client.then(function(api) {
return api.user.getUserDevices({
serial: serial
, fields: 'serial,present,ready,using,owner'
}).then(function(res) {
// check if user has that device or not
var devices = res.obj.devices
var hasDevice = false
devices.forEach(function(device) {
if(device.serial === serial) {
hasDevice = true;
}
})
if (!hasDevice) {
console.log('You do not own that device')
return
}
return api.user.deleteUserDeviceBySerial({
serial: serial
}).then(function(res) {
if (!res.obj.success) {
console.log('Could not disconnect')
return
}
console.log('Device disconnected successfully!')
})
})
})
执行命令,断开设备
node stf-disconnect.js xxxx # Device disconnected successfully!



