From 64b113448250de78d389d2320a076106debc3461 Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Mon, 29 May 2017 18:50:20 +0300 Subject: [PATCH] Add. Support DND feature via BLF key (#2606) --- .../conf/dialplan/490_do-not-disturb.xml | 6 ++- .../install/scripts/call_flow_subscribe.lua | 54 ++++++++++++++++--- resources/install/scripts/do_not_disturb.lua | 16 ++++++ .../resources/functions/presence_in.lua | 8 +++ 4 files changed, 76 insertions(+), 8 deletions(-) diff --git a/app/dialplan/resources/switch/conf/dialplan/490_do-not-disturb.xml b/app/dialplan/resources/switch/conf/dialplan/490_do-not-disturb.xml index 63bfbeabf5..c019f7280e 100644 --- a/app/dialplan/resources/switch/conf/dialplan/490_do-not-disturb.xml +++ b/app/dialplan/resources/switch/conf/dialplan/490_do-not-disturb.xml @@ -8,9 +8,13 @@ - + + + + + diff --git a/resources/install/scripts/call_flow_subscribe.lua b/resources/install/scripts/call_flow_subscribe.lua index 7931c8a4c0..765dd3b606 100644 --- a/resources/install/scripts/call_flow_subscribe.lua +++ b/resources/install/scripts/call_flow_subscribe.lua @@ -10,7 +10,7 @@ local find_call_flow do local find_call_flow_sql = [[select t1.call_flow_uuid, t1.call_flow_status from v_call_flows t1 inner join v_domains t2 on t1.domain_uuid = t2.domain_uuid -where t2.domain_name = '%s' and t1.call_flow_feature_code = '%s' +where t2.domain_name = :domain_name and t1.call_flow_feature_code = :feature_code ]] function find_call_flow(user) @@ -18,8 +18,7 @@ function find_call_flow(user) if not domain_name then return end local dbh = Database.new('system') if not dbh then return end - local sql = string.format(find_call_flow_sql, dbh:escape(domain_name), dbh:escape(ext)) - local row = dbh:first_row(sql) + local row = dbh:first_row(find_call_flow_sql, {domain_name = domain_name, feature_code = ext}) dbh:release() if not row then return end return row.call_flow_uuid, row.call_flow_status @@ -27,6 +26,23 @@ end end +local find_dnd do +local find_dnd_sql = [[select t1.do_not_disturb +from v_extensions t1 inner join v_domains t2 on t1.domain_uuid = t2.domain_uuid +where t2.domain_name = :domain_name and (t1.extension = :extension or t1.number_alias=:extension)]] + +find_dnd = function(user) + local ext, domain_name = split_first(user, '@', true) + if not domain_name then return end + local dbh = Database.new('system') + if not dbh then return end + local dnd = dbh:first_value(find_dnd_sql, {domain_name = domain_name, extension = ext}) + dbh:release() + return dnd +end + +end + local service_name = "call_flow" local pid_file = scripts_dir .. "/run/" .. service_name .. ".tmp" @@ -51,11 +67,9 @@ events:bind("CUSTOM::fusion::service::control", function(self, name, event) log.warningf('Unknown service command: %s', command or '') end) --- FS receive SUBSCRIBE to BLF from device -events:bind("PRESENCE_PROBE", function(self, name, event) - --handle only blf with `flow+` prefix - if event:getHeader('proto') ~= 'flow' then return end +local protocols = {} +protocols.flow = function(event) local from, to = event:getHeader('from'), event:getHeader('to') local expires = tonumber(event:getHeader('expires')) if expires and expires > 0 then @@ -69,6 +83,32 @@ events:bind("PRESENCE_PROBE", function(self, name, event) else log.noticef("%s UNSUBSCRIBE from %s", from, to) end +end + +protocols.dnd = function(event) + local from, to = event:getHeader('from'), event:getHeader('to') + local expires = tonumber(event:getHeader('expires')) + if expires and expires > 0 then + local proto, user = split_first(to, '+', true) + user = user or proto + local dnd_status = find_dnd(user) + if dnd_status then + log.noticef("Find DND: %s staus: %s", to, tostring(dnd_status)) + presence_in.turn_lamp(dnd_status == "true", to) + else + log.warningf("Can not find DND: %s", to) + end + else + log.noticef("%s UNSUBSCRIBE from %s", from, to) + end +end + +-- FS receive SUBSCRIBE to BLF from device +events:bind("PRESENCE_PROBE", function(self, name, event) + local proto = event:getHeader('proto') + local handler = proto and protocols[proto] + if not handler then return end + return handler(event) end) log.notice("start") diff --git a/resources/install/scripts/do_not_disturb.lua b/resources/install/scripts/do_not_disturb.lua index 9edd40ba1d..2e64a56d20 100644 --- a/resources/install/scripts/do_not_disturb.lua +++ b/resources/install/scripts/do_not_disturb.lua @@ -43,6 +43,8 @@ --include config.lua require "resources.functions.config"; + local presence_in = require "resources.functions.presence_in" + --check if the session is ready if ( session:ready() ) then --answer the call @@ -57,6 +59,7 @@ extension_uuid = session:getVariable("extension_uuid"); context = session:getVariable("context"); if (not context ) then context = 'default'; end + toggle = (enabled == "toggle") --set the sounds path for the language, dialect and voice default_language = session:getVariable("default_language"); @@ -93,6 +96,9 @@ accountcode = row.accountcode; follow_me_uuid = row.follow_me_uuid; do_not_disturb = row.do_not_disturb; + if toggle then + enabled = (do_not_disturb == 'true') and 'false' or 'true' + end --freeswitch.consoleLog("NOTICE", "[do_not_disturb] extension "..row.extension.."\n"); --freeswitch.consoleLog("NOTICE", "[do_not_disturb] accountcode "..row.accountcode.."\n"); end); @@ -210,4 +216,14 @@ --end the call session:hangup(); + -- BLF for display DND status + local function dnd_blf(enabled, id, domain) + local user = string.format('dnd+%s@%s', id, domain) + presence_in.turn_lamp(enabled, user) + end + + dnd_blf(enabled == "true", extension, domain_name) + if number_alias and #number_alias > 0 then + dnd_blf(enabled == "true", number_alias, domain_name) + end end diff --git a/resources/install/scripts/resources/functions/presence_in.lua b/resources/install/scripts/resources/functions/presence_in.lua index 988850119b..04fa0e733a 100644 --- a/resources/install/scripts/resources/functions/presence_in.lua +++ b/resources/install/scripts/resources/functions/presence_in.lua @@ -1,6 +1,10 @@ require "resources.functions.split" +local api = require "resources.functions.api" +local log = require "resources.functions.log".presence local function turn_lamp(on, user, uuid) + log.debugf('turn_lamp: %s - %s(%s)', tostring(user), tostring(on), type(on)) + local userid, domain, proto = split_first(user, "@", true) proto, userid = split_first(userid, "+", true) if userid then @@ -9,6 +13,7 @@ local function turn_lamp(on, user, uuid) proto = "sip" end + uuid = uuid or api:execute('create_uuid') local event = freeswitch.Event("PRESENCE_IN"); event:addHeader("proto", proto); @@ -26,6 +31,9 @@ local function turn_lamp(on, user, uuid) else event:addHeader("answer-state", "terminated"); end + + -- log.debug(event:serialize()) + event:fire(); end