From 2d31abd1c35bd43afa8f87849d9d38592d980f67 Mon Sep 17 00:00:00 2001 From: clerie Date: Sun, 19 Jun 2022 16:33:44 +0200 Subject: [PATCH] Automatically assign a phone number to new devices --- fieldpoc/config.py | 26 ++++++++++++++++++++++++++ fieldpoc/dect.py | 27 +++++++++++++++++++++++++-- fieldpoc/fieldpoc.py | 2 +- fieldpoc_extensions.json | 18 +++++++++++++++++- 4 files changed, 69 insertions(+), 4 deletions(-) diff --git a/fieldpoc/config.py b/fieldpoc/config.py index 6234722..02d0278 100644 --- a/fieldpoc/config.py +++ b/fieldpoc/config.py @@ -13,6 +13,7 @@ class DectConfig: def check(self): return True + class Config: def __init__(self, c): self._c = c @@ -20,3 +21,28 @@ class Config: def check(self): return self.dect.check() + + +class ExtensionConfig: + def __init__(self, c): + self.num = c[0] + self._c = c[1] + + def __getattr__(self, name): + if name in self._c.keys(): + return self._c.get(name) + else: + raise AttributeError() + + +class Extensions: + def __init__(self, c): + self._c = c + self.extensions = [] + for e in self._c.get("extensions", {}).items(): + self.extensions.append(ExtensionConfig(e)) + + def extensions_by_type(self, t): + for e in self.extensions: + if e.type == t: + yield e diff --git a/fieldpoc/dect.py b/fieldpoc/dect.py index 2bec9a6..fbdb73b 100644 --- a/fieldpoc/dect.py +++ b/fieldpoc/dect.py @@ -15,10 +15,33 @@ class Dect: ommsync=True, ) + def get_temp_number(self): + temp_num_prefix = next(self.fp.extensions.extensions_by_type("temp")).num + 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))] + + 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) + + def get_sip_password_for_number(self, num): + return num + + def run(self): self._init_client() while True: - print("meow") - print(self.c.ping()) + unbound_devices = self.c.find_devices(lambda d: d.relType == mitel_ommclient2.types.PPRelTypeType("Unbound")) + + for d in unbound_devices: + print(d) + temp_num = self.get_temp_number() + u = self.c.create_user(temp_num) + print(u) + self.c.attach_user_device(u.uid, d.ppn) + self.c.set_user_relation_fixed(u.uid) + self.c.set_user_sipauth(u.uid, temp_num, self.get_sip_password_for_number(temp_num)) + time.sleep(2) diff --git a/fieldpoc/fieldpoc.py b/fieldpoc/fieldpoc.py index d034ad7..0392d4a 100644 --- a/fieldpoc/fieldpoc.py +++ b/fieldpoc/fieldpoc.py @@ -27,4 +27,4 @@ class FieldPOC: self.config = config.Config(json.loads(self.config_file_path.read_text())) def _load_extensions(self): - self.extensions = json.loads(self.extensions_file_path.read_text()) + self.extensions = config.Extensions(json.loads(self.extensions_file_path.read_text())) diff --git a/fieldpoc_extensions.json b/fieldpoc_extensions.json index 0967ef4..a890f65 100644 --- a/fieldpoc_extensions.json +++ b/fieldpoc_extensions.json @@ -1 +1,17 @@ -{} +{ + "extensions": { + "2574": { + "name": "clerie", + "type": "dect", + "dect_claim_token": "2574" + }, + "9998": { + "name": "Temporary Numbers", + "type": "temp" + }, + "9999": { + "name": "DECT Claim Token Pool", + "type": "claim_token" + } + } +}