From 4382942ad87419b7bdc17c22835338cc26b63cfb Mon Sep 17 00:00:00 2001 From: Ember 'n0emis' Keske Date: Wed, 20 Jul 2022 15:44:11 +0200 Subject: [PATCH] sync after creating temp-extensions --- .gitignore | 1 + fieldpoc/dect.py | 46 ++++++++++++++++++++++++++++++++++------ fieldpoc/fieldpoc.py | 1 + fieldpoc/routing.py | 4 +++- fieldpoc_extensions.json | 2 +- 5 files changed, 46 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index a5cd500..5366ccf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ __pycache__ dist/ +.venv diff --git a/fieldpoc/dect.py b/fieldpoc/dect.py index 32508fd..bc428d8 100644 --- a/fieldpoc/dect.py +++ b/fieldpoc/dect.py @@ -18,15 +18,31 @@ class Dect: ommsync=True, ) - def get_temp_number(self): - temp_num_prefix = next(self.fp.extensions.extensions_by_type("temp")).num + @property + def temp_num_prefix(self): + return next(self.fp.extensions.extensions_by_type("temp")).num + + def load_temp_extensions(self): current_temp_extension = 0 - used_temp_extensions = [u.num[len(temp_num_prefix):] for u in self.c.find_users(lambda u: u.num.startswith(temp_num_prefix))] + used_temp_extensions = self.c.find_users(lambda u: u.num.startswith(self.temp_num_prefix)) + for u in used_temp_extensions: + temp_num = u.num + self.fp.temp_extensions[temp_num] = { + "name": f"Temp {temp_num[4:]}", + "type": "dect", + "trunk": False, + "dialout_allowed": False, + } + + + def get_temp_number(self): + current_temp_extension = 0 + used_temp_extensions = [num[len(self.temp_num_prefix):] for num, ext in self.fp.temp_extensions.items()] while "{:0>4}".format(current_temp_extension) in used_temp_extensions: current_temp_extension += 1 - return "{}{:0>4}".format(temp_num_prefix, current_temp_extension) + return "{}{:0>4}".format(self.temp_num_prefix, current_temp_extension) def get_sip_password_for_number(self, num): return num @@ -36,10 +52,12 @@ class Dect: self.c.attach_user_device(u.uid, d.ppn) self.c.set_user_relation_fixed(u.uid) self.c.set_user_sipauth(u.uid, num, self.get_sip_password_for_number(num)) + return u def run(self): logger.info("initialising connection to OMM") self._init_client() + self.load_temp_extensions() while True: msg = self.fp.queues["dect"].get() @@ -53,6 +71,7 @@ class Dect: extensions = self.fp.extensions.extensions_by_type("dect") extensions_by_num = {e.num: e for e in extensions} extensions_by_ipei = {e._c['dect_ipei']: e for _, e in extensions_by_num.items() if e._c.get('dect_ipei')} + created_tmp_ext = False users_by_ext = {} users_by_uid = {} @@ -90,15 +109,30 @@ class Dect: ui = users_by_uid.get(d.uid) if ui.num != e.num: logger.debug(f'User for {d} has wrong number') + if self.fp.temp_extensions.get(ui.num): + self.fp.temp_extensions.pop(ui.num) self.c.set_user_num(d.uid, e.num) self.c.set_user_sipauth(d.uid, e.num, self.get_sip_password_for_number(e.num)) + self.c.set_user_name(user.uid, e._c['name']) else: logger.debug(f'Creating and binding user for {d}') - self.create_and_bind_user(d, e.num) + user = self.create_and_bind_user(d, e.num) + self.c.set_user_name(user.uid, e._c['name']) elif d.relType == mitel_ommclient2.types.PPRelTypeType("Unbound"): temp_num = self.get_temp_number() logger.debug(f'Creating and binding tmp-user for {d}: {temp_num}') - self.create_and_bind_user(d, temp_num) + user = self.create_and_bind_user(d, temp_num) + self.c.set_user_name(user.uid, f"Temp {temp_num[4:]}") + self.fp.temp_extensions[temp_num] = { + "name": f"Temp {temp_num[4:]}", + "type": "dect", + "trunk": False, + "dialout_allowed": False, + } + created_tmp_ext = True + + if created_tmp_ext: + self.fp.queues['routing'].put({"type": "sync"}) self.c.connection.close() diff --git a/fieldpoc/fieldpoc.py b/fieldpoc/fieldpoc.py index 8737c39..bd452d3 100644 --- a/fieldpoc/fieldpoc.py +++ b/fieldpoc/fieldpoc.py @@ -17,6 +17,7 @@ logger = logging.getLogger("fieldpoc.fieldpoc") class FieldPOC: config = None extensions = None + temp_extensions = {} def __init__(self, config_file_path, extensions_file_path): logger.info("loading configuration") diff --git a/fieldpoc/routing.py b/fieldpoc/routing.py index d510f9f..fddd85f 100644 --- a/fieldpoc/routing.py +++ b/fieldpoc/routing.py @@ -488,7 +488,9 @@ class Routing: logger.info("syncing") state_fieldpoc = BackendNerd() - state_fieldpoc.load(self.fp.extensions._c) + extensions = self.fp.extensions._c.copy() + extensions['extensions'].update(self.fp.temp_extensions) + state_fieldpoc.load(extensions) state_yate = BackendYwsd() state_yate.load("postgresql+psycopg2://{}:{}@{}/{}".format( self.fp.config.database.username, diff --git a/fieldpoc_extensions.json b/fieldpoc_extensions.json index 6d4027a..cff74c5 100644 --- a/fieldpoc_extensions.json +++ b/fieldpoc_extensions.json @@ -95,7 +95,7 @@ "name": "Temporary Numbers", "trunk": false, "dialout_allowed": true, - "type": "static" + "type": "temp" }, "9999": { "name": "DECT Claim Extensions",