python3 automatically identifies the connection status of usb that is the judgment method for usb reconnection

  • 2021-07-09 08:34:36
  • OfStack

When doing automated testing, you need to judge whether usb is connected in two situations (note, this article only uses adb command to use control mobile phone)

One is to start testing (the premise is to test multiple devices at the same time), and hope to wait for all devices usb to be identified and then test at the same time. For this one, of course, you can open another window to continuously input adb devices for testing, but it is not enough for AI.

One is that sometimes usb will be disconnected automatically during the test. At this time, if it is not judged whether usb is reconnected, the test will be executed further, and the test specifies that it failed.

In the first case, the idea is to use adb devices to continuously obtain the currently connected device id.

1. Get 1 devlist first and calculate the number of devices currently connected.

2. Using while cycle, when an devlist is retrieved again, the number of devices is greater than the previous number of devices, then new devlist is traversed, and when new device is not in old devlist, deviceid is produced from print, which realizes real-time identification and printing of usb.

The specific implementation is as follows:


import os
from time import sleep
 
#  Acquisition device id List 
def getdevlist():
  devlist = []
  connectfile = os.popen('adb devices')
  list = connectfile.readlines()
  # print(list)
  for i in range(len(list)):
    if list[i].find('\tdevice') != -1:
      temp = list[i].split('\t')
      devlist.append(temp[0])
  return devlist
 
 
connectdevice = input(' Please enter the number of devices you want to connect at the same time :')
number = int(connectdevice.strip())
 
while True:
  lists = getdevlist()
  devnum = len(lists)
  id = 1
  tempdevlist = getdevlist()
  if devnum < number:
    print(f'\n If the equipment is not fully identified, it should be identified {number} Taiwan equipment !\n Currently recognized {devnum} Taiwan equipment , Please connect the device and wait for identification :\n\n')
    for i in range(devnum):
      print(f' Equipment {id}: {lists[i]}')
      id = id + 1
  #  Wait for all devices to be identified 
  while devnum < number:
    lists = getdevlist()
    curnum = len(lists)
    if curnum > devnum:
      for i in range(len(lists)):
        if lists[i] not in tempdevlist:
          print(f' Equipment {id}: {lists[i]}')
          id = id + 1
          tempdevlist = getdevlist()
      devnum = curnum
    
  print(f'\n All devices are fully identified ! There is currently a connection {len(getdevlist())} Taiwan equipment .\n\n')

Case 2: The premise of the second case is that the deviceid of the currently connected device is known. This is not difficult to understand and obtain. Its thinking is not much different from that of the first case, But I encapsulated it as a function. Application scenario in fact, in the actual application process, There are always one or two places, and the probability of usb is disconnected and then automatically connected, but this will fail to execute. Therefore, it is necessary to know clearly the scene with such probability of fail before reconnecting in the appropriate place when judging, so as to make the code concise.


import os
from time import sleep
 
def reconnectAction(deviceid):
  devlist = getdevlist()
  print(f' Equipment {deviceid} Attempting to reconnect .')
  id = 1
  while deviceid not in devlist:
    print(f' No. 1 {id} Times  ', end = ' ')
    devtuple = getdevlist()
    id = id + 1
  print(f'\n Equipment {deviceid} Connection reestablished successfully .')
  sleep(1)
 
 
# example:
if __name__ == '__main__':
  deviceid = '12lk34fkjaaf'
  if clickScreen(openfilemanager, deviceid) != 0:
    print(f'{openfilemanager[1]}: Execution failed ' )
    reconnectAction(deviceid)
    clickScreen(openfilemanager, deviceid)

OK, mission accomplished.


Related articles: