目录
Asterisk AMI接口源码下载地址:http://download.csdn.net/detail/manageruser/9912291
Asterisk AMI在线手册下载地址:http://download.csdn.net/download/manageruser/9912321
package AMIapiBase;
import java.io.IOException;
import org.asteriskjava.manager.TimeoutException;
import org.asteriskjava.manager.action.CommandAction;
import org.asteriskjava.manager.action.ConfbridgeKickAction;
import org.asteriskjava.manager.action.ConfbridgeListAction;
import org.asteriskjava.manager.action.ConfbridgeMuteAction;
import org.asteriskjava.manager.action.ConfbridgeUnmuteAction;
import org.asteriskjava.manager.action.DbDelAction;
import org.asteriskjava.manager.action.DbGetAction;
import org.asteriskjava.manager.action.DbPutAction;
import org.asteriskjava.manager.action.ExtensionStateAction;
import org.asteriskjava.manager.action.GetConfigAction;
import org.asteriskjava.manager.action.GetVarAction;
import org.asteriskjava.manager.action.HangupAction;
import org.asteriskjava.manager.action.MonitorAction;
import org.asteriskjava.manager.action.OriginateAction;
import org.asteriskjava.manager.action.UpdateConfigAction;
import org.asteriskjava.manager.response.ManagerResponse;
public class AMIApp {
public AMIApp(){}
public ManagerResponse AMIAPP_CallExtension(String channel,String exten)
throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
//实例化命令响应对象
ManagerResponse response = new ManagerResponse();
//实例化Originate命令对象
OriginateAction originate = new OriginateAction();
originate.setActionId(ConnectAMI.getActionID("call")); // 设置actionid
originate.setChannel("SIP/"+channel); // 设置主叫分机
originate.setContext("internal"); // 设置路由器
originate.setExten(exten); // 设置被叫分机
originate.setPriority(new Integer(1)); // 设置进入dialplan优先级
originate.setTimeout(new Long(30000)); // 设置呼叫超时
originate.setCallerId(exten);
originate.setAsync(true); // 设置异步才会有结果返回
response = ConnectAMI.getConnect().sendAction(originate);
return response;
}
// 挂断某个通道
public ManagerResponse AMIAPP_Hangup(String channel, Integer cause) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
ManagerResponse response = new ManagerResponse();
HangupAction hangup = new HangupAction();
hangup.setActionId(ConnectAMI.getActionID("hangup"));
hangup.setChannel("SIP/"+channel);
hangup.setCause(cause);
response = ConnectAMI.getConnect().sendAction(hangup);
return response;
}
//获取指定分机的状态
public ManagerResponse AMIAPP_getExtensionState(String context, String extension)
throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
ManagerResponse response = new ManagerResponse();
ExtensionStateAction extensionstate = new ExtensionStateAction();
extensionstate.setActionId(ConnectAMI.getActionID("extensionstate"));
extensionstate.setContext(context);
extensionstate.setExten(extension);
response = ConnectAMI.getConnect().sendAction(extensionstate);
return response;
}
// 创建会议室,并添加用户
public ManagerResponse AMIAPP_CreateRoomAddUser(String conference, String exten)
throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
ManagerResponse response = new ManagerResponse();
OriginateAction originate = new OriginateAction();
originate.setActionId(ConnectAMI.getActionID("conference"));
originate.setChannel("SIP/"+ exten);
originate.setApplication("ConfBridge");
originate.setData(conference);
originate.setTimeout(new Long(30000));
originate.setCallerId("ConferenceRoom_"+conference);
originate.setAsync(true);
response = ConnectAMI.getConnect().sendAction(originate);
return response;
}
// 查询会议室成员
public ManagerResponse AMIAPP_getRoomUserList(String conference)
throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
ManagerResponse response = new ManagerResponse();
ConfbridgeListAction list = new ConfbridgeListAction();
list.setActionId(ConnectAMI.getActionID("list"));
list.setConference(conference);
response = ConnectAMI.getConnect().sendAction(list);
return response;
}
// 从会议室中踢出一个成员
public ManagerResponse AMIAPP_ConfBridgeKick(String conference, String channel)
throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
ManagerResponse response = new ManagerResponse();
ConfbridgeKickAction kick = new ConfbridgeKickAction();
kick.setActionId(ConnectAMI.getActionID("kick"));
kick.setConference(conference);
kick.setChannel(channel);
response = ConnectAMI.getConnect().sendAction(kick);
return response;
}
//静音设置
public ManagerResponse AMIAPP_ConfBridgeMute(String conference, String channel)
throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
ManagerResponse response = new ManagerResponse();
ConfbridgeMuteAction mute = new ConfbridgeMuteAction();
mute.setActionId(ConnectAMI.getActionID("mute"));
mute.setConference(conference);
mute.setChannel(channel);
response = ConnectAMI.getConnect().sendAction(mute);
return response;
}
// 取消静音
public ManagerResponse AMIAPP_ConfbridgeUnmute(String conference, String channel)
throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
ManagerResponse response = new ManagerResponse();
ConfbridgeUnmuteAction unmute = new ConfbridgeUnmuteAction();
unmute.setActionId(ConnectAMI.getActionID("unmute"));
unmute.setConference(conference);
unmute.setChannel(channel);
response = ConnectAMI.getConnect().sendAction(unmute);
return response;
}
//发送一个CLI命令
public ManagerResponse AMIAPP_CLICommand(String command) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
ManagerResponse response = new ManagerResponse();
CommandAction commandaction = new CommandAction();
commandaction.setActionId(ConnectAMI.getActionID("commandaction"));
commandaction.setCommand(command);
response = ConnectAMI.getConnect().sendAction(commandaction);
return response;
}
// 获取Asterisk某个配置文件
public ManagerResponse AMIAPP_getConfig(String filename) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
ManagerResponse response = new ManagerResponse();
GetConfigAction getconfig = new GetConfigAction();
getconfig.setActionId(ConnectAMI.getActionID("getconfig"));
getconfig.setFilename(filename);
response = ConnectAMI.getConnect().sendAction(getconfig);
return response;
}
// 获取某个通道变量
public ManagerResponse AMIAPP_getVal(String channel, String var) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
ManagerResponse response = new ManagerResponse();
GetVarAction getvar = new GetVarAction();
getvar.setActionId(ConnectAMI.getActionID("getvar"));
getvar.setChannel(channel);
getvar.setVariable(var);
response = ConnectAMI.getConnect().sendAction(getvar);
return response;
}
// 监控某个通道,注意,pathname是linux系统文件路径,默认为.war后缀
public ManagerResponse AMIAPP_MonitorChannel(String channel, String pathname) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
ManagerResponse response = new ManagerResponse();
MonitorAction monitor = new MonitorAction();
monitor.setActionId(ConnectAMI.getActionID("monitor"));
monitor.setChannel(channel);
monitor.setFile(pathname);
response = ConnectAMI.getConnect().sendAction(monitor);
return response;
}
// 获取family组的key值
public ManagerResponse AMIAPP_DBget(String family, String key) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
ManagerResponse response = new ManagerResponse();
DbGetAction dbget = new DbGetAction();
dbget.setActionId(ConnectAMI.getActionID("dbget"));
dbget.setFamily(family);
dbget.setKey(key);
response = ConnectAMI.getConnect().sendAction(dbget);
return response ;
}
// 写入数据库值
public ManagerResponse AMIAPP_Dbput(String family, String key, String value) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
ManagerResponse response = new ManagerResponse();
DbPutAction dbput = new DbPutAction();
dbput.setActionId(ConnectAMI.getActionID("dbput"));
dbput.setFamily(family);
dbput.setKey(key);
dbput.setVal(value);
response = ConnectAMI.getConnect().sendAction(dbput);
return response;
}
public ManagerResponse AMIAPP_DbDel(String family, String key) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
ManagerResponse response = new ManagerResponse();
DbDelAction dbdel = new DbDelAction();
dbdel.setActionId(ConnectAMI.getActionID("dbdel"));
dbdel.setFamily(family);
dbdel.setKey(key);
response = ConnectAMI.getConnect().sendAction(dbdel);
return response;
}
//更新配置文件,但是一次更新一条信息
public ManagerResponse AMIAPP_UpdateConfig(String filename,String action,Boolean isreload,
String category,String variable,String value)
throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
ManagerResponse response = new ManagerResponse();
UpdateConfigAction updateconfig = new UpdateConfigAction();
updateconfig.setActionId(ConnectAMI.getActionID("updateconfig"));
updateconfig.setReload(isreload);
updateconfig.setSrcFilename(filename);
updateconfig.setDstFilename(filename);
updateconfig.addCommand(getUpdateConfigAction(action),category, variable, value, null);
response = ConnectAMI.getConnect().sendAction(updateconfig);
return response;
}
//更新配置文件,一次新建多条信息
public ManagerResponse AMIAPP_UpdateSipConf(String filename,String category,String secret,
String context, String fullname, String videoformat)
throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
ManagerResponse response;
response = this.AMIAPP_UpdateConfig(filename, "delcat", false,category, null, null);
response = this.AMIAPP_UpdateConfig(filename, "newcat", false,category, null, null);
response = this.AMIAPP_UpdateConfig(filename, "append", false,category, "secret", secret);
response = this.AMIAPP_UpdateConfig(filename, "append", false,category, "context", context);
response = this.AMIAPP_UpdateConfig(filename, "append", false,category, "fullname", fullname);
response = this.AMIAPP_UpdateConfig(filename, "append", false,category, "port", "5060");
response = this.AMIAPP_UpdateConfig(filename, "append", false,category, "type", "friend");
response = this.AMIAPP_UpdateConfig(filename, "append", false,category, "host", "dynamic");
response = this.AMIAPP_UpdateConfig(filename, "append", false,category, "qualify", "yes");
response = this.AMIAPP_UpdateConfig(filename, "append", false,category, "nat", "no");
response = this.AMIAPP_UpdateConfig(filename, "append", false,category, "canreinvite", "yes");
response = this.AMIAPP_UpdateConfig(filename, "append", true,category, "allow", videoformat);
return response;
}
//获取updateconfig使用类别
public static String getUpdateConfigAction(String action){
String retAction = null;
//新建类别
if("newcat".equalsIgnoreCase(action)){
retAction = UpdateConfigAction.ACTION_NEWCAT;
}
// 重命名类别
else if("renamecat".equalsIgnoreCase(action)){
retAction = UpdateConfigAction.ACTION_RENAMECAT;
}
// 删除类别
else if("delcat".equalsIgnoreCase(action)){
retAction = UpdateConfigAction.ACTION_DELCAT;
}
// 更新
else if("update".equalsIgnoreCase(action)){
retAction = UpdateConfigAction.ACTION_UPDATE;
}
// 删除
else if("delete".equalsIgnoreCase(action)){
retAction = UpdateConfigAction.ACTION_DELETE;
}
// 追加
else if("append".equalsIgnoreCase(action)){
retAction = UpdateConfigAction.ACTION_APPEND;
}else{
retAction = null;
}
return retAction;
}
public static String getExtensionState(int status){
String statename="";
switch(status){
case -1: statename = "扩展找不到"; break;
case 0: statename = "空闲"; break;
case 1: statename = "在使用"; break;
case 2: statename = "忙"; break;
case 4: statename = "不可用"; break;
case 8: statename = "振铃"; break;
case 16: statename= "正等待"; break;
}
return statename;
}
//获得挂机原因
public static String getHangup(int status){
String statename="";
switch(status){
case 1: statename="未分配";break;
case 2: statename="无到达路由";break;
case 3: statename="无路由目的地";break;
case 6: statename="无效通道";break;
case 7: statename="正在通话中";break;
case 16: statename="正常挂机";break;
case 17: statename="用户忙";break;
case 18: statename="无应答";break;
case 19: statename="无人接听";break;
case 21: statename="拒绝接听";break;
case 22: statename="号码已更改";break;
case 27: statename="目的次序不对";break;
case 28: statename="无效的数字格式";break;
case 29: statename="设备拒绝";break;
case 30: statename="查询响应";break;
case 31: statename="正常未指定";break;
case 34: statename="正常的呼叫拥塞";break;
case 38: statename="网络状态差";break;
case 41: statename="正常的临时故障";break;
case 42: statename="交换机拥塞";break;
case 43: statename="信息过时";break;
case 44: statename="请求通道无效";break;
case 45: statename="被抢占";break;
case 50: statename="无呼叫保持或呼叫等待";break;
case 52: statename="禁止呼出";break;
case 54: statename="禁止呼入";break;
case 57: statename="负载过重导致失效";break;
case 58: statename="负载过多导致不可达";break;
case 65: statename="负载过多导致为实现";break;
case 66: statename="通道未实现";break;
case 69: statename="设备未实现";break;
case 81: statename="无效的呼叫证明";break;
case 88: statename="不相容的目的地";break;
case 95: statename="无效的不明信息";break;
case 96: statename="IE浏览器关闭";break;
case 97: statename="不识别的信息类型";break;
case 98: statename="错误信息";break;
case 99: statename="无IE浏览器";break;
case 100: statename="无效的IE信息";break;
case 101: statename="呼叫状态错误";break;
case 102: statename="计时到达";break;
case 103: statename="强制性IE浏览器长度误差";break;
case 111: statename="协议错误";break;
case 127: statename="互通";break;
case 0: statename="管理人员挂机";break;
}
return statename;
}
// 从通道中获取座机号
public static String getHangupExten(String channel){
return channel.substring(channel.indexOf('/')+1, channel.length());
}
// 从会议室成员中获取座机号
public static String getConferenceUser(String exten){
return exten.substring(exten.indexOf('/')+1, exten.indexOf('-'));
}
}