6 Star 21 Fork 8

调试中.../Minicontrol

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
MiniTouch.py 4.63 KB
一键复制 编辑 原始数据 按行查看 历史
yao.fu 提交于 2017-09-27 21:57 . 1. 修复断开后端口未关闭的问题
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 20 10:06:11 2016
@author: yao.fu
"""
import socket
import threading
import random
from time import sleep
from CommonUtils import MINI_TOUCH_BINARY_DIR, MINI_TOUCH_BINARY_NAME, MINI_EXECUTE_DEVICE_DIR, \
CMD_GET_VERSION, ABI_PROP, get_common_cmd
from AdbUtils import adb_forward, adb_get_devices, adb_push, adb_chmod, adb_kill_pid, adb_get_prop, adb_execute_shell, \
adb_remove_forward
from DebugHelper import log_debug, LOG_TAG_MINI_TOUCH
HOST = "localhost"
class MiniTouchServer(threading.Thread):
def __init__(self, t_name):
self.mini_touch_process = None
threading.Thread.__init__(self, name=t_name)
def run(self):
print("Mini touch Server start")
cmd = MINI_EXECUTE_DEVICE_DIR + MINI_TOUCH_BINARY_NAME
ret, msg = adb_execute_shell(cmd)
log_debug(LOG_TAG_MINI_TOUCH, "Mini touch Server stop")
class MiniTouch:
def __init__(self):
self.version = -1
self.max_point = -1
self.max_width = -1
self.max_height = -1
self.max_press = -1
self.pid = -1
self.abi = None
self.mini_touch_server = None
self.client_socket = None
self.port = -1
def prepare(self):
device, has = adb_get_devices()
if has is False:
return -1
self.abi, error = adb_get_prop(ABI_PROP)
if self.abi is None and error.strip():
return -2
ret, error = adb_push(MINI_TOUCH_BINARY_DIR + self.abi + "/" + MINI_TOUCH_BINARY_NAME, MINI_EXECUTE_DEVICE_DIR)
ret, error = adb_chmod(777, MINI_EXECUTE_DEVICE_DIR + MINI_TOUCH_BINARY_NAME)
return 0
def start_mini_touch(self):
self.port = random.randint(2000, 50000)
log_debug(LOG_TAG_MINI_TOUCH, "start_mini_touch port ", self.port)
self.start_mini_touch_server()
return self.connect_mini_touch_server(self.port)
def start_mini_touch_server(self):
self.mini_touch_server = MiniTouchServer('mini touch server')
# self.mini_touch_server.setDaemon(False)
self.mini_touch_server.start()
def connect_mini_touch_server(self, port):
sleep(3)
adb_forward(self.port, "minitouch")
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
error = self.client_socket.connect_ex((HOST, port))
log_debug(LOG_TAG_MINI_TOUCH, "connect server ret ", error)
if error == 0:
self.get_info()
return 0
else:
self.stop_mini_touch()
return error
def get_info(self):
if self.client_socket is None:
return
self.client_socket.sendall(CMD_GET_VERSION)
try:
info = self.client_socket.recv(1024)
self.parse_info(info)
ret = self.send_to_server(get_common_cmd())
except:
self.stop_mini_touch()
return -1
return ret
# v 1
# ^ 5 720 1280 0
# $ 10280
def parse_info(self, info):
if len(info) <= 0:
return
ifs = info.splitlines()
if len(ifs) >= 3:
temps = ifs[0].split(" ")
if len(temps) >= 2:
self.version = temps[1]
temps = ifs[1].split(" ")
if len(temps) >= 5:
self.max_point = temps[1]
self.max_width = temps[2]
self.max_height = temps[3]
self.max_press = temps[4]
temps = ifs[2].split(" ")
if len(temps) >= 2:
self.pid = temps[1]
log_debug(LOG_TAG_MINI_TOUCH, "mini touch version: " + str(self.version))
log_debug(LOG_TAG_MINI_TOUCH, "mini touch max point: " + str(self.max_point))
log_debug(LOG_TAG_MINI_TOUCH, "mini touch max width: " + str(self.max_width))
log_debug(LOG_TAG_MINI_TOUCH, "mini touch max height: " + str(self.max_height))
log_debug(LOG_TAG_MINI_TOUCH, "mini touch max press: " + str(self.max_press))
log_debug(LOG_TAG_MINI_TOUCH, "mini touch pid: " + str(self.pid))
def send_to_server(self, cmd):
# log_debug(LOG_TAG_MINI_TOUCH, "send cmd", cmd)
if self.client_socket is None:
return -1
try:
self.client_socket.sendall(str(cmd))
except Exception as e:
self.stop_mini_touch()
log_debug(LOG_TAG_MINI_TOUCH, e)
return -1
return 0
def stop_mini_touch(self):
if self.client_socket is not None:
self.client_socket.close()
self.client_socket = None
adb_remove_forward(self.port)
if self.pid > 1000:
adb_kill_pid(self.pid)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/lookfuyao/minicontrol.git
[email protected]:lookfuyao/minicontrol.git
lookfuyao
minicontrol
Minicontrol
master

搜索帮助