Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python -u
- # -*- coding: iso8859-1 -*-
- import logging
- logging.basicConfig()
- logging.getLogger("").setLevel(logging.DEBUG)
- log = logging.getLogger("AMI")
- from twisted.internet import reactor
- from starpy import manager
- class MyAMIFactory(manager.AMIFactory):
- amiWorker = None
- def __init__(self, username, password, worker):
- log.info("Inicializando MyAMIFactory...")
- self.amiWorker = worker
- manager.AMIFactory.__init__(self, username, password)
- def clientConnectionLost(self, connector, reason):
- log.error("Connection Lost, reason: %s" % reason.value)
- self.amiWorker.onLoseConnection(reason)
- reactor.callLater(10, self.amiWorker.connect)
- def clientConnectionFailed(self, connector, reason):
- log.error("Connection Lost, reason: %s" % reason.value)
- self.amiWorker.onLoseConnection(reason)
- reactor.callLater(10, self.amiWorker.connect)
- class AMIWorker():
- connected = False
- amiFactory = None
- ami = None
- ami_host = None
- ami_port = None
- ami_user = None
- ami_pass = None
- def __init__(self, hostname = '127.0.0.1', port = 5038, username = 'ami_user', password = 'ami_pass'):
- log.info("Inicializando AMIWorker...")
- self.ami_host = hostname
- self.ami_port = port
- self.ami_user = username
- self.ami_pass = password
- self.amiFactory = MyAMIFactory(self.ami_user, self.ami_pass, self)
- self.handlers = {
- "Newchannel" : self._handleNewchannel,
- "Hangup" : self._handleHangup
- }
- reactor.callLater(1, self.connect)
- def connect(self):
- log.info("Tentando se conectar a %s:%s" % (self.ami_host, self.ami_port))
- d = self.amiFactory.login(self.ami_host, self.ami_port)
- d.addCallback(self.onLoginSuccess)
- d.addErrback(self.onLoginFailure)
- return d
- def onLoginSuccess(self, ami):
- log.info("Autenticacao bem sucedida...")
- self.ami = ami
- self.connected = True
- for event, handler in self.handlers.items():
- self.ami.registerEvent(event, handler)
- def onLoginFailure(self, reason):
- log.error("Falha de Autenticacao: %s" % reason.value)
- self.connected = False
- def onLoseConnection(self, reason):
- log.error("Perda de Conexao: %s" % reason.value)
- self.connected = False
- def _handleNewchannel(self, ami, event):
- log.debug("Recebido Evento Newchannel...")
- print event # isso eh um dicionario
- def _handleHangup(self, ami, event):
- log.debug("Recebido Evento Hangup...")
- print event # isso eh um dicionario
- if __name__ == '__main__':
- ami = AMIWorker('127.0.0.1', 5038, 'guax', 'guaxinim')
- reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement