This example uses a script to log in, click, slide to the bottom of the screen, click, and slide to the bottom again.
var {request} = require('ringo/httpclient');
var base64 = require('ringo/base64');
var subproc = require('ringo/subprocess');
var userpass = base64.encode('sigma:jiehua');
var prefix = 'http://127.0.0.1:8090/TotalControl/v1/'
var token = null;
function sleep(sec) {
subproc.command('sleep '+sec);
}
function restError(ret, url) {
this.status = ret.status;
this.message = "Unknown error";
this.code = 0;
this.url = url;
if (ret.status == '403') {
// Just handle the first error
var value = JSON.parse(ret.content);
this.code = value.errors[0].code;
this.message = value.errors[0].message;
}
}
function sendRequest(url, opt) {
if (!opt) opt={};
var fullurl = prefix + url + "?token=" + token;
if (opt.params) {
fullurl += '&' + opt.params;
}
var method = opt.method || 'GET';
var robj = {method:method, url: fullurl};
if (opt.headers) {
robj.headers = opt.headers;
}
print(fullurl);
var exchange = request(robj);
if (exchange.status == 200) {
return({status: true, value: JSON.parse(exchange.content)});
}
throw new restError(exchange, fullurl);
}
function fastSwipeParam() {
var ftob = '[';
for (var i = 0.8; i >= 0.05; i -= 0.15) {
ftob += '[0.5000,' + i.toFixed(4) + ',1],';
}
ftob = ftob.slice(0,ftob.length-1) + ']';
return(ftob);
}
var exchange = request({
method: 'GET',
url: prefix+'login',
headers: {
'Authorization': userpass
}
});
if(exchange.status == 200) {
token=JSON.parse(exchange.content).value.token;
}
try {
var ret = sendRequest('devices/main');
var device = ret.value.id;
var req = 'devices/' + device + '/screen/inputs';
ret = sendRequest(req, {method: 'POST', params: 'x=0.2625&y=0.4583'});
sleep(3);
var ftob = fastSwipeParam();
ret = sendRequest(req, {method: 'POST', params: 'coord='+ftob});
sleep(1);
ret = sendRequest(req, {method: 'POST', params: 'x=0.3848&y=0.8345'});
sleep(2);
ret = sendRequest(req, {method: 'POST', params: 'coord='+ftob});
} catch (e) {
print('Error found! Status = ' + e.status + ', code = ' + e.code +
'\nURL = ' + e.url + '\nmessage = ' + e.message);
quit(1);
}
quit(0);