forked from gonicus/fsqueuemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backends.py
113 lines (95 loc) · 4.59 KB
/
backends.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# This file is part of fsqueuemon - https://github.com/gonicus/fsqueuemon
# Copyright (C) 2014 GONICUS GmbH, Germany - http://www.gonicus.de
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2
# of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
from xmlrpclib import ServerProxy
from xml.etree.cElementTree import XML
import datetime
class CallcenterStatusBackend(object):
domain = 'mydomain.example.com'
def __init__(self, uri):
self.server = ServerProxy(uri)
def _parse_callcenter(self, apiresult):
"""Parse output of mod_callcenter's api output"""
data = []
lines = apiresult.splitlines()
if lines:
keys = lines[0].strip().split('|')
for line in lines[1:]:
fields = line.split('|')
if len(fields) != len(keys):
continue
entry = {}
for i in range(0, len(keys)):
entry[keys[i]] = fields[i]
data.append(entry)
return data
def _parse_xml(self, apiresult):
data = []
for row in XML(apiresult):
data.append(dict([(e.tag, e.text) for e in row]))
return data
def _get_user_data(self, extension, data):
presence_id = self.server.freeswitch.api('user_data', '%s@%s %s' % (extension, self.domain, data))
if presence_id:
return presence_id
return None
def get_agents(self):
# Get list of all defined agents
output = self.server.freeswitch.api('callcenter_config', 'agent list')
agents = dict([(agent['name'], agent) for agent in self._parse_callcenter(output)])
# Get tiers and update agent info
output = self.server.freeswitch.api('callcenter_config', 'tier list')
tiers = self._parse_callcenter(output)
for tier in tiers:
agent = agents.get(tier['agent'])
if not agent:
continue
if not agent.get('queues'):
agent['queues'] = []
agent['queues'].append({'queue': tier['queue'], 'level': tier['level'], 'position': tier['position']})
presence_table = {}
# Get extension, real name and presence ID of logged in agents
for agtid, agent in agents.iteritems():
if agent['contact']:
contact = agent['contact'].split('/')
if contact[0].endswith('loopback'):
agent['extension'] = contact[1]
if contact[1] and contact[1][0] not in ('0', '9'):
realname = self._get_user_data(contact[1], 'var effective_caller_id_name')
if realname:
agent['realname'] = realname
presence_id = self._get_user_data(contact[1], 'var presence_id')
if presence_id:
agent['presence_id'] = presence_id
presence_table[presence_id] = agent
# Get currently active channels
output = self.server.freeswitch.api('show', 'channels as xml')
channels = self._parse_xml(output)
# Update presence state from active channels
for channel in channels:
if channel['presence_id'] in presence_table:
presence_table[channel['presence_id']]['callstate'] = channel['callstate']
presence_table[channel['presence_id']]['direction'] = 'caller' if channel['direction'] == 'inbound' else 'callee'
return agents
def get_queues(self):
output = self.server.freeswitch.api('callcenter_config', 'queue list')
queues = dict([(queue['name'], queue) for queue in self._parse_callcenter(output)])
for queue in queues:
output = self._parse_callcenter(self.server.freeswitch.api('callcenter_config', 'queue list members %s' % queue))
output.sort(key=lambda m: m['system_epoch'])
queues[queue]['members'] = output
queues[queue]['waiting_count'] = len([x for x in output if x['state'] == 'Waiting'])
return queues