summaryrefslogblamecommitdiffstats
path: root/For Weekly Test/12-09-2011/usbDetect.py
blob: 311ebb2a0e455711039ba1c6bb3eaf4a1a216731 (plain) (tree)





















































































































































                                                                                             
from serial import * #serial port library
import sys

def initLog():
	portLog = os.popen('dmesg | grep \'pl2303 converter now attached to ttyUSB\'').read()

	dmesgfile = open('dmesg.log', 'w')                    
	dmesgfile.write(portLog)   
	dmesgfile.close()
               
def initLine():
	global i
	logfile = open('dmesg.log')
	i = 0

	while 1:
    		line = logfile.readline()
    
    		if not line: 
			break
    		i = i+1
	logfile.close()

def initPort():
	global lines
	x = 0
	logfile = open('dmesg.log')
	while 1:
    		line = logfile.readline()
    		x = x+1
    		if not line: 
			break
    		if x == i:
			lines = line
	logfile.close()

def newConf(device):
	
	portDev = '[/dev/'+port+']\n'
	file.write('['+device+'] ')                 
	file.writelines(portDev)

def searchList():
	global found
	for item in portList:
		if item[1] == port:
			found = 1
		else:
			found = 0
def initO2():

	print "pluged in, O2 Mobile Device [then Enter]"
	input = sys.stdin.readline().rstrip("\r\n")
	scanPort()
	portList.append(['O2', port])
	newConf('O2')

def initVoda():

	print "pluged in, Vodaphone Mobile Device [then Enter]"
	input = sys.stdin.readline().rstrip("\r\n")
	scanPort()
	searchList()
	if found == 1:
		initRepeat()
	if found == 0:
		portList.append(['Voda', port])
		newConf('Voda')
	if repeat == True:
		initVoda()

def initEplus():
	print "pluged in, Eplus Mobile Device [then Enter]"
	input = sys.stdin.readline().rstrip("\r\n")
	scanPort()
	
	searchList()
	if found == 1:
		initRepeat()
	if found == 0:
		portList.append(['Eplus', port])
		newConf('Eplus')
	if repeat == True:
		initEplus()

def initTmobile():

	print "pluged in, T-Mobile Mobile Device [then Enter]"
	input = sys.stdin.readline().rstrip("\r\n")
	scanPort()
	searchList()
	if found == 1:
		initRepeat()
	if found == 0:
		portList.append(['Tm', port])
		newConf('Tm')
	if repeat == True:
		initTmobile()

def initRepeat():
	global repeat
	while True:
		print 'No new device has pluged in'
		print 't = try again, i = ignore, q = quit'
		input = sys.stdin.readline().rstrip("\r\n")
		if input == 'i':
			repeat = False
			break
		elif input == 'q':
			file.close()
			sys.exit()
		elif input == 't':
			repeat = True
			break

def scanPort():
	global port
	initLog()
	initLine()
	initPort()
	port = str(lines[59:66])
	

global file
global portList
portList = list()
while True:
        print "Mobile device port configuration"
        print "Menu:  n = make new configuration, q = quit"

        input = sys.stdin.readline().rstrip("\r\n")
        if input == "n":
		file = open('devicePortList.log', 'w')
		print "== Pluged Out all mobile devices [then Enter] =="
		input = sys.stdin.readline().rstrip("\r\n")

		initO2()
		initVoda()
		initEplus()
		initTmobile()

		file.close()

	if input == "q":
		break