-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·67 lines (58 loc) · 2.49 KB
/
server.py
File metadata and controls
executable file
·67 lines (58 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
import sys
import json
import asyncio
import websockets
from switchio import get_connection
msisdn = None
sockets = []
def filter(input):
output = {}
for key in ['Event-Name','Job-Command','Job-UUID','Unique-ID','Caller-Logical-Direction','Caller-Caller-ID-Number','Caller-Destination-Number','Hangup-Cause']:
if key in input:
output[key] = input[key]
return output
async def request(data):
global connection
print('REQUEST : '+data)
response = await connection.bgapi(data)
print('RESPONSE : '+json.dumps(filter(response)))
async def commands(websocket, path):
global msisdn
sockets.append(websocket)
try:
while(loop.is_running()):
message = await websocket.recv()
print('COMMAND : '+message)
command = json.loads(message)
if (command['action'] == 'call' and command['destination']):
await request('originate {origination_caller_id_number='+msisdn+'}sofia/gateway/mss/'+command['destination']+' &park()')
elif (command['action'] == 'answer' and command['uuid']):
await request('uuid_answer '+command['uuid'])
elif (command['action'] == 'play' and command['uuid'] and command['file']):
await request('uuid_broadcast '+ command['uuid']+' playback::/home/app/EslWebSocketApp/'+command['file'])
elif (command['action'] == 'hangup' and command['uuid']):
await request('uuid_kill '+command['uuid']+' CALL_REJECTED')
except BaseException as e:
print("ERROR : {} : {}".format(type(e).__name__, e))
finally:
sockets.remove(websocket)
async def events(host, loop):
global connection
while(loop.is_running()):
event = filter(await connection.recv_event())
print('EVENT : '+json.dumps(event))
global sockets
for socket in sockets:
await socket.send(json.dumps(event))
if len(sys.argv) == 2:
try:
msisdn = sys.argv[1]
loop = asyncio.get_event_loop()
connection = get_connection('sipbox', loop=loop)
connection.connect()
connection.subscribe(['BACKGROUND_JOB','CHANNEL_PARK','CHANNEL_ANSWER','PLAYBACK_START','PLAYBACK_STOP','CHANNEL_HANGUP'])
loop.run_until_complete(websockets.serve(commands, 'localhost', 8765))
loop.run_until_complete(events('sipbox',loop))
except BaseException as e:
print("ERROR : {} : {}".format(type(e).__name__, e))