feat: add tap text for hrp ui server

This commit is contained in:
lilong.129
2024-11-04 18:02:11 +08:00
parent 0055e90e38
commit 6fb99114f4
3 changed files with 44 additions and 25 deletions
+1 -1
View File
@@ -1 +1 @@
v5.0.0+2411031830 v5.0.0+2411041802
+1
View File
@@ -10,6 +10,7 @@ type TapRequest struct {
X float64 `json:"x"` X float64 `json:"x"`
Y float64 `json:"y"` Y float64 `json:"y"`
Duration float64 `json:"duration"` Duration float64 `json:"duration"`
Text string `json:"text"`
} }
type DragRequest struct { type DragRequest struct {
+42 -24
View File
@@ -124,7 +124,21 @@ func tapHandler(c *gin.Context) {
} }
dExt := driverObj.(*uixt.DriverExt) dExt := driverObj.(*uixt.DriverExt)
if tapReq.X < 1 && tapReq.Y < 1 { if tapReq.Text != "" {
err := dExt.TapByOCR(tapReq.Text, uixt.WithPressDuration(tapReq.Duration))
if err != nil {
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to tap text %s", c.HandlerName(), tapReq.Text))
c.JSON(http.StatusInternalServerError,
HttpResponse{
Result: false,
ErrorCode: InternalServerErrorCode,
ErrorMsg: InternalServerErrorMsg,
},
)
c.Abort()
return
}
} else if tapReq.X < 1 && tapReq.Y < 1 {
err := dExt.TapXY(tapReq.X, tapReq.Y, uixt.WithPressDuration(tapReq.Duration)) err := dExt.TapXY(tapReq.X, tapReq.Y, uixt.WithPressDuration(tapReq.Duration))
if err != nil { if err != nil {
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to tap %f, %f", c.HandlerName(), tapReq.X, tapReq.Y)) log.Err(err).Msg(fmt.Sprintf("[%s]: failed to tap %f, %f", c.HandlerName(), tapReq.X, tapReq.Y))
@@ -427,22 +441,15 @@ func sourceHandler(c *gin.Context) {
} }
func adbSourceHandler(c *gin.Context) { func adbSourceHandler(c *gin.Context) {
deviceObj, exists := c.Get("device") driverObj, exists := c.Get("driver")
if !exists { if !exists {
log.Error().Msg(fmt.Sprintf("[%s]: Driver Not exsit", c.HandlerName())) log.Error().Msg(fmt.Sprintf("[%s]: Driver Not exsit", c.HandlerName()))
c.JSON(http.StatusBadRequest, HttpResponse{Result: "", ErrorCode: InvalidParamErrorCode, ErrorMsg: fmt.Sprintf(InvalidParamErrorMsg, "device")}) c.JSON(http.StatusBadRequest, HttpResponse{Result: "", ErrorCode: InvalidParamErrorCode, ErrorMsg: fmt.Sprintf(InvalidParamErrorMsg, "driver")})
c.Abort() c.Abort()
return return
} }
device := deviceObj.(*uixt.AndroidDevice) driver := driverObj.(*uixt.DriverExt)
driver, err := device.NewAdbDriver() source, err := driver.Driver.Source()
if err != nil {
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to new adb driver", c.HandlerName()))
c.JSON(http.StatusInternalServerError, HttpResponse{Result: "", ErrorCode: InternalServerErrorCode, ErrorMsg: InternalServerErrorMsg})
c.Abort()
return
}
source, err := driver.Source()
if err != nil { if err != nil {
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to get adb source", c.HandlerName())) log.Err(err).Msg(fmt.Sprintf("[%s]: failed to get adb source", c.HandlerName()))
c.JSON(http.StatusInternalServerError, HttpResponse{Result: "", ErrorCode: InternalServerErrorCode, ErrorMsg: InternalServerErrorMsg}) c.JSON(http.StatusInternalServerError, HttpResponse{Result: "", ErrorCode: InternalServerErrorCode, ErrorMsg: InternalServerErrorMsg})
@@ -508,21 +515,30 @@ func logoutHandler(c *gin.Context) {
c.JSON(http.StatusOK, HttpResponse{Result: true}) c.JSON(http.StatusOK, HttpResponse{Result: true})
} }
var uiClients = make(map[string]*uixt.DriverExt) // UI automation clients for iOS and Android, key is udid/serial
func parseDeviceInfo() gin.HandlerFunc { func parseDeviceInfo() gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
platform := c.Param("platform") platform := c.Param("platform")
serial := c.Param("serial")
if serial == "" {
log.Error().Str("platform", platform).Msg(fmt.Sprintf("[%s]: serial is empty", c.HandlerName()))
c.JSON(http.StatusBadRequest, HttpResponse{
ErrorCode: InvalidParamErrorCode,
ErrorMsg: fmt.Sprintf(InvalidParamErrorMsg, "serial"),
})
c.Abort()
return
}
// get cached driver
if driver, ok := uiClients[serial]; ok {
c.Set("driver", driver)
c.Next()
return
}
switch strings.ToLower(platform) { switch strings.ToLower(platform) {
case "android": case "android":
serial := c.Param("serial")
if serial == "" {
log.Error().Str("platform", platform).Msg(fmt.Sprintf("[%s]: serial is empty", c.HandlerName()))
c.JSON(http.StatusBadRequest, HttpResponse{
ErrorCode: InvalidParamErrorCode,
ErrorMsg: fmt.Sprintf(InvalidParamErrorMsg, "serial"),
})
c.Abort()
return
}
device, err := uixt.NewAndroidDevice(uixt.WithSerialNumber(serial), uixt.WithStub(true)) device, err := uixt.NewAndroidDevice(uixt.WithSerialNumber(serial), uixt.WithStub(true))
if err != nil { if err != nil {
log.Error().Err(err).Str("platform", platform).Str("serial", serial).Msg(fmt.Sprintf("[%s]: Device Not Found", c.HandlerName())) log.Error().Err(err).Str("platform", platform).Str("serial", serial).Msg(fmt.Sprintf("[%s]: Device Not Found", c.HandlerName()))
@@ -533,8 +549,8 @@ func parseDeviceInfo() gin.HandlerFunc {
c.Abort() c.Abort()
return return
} }
c.Set("device", device) // c.Set("device", device)
driver, err := device.NewDriver(uixt.WithDriverImageService(false), uixt.WithDriverResultFolder(false)) driver, err := device.NewDriver(uixt.WithDriverImageService(true), uixt.WithDriverResultFolder(true))
if err != nil { if err != nil {
log.Error().Err(err).Str("platform", platform).Str("serial", serial).Msg(fmt.Sprintf("[%s]: Failed New Driver", c.HandlerName())) log.Error().Err(err).Str("platform", platform).Str("serial", serial).Msg(fmt.Sprintf("[%s]: Failed New Driver", c.HandlerName()))
c.JSON(http.StatusInternalServerError, HttpResponse{ c.JSON(http.StatusInternalServerError, HttpResponse{
@@ -545,6 +561,8 @@ func parseDeviceInfo() gin.HandlerFunc {
return return
} }
c.Set("driver", driver) c.Set("driver", driver)
// cache driver
uiClients[serial] = driver
default: default:
c.JSON(http.StatusBadRequest, HttpResponse{ c.JSON(http.StatusBadRequest, HttpResponse{
ErrorCode: InvalidParamErrorCode, ErrorCode: InvalidParamErrorCode,