栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Linux、MacOS本地调用STF远程设备(stf.sh、stf-connect.js、stf-disconnect.js)

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux、MacOS本地调用STF远程设备(stf.sh、stf-connect.js、stf-disconnect.js)

前提条件
远程服务器STF已连接手机
官方脚本地址

方式1:使用sh脚本(本地为Linux或MacOS系统) 官方脚本

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!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/511998.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号