backport to panoramisk==1.0 + refactoring

This commit is contained in:
boyska 2022-03-15 18:41:08 +01:00
parent 94e5cc5293
commit 36cff1c634
4 changed files with 96 additions and 55 deletions

48
asterisk_misc_common.py Normal file
View file

@ -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()

View file

@ -1,18 +1,17 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import asyncio
import json import json
import logging import logging
import tempfile import tempfile
from argparse import ArgumentParser from argparse import ArgumentParser
from pathlib import Path from pathlib import Path
from panoramisk import Manager
from panoramisk.actions import Action from panoramisk.actions import Action
import asterisk_misc_common
log = logging.getLogger("ring") log = logging.getLogger("ring")
LOOP = asyncio.get_event_loop()
STATEFILE = None STATEFILE = None
@ -89,8 +88,6 @@ def refresh(manager, *args):
async def init(manager): async def init(manager):
# manager.send_command('sip show peers')
manager.register_event("*", on_tutto) manager.register_event("*", on_tutto)
for event in [ for event in [
"FullyBooted", "FullyBooted",
@ -107,31 +104,23 @@ async def init(manager):
async def on_shutdown(m): async def on_shutdown(m):
pass print("Shutting down...")
def main(): def main():
global STATEFILE global STATEFILE
p = ArgumentParser() p = ArgumentParser()
p.add_argument("--state-file", type=Path, default=Path("./ring-state.json")) p.add_argument("--state-file", type=Path, default=Path("./ring-state.json"))
asterisk_misc_common.add_arguments(p)
args = p.parse_args() args = p.parse_args()
asterisk_misc_common.do_common_options(args)
STATEFILE = args.state_file STATEFILE = args.state_file
logging.basicConfig(level=logging.DEBUG) manager = asterisk_misc_common.get_manager(args)
logging.getLogger("panoramisk.manager").setLevel(logging.WARNING) asterisk_misc_common.run_manager(
manager = Manager( manager, args, on_startup=init, on_shutdown=on_shutdown
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.connect(run_forever=True, on_startup=init, on_shutdown=on_shutdown)
if __name__ == "__main__": if __name__ == "__main__":

29
transfer_back/mixer.py Normal file → Executable file
View file

@ -14,13 +14,13 @@ speaker.
""" """
import asyncio
import logging import logging
from argparse import ArgumentParser
from panoramisk import Manager
from panoramisk.actions import Action from panoramisk.actions import Action
LOOP = asyncio.get_event_loop() import asterisk_misc_common
CONFERENCE = "400" CONFERENCE = "400"
APP = None APP = None
@ -175,21 +175,16 @@ async def on_shutdown(m):
def main(): def main():
logging.basicConfig(level=logging.DEBUG) p = ArgumentParser()
logging.getLogger("panoramisk.manager").setLevel(logging.WARNING)
manager = Manager( asterisk_misc_common.add_arguments(p)
loop=LOOP, args = p.parse_args()
host="127.0.0.1", asterisk_misc_common.do_common_options(args)
port=5038,
ssl=False, manager = asterisk_misc_common.get_manager(args)
encoding="utf8", asterisk_misc_common.run_manager(
username="admin", manager, args, on_startup=init, on_shutdown=on_shutdown
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
) )
manager.connect(run_forever=True, on_startup=init, on_shutdown=on_shutdown)
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -1,14 +1,12 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import asyncio
import logging import logging
import urllib.parse import urllib.parse
from argparse import ArgumentParser
from panoramisk import Manager
from panoramisk.actions import Action from panoramisk.actions import Action
LOOP = asyncio.get_event_loop() import asterisk_misc_common
log = logging.getLogger("transferback") log = logging.getLogger("transferback")
@ -105,12 +103,28 @@ class TransferAndTakeBack(BaseApp):
# meant to be overridden by children {{{ # meant to be overridden by children {{{
def get_destination(self): def get_destination(self):
"""
la destination e' tipicamente una confbridge in cui l'utente remoto
incontra lo speaker
"""
return "private,9438,1" return "private,9438,1"
def get_backroom(self): def get_backroom(self):
"""
nella backroom, l'utente locale si incontra nuovamente con l'utente
remoto
"""
return "private,9401,1" return "private,9401,1"
def get_waiting_room(self): 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" return "private,9401,1"
def is_context_auth(self, context): def is_context_auth(self, context):
@ -246,21 +260,16 @@ async def on_shutdown(m):
def main(): def main():
logging.basicConfig(level=logging.DEBUG) p = ArgumentParser()
logging.getLogger("panoramisk.manager").setLevel(logging.WARNING)
manager = Manager( asterisk_misc_common.add_arguments(p)
loop=LOOP, args = p.parse_args()
host="127.0.0.1", asterisk_misc_common.do_common_options(args)
port=5038,
ssl=False, manager = asterisk_misc_common.get_manager(args)
encoding="utf8", asterisk_misc_common.run_manager(
username="admin", manager, args, on_startup=init, on_shutdown=on_shutdown
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
) )
manager.connect(run_forever=True, on_startup=init, on_shutdown=on_shutdown)
if __name__ == "__main__": if __name__ == "__main__":