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
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__":

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

@ -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__":

View file

@ -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__":