base_task.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. from datetime import datetime
  5. from typing import Union
  6. from module.atom.image import RuleImage
  7. from module.atom.click import RuleClick
  8. from module.atom.long_click import RuleLongClick
  9. from module.atom.swipe import RuleSwipe
  10. from module.logger import logger
  11. from module.base.timer import Timer
  12. from module.config.config import Config
  13. from module.device.device import Device
  14. class BaseTask:
  15. config: Config = None
  16. device: Device = None
  17. folder: str
  18. name: str
  19. stage: str
  20. limit_time: datetime # 限制运行的时间,是软时间,不是硬时间
  21. def __init__(self, config: Config, device: Device) -> None:
  22. self.config = config
  23. self.device = device
  24. self.interval_timer = {} # 这个是用来记录每个匹配的运行间隔的,用于控制运行频率
  25. def screenshot(self):
  26. """
  27. 截图 引入中间函数的目的是 为了解决如协作的这类突发的事件
  28. :return:
  29. """
  30. return self.device.screenshot()
  31. def appear(self, target: RuleImage, interval: float =None, threshold: float =None):
  32. """
  33. :param target: 匹配的目标可以是RuleImage, 也可以是RuleOcr
  34. :param interval:
  35. :param threshold:
  36. :return:
  37. """
  38. if not isinstance(target, RuleImage):
  39. return False
  40. if interval:
  41. if target.name in self.interval_timer:
  42. if self.interval_timer[target.name].limit != interval:
  43. self.interval_timer[target.name] = Timer(interval)
  44. else:
  45. self.interval_timer[target.name] = Timer(interval)
  46. if not self.interval_timer[target.name].reached():
  47. return False
  48. appear = target.match(self.device.image, threshold=threshold)
  49. if appear and interval:
  50. logger.info(f'Image matches to {target.name}')
  51. self.interval_timer[target.name].reset()
  52. return appear
  53. def appear_then_click(self,
  54. target: RuleImage,
  55. action: Union[RuleClick, RuleLongClick]=None,
  56. interval: float=None,
  57. threshold: float=None,
  58. duration: float=None):
  59. """
  60. 出现了就点击,默认点击图片的位置,如果添加了click参数,就点击click的位置
  61. :param duration: 如果是长按,可以手动指定duration,不指定默认.单位是ms!!!!
  62. :param action: 可以是RuleClick, 也可以是RuleLongClick
  63. :param target: 可以是RuleImage后续支持RuleOcr
  64. :param interval:
  65. :param threshold:
  66. :return: True or False
  67. """
  68. if not isinstance(target, RuleImage):
  69. return False
  70. appear = self.appear(target, interval=interval, threshold=threshold)
  71. if appear and not action:
  72. x, y = target.coord()
  73. self.device.click(x, y, control_name=target.name)
  74. elif appear and action:
  75. x, y = action.coord()
  76. if isinstance(action, RuleLongClick):
  77. if duration is None:
  78. self.device.long_click(x, y, duration=action.duration/1000, control_name=target.name)
  79. else:
  80. self.device.long_click(x, y, duration=duration/1000, control_name=target.name)
  81. elif isinstance(action, RuleClick):
  82. self.device.click(x, y, control_name=target.name)
  83. return appear
  84. def wait_until_appear(self,
  85. target: RuleImage,
  86. skip_first_screenshot=False) -> None:
  87. """
  88. 等待直到出现目标
  89. :param target:
  90. :param skip_first_screenshot:
  91. :return:
  92. """
  93. while 1:
  94. if skip_first_screenshot:
  95. skip_first_screenshot = False
  96. else:
  97. self.screenshot()
  98. if self.appear(target):
  99. break
  100. def wait_until_appear_then_click(self,
  101. target: RuleImage,
  102. action: Union[RuleClick, RuleLongClick]=None) -> None:
  103. """
  104. 等待直到出现目标,然后点击
  105. :param action:
  106. :param target:
  107. :return:
  108. """
  109. self.wait_until_appear(target)
  110. if action is None:
  111. self.device.click(target.coord(), control_name=target.name)
  112. elif isinstance(action, RuleLongClick):
  113. self.device.long_click(target.coord(), duration=action.duration/1000, control_name=target.name)
  114. elif isinstance(action, RuleClick):
  115. self.device.click(target.coord(), control_name=target.name)
  116. def wait_until_disappear(self, target: RuleImage) -> None:
  117. while 1:
  118. self.screenshot()
  119. if not self.appear(target):
  120. break
  121. def wait_until_stable(self,
  122. target: RuleImage,
  123. timer=Timer(0.3, count=1),
  124. timeout=Timer(5, count=10),
  125. skip_first_screenshot=True):
  126. """
  127. 等待目标稳定,即连续多次匹配成功
  128. :param target:
  129. :param timer:
  130. :param timeout:
  131. :param skip_first_screenshot:
  132. :return:
  133. """
  134. target._match_init = False
  135. timeout.reset()
  136. while 1:
  137. if skip_first_screenshot:
  138. skip_first_screenshot = False
  139. else:
  140. self.screenshot()
  141. if target._match_init:
  142. if target.match(self.device.image):
  143. if timer.reached():
  144. break
  145. else:
  146. # button.load_color(self.device.image)
  147. timer.reset()
  148. else:
  149. # target.load_color(self.device.image)
  150. target._match_init = True
  151. if timeout.reached():
  152. logger.warning(f'wait_until_stable({target}) timeout')
  153. break
  154. def swipe(self, swipe: RuleSwipe, interval: float =None) -> None:
  155. """
  156. :param interval:
  157. :param swipe:
  158. :return:
  159. """