From c7c299e0501d2ffa8a81b0165297562c0cdaeafe Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Tue, 20 Oct 2015 15:15:42 +0300 Subject: [PATCH 1/3] Fix. Use `api show channels like` did not work on fusion with BDR. --- .../install/scripts/app/ring_groups/index.lua | 34 ++--------- .../resources/functions/channel_utils.lua | 57 +++++++++++++++++++ 2 files changed, 62 insertions(+), 29 deletions(-) diff --git a/resources/install/scripts/app/ring_groups/index.lua b/resources/install/scripts/app/ring_groups/index.lua index 553cb69ee1..25e07493f7 100644 --- a/resources/install/scripts/app/ring_groups/index.lua +++ b/resources/install/scripts/app/ring_groups/index.lua @@ -38,7 +38,7 @@ local log = require "resources.functions.log".ring_group require "resources.functions.explode"; require "resources.functions.base64"; require "resources.functions.file_exists"; - require "resources.functions.explode"; + require "resources.functions.channel_utils" --get the variables domain_name = session:getVariable("domain_name"); @@ -439,34 +439,10 @@ local log = require "resources.functions.log".ring_group extension_uuid = trim(api:executeString(cmd)); --send to user local dial_string_to_user = "[sip_invite_domain="..domain_name..","..group_confirm.."leg_timeout="..destination_timeout..","..delay_name.."="..destination_delay..",dialed_extension=" .. row.destination_number .. ",extension_uuid="..extension_uuid .. row.record_session .. "]user/" .. row.destination_number .. "@" .. domain_name; - if (ring_group_skip_active ~= nil) then - if (ring_group_skip_active == "true") then - cmd = "show channels like "..destination_number; - reply = trim(api:executeString(cmd)); - --freeswitch.consoleLog("notice", "[ring group] reply "..cmd.." " .. reply .. "\n"); - exploded_reply = {}; - exploded_reply = explode(",",reply); - - if (reply == "0 total.") then - dial_string = dial_string_to_user - else - idle_extension=true; - - if (exploded_reply ~= nil) then - for i,v in ipairs(exploded_reply) do - if(v==destination_number.."@"..domain_name) then - idle_extension=false; - end - end - end - - if(idle_extension) then - dial_string = dial_string_to_user; - end - end - else - --look inside the reply to check for the correct domain_name - dial_string = dial_string_to_user; + if (ring_group_skip_active == "true") then + local channels = channels_by_number(destination_number, domain_name) + if (not channels) or #channels == 0 then + dial_string = dial_string_to_user end else dial_string = dial_string_to_user; diff --git a/resources/install/scripts/resources/functions/channel_utils.lua b/resources/install/scripts/resources/functions/channel_utils.lua index a078939d2e..6fb859adc7 100644 --- a/resources/install/scripts/resources/functions/channel_utils.lua +++ b/resources/install/scripts/resources/functions/channel_utils.lua @@ -1,3 +1,29 @@ +require 'resources.config' +require 'resources.functions.trim' +require 'resources.functions.file_exists' +require 'resources.functions.database_handle' + +local function create_dbh(name) + local dbh = assert(name) + if type(name) == 'string' then + if name == 'switch' and file_exists(database_dir.."/core.db") then + dbh = freeswitch.Dbh("sqlite://"..database_dir.."/core.db") + else + dbh = database_handle(name) + end + end + assert(dbh:connected()) + return dbh +end + +local function dbh_fetch_all(dbh, sql) + local result = {} + local ok, err = dbh:query(sql, function(row) + result[#result + 1] = row + end) + if not ok then return nil, err end + return result +end local api = api or freeswitch.API() @@ -18,3 +44,34 @@ function channel_evalute(uuid, cmd) return result end + +local function switchname() + local result = api:executeString("switchname") + + if result:sub(1, 4) == '-ERR' then return nil, result end + if result == '_undef_' then return false end + + return result +end + +function channels_by_number(number, domain) + local hostname = assert(switchname()) + local dbh = create_dbh('switch') + + local full_number = number .. '@' .. (domain or '%') + + local sql = ([[select * from channels where hostname='%s' and ( + (context = '%s' and (cid_name = '%s' or cid_num = '%s')) + or name like '%s' or presence_id like '%s' or presence_data like '%s' + ) + order by created_epoch + ]]):format(hostname, + domain, number, number, + full_number, full_number, full_number + ) + + local rows = assert(dbh_fetch_all(dbh, sql)) + + dbh:release() + return rows +end From ac724d46b60ced9f38bd20dd514fabf9b3c6fc2b Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Tue, 20 Oct 2015 20:29:57 +0300 Subject: [PATCH 2/3] Add. cache `switchname` --- .../install/scripts/resources/functions/channel_utils.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/resources/install/scripts/resources/functions/channel_utils.lua b/resources/install/scripts/resources/functions/channel_utils.lua index 6fb859adc7..b4a1c55477 100644 --- a/resources/install/scripts/resources/functions/channel_utils.lua +++ b/resources/install/scripts/resources/functions/channel_utils.lua @@ -45,12 +45,16 @@ function channel_evalute(uuid, cmd) return result end +local _switchname local function switchname() + if _switchname then return _switchname end + local result = api:executeString("switchname") if result:sub(1, 4) == '-ERR' then return nil, result end if result == '_undef_' then return false end + _switchname = result return result end From fdb6e818a9839774ca4de156e7db70bf0af9dc58 Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Sun, 25 Oct 2015 10:05:44 +0300 Subject: [PATCH 3/3] Use database class --- .../resources/functions/channel_utils.lua | 28 ++----------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/resources/install/scripts/resources/functions/channel_utils.lua b/resources/install/scripts/resources/functions/channel_utils.lua index b4a1c55477..5cf33c5f8f 100644 --- a/resources/install/scripts/resources/functions/channel_utils.lua +++ b/resources/install/scripts/resources/functions/channel_utils.lua @@ -1,29 +1,7 @@ require 'resources.config' require 'resources.functions.trim' -require 'resources.functions.file_exists' -require 'resources.functions.database_handle' -local function create_dbh(name) - local dbh = assert(name) - if type(name) == 'string' then - if name == 'switch' and file_exists(database_dir.."/core.db") then - dbh = freeswitch.Dbh("sqlite://"..database_dir.."/core.db") - else - dbh = database_handle(name) - end - end - assert(dbh:connected()) - return dbh -end - -local function dbh_fetch_all(dbh, sql) - local result = {} - local ok, err = dbh:query(sql, function(row) - result[#result + 1] = row - end) - if not ok then return nil, err end - return result -end +local Database = require 'resources.functions.database' local api = api or freeswitch.API() @@ -60,7 +38,7 @@ end function channels_by_number(number, domain) local hostname = assert(switchname()) - local dbh = create_dbh('switch') + local dbh = Database.new('switch') local full_number = number .. '@' .. (domain or '%') @@ -74,7 +52,7 @@ function channels_by_number(number, domain) full_number, full_number, full_number ) - local rows = assert(dbh_fetch_all(dbh, sql)) + local rows = assert(dbh:fetch_all(sql)) dbh:release() return rows