summaryrefslogblamecommitdiffstats
path: root/Controller/Controller.py
blob: 8e8f1959687f34d067096727872d286b460108e8 (plain) (tree)

















































                                                                                                

                          
        








































































                                                                                                                                                       














































































                                                                                                                                                                                                                                                 

                                     










































































































                                                                                                                                                            
import sys
import os
import re
import time
import MySQLdb
import subprocess
from time import sleep
from datetime import datetime


stop = "False"
status = ""
dbSStatus = ""

#function to check DBconnection
def FuncDbConnect():

 global dbStatus
 try:
    	mydb = MySQLdb.connect('localhost', 'root', 'randompasswordSQL','GSMTesting');  	
	db=mydb.cursor()
	dbStatus = "Database connection establish"

 except MySQLdb.Error, e:
	print "Error %d: %s" % (e.args[0],e.args[1])


#function to ping
def FuncPing(ip):

 global status
 lifeline = re.compile(r"(\d) received")
 report = ("No response","Partial Response","Alive")

 A=1,2,3
 for host in A:
   
   pingaling = os.popen("ping -q -c2 "+ip,"r")

   while 1:
      line = pingaling.readline()
      if not line: break
      igot = re.findall(lifeline,line)
      if igot:
	       
	status= report[int(igot[0])]

# all Test case function

# Begin test case SIP to GSM RZ

def FuncSip2GsmRz(idTask):
	
	#first make test from sip to gsm1 rz
	try:
		# open SIP caller handler
		command="--command=python SIPCall3.py"
		subprocess.Popen(args=["gnome-terminal", command])
		
		# wait respond from handler
		if sipHandle == "ok":
			
			try:
				command="--command=python GSMCaller.py "+idTask +callerNumber # get test id and caller number
				subprocess.Popen(args=["gnome-terminal", command])
		
			#wait respond from gsm handler

				if gsmHandler == "ok":
					#send destination number to sip caller and signal to start the call
					#wait respond from both of handler
					#if failed, try one more time
						#if still failed, save to db and update variable gsmRz2SIP == "1", so we make automatic test vice versa
					# else: save to database, tell handler to terminated
				else:
					statusTest = "444, GSM Reciever handler no respond"
					#tell handler to terminate
					break

			except ValueError:    
				print "444, Error to open Handler"
				break

		else:
			statusTest = "444, SIP call handler no respond"
			break
				
	except ValueError:    
		print "444, Error to open Handler"

	#seconde make test from sip to gsm2 rz
	try:
		# open SIP caller handler
		command="--command=python SIPCall3.py"
		subprocess.Popen(args=["gnome-terminal", command])
		
		# wait respond from handler
		if sipHandle == "ok":
			
			try:
				command="--command=python GSMCaller.py "+idTask +callerNumber # get test id and caller number
				subprocess.Popen(args=["gnome-terminal", command])
		
			#wait respond from gsm handler

				if gsmHandler == "ok":
					#send destination number to sip caller and signal to start the call
					#wait respond from both of handler
					#if failed, try one more time
						#if still failed, save to db and update variable gsmRz2SIP == "1", so we make automatic test vice versa
					# else: save to database, tell handler to terminated
				else:
					statusTest = "444, GSM Reciever handler no respond"
					#tell handler to terminate
					break

			except ValueError:    
				print "444, Error to open Handler"
				break

		else:
			statusTest = "444, SIP call handler no respond"
			break
				
	except ValueError:    
		print "444, Error to open Handler"	
		
# Begin test case SIP to Local SIP

# Begin test case SIP to Landline

# Begin test case SIP to Ext. GSM

# Begin test case GSM 1 RZ to GSM 2 RZ

# Begin test case GSM 1 RZ to GSM 3 RZ

# Begin test case GSM 2 RZ to GSM 3 RZ

# Begin test case GSM RZ to Ext. GSM

# Begin test case GSM RZ to SIP, if already have SIP - GSM RZ dont do this test. (we can easy update variable gsm2Sip when SIP to gsm rz test succed from 1 to 0 because 1 mean do the test) but if the result failed, do it.

# Begin test case GSM RZ to Landline

# Begin test case GSM RZ to Local SIP

# Begin test case Ext. GSM to SIP, if already have SIP to Ext. GSM don do this. but if the result failed, do it.

# Begin test case Ext. GSM - GSM RZ, if already have GSM to Ext. GSM test, dont do this. but if the result failed, do it.

# Begin test case Landline to GSM RZ, if already have GSM RZ to Landline, dont do this. but if the result failed, do it.

# Begin test case Landline to SIP, if already have SIP to Landline, Dont do this. but if the result failed, do it.

# Main software



# Check DB connection
FuncDbConnect()
if dbStatus = "Database connection establish":
	
	FuncPing("132.230.4.8")
	sipServerStatus = status

	FuncPing("132.230.4.8")
	sipLocalStatus = status
	
	FuncPing("132.230.4.8")
	sipGateStatus = status

	FuncPing("132.230.4.8")
	btsBox1Status = status # it will be on building 101

	FuncPing("132.230.4.8")
	btsBox2Status = status # it will be on the building 52

	FuncPing("132.230.4.8")
	btsBox3Status = status # dont need, since we put the device on RZ building.just direct connect to the server

# Fetch The task information from DB
	db.execute ("Select IDTask, SIP2GSMRZ, SIP2LocSIP, SIP2Lnline, SIP2ExtGSM, GSMRZ2GSMRZ, GSMRZ2ExtGSM, GSMRZ2SIP, GSMRZ2Lnline, GSMRZ2LocSIP, ExtGSM2SIP, ExtGSM2GSMRZ, Lnline2GSMRZ, Lnline2SIP, timeStamp, status from TaskInformation")
	rows = db.fetchall()	

	for row in rows:
		idTask=row[0]
		sip2GsmRz=row[1]
		sip2LocSip=row[2]
		sip2LnLine=row[3]
		sip2ExtGsm=row[4]
		gsmRz2Gsmrz=row[5]
		gsmRz2ExtGsmRz=row[6]
		gsmRz2Sip=row[7]
		gsmRz2LnLine=row[8]
		gsmRz2LocSip=row[9]
		extGsm2Sip=row[10]
		extGsm2GsmRz=row[11]
		lnLine2GsmRz=row[12]
		lnLine2Sip=row[13]
		timeStamp=row [14]
		status=row[15]

	if sip2Gsmrz == "1" and sipServerStatus <> "No response":
		#call Function for Sip to Gsm
		FuncSip2GsmRz(idTask)

	if sip2LocSip == "1" and sipServerStatus <> "No response" and sipLocalStatus <> "No response":
		# call function for sip to local sip
	if sip2LnLine == "1" and sipServerStatus <> "No response" and sipGateStatus <> "No response":
	
	if sip2ExtGsm == "1" and sipServerStatus <> "No response":

	if gsmRz2GsmRz == "1":
		if btsBox1Status <> "No response":
			if btsBox2Status <> "No response":
				#call function gsm 1 rz to gsm 2 rz
			if btsBox3Status <> "No response":
				#call function gsm 1 rz to gsm 3 rz
		if btsBox2Status <> "No response" and btsBox3Status <> "No response":
			# call function gsm 2 rz to gsm 3 rz

	if gsmRz2ExtGsm == "1":
		if btsBox1Status <> "No response": # I'm not sure with this part, maybe better we make 1 script which can cover all the test to external GSM
			#call function gsm 1 rz to o2
			#call function gsm 1 rz to vodaphone
			#call function gsm 1 rz to Tmobile
			#call function gsm 1 rz to Eplus
		if btsBox2Status <> "No response":
			#call function gsm 2 rz to o2
			#call function gsm 2 rz to vodaphone
			#call function gsm 2 rz to Tmobile
			#call function gsm 2 rz to Eplus
		if btsBox3Status <> "No response":
			#call function gsm 3 rz to o2
			#call function gsm 3 rz to vodaphone
			#call function gsm 3 rz to Tmobile
			#call function gsm 3 rz to Eplus
	
	if gsmRz2SIP == "1":
		if btsBox1Status <> "No response": 
			#call function gsm 1 rz to SIP

		if btsBox2Status <> "No response":
			#call function gsm 2 rz to SIP

		if btsBox3Status <> "No response":
			#call function gsm 3 rz to SIP

	if gsmRz2LnLine == "1" and sipGateStatus <> "No response":
		if btsBox1Status <> "No response": 
			#call function gsm 1 rz to landline

		if btsBox2Status <> "No response":
			#call function gsm 2 rz to Landline

		if btsBox3Status <> "No response":
			#call function gsm 3 rz to Landline

	if gsmRz2LocSip == "1" and sipLocalStatus <> "No response":
		if btsBox1Status <> "No response": 
			#call function gsm 1 rz to local SIP

		if btsBox2Status <> "No response":
			#call function gsm 2 rz to local SIP

		if btsBox3Status <> "No response":
			#call function gsm 3 rz to local SIP
	
	if extGsm2SIP == "1" and sipServerStatus <> "No response":
		#call function o2 to SIP
		#call function vodaphone to SIP
		#call function Tmobile to SIP
		#call function Eplus to SIP
	
	if extGsm2GsmRz == "1":
		if btsBox1Status <> "No response": 
			#call function o2 to GSM1 RZ
			#call function vodaphone to GSM1 RZ
			#call function Tmobile to GSM1 RZ
			#call function Eplus to GSM1 RZ
		if btsBox2Status <> "No response":
			#call function o2 to GSM2 RZ
			#call function vodaphone to GSM2 RZ
			#call function Tmobile to GSM2 RZ
			#call function Eplus to GSM2 RZ
		if btsBox3Status <> "No response":
			#call function o2 to GSM3 RZ
			#call function vodaphone to GSM3 RZ
			#call function Tmobile to GSM3 RZ
			#call function Eplus to GSM3 RZ
		
	if lnLine2GsmRz == "1" and sipGateStatus <> "No response":
		
		if btsBox1Status <> "No response": 
			#call function landline to GSM1 RZ

		if btsBox2Status <> "No response":
			#call function landline to GSM2 RZ

		if btsBox3Status <> "No response":
			#call function landline to GSM3 RZ

	if lnLine2Sip == "1" and sipGateStatus <> "No response":
		if sipServerStatus <> "No response":
			#call function landline to SIP	

# will exit if DB connection failed
else:
	sys.exit(5)