Hal De преди 2 години
родител
ревизия
d8b036ee56
променени са 1 файла, в които са добавени 36 реда и са изтрити 0 реда
  1. 36 0
      app/app.py

+ 36 - 0
app/app.py

@@ -95,6 +95,7 @@ app.config.update({
 app.cache = {'devices':{},
              'usermap':{},
              'devicemap':{},
+             'usernamemap':{},
              'ustates':{},
              'pstates':{},
              'queues':{},
@@ -1115,6 +1116,9 @@ async def setDeviceUser(device, user):
 async def getUserDevice(user):
   return await amiDBGet('AMPUSER', '{}/device'.format(user))
 
+async def getUserName(user):
+  return await amiDBGet('AMPUSER', '{}/cidname'.format(user))
+
 async def setUserDevice(user, device):
   if device is None:
     return await amiDBDel('AMPUSER', '{}/device'.format(user))
@@ -1157,6 +1161,11 @@ async def setUserDeviceStates(user, device, ast):
 
 async def refreshStatesCache():
   app.cache['ustates'] = await amiExtensionStateList()
+  username = {}
+  for u in app.cache['ustates']:
+    username[u] = await getUserName(u)
+  app.cache['usernamemap'] = copy.deepcopy(username)
+  app.logger.warning('User Names {}'.format(app.cache['usernamemap']))
   app.cache['pstates'] = await amiPresenceStateList()
   return len(app.cache['ustates'])
 
@@ -1194,6 +1203,7 @@ async def rebindLostDevices():
     usermap[device] = user
     if user != 'none':
       devicemap[user] = device
+
   app.cache['usermap'] = copy.deepcopy(usermap)
   app.cache['devicemap'] = copy.deepcopy(devicemap)
 
@@ -1209,6 +1219,11 @@ async def userStateChangeCallback(user, state, prevState = None):
              'prev_state':prevState}
       reply = await doCallback(device, _cb)
 
+  _cb = {'webhook_name': 'user_status',
+             'user': user,
+             'state': state,
+             'prev_state':prevState}
+  reply = await doCallback('user_status', _cb)
   #app.logger.warning('{} changed state to: {}'.format(user, state))
   return reply
 
@@ -1220,6 +1235,9 @@ def getUserStateCombined(user):
 def getUsersStatesCombined():
   return {user:getUserStateCombined(user) for user in app.cache['ustates']}
 
+def getUsersNameStatesCombined():
+  return {user:{'state': getUserStateCombined(user),'name':app.cache['usernamemap'][user]} for user in app.cache['ustates']}
+
 @app.route('/atxfer/<userA>/<userB>')
 class AtXfer(Resource):
   @authRequired
@@ -1429,6 +1447,24 @@ class UsersStates(Resource):
     #  return stateCacheEmpty()
 
     return successReply(getUsersStatesCombined())
+  
+@app.route('/users/namestates')
+class UsersNameStates(Resource):
+  @authRequired
+  @app.response(HTTPStatus.OK, 'JSON reply with user:{"state":state,"name":name} map or error message')
+  @app.response(HTTPStatus.UNAUTHORIZED, 'Authorization required')
+  async def get(self):
+    '''Returns all users with their combined states.
+    Possible states are: available, away, dnd, inuse, busy, unavailable, ringing
+    '''
+    if not request.admin:
+      abort(401)
+    #app.logger.warning('request device: {}'.format(request.device))
+    #usersCount = await refreshStatesCache()
+    #if usersCount == 0:
+    #  return stateCacheEmpty()
+
+    return successReply(getUsersNameStatesCombined())
 
 @app.route('/users/states/<users_list>')
 class UsersStatesSelected(Resource):