Automatically assign a phone number to new devices

This commit is contained in:
clerie 2022-06-19 16:33:44 +02:00
parent 027e4b3a7f
commit 2d31abd1c3
4 changed files with 69 additions and 4 deletions

View File

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

View File

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

View File

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

View File

@ -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"
}
}
}