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