You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.6 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package crontask
import (
"autogo/dbsql"
"autogo/models"
"autogo/monkey"
"fmt"
"os/exec"
"strings"
"github.com/robfig/cron/v3"
"github.com/spf13/cast"
)
func Run() {
crontab := cron.New(cron.WithSeconds())
// crontab.AddFunc("0 */1 * * * ?", CheckAndroidDevices)
// crontab.AddFunc("0 */1 * * * ?", CheckIOSDevices)
crontab.AddFunc("*/15 * * * * ?", CheckMonkeyTasks)
crontab.Start()
fmt.Println("[Crontab] 定时任务启动")
}
func CheckAndroidDevices() {
// fmt.Println("[Crontab] 检查安卓设备")
command := `devices` // | grep product.marketname'
cmd := exec.Command("adb", command)
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(err)
}
strs := strings.Split(string(out), "\n")
device := make(map[string]string)
for i, v := range strs {
if i == 0 || len(v) < 4 {
continue
}
_strs := strings.Split(v, "\t")
device[_strs[0]] = _strs[1]
}
db, err := dbsql.GetConn(dbsql.DSN)
if err != nil {
fmt.Println("[Crontab] 数据库连接失败")
return
}
defer dbsql.Close(db)
var list []models.Device
db.Model(models.Device{}).Where("platform = ? AND is_del = 0", "adr").Find(&list)
for _, v := range list {
if vv, ok := device[v.Udid]; ok {
// 数据表中的设备adb有获取到
switch v.Status {
case "busy":
continue
case "unauthorized":
if strings.Contains(vv, "device") {
db.Model(models.Device{}).Where("udid = ?", v.Udid).Update("status", "online")
fmt.Println("设备", v.Udid, "已连接")
}
case "offline":
if strings.Contains(vv, "device") {
db.Model(models.Device{}).Where("udid = ?", v.Udid).Update("status", "online")
fmt.Println("设备", v.Udid, "已连接")
} else {
db.Model(models.Device{}).Where("udid = ?", v.Udid).Update("status", "unauthorized")
fmt.Println("设备", v.Udid, "已连接但未认证")
}
case "online":
// 保持亮屏
monkey.KeepAndroidDevice(v.Udid)
}
} else {
// 数据表中的设备adb没有获取到视为离线
if v.Status != "offline" {
db.Model(models.Device{}).Where("udid = ?", v.Udid).Update("status", "offline")
fmt.Println("设备", v.Udid, "已离线")
}
}
}
}
func checkTaskPids(pids []models.MonkeyPid) int {
if len(pids) == 0 {
return -1
}
var strs []string
for _, v := range pids {
strs = append(strs, cast.ToString(v.PId))
}
pids_str := strings.Join(strs, "|")
cmd_str := "ps -ef | grep -v -E 'bash|grep' | grep -E '" + pids_str + "'"
fmt.Println(cmd_str)
cmd := exec.Command("bash", "-c", cmd_str)
cmd.Run()
return cmd.ProcessState.ExitCode()
}