Hal De 4 년 전
부모
커밋
d9fedabdde
1개의 변경된 파일43개의 추가작업 그리고 18개의 파일을 삭제
  1. 43 18
      app/app.py

+ 43 - 18
app/app.py

@@ -92,7 +92,8 @@ app.cache = {'devices':{},
              'devicemap':{},
              'ustates':{},
              'pstates':{},
-             'queues':{}}
+             'queues':{},
+             'calls':{}}
 
 manager = Manager(
   loop=main_loop,
@@ -163,25 +164,49 @@ async def presenceStatusCallback(mngr: Manager, msg: Message):
     if combinedState != prevState:
       await userStateChangeCallback(user, combinedState, prevState)
 
+@manager.register_event('Hangup')
+async def hangupCallback(mngr: Manager, msg: Message):
+  app.logger.warning(msg)
+
 @manager.register_event('Newchannel')
 async def newchannelCallback(mngr: Manager, msg: Message):
-  if (msg.channelstate == '4') and (msg.exten in app.cache['devicemap']) and ('HTTP_CLIENT' in app.config):
-    user = msg.exten
-    device = app.cache['devicemap'][user]
-    if msg.calleridnum in app.cache['usermap']:
-      cid = app.cache['usermap'][msg.calleridnum]
-    else:
-      cid = msg.calleridnum
-    uid = msg.uniqueid
-    row = await db.fetch_one(query='SELECT url FROM callback_urls WHERE device = :device',
-                             values={'device': app.cache['devicemap'][user]})
-    if row is not None:
-      reply = await app.config['HTTP_CLIENT'].post(row['url'],
-                                                   json={'user': user,
-                                                         'device': device,
-                                                         'state': 'ringing',
-                                                         'callerId': cid,
-                                                         'uniqueId': uid})
+  #app.logger.warning(msg)
+  if (msg.channelstate == '4') and ('HTTP_CLIENT' in app.config):
+    did = None
+    cid = None
+    user = None
+    device = None
+    uid = None
+    if msg.context in ('from-pstn'):
+      app.cache['calls'][msg.uniqueid]=msg
+    elif ((msg.context in ('from-queue')) and
+          (msg.linkedid in app.cache['calls']) and
+          (msg.exten in app.cache['devicemap'])):
+      did = app.cache['calls'][msg.linkedid].exten
+      cid = app.cache['calls'][msg.linkedid].calleridnum
+      user = msg.exten
+      device = app.cache['devicemap'][user]
+      uid = msg.linkedid
+    elif ((msg.context in ('from-internal')) and
+          (msg.exten in app.cache['devicemap'])):
+      user = msg.exten
+      device = app.cache['devicemap'][user]
+      if msg.calleridnum in app.cache['usermap']:
+        cid = app.cache['usermap'][msg.calleridnum]
+      else:
+        cid = msg.calleridnum
+      uid = msg.uniqueid
+    if device is not None:
+      row = await db.fetch_one(query='SELECT url FROM callback_urls WHERE device = :device',
+                               values={'device': app.cache['devicemap'][user]})
+      if (row is not None) and ('url' in row) and (row['url'].startswith('http')):
+        reply = await app.config['HTTP_CLIENT'].post(row['url'],
+                                                     json={'user': user,
+                                                           'device': device,
+                                                           'state': 'ringing',
+                                                           'callerId': cid,
+                                                           'did': did,
+                                                           'callId': uid})
 
 async def getCDR(start=None,
                  end=None,