diff --git a/freeswitch_click2dial/freeswitch_click2dial.py b/freeswitch_click2dial/freeswitch_click2dial.py index 7495b4b..67f37d7 100644 --- a/freeswitch_click2dial/freeswitch_click2dial.py +++ b/freeswitch_click2dial/freeswitch_click2dial.py @@ -28,9 +28,9 @@ try: except ImportError: import ESL # import sys -import csv import StringIO import re +import json _logger = logging.getLogger(__name__) @@ -247,27 +247,17 @@ class freeswitch_server(orm.Model): cr, uid, context=context) calling_party_number = False try: - ret = fs_manager.api( - 'show', "calls as delim |") - f = StringIO.StringIO(ret.getBody()) - reader = csv.DictReader(f, delimiter='|') - for row in reader: - if ("uuid" not in row or row["uuid"] == "" or - row["uuid"] == "uuid"): - break - if row["callstate"] not in ["EARLY", "ACTIVE", "RINGING"]: - continue - if row["direction"] == "outbound": - if (row["cid_num"] == str(user.internal_number) or - row["accountcode"] == str(user.internal_number)): - if row["dest"] and row["dest"] is not None: - calling_party_number = row["dest"] - elif (row["b_cid_num"] == str(user.internal_number) or - row["accountcode"] == str(user.internal_number) or - re.sub(r':', r'/', row["b_name"]) == - user.freeswitch_chan_type + '/' + user.resource + '/'): - if row["cid_num"] and row["cid_num"] is not None: - calling_party_number = row["cid_num"] + request = "channels like /" + re.sub(r'/', r':', user.resource) + \ + ("/" if user.freeswitch_chan_type == "FreeTDM" else "@") + \ + " as json" + ret = fs_manager.api('show', str(request)) + f = json.load(StringIO.StringIO(ret.getBody())) + if int(f['row_count']) > 0: + if (f['rows'][0]['initial_cid_name'] == 'Odoo Connector' or + f['rows'][0]['direction'] == 'inbound'): + calling_party_number = f['rows'][0]['dest'] + else: + calling_party_number = f['rows'][0]['cid_num'] except Exception, e: _logger.error( "Error in the Status request to FreeSWITCH server %s"