Newer
Older
Import / projects / LGN-IP3870 / t / orig / trapManager.py
import os
from setting import setting
import status
from runtime import mmiDebug as MD 
import config
import runtime

class TrapHandler:
	LEVEL_OFF = 1
	LEVEL_ERROR = 2
	LEVEL_QOS = 3
	LEVEL_ERROR_AND_QOS = 4
	LEVEL_ALWAYS_SEND = 5

	EVENT_NORMAL = 1
	EVENT_ERROR = 2
	EVENT_QOS_WARNING = 3

	REG_SUCCESS = 1
	REG_FAILURE = 2
	
	def __init__(self):
		self.q850 = Q850()

		self.callInfoControlLevel = setting.callinfo_trap_control
		self.callStatisticsTrapInterval = setting.call_statistics_trap_control

		self.callResult = 2

		self.firstRegistration = True
		self.registrationStatus = self.REG_SUCCESS

		setting.set_voipOrgCall(0)
		setting.set_voipOrgCallTime(0)
		setting.set_voipTermCall(0)
		setting.set_voipTermCallTime(0)
		setting.set_callSucc(0)
		setting.set_callErr(0)
		setting.set_errorCall(0)
		setting.set_dropCall(0)

		self.callStatisticstrapTimer = None
		#self.startCallStatisticsTrapTimer()

	def setRegisterStatus(self, registrationStatus):
		self.registrationStatus = registrationStatus
		
	def startCallStatisticsTrapTimer(self, callStatisticsTrapInterval=None):
		MD.mmiTrace('TrapHandler:start or restart call statistics Trap')

		def sendCallStatisticsTrap():
			runtime.trapHandler.callStatisticsTrap()
			self.callStatisticstrapTimer = utils.Timer(self.callStatisticsTrapInterval, sendCallStatisticsTrap)

		import utils
		if callStatisticsTrapInterval !=0 :
			self.callStatisticsTrapInterval = callStatisticsTrapInterval
			self.callStatisticstrapTimer = utils.Timer(self.callStatisticsTrapInterval, sendCallStatisticsTrap)
		else:
			self.callStatisticstrapTimer = None

	def setCallResult(self, callResult):
		self.callResult = callResult
		
	def linkUpTrap(self):
		if not config.trap:
			return
		#os.system('linkup_noti.sh &')	

	def sendSipRegisterTrap(self, registrationStatus, reasonCode=200):
		if self.firstRegistration:
			self.firstRegistration = False
			if registrationStatus == self.REG_SUCCESS:
				self.sipRegisteredTrap()
			else:
				self.sipUnregisteredTrap(reasonCode)
			self.registrationStatus = registrationStatus
		else:
			if self.registrationStatus != registrationStatus:
				if registrationStatus == self.REG_SUCCESS:
					self.sipRegisteredTrap()
				else:
					self.sipUnregisteredTrap(reasonCode)
				self.registrationStatus = registrationStatus
	
	def sipRegisteredTrap(self):
		if not config.trap:
			return

		MD.mmiTrace('REG ============= ***')
		#os.system('sipregistered_noti.sh 1 &')

	def sipUnregisteredTrap(self, sipReasonCode):
		if not config.trap:
			return

		MD.mmiTrace('UNREG ============= sipReasonCode = ', sipReasonCode)
		code = self.q850.getQ850Reason(sipReasonCode)
		status.reason_code = str(code)
		#command = 'sipunregistered_noti.sh 1 ' + str(code) + ' &'
		#os.system(command)

	def getCallInfoControlLevel(self):
		return setting.callinfo_trap_control

	def getTrapEventType(self, response):
		eventType = 0
		callJitter = 0
		callDelay = 0
		callLoss = 0

		try:
			eptStatFile = open('/tmp/ept_stat.txt')
			for line in eptStatFile:
				if line.find(':')  != -1:
					name, value = line.split(':')
					if name == 'egrRtpJitter':
						callJitter = int(value)
					elif name == 'igrRtpLatency':
						callDelay = int(value)
					elif name == 'egrRtpSesLostPkt':
						callLoss = int(value)
			eptStatFile.close()
		except:
			callJitter = 0
			callDelay = 0
			callLoss = 0
			
		if status.callErrorSet:
			eventType = self.EVENT_ERROR
			status.callErrorSet = False	
		elif not response:
			eventType = self.EVENT_NORMAL	
		elif response-200 < 100:
			eventType = self.EVENT_NORMAL
		else:
			eventType = self.EVENT_ERROR

		if callDelay > 150 or callJitter > 50 or callLoss > 1:
			eventType = self.EVENT_QOS_WARNING

		return [eventType, callJitter, callDelay, callLoss]	
			
	def callInfoTrap(self, response):
		if not config.trap:
			return
			
		eventType, callJitter, callDelay, callLoss = self.getTrapEventType(response)
		callResult = self.callResult
		self.callResult = 2

		MD.mmiTrace('response = ', response)
		if response:
			callReleaseReason = self.q850.getQ850Reason(response)
		else:
			callReleaseReason = 200			

		sendNow = False
		trapLevel = self.getCallInfoControlLevel()
		if trapLevel == self.LEVEL_OFF:
			sendNow = False
		elif trapLevel == self.LEVEL_ERROR:
			if eventType == self.EVENT_ERROR:
				sendNow = True
		elif trapLevel == self.LEVEL_QOS:
			if eventType == self.EVENT_QOS_WARNING:
				sendNow = True
		elif trapLevel == self.LEVEL_ERROR_AND_QOS:
			if eventType == self.EVENT_ERROR or eventType == self.EVENT_QOS_WARNING:
				sendNow = True
		else:
			sendNow = True

		MD.mmiTrace('trapLevel = ', trapLevel)
		MD.mmiTrace('eventType = ', eventType)
		MD.mmiTrace('callResult = ', callResult)
		MD.mmiTrace('callReleaseReason = ', callReleaseReason)
		MD.mmiTrace('callDelay = ', callDelay)
		MD.mmiTrace('callJitter = ', callJitter)
		MD.mmiTrace('callLoss = ', callLoss)

		if sendNow:
			#command = '/usr/local/bin/callinfo_noti.sh 1 %d %d %d %d %d %d &' % (eventType, callResult, callReleaseReason, callDelay, callJitter, callLoss)
			#os.system(command)
			pass

	def getCallStatisticsTrapInterval(self):
		return setting.call_statistics_trap_control
		
	def callStatisticsTrap(self):
		if not config.trap:
			return

		from vdcisetting import vdci_setting
		handytelno = vdci_setting.tel_num

		#command = 'callstatistics_noti.sh 1 \"%s\" %d %d %d %d %d %d %d &'  % (handytelno, setting.voipOrgCall, setting.voipTermCall, \
		#	setting.voipOrgCallTime/60, setting.voipTermCallTime/60, setting.callSucc, setting.callErr, setting.dropCall) 
		
		#MD.mmiTrace('callStatisticsTrap::command::', command)
		#os.system(command)
	
class Q850:
	DICT_SIP_TO_Q850 = { -1:127, \
						 200:200, \
						 400:127, 401:57, 402:21, 403:57, 404:1, 405:127, 406:127, 407:21, \
						 408:102, 409:41, 410:1, 411:127, 413:127, 414:127, 415:79, 420:127, \
						 480:18, 481:127, 482:127, 483:127, 484:28, 485:1, 486:17, 487:127, 488:127, \
						 500:41, 501:79, 502:38, 503:63, 504:102, 505:127, 580:47, \
						 600:17, 603:21, 604:1, 606:58 }
	def __init__(self):
		pass

	def getQ850Reason(self, sipReasonCode):
		result = self.DICT_SIP_TO_Q850.get(sipReasonCode)
		if result:
			return result
		else:
			if result > 400 and result <500:
				return 127 # 400
			elif result > 500 and result < 600:
				return 41 # 500
			elif result > 600 and result < 700:
				return 17 # 600