{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from airtest.core import api as airtest\n",
    "from airtest.aircv import *\n",
    "import time\n",
    "import datetime\n",
    "import  logging\n",
    "import re \n",
    "from orc import orc  \n",
    "import os\n",
    "logger = logging.getLogger(\"airtest\")\n",
    "logger2 = logging.getLogger(\"ppocr\")\n",
    "logger.setLevel(logging.WARNING)\n",
    "logger2.setLevel(logging.WARNING)\n",
    "times = time.localtime(time.time())\n",
    "week=datetime.datetime(times.tm_year,times.tm_mon,times.tm_mday).weekday()\n",
    "day = times.tm_mday"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info = True\n",
    "\n",
    "os.system(\"adb devices\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[19:08:42][WARNING]<airtest.core.api> Device:192.168.0.218:5555 updated <airtest.core.android.android.Android object at 0x000002327665F440> -> <airtest.core.android.android.Android object at 0x0000023276A82150>\n"
     ]
    }
   ],
   "source": [
    "def c():\n",
    "    airtest.connect_device(\"Android://127.0.0.1:5037/192.168.0.218:5555\")\n",
    "c()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "threshold = 0.92\n",
    "threshold1= 0.75\n",
    "def t(path=False):\n",
    "    if not path:\n",
    "        airtest.snapshot(filename=\"demo.png\",quality=90)\n",
    "    else:\n",
    "        airtest.snapshot(filename=f\"./test/imgs/{path}.png\",quality=90)\n",
    "def l(flag):\n",
    "    if flag: logger.setLevel(logging.DEBUG)\n",
    "    else:logger.setLevel(logging.WARNING)\n",
    "def log(information):\n",
    "    info and print(information)\n",
    "def local(v1,v2,paddle=True):\n",
    "    local = aircv.crop_image(airtest.G.DEVICE.snapshot(),(v1[0],v1[1],v2[0],v2[1]))\n",
    "    pil_image = cv2_2_pil(local)\n",
    "    pil_image.save(\"./test/imgs/demo.png\", quality=99, optimize=True)\n",
    "    if paddle:\n",
    "        ocr=PaddleOCR(use_angle_cls = True,use_gpu= False) \n",
    "        text=ocr.ocr(\"./test/imgs/demo.png\",cls=True)\n",
    "        text = np.array(text)[0]\n",
    "        for i in text:\n",
    "            for k in i[0]:\n",
    "                k[0]+=v1[0]\n",
    "                k[1]+=v1[1]\n",
    "    else:\n",
    "        text = orc(img='./test/imgs/demo.png')\n",
    "    return text\n",
    "def axis(e):\n",
    "    e = str(e)\n",
    "    x = int(re.search(r'pos: \\((.*), (.*)\\), message:',e).group(1))\n",
    "    y = int(re.search(r'pos: \\((.*), (.*)\\), message:',e).group(2))\n",
    "    return (x,y)\n",
    "def click(path,threshold=threshold,rgb=False,load=0):\n",
    "    if isinstance(path,bool):return False\n",
    "    if isinstance(path,str) :\n",
    "        if info:\n",
    "            start_time = time.time()\n",
    "            log(f\"检测{path}\")\n",
    "        airtest.touch(airtest.Template(filename=f\"./test/imgs/{path}.png\",threshold=threshold,rgb=rgb))\n",
    "        if info:\n",
    "            end_time = time.time()\n",
    "            log(f\"点击{path},耗时{int((end_time-start_time)*1000)}毫秒\")\n",
    "    else:\n",
    "        if path[0]<1:\n",
    "            path = (1080*path[0],2310*path[1])\n",
    "        airtest.touch(path)\n",
    "        log(f\"点击坐标{path}\")\n",
    "    time.sleep(load)\n",
    "    return True\n",
    "def offShow(count=1,load=1.5):\n",
    "    for i in range(count):\n",
    "        airtest.touch((258,90))\n",
    "        log(\"关闭显示\")\n",
    "        time.sleep(load)\n",
    "def back(count=1,load=0):\n",
    "    for i in range(count):\n",
    "        airtest.touch((72,139))\n",
    "        log(\"返回\")\n",
    "        if load==0:\n",
    "            pass\n",
    "        else:\n",
    "            time.sleep(load)\n",
    "def wait(path,threshold=threshold,rgb=False,timeout=60,interval=1):\n",
    "    if info:\n",
    "        start_time = time.time()\n",
    "        log(f\"等待{path}出现\")\n",
    "    loc = airtest.wait(airtest.Template(filename=f\"./test/imgs/{path}.png\",threshold=threshold,rgb=rgb),timeout=timeout,interval=interval,)\n",
    "    if info:\n",
    "        end_time = time.time()\n",
    "        log(f\"已识别{path},耗时{int((end_time-start_time)*1000)}毫秒\")\n",
    "    return loc\n",
    "def exists(path,threshold=threshold,rgb=False,all=False):\n",
    "    if info:\n",
    "        start_time = time.time()\n",
    "        log(f\"检测是否存在{path}\")\n",
    "    if all:\n",
    "        vs = airtest.find_all(airtest.Template(filename=f\"./test/imgs/{path}.png\",threshold=threshold,rgb=rgb))\n",
    "        if vs:\n",
    "            result = [i[\"result\"] for i in vs]\n",
    "            if info:\n",
    "                end_time = time.time()\n",
    "                log(f\"存在{path},坐标{result},耗时{int((end_time-start_time)*1000)}毫秒\")\n",
    "            return  result\n",
    "        else:\n",
    "            if info:\n",
    "                end_time = time.time()\n",
    "                log(f\"不存在{path},耗时{int((end_time-start_time)*1000)}毫秒\")\n",
    "            return False\n",
    "    if info:\n",
    "        start_time = time.time()\n",
    "    loc = airtest.exists(airtest.Template(filename=f\"./test/imgs/{path}.png\",threshold=threshold,rgb=rgb))\n",
    "    if info:\n",
    "        end_time = time.time()\n",
    "        if loc :log(f\"存在{path},坐标{loc},耗时{int((end_time-start_time)*1000)}毫秒\")\n",
    "        else:log(f\"不存在{path},耗时{int((end_time-start_time)*1000)}毫秒\")\n",
    "    return loc\n",
    "def die(path,second = 30,threshold=threshold1,rgb=False):\n",
    "    if info:\n",
    "        start_time = time.time()\n",
    "        log(f\"等待{path}消失\")\n",
    "    end_time = datetime.datetime.now() + datetime.timedelta(seconds=second)\n",
    "    flag = True\n",
    "    while airtest.exists(airtest.Template(filename=f\"./test/imgs/{path}.png\",threshold=threshold,rgb=rgb)):\n",
    "        if datetime.datetime.now() >end_time:\n",
    "            flag = False\n",
    "            log(f\"检测超时\")\n",
    "            raise \"超时\"\n",
    "            break \n",
    "        time.sleep(1.5)\n",
    "    if info:\n",
    "        end_time = time.time()\n",
    "        flag  and log(f\"{path}已消失,耗时{int((end_time-start_time)*1000)}毫秒\")\n",
    "    return flag\n",
    "def text(string:str = \"\",enter:bool=True):\n",
    "    if info:\n",
    "        log(f\"输入{string}\")\n",
    "    airtest.text(string,enter=enter)\n",
    "def sort(arr1,dimension):\n",
    "    if not arr1:\n",
    "        return False\n",
    "    arr = arr1[:]\n",
    "    n = len(arr)\n",
    "    for i in range(n):\n",
    "        flag = True\n",
    "        for j in range(0, n-i-1):\n",
    "            if arr[j][dimension] > arr[j+1][dimension] :\n",
    "                arr[j], arr[j+1] = arr[j+1], arr[j]\n",
    "                flag = False\n",
    "        if flag:break\n",
    "    return arr\n",
    "# 定义一个函数contain，用于判断一个数组arr中是否包含一个数组v，range参数默认值为30\n",
    "def contain(arr,v,range=30):\n",
    "    # 如果v为False，则返回False\n",
    "    if v == False:\n",
    "        return False\n",
    "    # 遍历arr中的每一个元素\n",
    "    for i in arr:\n",
    "        # 如果i[0]-range小于v[0]小于i[0]+range，且i[1]-range小于v[1]小于i[1]+range，则返回(v[0]-20,v[1]+65)\n",
    "        if (i[0]-range)<v[0]<(i[0]+range) and i[1]-range<v[1]<i[1]+range:\n",
    "            return (v[0]-20,v[1]+65)\n",
    "    # 如果遍历完arr中的每一个元素，没有找到满足条件的元素，则返回False\n",
    "    return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "t()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "等待桐行飞讯出现\n",
      "已识别桐行飞讯,耗时156毫秒\n",
      "点击坐标(666, 40)\n",
      "检测打电话\n",
      "点击打电话,耗时319毫秒\n",
      "检测微信打电话\n",
      "点击微信打电话,耗时319毫秒\n",
      "等待通话结束出现\n",
      "已识别通话结束,耗时11918毫秒\n",
      "等待桐行飞讯出现\n",
      "已识别桐行飞讯,耗时4507毫秒\n",
      "点击坐标(666, 40)\n",
      "检测打电话\n",
      "点击打电话,耗时319毫秒\n",
      "检测微信打电话\n",
      "点击微信打电话,耗时293毫秒\n",
      "等待通话结束出现\n",
      "已识别通话结束,耗时5926毫秒\n",
      "等待桐行飞讯出现\n",
      "已识别桐行飞讯,耗时4489毫秒\n",
      "点击坐标(666, 40)\n",
      "检测打电话\n",
      "点击打电话,耗时321毫秒\n",
      "检测微信打电话\n",
      "点击微信打电话,耗时284毫秒\n",
      "等待通话结束出现\n",
      "已识别通话结束,耗时4486毫秒\n",
      "等待桐行飞讯出现\n",
      "已识别桐行飞讯,耗时4597毫秒\n",
      "点击坐标(666, 40)\n",
      "检测打电话\n",
      "点击打电话,耗时305毫秒\n",
      "检测微信打电话\n",
      "点击微信打电话,耗时289毫秒\n",
      "等待通话结束出现\n",
      "已识别通话结束,耗时5935毫秒\n",
      "等待桐行飞讯出现\n",
      "已识别桐行飞讯,耗时4310毫秒\n",
      "点击坐标(666, 40)\n",
      "检测打电话\n",
      "点击打电话,耗时305毫秒\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[20], line 3\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m200\u001b[39m):\n\u001b[0;32m      2\u001b[0m     click(wait(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m桐行飞讯\u001b[39m\u001b[38;5;124m\"\u001b[39m),load\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m3.5\u001b[39m)\n\u001b[1;32m----> 3\u001b[0m     click(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m打电话\u001b[39m\u001b[38;5;124m\"\u001b[39m,load\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m3.5\u001b[39m)\n\u001b[0;32m      4\u001b[0m     click(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m微信打电话\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m      5\u001b[0m     wait(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m通话结束\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "Cell \u001b[1;32mIn[6], line 48\u001b[0m, in \u001b[0;36mclick\u001b[1;34m(path, threshold, rgb, load)\u001b[0m\n\u001b[0;32m     46\u001b[0m     airtest\u001b[38;5;241m.\u001b[39mtouch(path)\n\u001b[0;32m     47\u001b[0m     log(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m点击坐标\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpath\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m---> 48\u001b[0m time\u001b[38;5;241m.\u001b[39msleep(load)\n\u001b[0;32m     49\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    },
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m在当前单元格或上一个单元格中执行代码时 Kernel 崩溃。\n",
      "\u001b[1;31m请查看单元格中的代码，以确定故障的可能原因。\n",
      "\u001b[1;31m单击<a href='https://aka.ms/vscodeJupyterKernelCrash'>此处</a>了解详细信息。\n",
      "\u001b[1;31m有关更多详细信息，请查看 Jupyter <a href='command:jupyter.viewOutput'>log</a>。"
     ]
    }
   ],
   "source": [
    "for i in range(200):\n",
    "    click(wait(\"桐行飞讯\"),load=3.5)\n",
    "    click(\"打电话\",load=3.5)\n",
    "    click(\"微信打电话\")\n",
    "    wait(\"通话结束\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
