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

318 lines
6.3 KiB

package utils
import (
"encoding/json"
"fmt"
"strings"
"time"
mqtt "automatedtesting/usecases_server/utils/cyllib/mqtt"
"automatedtesting/usecases_server/utils/cyllib/re"
"automatedtesting/usecases_server/utils/cyllib/ssh"
MQTT "github.com/eclipse/paho.mqtt.golang"
"github.com/gin-gonic/gin"
)
type Inputdata struct {
Id int
Name string
Input string
}
func Expre(exp bool, trueVal, falseVal interface{}) interface{} {
if exp {
return trueVal
} else {
return falseVal
}
}
func GetReStepAndExpect(step, expect, real string) (steps, expects, reals []string) {
exp := `\d+\..+[\r\n]?(?:(?!\d+\.).*[\r\n]?)*`
steps = re.FindAll2(exp, step)
expects = re.FindAll2(exp, expect)
reals = re.FindAll2(exp, real)
return
}
func GetPage(pages string) (int, int) {
var page map[string]int
json.Unmarshal([]byte(pages), &page)
return page["currentPage"], page["pageSize"]
}
func GetInputData(ipt string) []Inputdata {
pts := []Inputdata{}
ipts := re.FindAll(`\{"id":\d+,"name":".+?","input":".*?"\}`, ipt)
for _, d := range ipts {
pt := Inputdata{}
json.Unmarshal([]byte(d[0]), &pt)
if pt.Input != "" {
pts = append(pts, pt)
}
}
return pts
}
func GetCheckData(ch string) Checked {
chd := Checked{}
json.Unmarshal([]byte(ch), &chd)
return chd
}
type Checked struct {
Succ string
Fail string
}
type PublicData struct {
Id interface{} `json:"id"`
Uid interface{} `json:"uid"`
Power bool `json:"power"`
Limit int `json:"limit"`
Offset int `json:"offset"`
Input []Inputdata `json:"input"`
Checked Checked `json:"checked"`
}
func GetPublicParam(c *gin.Context) *PublicData {
public := PublicData{}
uid, _ := c.Get("uid")
power, _ := c.Get("power")
// 获取页码
pages := c.Query("page")
CurrentPage, PageSize := GetPage(pages)
off := (CurrentPage - 1) * PageSize
ipt := c.Query("input")
input := GetInputData(ipt)
id := c.Query("id")
ch := c.Query("checked")
public.Checked = GetCheckData(ch)
public.Id = id
public.Uid = uid
public.Power = power.(bool)
public.Limit = PageSize
public.Offset = off
public.Input = input
return &public
}
// 链式法生成语句,按照table->select->where调用(每个方法调用一次,调用多次未实现)
type SqlJoin struct {
// table []string //表名
// name []string //别名
table map[string]string //表名--key表名 val 别名
selected string
from string
where string
orderby string
limit string
offset string
}
//
// table由表名和别名组成
func Table(table ...string) *SqlJoin {
sql := SqlJoin{}
sql.table = make(map[string]string)
sql.from = "FROM "
for i, d := range table {
sql.from += d + " "
if i < len(table)-1 {
sql.from += ","
}
}
return &sql
}
func (s *SqlJoin) Sql() string {
return s.selected + s.from + s.where + s.orderby + s.limit + s.offset
}
func (s *SqlJoin) Select(field ...string) *SqlJoin {
if len(field) == 0 {
panic("Field is empty")
}
s.selected = "SELECT "
sel := ""
for i, d := range field {
sel += d
if i < len(field)-1 {
sel += ","
} else {
sel += " "
}
}
s.selected += sel
return s
}
func (s *SqlJoin) Where(judge string, val ...interface{}) *SqlJoin {
sp := strings.Split(judge, "?")
spn := sp[:len(sp)-1]
s.where = "WHERE "
if len(spn) != len(val) {
panic("Different numbers of data are passed in.")
}
for i := range spn {
s.where += spn[i] + fmt.Sprint(val[i])
}
if sp[len(sp)-1] != "" {
s.where += sp[len(sp)-1]
}
s.where += " "
return s
}
// sort = true 升序 false 降序
func (s *SqlJoin) OrderBy(order string) *SqlJoin {
// DESC 降序 ASC升序
s.orderby += "ORDER BY " + order + " "
return s
}
func (s *SqlJoin) Page(limit, offset interface{}) *SqlJoin {
s.limit += fmt.Sprint("LIMIT", " ", limit, " ")
s.offset += fmt.Sprint("OFFSET", " ", offset, " ")
return s
}
func LikeJoin(str string) (sp string) {
if strings.Contains(str, "at ") {
sp = strings.ReplaceAll(str, `at `, `AT+`)
} else if strings.Contains(str, "AT ") {
sp = strings.ReplaceAll(str, `AT `, `AT+`)
}
sp = "%%" + sp + "%%"
return
}
type Delay struct {
flag bool
timer *time.Ticker
}
func DelayMany(t time.Duration, method func()) *Delay {
del := Delay{
flag: true,
timer: time.NewTicker(t),
}
go func() {
for del.flag {
select {
case <-del.timer.C:
method()
default:
time.Sleep(time.Millisecond)
}
}
}()
return &del
}
func (s *Delay) Close() {
s.flag = false
s.timer.Stop()
}
func DelayOnce(t time.Duration, method func()) *time.Timer {
timer := time.NewTimer(t)
go func() {
<-timer.C
method()
timer.Stop()
}()
return timer
}
func TernaryVal(f bool, a, b interface{}) interface{} {
if f {
return a
}
return b
}
func TernaryFunc(f bool, a, b func()) {
if f {
a()
}
b()
}
//该方法char参数只处理第一个字符,其他字符不处理
func charToByte(b byte) byte {
if b >= 97 && b <= 102 {
b = b - 97 + 10
} else if b >= 65 && b <= 70 {
b = b - 65 + 10
} else if b >= 48 && b <= 57 {
b = b - 48
} else {
b = 0
}
return b
}
func StrToHex(str string) []byte {
var bytes = make([]byte, 0)
endflag := false
for i := 0; i < len(str); i += 2 {
s0 := charToByte(str[i])
s1 := s0
if i+1 < len(str) {
s1 = charToByte(str[i+1])
} else {
s1 = 0
endflag = true
}
if !endflag {
bytes = append(bytes, s0<<4|s1)
} else {
bytes = append(bytes, s0)
}
}
return bytes
}
// func DelayOnces(t time.Duration, method func(param interface{}), param interface{}) *time.Timer {
// timer := time.NewTimer(t)
// go func() {
// for {
// <-timer.C
// method(param)
// }
// }()
// return timer
// }
// --------------------------------------mqtt
func GetId(c *gin.Context) string {
uid, _ := c.Get("uid")
uname, _ := c.Get("uaccount")
id := fmt.Sprint(uid, uname)
return id
}
func CheckMqttMsg(msg ssh.SSHMsg) bool {
var ch = make(chan bool, 1)
go func() {
tim := DelayOnce(time.Second*2, func() {
ch <- false
mqtt.UnSubscribe(mqtt.SubReplay)
})
mqtt.Subscribe(mqtt.SubReplay, func(c MQTT.Client, m MQTT.Message) {
replay := mqtt.Mqtt_GetReplay(m)
if replay.Name == "Runing" {
if replay.Ip == msg.Host {
mqtt.UnSubscribe(mqtt.SubReplay)
tim.Stop()
ch <- true
}
}
})
}()
mqtt.Push(mqtt.PushInstr, mqtt.Message{
Ip: msg.Host,
Name: "IsRuning",
Data: "",
})
return <-ch
}