Fix small bags and clear code in call_flow.

Move code to turn on/off BLF to separate function.
```Lua
presence_in.turn_lamp( toggle == "false",
  call_flow_feature_code.."@"..domain_name,
  call_flow_uuid
);
```

Close temp file in call_flow_monitor because on Windows it prevent to remove it.
Connect/release to database inside call_flow_monitor loop.
Check successful connection to database in call_flow_monitor loop so monitor did not crash
if connection temporary lost.
This commit is contained in:
Alexey Melnichuk 2015-10-02 14:02:13 +04:00
parent b8823225c5
commit 679905e171
3 changed files with 106 additions and 132 deletions

View File

@ -35,6 +35,10 @@
require "resources.functions.database_handle"; require "resources.functions.database_handle";
dbh = database_handle('system'); dbh = database_handle('system');
local log = require "resources.functions.log".call_flow
local presence_in = require "resources.functions.presence_in"
if (session:ready()) then if (session:ready()) then
--get the variables --get the variables
domain_name = session:getVariable("domain_name"); domain_name = session:getVariable("domain_name");
@ -51,11 +55,9 @@ if (session:ready()) then
if (not default_voice) then default_voice = 'callie'; end if (not default_voice) then default_voice = 'callie'; end
--get the extension list --get the extension list
sql = [[SELECT * FROM v_call_flows sql = "SELECT * FROM v_call_flows where call_flow_uuid = '"..call_flow_uuid.."'"
where call_flow_uuid = ']]..call_flow_uuid..[[']] -- .. "and call_flow_enabled = 'true'"
--and call_flow_enabled = 'true' --log.notice("SQL: %s", sql);
--freeswitch.consoleLog("notice", "SQL:" .. sql .. "\n");
app_data = "";
x = 0; x = 0;
dbh:query(sql, function(row) dbh:query(sql, function(row)
@ -68,17 +70,15 @@ if (session:ready()) then
call_flow_label = row.call_flow_label; call_flow_label = row.call_flow_label;
call_flow_anti_label = row.call_flow_anti_label; call_flow_anti_label = row.call_flow_anti_label;
if (string.len(call_flow_status) == 0) then if #call_flow_status == 0 then
call_flow_status = "true";
end
if (call_flow_status == "true") then
app = row.call_flow_app; app = row.call_flow_app;
data = row.call_flow_data data = row.call_flow_data
else else
if (call_flow_status == "true") then app = row.call_flow_anti_app;
app = row.call_flow_app; data = row.call_flow_anti_data
data = row.call_flow_data
else
app = row.call_flow_anti_app;
data = row.call_flow_anti_data
end
end end
end); end);
@ -99,70 +99,34 @@ if (session:ready()) then
end end
--feature code - toggle the status --feature code - toggle the status
if (string.len(call_flow_status) == 0) then toggle = (call_flow_status == "true") and "false" or "true"
toggle = "false";
else -- turn the lamp
if (call_flow_status == "true") then presence_in.turn_lamp( toggle == "false",
toggle = "false"; call_flow_feature_code.."@"..domain_name,
else call_flow_uuid
toggle = "true"; );
end
end local active_flow_label = (toggle == "true") and call_flow_label or call_flow_anti_label
if (toggle == "true") then --answer and play a tone
--set the presence to terminated - turn the lamp off: session:answer();
event = freeswitch.Event("PRESENCE_IN"); if #active_flow_label > 0 then
event:addHeader("proto", "sip"); api = freeswitch.API();
event:addHeader("event_type", "presence"); reply = api:executeString("uuid_display "..session:get_uuid().." "..active_flow_label);
event:addHeader("alt_event_type", "dialog");
event:addHeader("Presence-Call-Direction", "outbound");
event:addHeader("state", "Active (1 waiting)");
event:addHeader("from", call_flow_feature_code.."@"..domain_name);
event:addHeader("login", call_flow_feature_code.."@"..domain_name);
event:addHeader("unique-id", call_flow_uuid);
event:addHeader("answer-state", "terminated");
event:fire();
--answer and play a tone
session:answer();
if (string.len(call_flow_label) > 0) then
api = freeswitch.API();
reply = api:executeString("uuid_display "..session:get_uuid().." "..call_flow_label);
end
session:execute("sleep", "2000");
session:execute("playback", "tone_stream://%(200,0,500,600,700)");
--show in the console
freeswitch.consoleLog("notice", "Call Flow: label="..call_flow_label..",status=true,uuid="..call_flow_uuid.."\n");
else
--set presence in - turn lamp on
event = freeswitch.Event("PRESENCE_IN");
event:addHeader("proto", "sip");
event:addHeader("login", call_flow_feature_code.."@"..domain_name);
event:addHeader("from", call_flow_feature_code.."@"..domain_name);
event:addHeader("status", "Active (1 waiting)");
event:addHeader("rpid", "unknown");
event:addHeader("event_type", "presence");
event:addHeader("alt_event_type", "dialog");
event:addHeader("event_count", "1");
event:addHeader("unique-id", call_flow_uuid);
event:addHeader("Presence-Call-Direction", "outbound")
event:addHeader("answer-state", "confirmed");
event:fire();
--answer and play a tone
session:answer();
if (string.len(call_flow_anti_label) > 0) then
api = freeswitch.API();
reply = api:executeString("uuid_display "..session:get_uuid().." "..call_flow_anti_label);
end
session:execute("sleep", "2000");
session:execute("playback", "tone_stream://%(500,0,300,200,100,50,25)");
--show in the console
freeswitch.consoleLog("notice", "Call Flow: label="..call_flow_anti_label..",status=false,uuid="..call_flow_uuid.."\n");
end end
session:execute("sleep", "2000");
session:execute("playback", "tone_stream://%(200,0,500,600,700)");
--show in the console
log.noticef("label=%s,status=%s,uuid=%s", active_flow_label, toggle, call_flow_uuid);
--store in database
dbh:query("UPDATE v_call_flows SET call_flow_status = '"..toggle.."' WHERE call_flow_uuid = '"..call_flow_uuid.."'"); dbh:query("UPDATE v_call_flows SET call_flow_status = '"..toggle.."' WHERE call_flow_uuid = '"..call_flow_uuid.."'");
--hangup the call --hangup the call
session:hangup(); session:hangup();
else else
--app_data log.notice("execute " .. app .. " " .. data);
freeswitch.consoleLog("notice", "Call Flow: " .. app .. " " .. data .. "\n");
--exucute the application --exucute the application
session:execute(app, data); session:execute(app, data);

View File

@ -38,9 +38,11 @@
require "resources.functions.file_exists"; require "resources.functions.file_exists";
require "resources.functions.mkdir"; require "resources.functions.mkdir";
--connect to the database
require "resources.functions.database_handle"; require "resources.functions.database_handle";
dbh = database_handle('system');
local log = require "resources.functions.log".call_flow_monitor
local presence_in = require "resources.functions.presence_in"
--make sure the scripts/run dir exists --make sure the scripts/run dir exists
mkdir(scripts_dir .. "/run"); mkdir(scripts_dir .. "/run");
@ -61,69 +63,52 @@
--used to stop the lua service --used to stop the lua service
local file = assert(io.open(run_file, "w")); local file = assert(io.open(run_file, "w"));
file:write("remove this file to stop the script"); file:write("remove this file to stop the script");
file:close()
log.notice("Start")
--monitor the call flows status --monitor the call flows status
x = 0 local sql = "select d.domain_name, f.call_flow_uuid, f.call_flow_extension, f.call_flow_feature_code," ..
"f.call_flow_status, f.call_flow_label, f.call_flow_anti_label "..
"from v_call_flows as f, v_domains as d " ..
"where f.domain_uuid = d.domain_uuid " -- and call_flow_enabled = 'true'
while true do while true do
--get the extension list -- debug print
sql = [[select d.domain_name, f.call_flow_uuid, f.call_flow_extension, f.call_flow_feature_code, f.call_flow_status, f.call_flow_label, f.call_flow_anti_label
from v_call_flows as f, v_domains as d
where f.domain_uuid = d.domain_uuid]]
--and call_flow_enabled = 'true'
if (debug["sql"]) then if (debug["sql"]) then
freeswitch.consoleLog("notice", "SQL:" .. sql .. "\n"); log.notice("SQL:" .. sql);
end end
x = 0;
dbh:query(sql, function(row)
domain_name = row.domain_name;
call_flow_uuid = row.call_flow_uuid;
--call_flow_name = row.call_flow_name;
call_flow_extension = row.call_flow_extension;
call_flow_feature_code = row.call_flow_feature_code;
--call_flow_context = row.call_flow_context;
call_flow_status = row.call_flow_status;
--pin_number = row.call_flow_pin_number;
call_flow_label = row.call_flow_label;
call_flow_anti_label = row.call_flow_anti_label;
if (call_flow_status == "true") then --connect to the database
--set the presence to terminated - turn the lamp off: local dbh = database_handle('system');
event = freeswitch.Event("PRESENCE_IN");
event:addHeader("proto", "sip"); --get the extension list
event:addHeader("event_type", "presence"); if dbh:connected() then
event:addHeader("alt_event_type", "dialog"); dbh:query(sql, function(row)
event:addHeader("Presence-Call-Direction", "outbound"); local domain_name = row.domain_name;
event:addHeader("state", "Active (1 waiting)"); local call_flow_uuid = row.call_flow_uuid;
event:addHeader("from", call_flow_feature_code.."@"..domain_name); --local call_flow_name = row.call_flow_name;
event:addHeader("login", call_flow_feature_code.."@"..domain_name); --local call_flow_extension = row.call_flow_extension;
event:addHeader("unique-id", call_flow_uuid); local call_flow_feature_code = row.call_flow_feature_code;
event:addHeader("answer-state", "terminated"); --local call_flow_context = row.call_flow_context;
event:fire(); local call_flow_status = row.call_flow_status;
--show in the console --local pin_number = row.call_flow_pin_number;
if (debug["log"]) then local call_flow_label = row.call_flow_label;
freeswitch.consoleLog("notice", "Call Flow: label="..call_flow_label..",status=true,uuid="..call_flow_uuid.."\n"); local call_flow_anti_label = row.call_flow_anti_label;
end
else -- turn the lamp
--set presence in - turn lamp on presence_in.turn_lamp( call_flow_status == "false",
event = freeswitch.Event("PRESENCE_IN"); call_flow_feature_code.."@"..domain_name,
event:addHeader("proto", "sip"); call_flow_uuid
event:addHeader("login", call_flow_feature_code.."@"..domain_name); );
event:addHeader("from", call_flow_feature_code.."@"..domain_name);
event:addHeader("status", "Active (1 waiting)"); if (debug["log"]) then
event:addHeader("rpid", "unknown"); local label = (call_flow_status == "true") and call_flow_label or call_flow_anti_label
event:addHeader("event_type", "presence"); log.noticef("label=%s,status=%s,uuid=%s", label, call_flow_status, call_flow_uuid);
event:addHeader("alt_event_type", "dialog"); end
event:addHeader("event_count", "1"); end);
event:addHeader("unique-id", call_flow_uuid); end
event:addHeader("Presence-Call-Direction", "outbound");
event:addHeader("answer-state", "confirmed"); -- release dbh
event:fire(); dbh:release()
--show in the console
if (debug["log"]) then
freeswitch.consoleLog("notice", "Call Flow: label="..call_flow_anti_label..",status=false,uuid="..call_flow_uuid.."\n");
end
end
end);
--exit the loop when the file does not exist --exit the loop when the file does not exist
if (not file_exists(run_file)) then if (not file_exists(run_file)) then
@ -132,4 +117,6 @@
--sleep a moment to prevent using unecessary resources --sleep a moment to prevent using unecessary resources
freeswitch.msleep(sleep*1000); freeswitch.msleep(sleep*1000);
end end
log.notice("Stop")

View File

@ -0,0 +1,23 @@
local function turn_lamp(on, user, uuid)
local event = freeswitch.Event("PRESENCE_IN");
event:addHeader("proto", "sip");
event:addHeader("event_type", "presence");
event:addHeader("alt_event_type", "dialog");
event:addHeader("Presence-Call-Direction", "outbound");
event:addHeader("from", user);
event:addHeader("login", user);
event:addHeader("unique-id", uuid);
event:addHeader("status", "Active (1 waiting)");
if on then
event:addHeader("answer-state", "confirmed");
event:addHeader("rpid", "unknown");
event:addHeader("event_count", "1");
else
event:addHeader("answer-state", "terminated");
end
event:fire();
end
return {
turn_lamp = turn_lamp;
}