summaryrefslogtreecommitdiffstats
path: root/For Weekly Test/12-09-2011/usbDetect.py
blob: 695b59c373e1da41c00ae1aee15041e3f9cdd9c0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from serial import * #serial port library
from time import sleep
import sys
import checkingNumberClass

def initLog():
	sleep(0.5)
	portLog = os.popen('dmesg | grep \'pl2303 converter now attached to ttyUSB\'').read()
	sleep(0.5)
	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()
	print lines

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

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()
	elif found == 0:
		portList.append(['Voda', port])
		newConf('Voda')
	elif 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()
	elif found == 0:
		portList.append(['Eplus', port])
		newConf('Eplus')
	elif 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
sys.exit()