[sip/billing] Ajout de commentaires (et on appel hangup via AMI et pas l'AGI qui est bloqué)
This commit is contained in:
parent
21bca74028
commit
40eec6687a
2 changed files with 22 additions and 2 deletions
|
@ -10,6 +10,12 @@ from threading import Thread
|
|||
import gestion.secrets_new as secrets
|
||||
from sip.asterisk import AGI, Manager, NullRecv
|
||||
|
||||
|
||||
# Pour quitter, j'utilise os._exit(0) qui quitte un peut violement (comme un kill -9), sinon les threads ne se termine pas.
|
||||
# En vrai, il faudrait appeler os.exit (qui lève une exception dans le thread où il est appelé), rattraper l'exception,
|
||||
# faire se terminer tous les autres threads, puis quitter, mais j'ai la flemme.
|
||||
|
||||
## Redirection de stderr vers le fichier /tmp/breakage.log pour débugguer, c'est bien pratique
|
||||
se = os.open("/tmp/breakage.log", os.O_WRONLY|os.O_APPEND|os.O_CREAT)
|
||||
os.dup2(se, sys.stderr.fileno())
|
||||
os.close(se)
|
||||
|
@ -23,7 +29,7 @@ def hangup_callback(manager, params):
|
|||
def unreachble_callback(manager, params):
|
||||
agi=manager.agi()
|
||||
if params['Peer'] == "SIP/%s" % agi['callerid'] and params['PeerStatus'] in ['Unregistered','Unreachable']:
|
||||
agi.hangup(agi['channel'])
|
||||
manager.hangup(agi['channel'])
|
||||
os._exit(0)
|
||||
|
||||
def start_billing(agi, src, dest):
|
||||
|
@ -45,24 +51,36 @@ def dial(agi, to):
|
|||
os._exit(0)
|
||||
|
||||
if __name__ == '__main__' :
|
||||
# initialisation d'un AMI
|
||||
manager=Manager("billing", secrets.get('asterisk_billig_passwd') , server="127.0.0.1", event=True, auto_connect=False, timeout=10)
|
||||
syslog.openlog('billing')
|
||||
# initialisation d'un AGI (on lit des paramètres sur stdin, donc c'est bloquant jusqu'à la lecture d'une ligne vide)
|
||||
agi=manager.agi()
|
||||
# On lance le decompte du temps lorsque l'on passe à l'état Up.
|
||||
manager.register_events_callback('Newstate', Newstate_callback)
|
||||
# On coupe la communication si le client se déconnecte ou devient injoiniable (ce qui l'empêche de raccrocher lui même).
|
||||
manager.register_events_callback('PeerStatus', unreachble_callback)
|
||||
# Des choses à faire lorsque l'un des deux participant raccroche.
|
||||
manager.register_events_callback('Hangup', hangup_callback)
|
||||
|
||||
# On récupère le domain sip de la destination
|
||||
SIPDOMAIN=agi['SIPDOMAIN']
|
||||
syslog.syslog('%r' % SIPDOMAIN)
|
||||
# Si le domaine est absent ou local, alors c'est un appel vers le RTC, on passe par la ligne SIP ovh
|
||||
if not SIPDOMAIN or SIPDOMAIN in ['crans.org']:
|
||||
SIPDOMAIN='forfait-ovh'
|
||||
t=Thread(target=dial, args=(agi, "SIP/%s@%s" % (agi['extension'], SIPDOMAIN)))
|
||||
agi.answer()
|
||||
# On compose le numéro dans un thread a part puisque cela est bloquant jusqu'à ce que quelqu'un raccroche. On limite la communication à 30min (1800000 milliseconde)
|
||||
t=Thread(target=dial, args=(agi, "SIP/%s@%s,60,L(1800000:60000:20000)" % (agi['extension'], SIPDOMAIN)))
|
||||
t.start()
|
||||
|
||||
# On se connecte a l'AMI et on attent de recevoir des events
|
||||
while True:
|
||||
manager.connect()
|
||||
try:
|
||||
while True:
|
||||
manager.process_events()
|
||||
# Si le thread éxécutant dial est mort, c'est que quelqu'un à raccrocher, on quitte.
|
||||
if not t.isAlive():
|
||||
os._exit(0)
|
||||
except (socket.error, NullRecv):
|
||||
|
|
|
@ -455,6 +455,8 @@ class AGI(object):
|
|||
self.command("exec", app, *params)
|
||||
def dial(self, to):
|
||||
self.launch_app("dial", to)
|
||||
def answer(self):
|
||||
self.launch_app("Answer")
|
||||
def goto(self, arg):
|
||||
self.launch_app("goto", arg)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue