Kaynağa Gözat

UserEvents on bind|unbind

Hal De 3 yıl önce
ebeveyn
işleme
a7542a0dd5
1 değiştirilmiş dosya ile 24 ekleme ve 0 silme
  1. 24 0
      app/app.py

+ 24 - 0
app/app.py

@@ -363,6 +363,28 @@ async def amiPJSIPShowAors():
     app.logger.warning('AorsList: {}'.format(','.join(aors.keys())))
   return successReply(aors)
 
+async def amiUserEvent(name, data):
+  '''AMI UserEvent
+  Generates AMI Event using AMI action UserEvent with name and data supplied.
+
+  Parameters:
+    name (string): UserEvent name
+    data (dict): UserEvent data
+
+  Returns:
+    string: None if UserEvent was successfull, error message overwise
+  '''
+  reply = await manager.send_action({**{'Action': 'UserEvent',
+                                        'UserEvent': name},
+                                     **data})
+  app.logger.warning('UserEvent({})'.format(name))
+  if isinstance(reply, Message):
+    if reply.success:
+      return None
+    else:
+      return reply.message
+  return 'AMI error'
+
 async def amiSetVar(variable, value):
   '''AMI SetVar
   Sets variable using AMI action SetVar to value in background.
@@ -986,6 +1008,7 @@ class UserDeviceBind(Resource):
       return bindError(user, device)
     app.cache['usermap'][device] = user
     app.cache['devicemap'][user] = device
+    await amiUserEvent('DeviceBound',{'device': device, 'newUser': user, 'oldUser': currentUser})
     return successfullyBound(user, device)
 
 @app.route('/device/<device>/off')
@@ -1017,6 +1040,7 @@ class DeviceUnBind(Resource):
     await setDeviceUser(device, 'none') # Unbind user from device
     del app.cache['usermap'][device]
     del app.cache['devicemap'][currentUser]
+    await amiUserEvent('DeviceUnbound',{'device': device, 'oldUser': currentUser})
     return successfullyUnbound(currentUser, device)
 
 @app.route('/cdr')