generated from MaaXYZ/MaaPracticeBoilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
123 lines (96 loc) · 3.66 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# python -m pip install maafw
from maa.resource import Resource
from maa.controller import AdbController
from maa.tasker import Tasker
from maa.toolkit import Toolkit
from maa.custom_recognition import CustomRecognition
from maa.custom_action import CustomAction
from maa.notification_handler import NotificationHandler, NotificationType
def main():
user_path = "./"
Toolkit.init_option(user_path)
resource = Resource()
res_job = resource.post_path("sample/resource")
res_job.wait()
adb_devices = Toolkit.find_adb_devices()
if not adb_devices:
print("No ADB device found.")
exit()
# for demo, we just use the first device
device = adb_devices[0]
controller = AdbController(
adb_path=device.adb_path,
address=device.address,
screencap_methods=device.screencap_methods,
input_methods=device.input_methods,
config=device.config,
)
controller.post_connection().wait()
tasker = Tasker()
# tasker = Tasker(notification_handler=MyNotificationHandler())
tasker.bind(resource, controller)
if not tasker.inited:
print("Failed to init MAA.")
exit()
resource.register_custom_recognition("MyRec", MyRecongition())
task_detail = tasker.post_pipeline("StartUpAndClickButton").wait().get()
# do something with task_detail
class MyRecongition(CustomRecognition):
def analyze(
self,
context,
argv: CustomRecognition.AnalyzeArg,
) -> CustomRecognition.AnalyzeResult:
reco_detail = context.run_recognition(
"MyCustomOCR",
argv.image,
pipeline_override={"MyCustomOCR": {"roi": [100, 100, 200, 300]}},
)
# context is a reference, will override the pipeline for whole task
context.override_pipeline({"MyCustomOCR": {"roi": [1, 1, 114, 514]}})
# context.run_recognition ...
# make a new context to override the pipeline, only for itself
new_context = context.clone()
new_context.override_pipeline({"MyCustomOCR": {"roi": [100, 200, 300, 400]}})
reco_detail = new_context.run_recognition("MyCustomOCR", argv.image)
click_job = context.tasker.controller.post_click(10, 20)
click_job.wait()
context.override_next(argv.current_task_name, ["TaskA", "TaskB"])
return CustomRecognition.AnalyzeResult(
box=(0, 0, 100, 100), detail="Hello World!"
)
class MyNotificationHandler(NotificationHandler):
def on_resource_loading(
self,
noti_type: NotificationType,
detail: NotificationHandler.ResourceLoadingDetail,
):
print(f"on_resource_loading: {noti_type}, {detail}")
def on_controller_action(
self,
noti_type: NotificationType,
detail: NotificationHandler.ControllerActionDetail,
):
print(f"on_controller_action: {noti_type}, {detail}")
def on_tasker_task(
self, noti_type: NotificationType, detail: NotificationHandler.TaskerTaskDetail
):
print(f"on_tasker_task: {noti_type}, {detail}")
def on_task_next_list(
self,
noti_type: NotificationType,
detail: NotificationHandler.TaskNextListDetail,
):
print(f"on_task_next_list: {noti_type}, {detail}")
def on_task_recognition(
self,
noti_type: NotificationType,
detail: NotificationHandler.TaskRecognitionDetail,
):
print(f"on_task_recognition: {noti_type}, {detail}")
def on_task_action(
self, noti_type: NotificationType, detail: NotificationHandler.TaskActionDetail
):
print(f"on_task_action: {noti_type}, {detail}")
if __name__ == "__main__":
main()