Forráskód Böngészése

outbound client test

刘威 5 hónapja
szülő
commit
834161b138
2 módosított fájl, 72 hozzáadás és 59 törlés
  1. 65 55
      src/core/callcenter/esl/client.py
  2. 7 4
      src/core/callcenter/web.py

+ 65 - 55
src/core/callcenter/esl/client.py

@@ -3,12 +3,15 @@
 
 import json
 import random
+import socketserver
+
 import ESL
 import time
 import mmh3
 import threading
 import traceback
 import concurrent.futures
+import src.core.callcenter.cache as Cache
 from src.core.callcenter.constant import SK, EMPTY
 from src.core.callcenter.esl.constant.esl_constant import BRIDGE_VARIABLES, BRIDGE, HANGUP, NORMAL_CLEARING, SIP_HEADER, SPACE, SPLIT, SOFIA, \
     ORIGINATE, PARK, SET, EAVESDROP, SMF_ALEG, EXECUTE, PLAYBACK, PAUSE, TRANSFER, UUID_TRANSFER, UUID_BROADCAST, UUID_BREAK, UUID_HOLD, \
@@ -18,18 +21,17 @@ import src.core.callcenter.esl.handler as event_handler
 from src.core.callcenter.esl.constant.sip_header_constant import sipHeaderHoldMusic
 from src.core.callcenter.enumeration import CallCause
 from src.core.callcenter.esl.handler.default_esl_event_handler import DefaultEslEventHandler
-from src.core.voip.bot import BotAgent
 from src.core.voip.constant import *
 
 
 class InboundClient:
 
-    def __init__(self, logger):
+    def __init__(self, agent, logger):
         self.con = None
         self.thread_num = 32
         self.is_stopping = False
         self.logger = logger
-        self.bot_agent = BotAgent(logger)
+        self.bot_agent = agent
         self.handler_table = self.scan_esl_event_handlers()
         self.default_event_handler = DefaultEslEventHandler(self, self.bot_agent, self.logger)
         self.host, self.port, self.password = SERVE_HOST, '8021', '4918257983818884358'
@@ -371,55 +373,63 @@ class InboundClient:
         self.is_stopping = True
 
 
-# class OutboundClient:
-#
-#     class ESLRequestHandler(socketserver.BaseRequestHandler):
-#
-#         def setup(self):
-#             try:
-#                 print(self.client_address, 'connected!')
-#                 fd = self.request.fileno()
-#                 print('0000', fd)
-#                 con = ESL.ESLconnection(fd)
-#                 print('Connected: ', con.connected())
-#                 if con.connected():
-#                     info = con.getInfo()
-#                     print(json.loads(info.serialize('json')))
-#                     event_name = info.getHeader("Event-Name")
-#                     uuid = info.getHeader("unique-id")
-#                     print(uuid, event_name)
-#
-#                     # destination = "user/1001"
-#                     # msg = ESL.ESLevent("sendmsg", uuid)
-#                     # msg.addHeader("call-command", "execute")
-#                     # msg.addHeader("execute-app-name", "bridge")
-#                     # msg.addHeader("execute-app-arg", destination)
-#                     # # 发送消息以执行 bridge 操作
-#                     # con.sendEvent(msg)
-#                     # print(f"Call {uuid} is bridged to {destination}")
-#
-#                     con.execute("answer", "", uuid)
-#
-#                     # con.execute("transfer", "1001 XML default", uuid)
-#                     con.execute("bridge", "user/1001", uuid)
-#                     # try:
-#                     #     con.disconnect()
-#                     # except:
-#                     #     print('come in ')
-#                     #     traceback.print_exc()
-#                 else:
-#                     print("Failed to connect to FreeSWITCH")
-#             except:
-#                 print('come in 1')
-#                 traceback.print_exc()
-#
-#         def ack(self, con):
-#             OutboundClient.ack(self, con)
-#
-#     def start(self, HOST= '0.0.0.0', PORT=8084):
-#         # HOST, PORT = "0.0.0.0", 8084
-#
-#         # 创建一个 TCP 服务器
-#         with socketserver.TCPServer((HOST, PORT), self.ESLRequestHandler) as server:
-#             print(f"ESL server listening on {HOST}:{PORT}")
-#             server.serve_forever()
+class OutboundClient:
+
+    def __init__(self, agent, logger):
+        self.bot_agent = agent
+        self.logger = logger
+        threading.Thread(target=self.start, args=()).start()
+
+    class ESLRequestHandler(socketserver.BaseRequestHandler):
+        def __init__(self, outbound):
+            self.bot_agent = outbound.bot_agent
+            self.logger = outbound.logger
+
+        def setup(self):
+            try:
+                print(self.client_address, 'connected!')
+                fd = self.request.fileno()
+                con = ESL.ESLconnection(fd)
+                print('Connected: ', con.connected())
+                if con.connected():
+                    info = con.getInfo()
+
+                    # print(json.loads(info.serialize('json')))
+                    event_name = info.getHeader("Event-Name")
+                    device_id = info.getHeader("unique-id")
+                    kwargs = json.loads(info.serialize('json'))
+                    destination = self.bot_agent.register(**kwargs)
+                    self.logger.info("device_id=%s, destination=%s", device_id, destination)
+
+                    Cache.add_device_user_part(device_id, destination)
+                    con.execute("bridge", f"user/{destination}", device_id)
+
+                    # destination = "user/1001"
+                    # msg = ESL.ESLevent("sendmsg", uuid)
+                    # msg.addHeader("call-command", "execute")
+                    # msg.addHeader("execute-app-name", "bridge")
+                    # msg.addHeader("execute-app-arg", destination)
+                    # # 发送消息以执行 bridge 操作
+                    # con.sendEvent(msg)
+                    # print(f"Call {uuid} is bridged to {destination}")
+
+                    # con.execute("answer", "", uuid)
+
+                    # con.execute("transfer", "1001 XML default", uuid)
+                    # try:
+                    #     con.disconnect()
+                    # except:
+                    #     print('come in ')
+                    #     traceback.print_exc()
+                else:
+                    print("Failed to connect to FreeSWITCH")
+            except:
+                traceback.print_exc()
+
+    def start(self, HOST= '0.0.0.0', PORT=8084):
+        # HOST, PORT = "0.0.0.0", 8084
+
+        # 创建一个 TCP 服务器
+        with socketserver.TCPServer((HOST, PORT), self.ESLRequestHandler(self)) as server:
+            print(f"ESL server listening on {HOST}:{PORT}")
+            server.serve_forever()

+ 7 - 4
src/core/callcenter/web.py

@@ -8,11 +8,12 @@ import src.core.callcenter.cache as Cache
 from src.core.callcenter.agent import AgentService
 from src.core.callcenter.constant import success
 from src.core.callcenter.enumeration import CallType
-from src.core.callcenter.esl.client import InboundClient
+from src.core.callcenter.esl.client import InboundClient, OutboundClient
 from flask import Flask, request, render_template_string
 
 from src.core.callcenter.call import CallService
 from src.core.callcenter.model import MakeCallRequest, AgentInfo, AgentActionRequest, HangupCallRequest
+from src.core.voip.bot import BotAgent
 
 dictConfig({
         "version": 1,
@@ -49,9 +50,11 @@ dictConfig({
 app = Flask(__name__)
 app.config['SECRET_KEY'] = ''
 
-client = InboundClient(app.logger)
-call_service = CallService(client, app.logger)
-agent_service = AgentService(client, app.logger)
+agent = BotAgent(app.logger)
+inbound_client = InboundClient(agent, app.logger)
+outbound_client = OutboundClient(agent, app.logger)
+call_service = CallService(inbound_client, app.logger)
+agent_service = AgentService(inbound_client, app.logger)
 
 @app.route('/')
 def index():