替换 Asterisk Lua 中的 GotoIfTime

我正在使用Lua和Asterisk开发一个基于数据库的拨号计划项目。我在替换.conf拨号计划格式中的GotoIfTime()应用程序时遇到了问题。正如Asterisk Lua参考中所列出的,不应该在Lua中使用Goto Asterisk完成这项工作,对我来说这是很有意义的。

我的检查时间条件的方法与GotoIfTime应用程序完全相同,它将确认一周的某天,小时和分钟时间,然后根据帐户时区进行确认。这个系统将有许多账户,这些账户居住在不同的时区。根据账户定义的时区来决定账户经营的小时数。

我的数据库结构如下:

CREATE TABLE IF NOT EXISTS `hours` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`account_id` int(11) NOT NULL,
`day_start` varchar(3) NOT NULL,
`day_end` varchar(3) NOT NULL,
`hour_start` int(3) NOT NULL,
`minute_start` int(3) NOT NULL,
`hour_end` int(3) NOT NULL,
`minute_end` int(3) NOT NULL,
`closed_dest_type` varchar(20) NOT NULL,
`closed_dest_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
);

CREATE TABLE IF NOT EXISTS `accounts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`timezone` varchar(40) NOT NULL,
);

以下是Lua中这个操作的非常简化的用法:

if (open == true) then
    dial(dest_type, dest_id)
else
    dial(closed_dest_type, closed_dest_id)
end

这是我的困难点:

  1. 天数是通过范围指定的。当天数范围以字符串格式存储时,如何验证当前是一周的哪一天?

  2. 这个系统将使用多个服务器,可能在OS上设置不同的时区。使用GotoIfTime验证帐户时区很容易,因为我只需要将帐户时区数据库条目的结果指向GotoIfTime应用程序的时区参数即可。

点赞
用户861388
用户861388

如果选择 Lua 语言,你需要用 Lua 自己解析日期,并且比较日期的星期几。

或者你可以使用默认的 GotoIftime,但是它会跳出 Lua 函数,因此你需要编写其他内容。

一般情况下,除非你熟悉该语言,否则你不应该使用 Lua。如果你不是 Lua 专家,请使用你熟悉的语言(Perl、PHP等)的拨号计划(dialplan)或 fastAGI。

有关在 Lua 中解析日期的更多信息,请参见此链接:http://lua-users.org/wiki/DateAndTime

2013-12-23 12:11:33
用户1765690
用户1765690

经过一番改动和查看 Asterisk 源代码,我发现 Asterisk 中有内置的 ExecIfTime 应用程序。所以,我使用这个内置于 Asterisk 中的函数来完成我需要的操作,而不是在 Lua 中编写我的时间条件库。使用这种方法更简单。

local zone_dir = "/usr/share/zoneinfo/"

if (resHour.id[1] ~= nil) then
    for i=1,#resHour.id do

        local a = resHour.hour_start[i] .. ":"
        a = a .. resHour.minute_start[i] .. "-"
        a = a .. resHour.hour_end[i] .. ":"
        a = a .. resHour.minute_end[i] .. ","
        a = a .. resHour.day_start[i] .. "-"
        a = a .. resHour.day_end[i] .. ",*,*,"
        a = a .. zone_dir .. resAccount.timezone

        app.execiftime(a .. "?Set(open=1)")
     end

     if (channel.open:get() ~= "1") then
         closed()
     end
end

Asterisk 调试输出如下:

Executing [blah@from-external:1] execiftime("SIP/102-00000211", "9:0-19:0,mon-fri, ,,/usr/share/zoneinfo/America/Chicago?Set(open=1)"

2013-12-24 18:06:15