Improve ring_member.lua with consistent indentation, notes, and more if session:ready to prevent errors.

This commit is contained in:
markjcrane
2016-01-02 13:09:28 -07:00
parent 8512ba0182
commit 89b2da603f
+62 -53
View File
@@ -16,7 +16,7 @@
-- --
-- The Initial Developer of the Original Code is -- The Initial Developer of the Original Code is
-- Mark J Crane <markjcrane@fusionpbx.com> -- Mark J Crane <markjcrane@fusionpbx.com>
-- Copyright (C) 2010 -- Copyright (C) 2010-2016
-- All Rights Reserved. -- All Rights Reserved.
-- --
-- Contributor(s): -- Contributor(s):
@@ -30,62 +30,68 @@
require "resources.functions.database_handle"; require "resources.functions.database_handle";
dbh = database_handle('system'); dbh = database_handle('system');
sounds_dir = ""; --set default variables
recordings_dir = ""; sounds_dir = "";
pin_number = ""; recordings_dir = "";
max_tries = "3"; pin_number = "";
digit_timeout = "3000"; max_tries = "3";
digit_timeout = "3000";
--define uuid function
local random = math.random
local function uuid()
local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
return string.gsub(template, '[xy]', function (c)
local v = (c == 'x') and random(0, 0xf) or random(8, 0xb)
return string.format('%x', v)
end)
end
local random = math.random --get session variables
local function uuid() if (session:ready()) then
local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' session:answer();
return string.gsub(template, '[xy]', function (c) --session:execute("info", "");
local v = (c == 'x') and random(0, 0xf) or random(8, 0xb) destination_number = session:getVariable("user_name");
return string.format('%x', v) pin_number = session:getVariable("pin_number");
end) sounds_dir = session:getVariable("sounds_dir");
end ring_group_uuid = session:getVariable("ring_group_uuid");
end
if ( session:ready() ) then --get the domain uuid and set other required variables
session:answer(); if (session:ready()) then
--session:execute("info", ""); --get info for the ring group
destination_number = session:getVariable("user_name"); sql = "SELECT * FROM v_ring_groups ";
pin_number = session:getVariable("pin_number"); sql = sql .. "where ring_group_uuid = '"..ring_group_uuid.."' ";
sounds_dir = session:getVariable("sounds_dir"); status = dbh:query(sql, function(row)
ring_group_uuid = session:getVariable("ring_group_uuid"); domain_uuid = row["domain_uuid"];
end);
--get info for the ring group
sql = "SELECT * FROM v_ring_groups ";
sql = sql .. "where ring_group_uuid = '"..ring_group_uuid.."' ";
status = dbh:query(sql, function(row)
domain_uuid = row["domain_uuid"];
end);
destination_timeout = 15; destination_timeout = 15;
destination_delay = 0; destination_delay = 0;
--create the primary key uuid
ring_group_destination_uuid = uuid();
end
--pin number is not required
--press 1 to login and 2 to logout
ring_group_destination_uuid = uuid(); if (session:ready()) then
--pin number is not required
--press 1 to login and 2 to logout
menu_selection = session:playAndGetDigits(1, 1, max_tries, digit_timeout, "#", "ivr/ivr-enter_destination_telephone_number.wav", "", "\\d+"); menu_selection = session:playAndGetDigits(1, 1, max_tries, digit_timeout, "#", "ivr/ivr-enter_destination_telephone_number.wav", "", "\\d+");
freeswitch.consoleLog("NOTICE", "menu_selection: "..menu_selection.."\n"); freeswitch.consoleLog("NOTICE", "menu_selection: "..menu_selection.."\n");
if (menu_selection == "1") then if (menu_selection == "1") then
--first, check to see if is already in that ring group --first, check to see if the destination is already in this ring group
sql = [[ sql = [[
SELECT COUNT(*) AS in_group FROM SELECT COUNT(*) AS in_group FROM
v_ring_group_destinations v_ring_group_destinations
WHERE WHERE
domain_uuid = ']]..domain_uuid..[[' domain_uuid = ']]..domain_uuid..[['
AND ring_group_uuid = ']]..ring_group_uuid..[[' AND ring_group_uuid = ']]..ring_group_uuid..[['
AND destination_number = ']]..destination_number..[[' AND destination_number = ']]..destination_number..[['
]]; ]];
--freeswitch.consoleLog("NOTICE", "ring_group_member: SQL "..sql.."\n"); --freeswitch.consoleLog("NOTICE", "ring_group_member: SQL "..sql.."\n");
assert(dbh:query(sql, function(row) assert(dbh:query(sql, function(row)
if (row.in_group == "0") then if (row.in_group == "0") then
sql = [[ sql = [[
@@ -104,12 +110,12 @@ if ( session:ready() ) then
']]..destination_number..[[', ']]..destination_number..[[',
]]..destination_delay..[[, ]]..destination_delay..[[,
]]..destination_timeout..[[) ]]..destination_timeout..[[)
]]; ]];
--freeswitch.consoleLog("NOTICE", "ring_group_member: SQL "..sql.."\n"); --freeswitch.consoleLog("NOTICE", "ring_group_member: SQL "..sql.."\n");
dbh:query(sql); dbh:query(sql);
freeswitch.consoleLog("NOTICE", "ring_group_member: LOG IN\n"); freeswitch.consoleLog("NOTICE", "ring_group_member: LOG IN\n");
session:streamFile("ivr/ivr-you_are_now_logged_in.wav"); session:streamFile("ivr/ivr-you_are_now_logged_in.wav");
@@ -118,8 +124,7 @@ if ( session:ready() ) then
session:streamFile("ivr/ivr-you_are_now_logged_in.wav"); session:streamFile("ivr/ivr-you_are_now_logged_in.wav");
end end
end)); end));
end end
if (menu_selection == "2") then if (menu_selection == "2") then
sql = [[ sql = [[
@@ -136,10 +141,14 @@ if ( session:ready() ) then
freeswitch.consoleLog("NOTICE", "ring_group_member: LOG OUT\n"); freeswitch.consoleLog("NOTICE", "ring_group_member: LOG OUT\n");
session:streamFile("ivr/ivr-you_are_now_logged_out.wav"); session:streamFile("ivr/ivr-you_are_now_logged_out.wav");
end end
end
--wait for the file to be written before proceeding --wait for the file to be written before proceeding
if (session:ready()) then
--session:sleep(1000); --session:sleep(1000);
end
--hangup --hangup
if (session:ready()) then
session:hangup(); session:hangup();
end end