freeswitch lua mysql_freeswitch 折腾了一个星期 lua 实现的 act 程序,抛砖引玉

---上次发帖说用 sip做一个呼叫系统,折腾得好痛苦,如果有意的坛友可以参考一下,节省点时间和表情。肯定可以运行。如果有其中的高手,请指教一下。代码比较精糙。。哈

dbh=freeswitch.Dbh("odbc://fwsql");

laststat="";   --最后状态 1.hasline 2.notline 有否空内线 2.5 linenot  有空线到最后被用  3.linebusy  4.lineready 有空线是否能接入

badinline="";

function waittoinc(tmpsession)

local waitTimes = 10;

while tmpsession:ready() == false and waitTimes > 0 do

waitTimes = waitTimes - 1;

end

return tmpsession:ready()

end

function playwavs(tmpsession,wavfiles)

tmpsession:sleep(50);

if tmpsession:ready() then

freeswitch.consoleLog("err", "\n starting play wav");

tmpsession:streamFile(wavfiles);

freeswitch.consoleLog("err", "\n play wav end");

end

end

function getoneinline(notline)   --获得一条空闲内线在 inlineinfo 表内

local reinline="";

dbh:query("select top 1 inline from inlineinfo where busy=0 and inline<>'"..notline.."' order by [index] asc,calltime asc"

,function(row) reinline=row.inline  end )

if reinline==nil or reinline=="" then

laststat="notline";

return "";

end

laststat="hasline";

badinline=reinline;

return reinline;

end

function zoneinline(notline,calldh,callid) --获内线后,设置为忙

local reinline;

local revv;

local ktnow=os.date("%Y-%m-%d %H:%M:%S", os.time());

reinline=getoneinline(notline);

if reinline==nil then

reinline="";

return reinline;

end

dbh:query("update inlineinfo set busy=1,outline='"..calldh.."',callid='"..callid.."',calltime='"..ktnow.."' where inline='"..reinline.."' and busy=0");

revv=dbh:affected_rows();

badinline=reinline;

if revv>0 then

return reinline;

else

laststat="linenot";

return "";

end

end

function callinline(notline,calldh,callid)

local reinline=zoneinline(notline,calldh,callid);

if reinline==nil or reinline=="" then

return "",nil;

end

badinline=reinline;

local tmpnum=reinline;

local tmpsession= freeswitch.Session("user/"..tmpnum);

if waittoinc(tmpsession) then

laststat="lineready";

return tmpnum,tmpsession;

else

dbh:query("update inlineinfo set busy=0,[index]=(select isnull(count(*),0) from inlineinfo where busy=0)+1 where inline='"..reinline.."'")

laststat="linebusy";

return "",nil;

end

end

local session1 = session;

local cid=session1:getVariable("caller_id_number");

local dest_num=session1:getVariable("destination_number");

local kt=os.date("%Y-%m-%d %H:%M:%S", os.time());

local ktms=os.time();

dbh:query("insert into new通话(电话,kt,zt)values('"..cid.."','"..kt.."','"..dest_num.."')");

--playwavs(session1,"d:\\ffg\\wel.wav");

--如果座席所在的那条通道还没准备好,则进行等待

--session1:sleep(1800);

--if session1:ready() then

-- session1:answer();

-- end

local session2;

local inline;

local timess=0;

inline,session2=callinline("",dest_num,cid);--toonecall();

if inline=="" then

session1:sleep(50);

if (laststat=='linenot' or laststat=='linebusy') and badinline~='' and session1:ready() then

freeswitch.consoleLog("err", "\n channels2 is not ready, try next. laststat:"..laststat);

inline,session2=callinline(badinline,dest_num,cid);

end

end

if inline=="" then

session1:sleep(50);

if (laststat=='linenot' or laststat=='linebusy') and badinline~='' and session1:ready() then

freeswitch.consoleLog("err", "\n channels2 is not ready, try next. laststat:"..laststat);

inline,session2=callinline(badinline,dest_num,cid);

end

end

if  inline=="" then

-- 接通客户与座席

-- session2:setVariable("destination_number",cid);

freeswitch.consoleLog("err", "\n channels2 is not ready, please check it. laststat:"..laststat);

timess=-1;

playwavs(session1,"d:\\ffg\\busy.wav");

else

session1:sleep(50);

if session1:ready() then

freeswitch.bridge(session1, session2);

else

timess=-2;

end

end

local et=os.date("%Y-%m-%d %H:%M:%S", os.time())

local etms=os.time();

if timess==0 then

timess=etms-ktms;

end

dbh:query("update inlineinfo set busy=0,[index]=(select isnull(count(*),0) from inlineinfo where busy=0)+1 where inline='"..inline.."'")

dbh:query("insert into 通话(电话,kt,zt,st)values('"..cid.."','"..kt.."','"..et.."',"..timess..")")

dbh:release()

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

最后说明,用的是 ms sql  上面是代码,下面表结构

USE [fwdb]

GO

/****** Object:  Table [dbo].[inlineinfo]    Script Date: 07/25/2017 10:32:40 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

SET ANSI_PADDING ON

GO

CREATE TABLE [dbo].[inlineinfo](

[id] [int] IDENTITY(1,1) NOT NULL,

[inline] [varchar](40) NOT NULL,

[outline] [varchar](40) NULL,

[calltime] [varchar](20) NULL,

[busy] [int] NOT NULL,

[index] [int] NULL,

[callid] [varchar](40) NOT NULL,

CONSTRAINT [PK_netinlineinfo] PRIMARY KEY CLUSTERED

(

[id] ASC

)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

) ON [PRIMARY]

GO

SET ANSI_PADDING OFF

GO

USE [fwdb]

GO

/****** Object:  Table [dbo].[通话]    Script Date: 07/25/2017 10:33:31 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

SET ANSI_PADDING ON

GO

CREATE TABLE [dbo].[通话](

[id] [int] IDENTITY(1,1) NOT NULL,

[电话] [varchar](20) NULL,

[kt] [varchar](20) NULL,

[zt] [varchar](20) NULL,

[st] [int] NULL,

CONSTRAINT [PK_通话] PRIMARY KEY CLUSTERED

(

[id] ASC

)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

) ON [PRIMARY]

GO

SET ANSI_PADDING OFF

GO

USE [fwdb]

GO

/****** Object:  Table [dbo].[new通话]    Script Date: 07/25/2017 10:33:23 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

SET ANSI_PADDING ON

GO

CREATE TABLE [dbo].[new通话](

[id] [int] IDENTITY(1,1) NOT NULL,

[电话] [varchar](20) NULL,

[kt] [varchar](20) NULL,

[zt] [varchar](20) NULL,

CONSTRAINT [PK_net通话] PRIMARY KEY CLUSTERED

(

[id] ASC

)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

) ON [PRIMARY]

GO

SET ANSI_PADDING OFF

GO

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值