summaryrefslogtreecommitdiffstats
path: root/For Weekly Test/30-07-2011/GSMHandler.py
blob: a5b61c3d3948714cd59e9b9e4d3d5075212c547c (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import ServerClass
import GSMClass
import LogFileClass
from time import sleep
import sys

global portListen
global portAddress


if len(sys.argv) <> 3:
	print "Error given command"
	sys.exit(2)


portListen = int(sys.argv[1])
portAddress = sys.argv[2]

def initLogfile():
	global nameOfLogFile

	if portListen == 50007:
		nameOfLogFile = 'GSM RZ1 handler.log'

	elif portListen == 50010:
		nameOfLogFile = 'GSM O2 handler.log'

	elif portListen == 50011:
		nameOfLogFile = 'GSM Vodaphone handler.log'

	elif portListen == 50012:
		nameOfLogFile = 'GSM TMobile handler.log'

	elif portListen == 50013:
		nameOfLogFile = 'GSM EPlus handler.log'
	else:
		print "No port listening found"


baudRate = 19200

initLogfile()
logger = LogFileClass.Logging(nameOfLogFile)

errorCount = 0

logger.logEvent('')

whileCounter =0

#define global varibales
global lastState
global resetState
lastState = 0
resetState = 0


def initSystem():
	print 'init system'	
	
	global handlerSocket
	global gsmDevice
	global initTalkVar
	global lastState

	global numberToCall
	global resetState

	initTalkVar = 0 #variable used to know if we initialized the start talk
	lastState = 0 #variable used to know
	numberToCall = '000' #number to call 
	resetState = 0
	
	handlerSocket = ServerClass.ServerHandler(portListen)
	logger.logEvent('LISTEN ON PORT: '+str(portListen))

	#add this if you need it
	gsmDevice = GSMClass.serialPort(portAddress, baudRate, 45)

	initDevice = gsmDevice.portInit()
	########################################################

	#add nice formating to the log file :)
	anyConnection = handlerSocket.openSocket()


	if anyConnection == 1 and initDevice == 1:
		logger.logEvent('CONNECTION ESTABLISHED AND DEVICE WORKING: ' + str(handlerSocket.connectedTo()))
		return 1

	elif anyConnection == 1 and initDevice == 0:
		logger.logEvent('$connection established but device not working: ' + str(handlerSocket.connectedTo()))
		terminateConnection()
		return 2
	else:
		logger.logEvent('$no connection')
		sys.exit(1)
		return 0	

def receiveMessage(timeout):

	message = str(handlerSocket.receiveData(timeout))
	
	if message != '0' and message !='NO DATA': 
		print 'in receive message', message, lastState	
	
		if message == 'HELLO HANDLER' and lastState == 0:
			outcome = initTalk()
			if outcome == 1:
				logger.logEvent('TALK INITIALIZATION SENT')
			else:
				logger.logEvent('$talk initialization not sent: ' + str(outcome))

		elif message == 'RECEIVER' and lastState == 1:
			outcome = initReceiver()
			if outcome == 1:
				logger.logEvent('RECEIVER READY SENT')
			else:
				logger.logEvent('$receiver ready not sent: ' + str(outcome))

		elif message == 'RECEIVE START' and lastState == 2:
			outcome = receiveStart()
			if outcome == 1:
				logger.logEvent('RECEIVE STATUS REPORTED')
			else:
				logger.logEvent('$receive status not reported: ' + str(outcome))

		elif message[0:6] == 'CALLER' and lastState == 1:
			outcome = initCaller() 

			global numberToCall
			numberToCall = message[7:]
			if outcome == 1:
				logger.logEvent('CALLER READY SENT')
			else:
				logger.logEvent('$caller ready not sent: ' + str(outcome))
			
		elif message == 'CALL START' and lastState == 4:
			outcome = callStart(numberToCall)
			if outcome == 1:
				logger.logEvent('CALLER STATUS SENT')
			else:
				logger.logEvent('$caller status not sent: ' + str(outcome))

		elif message == 'TERMINATE CONNECTION' and (lastState == 5 or lastState == 3):
			outcome = terminateConnection()
			if outcome == 1:
				logger.logEvent('TERMINATED THE CONNECTION AFTER TEST')
			else:
				logger.logEvent('$connection could not be terminated after the test: ' + str(outcome))
		
		elif message == 'TERMINATE CONNECTION':
			outcome = terminateConnection()
			if outcome == 1:
				logger.logEvent('TERMINATED THE CONNECTION IN MIDDLE. IN STATE: ' + str(lastState) )
			else:
				logger.logEvent('$connection could not be terminated in middle: ' + str(outcome) + ' in state: ' + str(lastState))
		else:
			outcome = other()
			logger.logEvent('other appeared')
			terminateConnection()
			

	return 1
	#return 0

########INIT TALK PART########
def initTalk():
	print 'init talk'

	#initialize the talk between handler and controller
	global lastState
	lastState = 1
	sendMessage = handlerSocket.sendData('HELLO CONTROLLER')
	return sendMessage
##############################


########RECEIVE PART########
def initReceiver():
	print 'initReceiver'

	#init function to initialize the receiver
	global lastState
	lastState = 2	
	sendMessage = handlerSocket.sendData('RECEIVER READY')
	return sendMessage

def receiveStart():
	print 'receiveStart'

	#wait for a call and report if you received it and it was successfull or not
	global lastState
	lastState = 3
	receiveCall = gsmDevice.receiveCall()

	if receiveCall == 1:
		callSuccess = 'CALL OK'
	else:
		callSuccess = 'CALL NOT OK'
	
	tryHangUp = gsmDevice.hangUp()

	sendMessage = handlerSocket.sendData(callSuccess)
	return sendMessage
############################


########CALL PART########
def initCaller():
	print 'initCaller'

	#initialize caller here
	global lastState
	lastState = 4
	sendMessage =  handlerSocket.sendData('CALLER READY')
	return sendMessage

def callStart(numberToCall):
	print 'initCaller'
	
	#call the number here
	global lastState
	lastState = 5

	callSuccess = 'CALL NOT OK'

	tryCall = gsmDevice.callNumber(numberToCall)
	if tryCall != 1:
		callSuccess = 'CALL NOT OK'
	else:
		sleep(2)
		activeCall = gsmDevice.currentCall()
		counter = 0
		while(activeCall!=1):
			sleep(1)
			activeCall = gsmDevice.currentCall()
			if counter == 6:
				break
			counter += 1
		
		if activeCall == 1:
			callSuccess = 'CALL OK'
		else:
			callSuccess = 'CALL NOT OK'

	tryHangUp = gsmDevice.hangUp()

	return handlerSocket.sendData(callSuccess)
#########################


########TERMINATE PART########
def terminateConnection():
	print 'terminate connection'
	global resetState
	close = handlerSocket.closeConnection()
	resetState = 1
	sys.exit(0.5)
	return close
##############################

########TERMINATE PART########
def other():
	print 'other'
	global lastState
	global resetState
	
	close = handlerSocket.closeConnection()
	lastState = 8
	resetState = 1
	return 1
##############################



while 1:
	test = initSystem()
	if test == 1:
		print 'initialized system'
		receivedMessage = 0
		while receivedMessage < 4 and resetState!= 1:
			receivedMessage += receiveMessage(10)

	del handlerSocket
	del gsmDevice