diff --git a/main.py b/main.py index 5c5ff6f..59a254b 100644 --- a/main.py +++ b/main.py @@ -21,10 +21,6 @@ VISITOR_COUNT_FILE = "visitor_count" LIB = None def call(): - sip_server = str(getenv("SIP_SERVER")) - sip_username = str(getenv("SIP_USERNAME")) - sip_password = str(getenv("SIP_PASSWORD")) - # Initialize lib LIB = pj.Endpoint() LIB.libCreate() @@ -38,6 +34,24 @@ def call(): LIB.libStart() + while True: + try: + id = QUEUE.get(block = True) + + sip_server = str(getenv("SIP_SERVER")) + sip_username = str(getenv("SIP_USERNAME")) + sip_password = str(getenv("SIP_PASSWORD")) + + # Spawn audio on phone + call_phone(sip_server, sip_username, sip_password) + + # authorize guest after a certain amount of time + unifi.authorize_guest(id, 1) + except Exception as e: + print(f"Exception: {e}") + sleep(5) + +def call_phone(hostname, username, password): class MyAccount(pj.Account): def __init__(self): pj.Account.__init__(self) @@ -45,31 +59,6 @@ def call(): def onRegState(self, prm): print("Registration state:", prm.code) - # Account config - acc_cfg = pj.AccountConfig() - acc_cfg.idUri = f"sip:{sip_username}@{sip_server}" - acc_cfg.regConfig.registrarUri = f"sip:{sip_server}" - cred = pj.AuthCredInfo("digest", "*", sip_username, 0, sip_password) - acc_cfg.sipConfig.authCreds.append(cred) - - acc = MyAccount() - acc.create(acc_cfg) - - - while True: - try: - id = QUEUE.get(block = True) - - # Spawn audio on phone - call_phone(sip_server, acc) - - # authorize guest after a certain amount of time - unifi.authorize_guest(id, 1) - except: - sleep(5) - -def call_phone(hostname, acc): - # Make call class MyCall(pj.Call): def __init__(self, acc, dest_uri): pj.Call.__init__(self, acc) @@ -79,6 +68,17 @@ def call_phone(hostname, acc): ci = self.getInfo() print("Call state:", ci.stateText) + # Account config + acc_cfg = pj.AccountConfig() + acc_cfg.idUri = f"sip:{username}@{hostname}" + acc_cfg.regConfig.registrarUri = f"sip:{hostname}" + cred = pj.AuthCredInfo("digest", "*", username, 0, password) + acc_cfg.sipConfig.authCreds.append(cred) + + acc = MyAccount() + acc.create(acc_cfg) + + # Make call call = MyCall(acc, f"sip:3002@{hostname}") # Keep the program running for the duration of the ringtone