下面以魅族MX4 手机为例,以启动QQ音乐APP搜索“我的家乡”播放两分钟为例(目前用的是绝对坐标),来看Total Control 脚本REST API使用。
注意:
1、需在手机上安装 QQ音乐APP 打开一次跳过登录
2、nodejs 安装 request 模块 (npm install --save request)
3、TC 必须开启 sigma输入法
源码执行步骤:
1、获取Token
2、获取设备名称
3、启动 QQ 音乐
4、搜索歌曲 “我的家乡”
5、播放 “我的家乡”
6、两分钟后关闭 QQ音乐
7、执行: node app.js
源码:
//安装 request 模块 npm install --save request
const request = require('request');
const deviceUrlBase = 'http://127.0.0.1:8090/TotalControl/v1';
const runAppName = 'com.tencent.qqmusic'; //要运行 APP 的包名(第二步方法获得)
//REST API 账户:sigma 密码:jiehua
//console.log(_str);
//结果: c2lnbWE6amllaHVh
//开始
request({
url: deviceUrlBase + '/login',
method: "GET",
json: true,
headers: {
"Authorization": "c2lnbWE6amllaHVh",
}
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var _token = body.value.token;
console.log('获取Token:'+_token);
getDevice(_token);
}
});
//1、获取设备名称
function getDevice(token) {
console.log('1、获取设备名称');
//1、获取设备名称
request.get(deviceUrlBase + '/devices/main?token='+token, {json: true}, function (error, response, body) {
if (!error && response.statusCode == 200) {
//设备名称
var device = body.id;
console.log(device);
console.log('2、获取手机上当前运行APP 的包名');
//2、获取手机上当前运行APP 的包名
//用上面的方法获取 APP 包名 com.tencent.qqmusic
startApp(device,token);
}
});
}
//3、启动 QQ 音乐
function startApp(device, token) {
var runAppUrl = deviceUrlBase + '/devices/' + device + '/apps/' + runAppName + '?state=active&token='+token;
//参数
var requestData="";
console.log('3、启动 QQ 音乐');
//3、启动 QQ 音乐
request.post({
url: runAppUrl,
method: "POST",
json: true,
headers: {
"content-type": "application/json",
},
body: requestData
},
function(error, response, body) {
if (!error && response.statusCode == 200) {
//启动成功
if (body.status == true) {
console.log('在'+ device +'启动成功' + runAppName);
//延迟10秒执行,避开QQ音乐开屏广告
setTimeout(function() {
clickInputBox(device, token);
}, 10000);
} else {
//TODO
//启动失败
console.log('启动失败');
}
}
});
}
//4、获取输入框焦点
function clickInputBox(device, token) {
var clickInputUrl = deviceUrlBase + '/devices/' + device + '/screen/inputs';
//点击的坐标
var clickParam = {
"token":token,
"x":"613",//0.5321
"y":"247",//0.1234
"state":"press"
};
console.log('4、获取输入框焦点');
console.log(clickInputUrl+'---'+ JSON.stringify(clickParam));
request.post({
url: clickInputUrl,
method: "POST",
json: true,
headers: {
"content-type": "application/json",
},
body: clickParam
},
function(error, response, body) {
console.log(body);
if (!error && response.statusCode == 200) {
if (body.status == true) {
console.log('获取输入框焦点成功---');
setTimeout(function() {
inputText(device, token);
}, 2000);
} else {
console.log('获取输入框焦点失败');
}
}
});
}
//5、输入文本
function inputText(device, token) {
var TextUrl = deviceUrlBase + '/devices/' + device + '/screen/texts';
var textParam = {
"token":token,
"text":"我的家乡"
};
console.log('5、输入文本');
console.log(TextUrl+'---'+JSON.stringify(textParam));
request.post({
url: TextUrl,
method: "POST",
json: true,
headers: {
"content-type": "application/json",
},
body: textParam
},
function(error, response, body) {
if (!error && response.statusCode == 200) {
if (body.status == true) {
console.log('输入文本成功');
setTimeout(function() {
selectSong(device, token);
}, 2000);
} else {
console.log('输入文本失败');
}
}
});
}
//6、选择歌曲
function selectSong(device, token) {
var selectUrl = deviceUrlBase + '/devices/' + device + '/screen/inputs';
//点击的坐标
var selectParam = {
"token":token,
"x":"274",//0.2352
"y":"387",//0.1984
"state":"press"
};
console.log('6、选择歌曲');
console.log(selectUrl+'---'+ JSON.stringify(selectParam));
request.post({
url: selectUrl,
method: "POST",
json: true,
headers: {
"content-type": "application/json",
},
body: selectParam
},
function(error, response, body) {
if (!error && response.statusCode == 200) {
if (body.status == true) {
console.log('选择歌曲成功---');
setTimeout(function() {
sing(device, token);
}, 2000);
} else {
console.log('选择歌曲失败');
}
}
});
}
//7、播放歌曲
function sing(device, token) {
var clickUrl = deviceUrlBase + '/devices/' + device + '/screen/inputs';
//点击的坐标
var clickParam = {
"token":token,
"x":"111",//0.1024
"y":"675",//0.4417
"state":"press"
};
console.log('7、播放歌曲');
console.log(clickUrl+'---'+ JSON.stringify(clickParam));
request.post({
url: clickUrl,
method: "POST",
json: true,
headers: {
"content-type": "application/json",
},
body: clickParam
},
function(error, response, body) {
if (!error && response.statusCode == 200) {
if (body.status == true) {
console.log('播放唱歌曲成功---');
console.log('两分钟后关闭APP---');
setTimeout(function() {
closeApp(device, token);
}, 120000);
} else {
console.log('播放唱歌曲失败');
}
}
});
}
//8、关闭 QQ 音乐
function closeApp(device, token) {
var runAppUrl = deviceUrlBase + '/devices/' + device + '/apps/' + runAppName + '?state=inactive&token='+token;
//参数
var requestData="";
console.log('8、关闭 QQ 音乐');
request.post({
url: runAppUrl,
method: "POST",
json: true,
headers: {
"content-type": "application/json",
},
body: requestData
},
function(error, response, body) {
if (!error && response.statusCode == 200) {
if (body.status == true) {
console.log('关闭 APP 成功');
} else {
//TODO
console.log('启动 APP 失败');
}
}
});
}
该样例是基于Total Control 6.7.0 新版本写的脚本样例,因此大家需要等到TC 6.7.0版本上线以后,可以在脚本功能中运行看看效果。