Skip to content

Commit

Permalink
feat(teraterm mode): Support waitln command
Browse files Browse the repository at this point in the history
  • Loading branch information
ShuangLiang-OSS committed Mar 4, 2024
1 parent f6e4bbe commit ca66bd5
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 2 deletions.
20 changes: 20 additions & 0 deletions pyautoport/addon/adb.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def __init__(self):
self.running = False
self.cmd = 'adb shell'
ADBStrategy._first_init = False
self.read = {"exist": False, "find": '', "found": ''}

def _start_thread(self):
self.running = True
Expand Down Expand Up @@ -138,6 +139,10 @@ def _read(self, port):
f.write(read_data.replace('\r\n', '\n').replace('\r', ''))
f.flush()
has_message = True
if self.read["find"] != '' and self.read["find"] in read_data:
self.read["exist"] = True
self.read["found"] = read_data.replace('\r\n', '\n').replace('\r', '')
self.read['find'] = ''
else:
has_message = False
time.sleep(0.1)
Expand Down Expand Up @@ -185,6 +190,21 @@ def send_data_to_log(self, data):
f.write(data.encode('utf-8'))
f.write('\n'.encode('utf-8'))

def read_data(self, data):
""" read data via adb session """
start_time = time.time()
self.read = {"exist": False, "find": data, "found": ''}
while self.running:
if self.read["exist"]:
print(f'>>>>>>>>>> Found message: {self.read["found"]}')
return True
current_time = time.time() - start_time
if current_time >= self.timeout:
print('>>>>>>>>>> Not Found message, Timeout occureed!!!')
return False
print('>>>>>>>>>> Read log Thread not exist, Please check it.')
return False

def disconnect(self):
""" disconnect from adb """
if self.running:
Expand Down
4 changes: 4 additions & 0 deletions pyautoport/addon/addon.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ def send_data(self, data):
def send_data_to_log(self, data):
""" abstractmentod for send date to logfile """

@abstractmethod
def read_data(self, data):
""" abstractmentod for read data """

@abstractmethod
def disconnect(self):
""" abstractmentod for disconnect """
20 changes: 20 additions & 0 deletions pyautoport/addon/tty.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def __init__(self):
self.port = None
self.running = False
TTYStrategy._first_init = False
self.read = {"exist": False, "find": '', "found": ''}

def _start_thread(self):
self.running = True
Expand Down Expand Up @@ -86,6 +87,10 @@ def _read(self):
f.write(output.replace('\r\n', '\n').replace('\r', ''))
f.flush()
has_message = True
if self.read["find"] != '' and self.read["find"] in output:
self.read["exist"] = True
self.read["found"] = output.replace('\r\n', '\n').replace('\r', '')
self.read['find'] = ''
else:
has_message = False
time.sleep(0.1)
Expand Down Expand Up @@ -146,6 +151,21 @@ def send_data_to_log(self, data):
f.write(data.encode('utf-8'))
f.write('\n'.encode('utf-8'))

def read_data(self, data):
""" read data via uart session """
start_time = time.time()
self.read = {"exist": False, "find": data, "found": ''}
while self.running:
if self.read["exist"]:
print(f'>>>>>>>>>> Found message: {self.read["found"]}')
return True
current_time = time.time() - start_time
if current_time >= self.timeout:
print('>>>>>>>>>> Not Found message, Timeout occureed!!!')
return False
print('>>>>>>>>>> Read log Thread not exist, Please check it.')
return False

def disconnect(self):
""" disconnect from uart """
if self.running:
Expand Down
4 changes: 4 additions & 0 deletions pyautoport/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ def send_data_to_log(self, data):
""" send data to log """
self.strategy.send_data_to_log(data)

def read_data(self, data):
""" read data """
self.strategy.read_data(data)

def disconnect(self):
""" invoke disconnect """
self.strategy.disconnect()
14 changes: 14 additions & 0 deletions pyautoport/teraterm.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ def recv_handle(session, queue_recv):
time.sleep(float(data[data_index+1:]))
if data_function == 'send_log' and check_connection(session):
send_log(session, data[data_index+1:])
if data_function == 'wait_log' and check_connection(session):
wait_log(session, data[data_index+1:])
if data_function == 'logstop' and check_connection(session):
set_log(session, None, False)
time.sleep(0.3)
Expand Down Expand Up @@ -177,6 +179,10 @@ def send_log(session, text):
""" send text into log in session """
session.send_data_to_log(text)

def wait_log(session, text):
""" wait text in log in session """
session.read_data(text)

def client_socket_send(cmd, need_close=False):
""" checkt client socket has been created before send command """
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Expand Down Expand Up @@ -268,6 +274,14 @@ def send_log_via_bash():
text = ' '.join(args.text)
client_socket_send(f'itsend_log@{text}\n'.encode())

def wait_log_via_bash():
""" Python or Bash entry for wait string exist in log or timeout occureed """
parser = argparse.ArgumentParser()
parser.add_argument('text', nargs='+', help='Text to wait')
args = parser.parse_args()
text = ' '.join(args.text)
client_socket_send(f'itwait_log@{text}\n'.encode())

def session_stop():
""" Python or Bash entry for stop session """
client_socket_send('itstop@\n'.encode(), True)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
'logstop = pyautoport.teraterm:stop_log_via_bash',
'set_timestamp = pyautoport.teraterm:set_timestamp_via_bash',
'set_timeout = pyautoport.teraterm:set_timeout_via_bash',
'waitln = pyautoport.teraterm:wait_log_via_bash',
'disconnect = pyautoport.teraterm:disconnect_via_bash',
'session_stop = pyautoport.teraterm:session_stop',
],
Expand Down
16 changes: 14 additions & 2 deletions tests/test_session.bat
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ echo [Testing adb open and send]
connect adb
send ls
disconnect adb
powershell -Command "sleep -m 3000"
echo ### CONFIRM adb seesion not exists ###
powershell -Command "sleep -m 1000"
tasklist| findstr /i %process_name_adb%

echo [Testing double open and send]
Expand All @@ -35,16 +35,27 @@ echo [Testing switch session]
connect adb
send ls
connect uart
powershell -Command "sleep -m 1000"
mpause 1000
echo ### CONFIRM current session is uart ###
send ls

echo [Testing log display]
connect adb
set_timestamp
send ls
echo ### COMFIRM it takes 2sec to send log ###
mpause 2000
send ls
set_timeout 2
send "ping 127.0.0.1 -c 10"
echo ### COMFIRM check waitln succeed ###
waitln bytes

echo [Testing saving log]
logstart test.log
send pwd
logwrite ">>> log stop"
logstop
powershell -Command "sleep -m 1000"
echo ### CONFIRM saving log file exists ###
dir| findstr /i test.log
Expand All @@ -54,6 +65,7 @@ session_stop
powershell -Command "sleep -m 3000"
echo ### CONFIRM if all session process exists ###
tasklist| findstr /i %process_name_adb%
powershell -Command "sleep -m 3000"
tasklist| findstr /i %process_name_start%

pause
5 changes: 5 additions & 0 deletions tests/test_session.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,16 @@ send 'ls'
echo "### COMFIRM it takes 2sec to send log ###"
mpause 2000
send 'ls'
set_timeout 2
send 'ping 127.0.0.1 -c 10'
echo "### COMFIRM check waitln succeed ###"
waitln 'bytes'

sleep 3
echo "[Testing saving log]"
logstart test.log
send 'pwd'
logwrite '>>> log stop'
logstop
echo "### CONFIRM saving log file exists ###"
sleep 1
Expand Down

0 comments on commit ca66bd5

Please sign in to comment.