package monkey

import (
	"goqs/models"
	"os/exec"
	"time"

	log "github.com/sirupsen/logrus"
	"github.com/spf13/cast"
)

func runAndroidMonkeyCmd(task models.MonkeyTask) {
	pkg_path := "/home/tmp/pkg/" + cast.ToString(time.Now().Unix()) + ".apk"
	log.Debug("正在下载apk：", task.PackageURL)
	err := exec.Command("wget", task.PackageURL, "-O", pkg_path).Run()
	if err != nil {
		log.Error(err)
	}
	log.Debug("下载完毕，开始调用monkey服务，安装包路径", pkg_path)
	cmd_content := "cd /home/app/aimonkey && python3 main_run.py" +
		" -task_id " + cast.ToString(task.Id) +
		" -pkg_path " + pkg_path +
		" -package " + task.PackageName +
		" -activity " + task.LaunchActivity +
		" -count " + cast.ToString(task.DeviceAmount) +
		" -time " + cast.ToString(task.RunTime)
	log.Debug(cmd_content)
	cmd := exec.Command("bash", "-c", cmd_content)
	err = cmd.Run()
	if err != nil {
		log.Error(err)
	}
}
