cache.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.callcenter.data_handler import DataHandleServer
  12. from src.core.datasource import RedisHandler
  13. class Cache:
  14. def __init__(self, app):
  15. self.cacheDay = 7
  16. self.deviceCall = {}
  17. self.deviceUserPart = {}
  18. self.logger = app.logger
  19. self.redis_handler = RedisHandler()
  20. self.dataHandleServer = DataHandleServer(app)
  21. def get_agent_info(self, saas_id, agent_number):
  22. text = self.redis_handler.get(AGENT_INFO + saas_id + ":" + agent_number)
  23. self.logger.info('get_agent_info %s %s %s'%(saas_id, agent_number, text))
  24. if text:
  25. return AgentInfo.from_json(text)
  26. phone = self.dataHandleServer.get_agent_phone(saas_id, agent_number)
  27. agent_info = AgentInfo(saas_id=saas_id, sip_server=phone.sip_server, agent_number=agent_number)
  28. self.logger.info('get_agent_info %s %s %s'% (saas_id, agent_number, agent_info))
  29. self.add_agent_info(agent=agent_info)
  30. sys.stdout.flush() # 强制刷新输出缓冲区
  31. return agent_info
  32. def refresh_agent_token(self, agent_number, token):
  33. self.redis_handler.set(AGENT_TOKEN + token, agent_number, self.cacheDay * 24 * 60 * 60)
  34. def delete_key(self, key):
  35. self.redis_handler.redis.delete(key)
  36. def get_agent_number(self, token):
  37. return self.redis_handler.get(AGENT_TOKEN + token)
  38. # 缓存坐席
  39. def add_agent_info(self, call_info: CallInfo = None, agent: AgentInfo = None, call_id=None, device_id=None):
  40. if call_info and not agent:
  41. agent = self.get_agent_info(call_info.saas_id, call_info.agent_key)
  42. if not agent:
  43. return
  44. if call_id:
  45. agent.call_id = call_id
  46. if device_id:
  47. agent.device_id = device_id
  48. self.redis_handler.set(AGENT_INFO + agent.saas_id + ":" + agent.agent_number, agent.to_json_string(), self.cacheDay * 24 * 60 * 60)
  49. # 缓存CALL_INFO
  50. def add_call_info(self, call: CallInfo):
  51. for k, v in call.device_info_map.items():
  52. self.add_device(k, call.call_id)
  53. # print('liuwei::debugger::add_call_info call_id:%s, call=%s'% (call.call_id, call))
  54. self.redis_handler.set(CALL_INFO + call.call_id, call.to_json_string(), self.cacheDay * 24 * 60 * 60)
  55. def get_call_info_by_device_id(self, device_id):
  56. call_id = self.deviceCall.get(device_id)
  57. if not call_id:
  58. return self.get_call_info(call_id)
  59. # 获取callInfo
  60. def get_call_info(self, call_id):
  61. text = None
  62. if call_id:
  63. text = self.redis_handler.get(CALL_INFO + call_id)
  64. # print('liuwei::debugger::get_call_info call_id:%s, text:%s'% (call_id, text))
  65. if text:
  66. return CallInfo.from_json(text)
  67. def remove_call_info(self, call_id):
  68. if not call_id:
  69. return None
  70. call_info = self.get_call_info(call_id)
  71. if call_info and call_info.device_info_map:
  72. for k, v in call_info.device_info_map.items():
  73. self.deviceCall.pop(k)
  74. self.delete_key(CALL_INFO + call_id)
  75. def add_device(self, device_id, call_id):
  76. if not device_id or not call_id:
  77. return None
  78. self.deviceCall[device_id] = call_id
  79. def get_user_part(self, device_id):
  80. return self.deviceUserPart.get(device_id)
  81. def add_device_user_part(self, device_id, user_part):
  82. if not device_id or not user_part:
  83. return
  84. self.deviceUserPart[device_id] = user_part
  85. def get_route_gateway(self, saas_id):
  86. return RouteGateway(id=1,
  87. saas_id=saas_id,
  88. name='63366692',
  89. media_host='192.168.20.99',
  90. media_port=5060,
  91. caller_prefix='',
  92. called_prefix='',
  93. status=0)
  94. def add_delay_message(self, 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. self.redis_handler.redis.zadd(key, {msg : action_time})
  100. def get_delay_message(self, action):
  101. key = CTI_ENGINE_DELAY_ACTION % action
  102. current_time = int(time.time() * 1000) # 毫秒级时间戳
  103. members = self.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. self.redis_handler.redis.zrem(key, *action_list)
  110. return action_list
  111. def lock_delay_action(self, val):
  112. key = CTI_ENGINE_DELAY_ACTION_LOCK % val
  113. return self.redis_handler.redis.set(key, "1", ex=60*10, nx=True)
  114. def set_need_play_hold_music(self, call_id):
  115. key = NEED_PLAY_HOLD_MUSIC % call_id
  116. return self.redis_handler.redis.set(key, "1", ex=60 * 1, nx=True)
  117. def get_need_play_hold_music(self, call_id):
  118. key = NEED_PLAY_HOLD_MUSIC % call_id
  119. return self.redis_handler.redis.get(key)
  120. def del_need_play_hold_music(self, call_id):
  121. key = NEED_PLAY_HOLD_MUSIC % call_id
  122. self.delete_key(key)
  123. def set_after_play_hold_music(self, call_id):
  124. key = AFTER_PLAY_HOLD_MUSIC % call_id
  125. return self.redis_handler.redis.set(key, "1", ex=60 * 1, nx=True)
  126. def get_after_play_hold_music(self, call_id):
  127. key = AFTER_PLAY_HOLD_MUSIC % call_id
  128. return self.redis_handler.redis.get(key)