Explorar o código

евенты при локлаьных звонках на группу(очередь)

svetlana %!s(int64=3) %!d(string=hai) anos
pai
achega
a68cd342f6
Modificáronse 1 ficheiros con 58 adicións e 1 borrados
  1. 58 1
      app/app.py

+ 58 - 1
app/app.py

@@ -93,7 +93,8 @@ app.cache = {'devices':{},
              'ustates':{},
              'pstates':{},
              'queues':{},
-             'calls':{}}
+             'calls':{},
+             'cel_calls':{}}
 
 manager = Manager(
   loop=main_loop,
@@ -207,6 +208,62 @@ async def newchannelCallback(mngr: Manager, msg: Message):
                                                            'callerId': cid,
                                                            'did': did,
                                                            'callId': uid})
+                                                           
+@manager.register_event('CEL')
+async def celCallback(mngr: Manager, msg: Message):
+    if (msg.EventName == 'CHAN_START')  and (msg.LinkedID == msg.UniqueID): #save first msg
+        app.cache['cel_calls'][msg.LinkedID] = msg
+        app.cache['cel_calls'][msg.LinkedID]['channels'] = {}
+    if (msg.LinkedID in app.cache['cel_calls']):
+        first_message = app.cache['cel_calls'][msg.LinkedID]
+        cid = first_message.CallerIDnum
+        uid = first_message.LinkedID
+        if cid is not None and len(cid)<7: #for local calls only
+            if (msg.EventName == 'CHAN_START') and   msg.Context in ('from-queue') and ('HTTP_CLIENT' in app.config): #start dial   
+                app.cache['cel_calls'][msg.LinkedID]['channels'][msg.Exten] = msg.Channel
+                app.logger.warning('NEW CALLING LIST {}'.format(','.join(app.cache['cel_calls'][uid]['channels'].keys())))
+                
+                row = await db.fetch_one(query='SELECT url FROM callback_urls WHERE device = :device',
+                                       values={'device': cid})
+                if (row is not None) and (row['url'].startswith('http')):
+                    values = {'users': list(app.cache['cel_calls'][uid]['channels'].keys()),
+                          'state': 'group_ringing',
+                          'callId': uid}
+                app.logger.warning('call usrl {} values {}'.format(row['url'],values))
+                reply = await app.config['HTTP_CLIENT'].post(row['url'],
+                                        json=values)
+                else:
+                    app.logger.warning('no url for {}'.format(cid)) 
+            if (msg.EventName == 'CHAN_END')  and  msg.Context in ('from-queue') and ('HTTP_CLIENT' in app.config) and ('answered' not in  first_message):#end dial
+                called = msg.CallerIDname
+                app.cache['cel_calls'][uid]['channels'].pop(called, False)
+                app.logger.warning('NEW CALLING LIST {}'.format(','.join(app.cache['cel_calls'][uid]['channels'].keys())))
+                
+                row = await db.fetch_one(query='SELECT url FROM callback_urls WHERE device = :device',
+                                           values={'device': cid})
+                if (row is not None) and (row['url'].startswith('http')):
+                    values = {'users': list(app.cache['cel_calls'][uid]['channels'].keys()),
+                              'state': 'group_ringing',
+                              'callId': uid}
+                    app.logger.warning('call usrl {} values {}'.format(row['url'],values))
+                    reply = await app.config['HTTP_CLIENT'].post(row['url'],
+                                        json=values)
+            if (msg.EventName == 'ANSWER') and (msg.Application == 'AppDial') and (msg.LinkedID in app.cache['cel_calls']) and ('HTTP_CLIENT' in app.config):
+                called = msg.Exten
+                app.cache['cel_calls'][msg.LinkedID]['answered']=1;
+                app.logger.warning('answered {}'.format(called))
+                
+                row = await db.fetch_one(query='SELECT url FROM callback_urls WHERE device = :device',
+                                           values={'device': cid})
+                if (row is not None) and (row['url'].startswith('http')):
+                    values = {'user': called, 'state': 'group_answer', 'callId': uid}
+                    app.logger.warning('call usrl {} values {}'.format(row['url'],values))
+                    reply = await app.config['HTTP_CLIENT'].post(row['url'],
+                                        json=values)
+        if (msg.EventName == 'LINKEDID_END'):
+           app.cache['cel_calls'].pop(msg.LinkedID,False);
+           app.logger.warning('Left Calls {}'.format(','.join(app.cache['cel_calls'].keys())))
+            
 
 async def getCDR(start=None,
                  end=None,