1# -*- coding: utf-8 -*- 2# Copyright (c) 2023 Huawei Device Co., Ltd. 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import time 16import re 17import encodings 18import subprocess 19import argparse 20import os 21import cv2 22import pytesseract 23from ast import parse 24from PIL import Image 25 26 27def enter_cmd(mycmd, waittime): 28 if mycmd == "": 29 return 30 global cmd_retry_cnt 31 cmd_retry_cnt = 1 32 enter_cmd_retry = 2 33 while enter_cmd_retry: 34 enter_cmd_retry -= 1 35 try: 36 p = subprocess.Popen(mycmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 37 result, unused_err = p.communicate(timeout=25) 38 try: 39 result=result.decode(encoding="utf-8") 40 except UnicodeDecodeError: 41 result=result.decode('gbk', errors='ignore') 42 break 43 except Exception as e: 44 result = 'retry failed again' 45 print(e) 46 cmd_retry_cnt += 1 47 p.kill() 48 if waittime != 0: 49 time.sleep(waittime) 50 print(result) 51 return result 52 53 54def enter_shell_cmd(shellcmd, waittime, sn): 55 global sn1 56 global sn2 57 if shellcmd == "": 58 return 59 cmd = "hdc_std -t {} shell \"{}\"".format(sn, shellcmd) 60 return enter_cmd(cmd, waittime) 61 62 63def file_from_dev(src, dst, sn): 64 cmd = "hdc_std -t {} file recv \"{}\" \"{}\"".format(sn, src, dst) 65 return enter_cmd(cmd, 2) 66 67 68def get_devices_sn(): 69 global sn1 70 global sn2 71 cmd_sn = os.popen("hdc_std list targets").read() 72 device_sn = re.findall('[\w+]{32}', cmd_sn) 73 sn1 = device_sn[0] 74 sn2 = device_sn[1] 75 print(sn1) 76 print(sn2) 77 78 79def orc(path): 80 pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe' 81 tessdata_dir_config = '--tessdata-dir "C:\\Program Files (x86)\\Tesseract-OCR\\tessdata"' 82 image = Image.open(path) 83 code = pytesseract.image_to_string(image, config=tessdata_dir_config) 84 return code 85 86 87def crop_picture(pic, target, crop_range): 88 pic_path = "{}\\{}".format(args.path, pic) 89 save_path = "{}\\{}".format(args.path, target) 90 im = cv2.imread(pic_path) 91 im = im[crop_range[0]:crop_range[1], crop_range[2]:crop_range[3]] 92 cv2.imwrite(save_path, im) 93 94 95def distributed_calc(): 96 time_one = 1 97 time_two = 2 98 time_four = 4 99 crop_range = [520, 585, 250, 460] 100 enter_shell_cmd("aa start -a ohos.samples.distributedcalc.MainAbility -b ohos.samples.distributedcalc",\ 101 time_four, sn1) 102 enter_shell_cmd("uinput -M -m 500 1130 -c 0", time_two, sn1) 103 enter_shell_cmd("uinput -M -m 500 1130 -c 0", time_two, sn1) 104 enter_shell_cmd("uinput -M -m 610 110 -c 0", time_two, sn1) 105 enter_shell_cmd("uinput -M -m 380 1150 -c 0", time_two, sn1) 106 enter_shell_cmd("uinput -M -m 610 110 -c 0", time_two, sn1) 107 enter_shell_cmd("snapshot_display -f /data/distributedcalc.jpeg", time_two, sn1) 108 file_from_dev("/data/distributedcalc.jpeg", "{}\\distributedcalc.jpeg".format(args.path), sn1) 109 enter_shell_cmd("uinput -M -m 580 1090 -c 0", time_two, sn1) 110 enter_shell_cmd("uinput -M -m 520 520 -c 0", time_two, sn2) 111 enter_shell_cmd("uinput -M -m 520 520 -c 0", time_two, sn2) 112 enter_shell_cmd("snapshot_display -f /data/pin.jpeg", time_two, sn2) 113 file_from_dev("/data/pin.jpeg", "{}\\pin.jpeg".format(args.path), sn2) 114 crop_picture("pin.jpeg", "pin_code.jpeg", crop_range) 115 enter_shell_cmd("uinput -M -m 340 530 -c 0", time_two, sn1) 116 enter_shell_cmd("uinput -M -m 340 530 -c 0", time_two, sn1) 117 enter_shell_cmd("uinput -M -m 60 1145 -c 0", time_two, sn1) 118 code = orc(f"{args.path}\\pin_code.jpeg") 119 pin_code = re.findall("[0-9]{6}", code)[0] 120 print(pin_code) 121 for i in pin_code: 122 if i == "0": 123 enter_shell_cmd("uinput -M -m 672 800 -c 0", time_one, sn1) 124 else: 125 j = int(i) - 1 126 dx = 42 + j * 70 127 enter_shell_cmd(f"uinput -M -m {dx} 800 -c 0", time_one, sn1) 128 time.sleep(1) 129 enter_shell_cmd("uinput -M -m 60 1145 -c 0", time_two, sn1) 130 enter_shell_cmd("uinput -M -m 500 600 -c 0", time_two, sn1) 131 enter_shell_cmd("killall ohos.samples.distributedcalc", time_two, sn1) 132 enter_shell_cmd("aa start -a ohos.samples.distributedcalc.MainAbility -b ohos.samples.distributedcalc",\ 133 time_four, sn1) 134 enter_shell_cmd("uinput -M -m 610 110 -c 0", time_two, sn1) 135 enter_shell_cmd("uinput -M -m 580 1090 -c 0", time_two, sn1) 136 enter_shell_cmd("uinput -M -m 580 1090 -c 0", time_two, sn1) 137 enter_shell_cmd("uinput -M -m 500 1130 -c 0", time_two, sn2) 138 enter_shell_cmd("aa dump -a | grep distributedcalc > /data/distributed_report.txt", time_two, sn2) 139 file_from_dev("/data/distributed_report.txt", "{}\\distributed_report.txt".format(args.path), sn2) 140 enter_shell_cmd("killall ohos.samples.distributedcalc", time_two, sn2) 141 enter_shell_cmd("killall ohos.samples.distributedcalc", time_two, sn1) 142 143 144if __name__ == "__main__": 145 parser = argparse.ArgumentParser(description='manual to this script') 146 parser.add_argument('--path', type=str, default = '.') 147 args = parser.parse_args() 148 149 sn1 = "" 150 sn2 = "" 151 get_devices_sn() 152 distributed_calc()