cache.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env python3
  2. # encoding:utf-8
  3. import json
  4. import sys
  5. import time
  6. import uuid
  7. from datetime import datetime
  8. from src.core.callcenter.constant import *
  9. from src.core.callcenter.api import AgentInfo, CallInfo, RouteGateway
  10. from src.core.callcenter.dao import Agent, Phone
  11. from src.core.datasource import RedisHandler
  12. cacheDay = 7
  13. deviceCall = {}
  14. deviceUserPart = {}
  15. redis_handler = RedisHandler()
  16. print(redis_handler.redis.info())
  17. def get_agent_info(saas_id, agent_number):
  18. text = redis_handler.get(AGENT_INFO + saas_id + ":" + agent_number)
  19. print('get_agent_info', saas_id, agent_number, text)
  20. sys.stdout.flush() # 强制刷新输出缓冲区
  21. if text:
  22. return AgentInfo.from_json(text)
  23. phone = get_agent_phone(saas_id, agent_number)
  24. agent_info = AgentInfo(saas_id=saas_id, sip_server=phone.sip_server, agent_number=agent_number)
  25. print('get_agent_info', saas_id, agent_number, agent_info)
  26. add_agent_info(agent=agent_info)
  27. sys.stdout.flush() # 强制刷新输出缓冲区
  28. return agent_info
  29. def refresh_agent_token(agent_number, token):
  30. redis_handler.set(AGENT_TOKEN + token, agent_number, cacheDay * 24 * 60 * 60)
  31. def delete_key(key):
  32. redis_handler.redis.delete(key)
  33. def get_agent_number(token):
  34. return redis_handler.get(AGENT_TOKEN + token)
  35. # 缓存坐席
  36. def add_agent_info(call_info: CallInfo = None, agent: AgentInfo = None, call_id=None, device_id=None):
  37. if call_info and not agent:
  38. agent = get_agent_info(call_info.saas_id, call_info.agent_key)
  39. if not agent:
  40. return
  41. if call_id:
  42. agent.call_id = call_id
  43. if device_id:
  44. agent.device_id = device_id
  45. redis_handler.set(AGENT_INFO + agent.saas_id + ":" + agent.agent_number, agent.to_json_string(), cacheDay * 24 * 60 * 60)
  46. # 缓存CALL_INFO
  47. def add_call_info(call: CallInfo):
  48. for k, v in call.device_info_map.items():
  49. add_device(k, call.call_id)
  50. # print('add_call_info', call.call_id, call.to_json_string())
  51. redis_handler.set(CALL_INFO + call.call_id, call.to_json_string(), cacheDay * 24 * 60 * 60)
  52. def get_call_info_by_device_id(device_id):
  53. call_id = deviceCall.get(device_id)
  54. if not call_id:
  55. return get_call_info(call_id)
  56. # 获取callInfo
  57. def get_call_info(call_id):
  58. text = None
  59. if call_id:
  60. text = redis_handler.get(CALL_INFO + call_id)
  61. # print('get_call_info', call_id, text)
  62. # sys.stdout.flush() # 强制刷新输出缓冲区
  63. if text:
  64. return CallInfo.from_json(text)
  65. def remove_call_info(call_id):
  66. if not call_id:
  67. return None
  68. call_info = get_call_info(call_id)
  69. if call_info and call_info.device_info_map:
  70. for k, v in call_info.device_info_map.items():
  71. deviceCall.pop(k)
  72. delete_key(CALL_INFO + call_id)
  73. def add_device(device_id, call_id):
  74. if not device_id or not call_id:
  75. return None
  76. deviceCall[device_id] = call_id
  77. def get_user_part(device_id):
  78. return deviceUserPart.get(device_id)
  79. def add_device_user_part(device_id, user_part):
  80. if not device_id or not user_part:
  81. return
  82. deviceUserPart[device_id] = user_part
  83. def get_route_gateway(saas_id):
  84. return RouteGateway(id=1,
  85. saas_id=saas_id,
  86. name='63366692',
  87. media_host='192.168.20.99',
  88. media_port=5060,
  89. caller_prefix='',
  90. called_prefix='',
  91. status=0)
  92. def get_agent_phone(saas_id, agent_num):
  93. return Phone.query.filter(Phone.saas_id == saas_id, Phone.phone_num == agent_num).first()
  94. def add_delay_message(action, delay_action, timeouts):
  95. delay_action.uuid = uuid.uuid4()
  96. key = CTI_ENGINE_DELAY_ACTION % action
  97. msg = delay_action.to_json_string()
  98. action_time = datetime.utcnow().timestamp() + timeouts * 1000
  99. redis_handler.redis.zadd(key, {msg : action_time})
  100. def get_delay_message(action):
  101. key = CTI_ENGINE_DELAY_ACTION % action
  102. current_time = int(time.time() * 1000) # 毫秒级时间戳
  103. members = redis_handler.redis.zrangebyscore(key, 0, current_time, start=0, num=DELAY_ACTION_BATCH_SIZE, withscores=True)
  104. if not members:
  105. return []
  106. # scored_entries = [{"member": entry[0].decode('utf-8'), "score": entry[1]} for entry in members]
  107. action_list = [entry[0].decode('utf-8') for entry in members]
  108. if action_list:
  109. redis_handler.redis.zrem(key, *action_list)
  110. return action_list
  111. def lock_delay_action(val):
  112. key = CTI_ENGINE_DELAY_ACTION_LOCK % val
  113. return redis_handler.redis.set(key, "1", ex=1000*10, nx=True)