From 36cff1c634ba8cb4f34838db0bdc5a61963f629f Mon Sep 17 00:00:00 2001 From: boyska Date: Tue, 15 Mar 2022 18:41:08 +0100 Subject: [PATCH] backport to panoramisk==1.0 + refactoring --- asterisk_misc_common.py | 48 ++++++++++++++++++++++++++++++++++ ring_mon/ring.py | 29 +++++++------------- transfer_back/mixer.py | 29 +++++++++----------- transfer_back/transfer_back.py | 45 ++++++++++++++++++------------- 4 files changed, 96 insertions(+), 55 deletions(-) create mode 100644 asterisk_misc_common.py mode change 100644 => 100755 transfer_back/mixer.py diff --git a/asterisk_misc_common.py b/asterisk_misc_common.py new file mode 100644 index 0000000..bad9216 --- /dev/null +++ b/asterisk_misc_common.py @@ -0,0 +1,48 @@ +import asyncio +import logging + +from panoramisk import Manager + +LOOP = asyncio.get_event_loop() + + +def add_arguments(p): + p.add_argument( + "--log-level", + default="INFO", + choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], + ) + return p + + +def do_common_options(args): + logging.basicConfig(level=logging.DEBUG) + logging.getLogger("panoramisk.manager").setLevel(logging.WARNING) + + +def get_manager(args): + manager = Manager( + loop=LOOP, + host="127.0.0.1", + port=5038, + ssl=False, + encoding="utf8", + username="ring", + secret="campanellino", + ping_delay=10, # Delay after start + ping_interval=10, # Periodically ping AMI (dead or alive) + reconnect_timeout=2, # Timeout reconnect if connection lost + ) + return manager + + +def run_manager(manager, args, on_startup=None, on_shutdown=None): + manager.connect() + try: + if on_startup: + manager.loop.run_until_complete(on_startup(manager)) + manager.loop.run_forever() + except KeyboardInterrupt: + if on_shutdown: + manager.loop.run_until_complete(on_shutdown(manager)) + manager.loop.close() diff --git a/ring_mon/ring.py b/ring_mon/ring.py index d6851e1..109f1f6 100755 --- a/ring_mon/ring.py +++ b/ring_mon/ring.py @@ -1,18 +1,17 @@ #!/usr/bin/env python3 -import asyncio import json import logging import tempfile from argparse import ArgumentParser from pathlib import Path -from panoramisk import Manager from panoramisk.actions import Action +import asterisk_misc_common + log = logging.getLogger("ring") -LOOP = asyncio.get_event_loop() STATEFILE = None @@ -89,8 +88,6 @@ def refresh(manager, *args): async def init(manager): - # manager.send_command('sip show peers') - manager.register_event("*", on_tutto) for event in [ "FullyBooted", @@ -107,31 +104,23 @@ async def init(manager): async def on_shutdown(m): - pass + print("Shutting down...") def main(): global STATEFILE p = ArgumentParser() p.add_argument("--state-file", type=Path, default=Path("./ring-state.json")) + + asterisk_misc_common.add_arguments(p) args = p.parse_args() + asterisk_misc_common.do_common_options(args) STATEFILE = args.state_file - logging.basicConfig(level=logging.DEBUG) - logging.getLogger("panoramisk.manager").setLevel(logging.WARNING) - manager = Manager( - loop=LOOP, - host="127.0.0.1", - port=5038, - ssl=False, - encoding="utf8", - username="ring", - secret="campanellino", - ping_delay=10, # Delay after start - ping_interval=10, # Periodically ping AMI (dead or alive) - reconnect_timeout=2, # Timeout reconnect if connection lost + manager = asterisk_misc_common.get_manager(args) + asterisk_misc_common.run_manager( + manager, args, on_startup=init, on_shutdown=on_shutdown ) - manager.connect(run_forever=True, on_startup=init, on_shutdown=on_shutdown) if __name__ == "__main__": diff --git a/transfer_back/mixer.py b/transfer_back/mixer.py old mode 100644 new mode 100755 index f1f0ae2..d98dd60 --- a/transfer_back/mixer.py +++ b/transfer_back/mixer.py @@ -14,13 +14,13 @@ speaker. """ -import asyncio import logging +from argparse import ArgumentParser -from panoramisk import Manager from panoramisk.actions import Action -LOOP = asyncio.get_event_loop() +import asterisk_misc_common + CONFERENCE = "400" APP = None @@ -175,21 +175,16 @@ async def on_shutdown(m): def main(): - logging.basicConfig(level=logging.DEBUG) - logging.getLogger("panoramisk.manager").setLevel(logging.WARNING) - manager = Manager( - loop=LOOP, - host="127.0.0.1", - port=5038, - ssl=False, - encoding="utf8", - username="admin", - secret="secret123password", - ping_delay=10, # Delay after start - ping_interval=10, # Periodically ping AMI (dead or alive) - reconnect_timeout=2, # Timeout reconnect if connection lost + p = ArgumentParser() + + asterisk_misc_common.add_arguments(p) + args = p.parse_args() + asterisk_misc_common.do_common_options(args) + + manager = asterisk_misc_common.get_manager(args) + asterisk_misc_common.run_manager( + manager, args, on_startup=init, on_shutdown=on_shutdown ) - manager.connect(run_forever=True, on_startup=init, on_shutdown=on_shutdown) if __name__ == "__main__": diff --git a/transfer_back/transfer_back.py b/transfer_back/transfer_back.py index ad3764e..4d5c1a9 100755 --- a/transfer_back/transfer_back.py +++ b/transfer_back/transfer_back.py @@ -1,14 +1,12 @@ #!/usr/bin/env python3 - -import asyncio import logging import urllib.parse +from argparse import ArgumentParser -from panoramisk import Manager from panoramisk.actions import Action -LOOP = asyncio.get_event_loop() +import asterisk_misc_common log = logging.getLogger("transferback") @@ -105,12 +103,28 @@ class TransferAndTakeBack(BaseApp): # meant to be overridden by children {{{ def get_destination(self): + """ + la destination e' tipicamente una confbridge in cui l'utente remoto + incontra lo speaker + """ return "private,9438,1" def get_backroom(self): + """ + nella backroom, l'utente locale si incontra nuovamente con l'utente + remoto + """ return "private,9401,1" def get_waiting_room(self): + """ + nella waiting room, l'utente locale sente musica d'attesa mentre aspetta + che una di queste due cose avvenga: + - l'utente remoto riaggancia + - l'utente locale riaggancia + - l'utente locale preme # + """ + return "private,9401,1" def is_context_auth(self, context): @@ -246,21 +260,16 @@ async def on_shutdown(m): def main(): - logging.basicConfig(level=logging.DEBUG) - logging.getLogger("panoramisk.manager").setLevel(logging.WARNING) - manager = Manager( - loop=LOOP, - host="127.0.0.1", - port=5038, - ssl=False, - encoding="utf8", - username="admin", - secret="secret123password", - ping_delay=10, # Delay after start - ping_interval=10, # Periodically ping AMI (dead or alive) - reconnect_timeout=2, # Timeout reconnect if connection lost + p = ArgumentParser() + + asterisk_misc_common.add_arguments(p) + args = p.parse_args() + asterisk_misc_common.do_common_options(args) + + manager = asterisk_misc_common.get_manager(args) + asterisk_misc_common.run_manager( + manager, args, on_startup=init, on_shutdown=on_shutdown ) - manager.connect(run_forever=True, on_startup=init, on_shutdown=on_shutdown) if __name__ == "__main__":