Hal De 3 лет назад
Родитель
Сommit
198bf05cc7
1 измененных файлов с 33 добавлено и 10 удалено
  1. 33 10
      app/app.py

+ 33 - 10
app/app.py

@@ -94,6 +94,7 @@ app.cache = {'devices':{},
              'pstates':{},
              'queues':{},
              'calls':{},
+             'cel_queue_calls':{},
              'cel_calls':{}}
 
 manager = Manager(
@@ -208,7 +209,7 @@ 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
@@ -218,11 +219,10 @@ async def celCallback(mngr: Manager, msg: Message):
         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   
+        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')):
@@ -234,11 +234,10 @@ async def celCallback(mngr: Manager, msg: Message):
                                             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
+            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')):
@@ -252,7 +251,6 @@ async def celCallback(mngr: Manager, msg: Message):
                 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')):
@@ -260,16 +258,41 @@ async def celCallback(mngr: Manager, msg: Message):
                     app.logger.warning('call usrl {} values {}'.format(row['url'],values))
                     reply = await app.config['HTTP_CLIENT'].post(row['url'],
                                         json=values)
- 
+
+        if (msg.Application == 'Queue') and (msg.Exten in ('1','2200')):
+           queue_changed=False;
+           if (msg.EventName == 'APP_START'):
+                app.cache['cel_queue_calls'][msg.LinkedID] = {'caller': msg.CallerIDnum, 'start': msg.EventTime}; 
+                queue_changed = 1;
+           if (msg.EventName == 'APP_END'):
+                call = app.cache['cel_queue_calls'].pop(msg.LinkedID,False);
+                queue_changed = (call != None);
+           if (msg.EventName == 'BRIDGE_ENTER'):
+                call = app.cache['cel_queue_calls'].pop(msg.LinkedID,False);
+                queue_changed = (call != None);
+           app.logger.warning('EVENT:{} changed:{}'.format(msg,queue_changed));
+           if queue_changed :
+               if (msg.EventName == 'APP_START'):
+                  row = await db.fetch_one(query='SELECT url FROM callback_urls WHERE device = :entity', values={'entity': 'queueEnter'});
+               else:
+                  row = await db.fetch_one(query='SELECT url FROM callback_urls WHERE device = :entity', values={'entity': 'queueLeave'});
+
+               if (row is not None) and (row['url'].startswith('http')):
+                    all_callers = app.cache['cel_queue_calls'] ;
+                    values = {'caller': msg.CallerIDnum, 'all': all_callers, 'callId': msg.LinkedID}
+                    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.cache['cel_queue_calls'].pop(msg.LinkedID,False);
            app.logger.warning('Left Calls {}'.format(','.join(app.cache['cel_calls'].keys())))
-        if (msg.EventName == 'USER_DEFINED')  and  msg.AppData == 'SETVARIABLE':
+        if (msg.EventName == 'USER_DEFINED')  and  (msg.AppData == 'SETVARIABLE'):
             app.logger.warning('SETVARIABLE({})'.format(msg))
             varname =  msg.Extra.split('=')[0]
             value = msg.Extra.split('=')[1]
             app.logger.warning('SETVARIABLE({}={})'.format(varname,value))
-            app.cache['cel_calls'][msg.linkedid][varname]=value           
+            app.cache['cel_calls'][msg.linkedid][varname]=value
 
 async def getCDR(start=None,
                  end=None,