Skip to content

Commit

Permalink
feat: add job and task job for MaaControllerId, MaaResourceId and Maa…
Browse files Browse the repository at this point in the history
…TaskId
  • Loading branch information
dongwlin committed Aug 10, 2024
1 parent c1176c3 commit dfa1526
Show file tree
Hide file tree
Showing 10 changed files with 1,131 additions and 80 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func main() {

res := maa.NewResource(nil)
defer res.Destroy()
resId := res.PostPath("sample/resource")
res.Wait(resId)
resJob := res.PostPath("sample/resource")
resJob.Wait()

devices := toolkit.AdbDevices()
if len(devices) == 0 {
Expand All @@ -103,8 +103,8 @@ func main() {
nil,
)
defer ctrl.Destroy()
ctrlId := ctrl.PostConnect()
ctrl.Wait(ctrlId)
ctrlJob := ctrl.PostConnect()
ctrlJob.Wait()

inst := maa.New(nil)
defer inst.Destroy()
Expand All @@ -125,8 +125,8 @@ func main() {
panic("Failed to init Maa Instance.")
}

taskId := inst.PostTask("TaskA", "{}")
inst.WaitTask(taskId)
taskJob := inst.PostTask("TaskA", "{}")
taskJob.Wait()
}

type MyRec struct {
Expand Down
87 changes: 47 additions & 40 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ type Controller interface {
Destroy()
Handle() unsafe.Pointer
SetOption(key CtrlOption, value unsafe.Pointer, valSize uint64) bool
PostConnect() int64
PostClick(x, y int32) int64
PostSwipe(x1, y1, x2, y2, duration int32) int64
PostPressKey(keycode int32) int64
PostInputText(text string) int64
PostStartApp(intent string) int64
PostStopApp(intent string) int64
PostTouchDown(contact, x, y, pressure int32) int64
PostTouchMove(contact, x, y, pressure int32) int64
PostTouchUp(contact int32) int64
PostScreencap() int64
Status(id int64) Status
Wait(id int64) Status
PostConnect() Job
PostClick(x, y int32) Job
PostSwipe(x1, y1, x2, y2, duration int32) Job
PostPressKey(keycode int32) Job
PostInputText(text string) Job
PostStartApp(intent string) Job
PostStopApp(intent string) Job
PostTouchDown(contact, x, y, pressure int32) Job
PostTouchMove(contact, x, y, pressure int32) Job
PostTouchUp(contact int32) Job
PostScreencap() Job
//Status(id int64) Status
//Wait(id int64) Status
Connected() bool
GetImage() (ImageBuffer, bool)
GetUUID() (string, bool)
Expand All @@ -84,64 +84,71 @@ func (c *controller) SetOption(key CtrlOption, value unsafe.Pointer, valSize uin
return C.MaaControllerSetOption(c.handle, C.int32_t(key), C.MaaOptionValue(value), C.uint64_t(valSize)) != 0
}

func (c *controller) PostConnect() int64 {
return int64(C.MaaControllerPostConnection(c.handle))
func (c *controller) PostConnect() Job {
id := int64(C.MaaControllerPostConnection(c.handle))
return NewJob(id, c.status)
}

func (c *controller) PostClick(x, y int32) int64 {
return int64(C.MaaControllerPostClick(c.handle, C.int32_t(x), C.int32_t(y)))
func (c *controller) PostClick(x, y int32) Job {
id := int64(C.MaaControllerPostClick(c.handle, C.int32_t(x), C.int32_t(y)))
return NewJob(id, c.status)
}

func (c *controller) PostSwipe(x1, y1, x2, y2, duration int32) int64 {
return int64(C.MaaControllerPostSwipe(c.handle, C.int32_t(x1), C.int32_t(y1), C.int32_t(x2), C.int32_t(y2), C.int32_t(duration)))
func (c *controller) PostSwipe(x1, y1, x2, y2, duration int32) Job {
id := int64(C.MaaControllerPostSwipe(c.handle, C.int32_t(x1), C.int32_t(y1), C.int32_t(x2), C.int32_t(y2), C.int32_t(duration)))
return NewJob(id, c.status)
}

func (c *controller) PostPressKey(keycode int32) int64 {
return int64(C.MaaControllerPostPressKey(c.handle, C.int32_t(keycode)))
func (c *controller) PostPressKey(keycode int32) Job {
id := int64(C.MaaControllerPostPressKey(c.handle, C.int32_t(keycode)))
return NewJob(id, c.status)
}

func (c *controller) PostInputText(text string) int64 {
func (c *controller) PostInputText(text string) Job {
cText := C.CString(text)
defer C.free(unsafe.Pointer(cText))
return int64(C.MaaControllerPostInputText(c.handle, cText))
id := int64(C.MaaControllerPostInputText(c.handle, cText))
return NewJob(id, c.status)
}

func (c *controller) PostStartApp(intent string) int64 {
func (c *controller) PostStartApp(intent string) Job {
cIntent := C.CString(intent)
defer C.free(unsafe.Pointer(cIntent))
return int64(C.MaaControllerPostStartApp(c.handle, cIntent))
id := int64(C.MaaControllerPostStartApp(c.handle, cIntent))
return NewJob(id, c.status)
}

func (c *controller) PostStopApp(intent string) int64 {
func (c *controller) PostStopApp(intent string) Job {
cIntent := C.CString(intent)
defer C.free(unsafe.Pointer(cIntent))
return int64(C.MaaControllerPostStopApp(c.handle, cIntent))
id := int64(C.MaaControllerPostStopApp(c.handle, cIntent))
return NewJob(id, c.status)
}

func (c *controller) PostTouchDown(contact, x, y, pressure int32) int64 {
return int64(C.MaaControllerPostTouchDown(c.handle, C.int32_t(contact), C.int32_t(x), C.int32_t(y), C.int32_t(pressure)))
func (c *controller) PostTouchDown(contact, x, y, pressure int32) Job {
id := int64(C.MaaControllerPostTouchDown(c.handle, C.int32_t(contact), C.int32_t(x), C.int32_t(y), C.int32_t(pressure)))
return NewJob(id, c.status)
}

func (c *controller) PostTouchMove(contact, x, y, pressure int32) int64 {
return int64(C.MaaControllerPostTouchMove(c.handle, C.int32_t(contact), C.int32_t(x), C.int32_t(y), C.int32_t(pressure)))
func (c *controller) PostTouchMove(contact, x, y, pressure int32) Job {
id := int64(C.MaaControllerPostTouchMove(c.handle, C.int32_t(contact), C.int32_t(x), C.int32_t(y), C.int32_t(pressure)))
return NewJob(id, c.status)
}

func (c *controller) PostTouchUp(contact int32) int64 {
return int64(C.MaaControllerPostTouchUp(c.handle, C.int32_t(contact)))
func (c *controller) PostTouchUp(contact int32) Job {
id := int64(C.MaaControllerPostTouchUp(c.handle, C.int32_t(contact)))
return NewJob(id, c.status)
}

func (c *controller) PostScreencap() int64 {
return int64(C.MaaControllerPostScreencap(c.handle))
func (c *controller) PostScreencap() Job {
id := int64(C.MaaControllerPostScreencap(c.handle))
return NewJob(id, c.status)
}

func (c *controller) Status(id int64) Status {
func (c *controller) status(id int64) Status {
return Status(C.MaaControllerStatus(c.handle, C.int64_t(id)))
}

func (c *controller) Wait(id int64) Status {
return Status(C.MaaControllerWait(c.handle, C.int64_t(id)))
}

func (c *controller) Connected() bool {
return C.MaaControllerConnected(c.handle) != 0
}
Expand Down
32 changes: 19 additions & 13 deletions instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern void _MaaAPICallbackAgent(_GoString_ msg, _GoString_ detailsJson, MaaTran
*/
import "C"
import (
"time"
"unsafe"
)

Expand Down Expand Up @@ -94,53 +95,58 @@ func (i *Instance) ClearCustomAction() bool {
}

// PostTask posts a task to the instance.
func (i *Instance) PostTask(entry, param string) int64 {
func (i *Instance) PostTask(entry, param string) TaskJob {
cEntry := C.CString(entry)
cParam := C.CString(param)
defer func() {
C.free(unsafe.Pointer(cEntry))
C.free(unsafe.Pointer(cParam))
}()
return int64(C.MaaPostTask(i.handle, cEntry, cParam))
id := int64(C.MaaPostTask(i.handle, cEntry, cParam))
return NewTaskJob(id, i.taskStatus, i.setTaskParam)
}

// PostRecognition posts a recognition to the instance.
func (i *Instance) PostRecognition(entry, param string) int64 {
func (i *Instance) PostRecognition(entry, param string) TaskJob {
cEntry := C.CString(entry)
cParam := C.CString(param)
defer func() {
C.free(unsafe.Pointer(cEntry))
C.free(unsafe.Pointer(cParam))
}()
return int64(C.MaaPostRecognition(i.handle, cEntry, cParam))
id := int64(C.MaaPostRecognition(i.handle, cEntry, cParam))
return NewTaskJob(id, i.taskStatus, i.setTaskParam)
}

// PostAction posts an action to the instance.
func (i *Instance) PostAction(entry, param string) int64 {
func (i *Instance) PostAction(entry, param string) TaskJob {
cEntry := C.CString(entry)
cParam := C.CString(param)
defer func() {
C.free(unsafe.Pointer(cEntry))
C.free(unsafe.Pointer(cParam))
}()
return int64(C.MaaPostAction(i.handle, cEntry, cParam))
id := int64(C.MaaPostAction(i.handle, cEntry, cParam))
return NewTaskJob(id, i.taskStatus, i.setTaskParam)
}

// SetTaskParam sets the parameter of a task.
func (i *Instance) SetTaskParam(id int64, param string) bool {
// setTaskParam sets the parameter of a task.
func (i *Instance) setTaskParam(id int64, param string) bool {
cParam := C.CString(param)
defer C.free(unsafe.Pointer(cParam))
return C.MaaSetTaskParam(i.handle, C.int64_t(id), cParam) != 0
}

// TaskStatus returns the status of a task identified by the id.
func (i *Instance) TaskStatus(id int64) Status {
// taskStatus returns the status of a task identified by the id.
func (i *Instance) taskStatus(id int64) Status {
return Status(C.MaaTaskStatus(i.handle, C.int64_t(id)))
}

// WaitTask waits for a task to finish.
func (i *Instance) WaitTask(id int64) Status {
return Status(C.MaaWaitTask(i.handle, C.int64_t(id)))
// WaitAll waits for all tasks to complete.
func (i *Instance) WaitAll() {
for i.Running() {
time.Sleep(time.Millisecond * 10)
}
}

// Running checks if the instance running.
Expand Down
50 changes: 50 additions & 0 deletions job.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package maa

import "time"

type Job struct {
id int64
statusFunc func(id int64) Status
}

func NewJob(id int64, statusFunc func(id int64) Status) Job {
return Job{
id: id,
statusFunc: statusFunc,
}
}

func (job Job) Status() Status {
return job.statusFunc(job.id)
}

func (job Job) Invalid() bool {
return job.Status().Invalid()
}

func (job Job) Pending() bool {
return job.Status().Pending()
}

func (job Job) Running() bool {
return job.Status().Running()
}

func (job Job) Success() bool {
return job.Status().Success()
}

func (job Job) Failure() bool {
return job.Status().Failure()
}

func (job Job) Done() bool {
return job.Status().Done()
}

func (job Job) Wait() bool {
for !job.Done() {
time.Sleep(time.Millisecond * 10)
}
return job.Success()
}
Loading

0 comments on commit dfa1526

Please sign in to comment.