asterisk-java ami4 一些基本功能的例子

比如:挂机,拨号,拨外线,保留通话,示闲,示忙等等。。。。

在api中可以获得这些方法说明

/**
  * 给分机挂机
  * @param Extension 要挂机的分机号
  * @return
  * @throws TimeoutException
  * @throws IOException
  * @throws IllegalStateException
  * @throws IllegalArgumentException
  */
 public static ManagerResponse getHangup(String Extension,Integer value) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
   if(BaseUtil.getConnection()==null){
    BaseUtil.getAsteriskJava(); 
   } 
   HangupAction hangupAction=new HangupAction();
   hangupAction.setActionId(BaseController.getactionId("Hangup", Extension));
   hangupAction.setCause(value);//挂机原因 
   hangupAction.setChannel(BaseUtil.getChannel(Extension));
   ManagerResponse response =BaseUtil.getConnection().sendAction(hangupAction);
   return response; 
 }
 /**
  * 分机外呼
  * @param Extension 发起呼叫的分机号
  * @param phone 被呼叫的外线号
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 @SuppressWarnings("deprecation")
 public static ManagerResponse getWOriginate(String Extension,String phone) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
   if(BaseUtil.getConnection()==null){
    BaseUtil.getAsteriskJava();
   }
   OriginateAction origaction=new OriginateAction();
   origaction.setContext("from-internal");//路由器
   String action=BaseController.getactionId("Originate", Extension);
   origaction.setActionId(action);//呼叫action id
   origaction.setChannel("SIP/"+Extension);//呼叫连接通道,呼叫时,先呼通自己
   origaction.setExten("799"+Extension+phone+"#");//要外呼的电话799+分机号是
   origaction.setPriority(1);//优先级
   origaction.setTimeout(20000);//呼叫等待(毫秒)
   origaction.setCallerId(phone);//呼叫号码
   origaction.setAsync(true);//设置异步才会有结果返回
   ManagerResponse response =BaseUtil.getConnection().sendAction(origaction); 
   System.out.println("外呼结果"+response);
   return response;
 }
 
 /**
  * 分机呼分机
  * @param Extension 发起呼叫的分机号
  * @param phone 被呼叫的分机号
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 @SuppressWarnings("deprecation")
 public static ManagerResponse getROriginate(String Extension,String phone) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
   if(BaseUtil.getConnection()==null){
    BaseUtil.getAsteriskJava();
   }
   OriginateAction origaction=new OriginateAction();
   origaction.setContext("from-internal");//路由器
   String action=BaseController.getactionId("Originate", Extension);
   origaction.setActionId(action);//呼叫action id
   origaction.setChannel("SIP/"+Extension);//呼叫连接通道,呼叫时,先呼通自己
   origaction.setExten(phone);//要外呼的电话
   origaction.setPriority(1);//优先级设置1时,不会回拨,为什么
   origaction.setTimeout(20000);//呼叫等待(毫秒)
   origaction.setCallerId(phone);//呼叫号码
   origaction.setAsync(true);//设置异步才会有结果返回
   ManagerResponse response =BaseUtil.getConnection().sendAction(origaction);  
   return response;
 }
 
 /**
  * 插入通话
  * @param Extension 发起呼叫的分机号
  * @param phone 被插入通话的分机号
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 @SuppressWarnings("deprecation")
 public static ManagerResponse getCOriginate(String Extension,String phone) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
   if(BaseUtil.getConnection()==null){
    BaseUtil.getAsteriskJava();
   }
   OriginateAction origaction=new OriginateAction();
   origaction.setContext("spy");//路由器
   String action=BaseController.getactionId("Originate", Extension);
   origaction.setActionId(action);//呼叫action id
   origaction.setChannel("SIP/"+Extension);//呼叫连接通道,呼叫时,先呼通自己
   origaction.setExten("557"+phone);//要插入通话的分机
   origaction.setPriority(1);//优先级
   origaction.setTimeout(20000);//呼叫等待(毫秒)
   origaction.setAsync(true);//设置异步才会有结果返回
   ManagerResponse response =BaseUtil.getConnection().sendAction(origaction);  
   return response;
 }
 /**
  * 监听通话
  * @param Extension 发起监听的分机号
  * @param phone 要监听的分机号
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 @SuppressWarnings("deprecation")
 public static ManagerResponse getJOriginate(String Extension,String phone) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
   if(BaseUtil.getConnection()==null){
    BaseUtil.getAsteriskJava();
   }
   OriginateAction origaction=new OriginateAction();
   origaction.setContext("spy");//路由器
   String action=BaseController.getactionId("Originate", Extension);
   origaction.setActionId(action);//呼叫action id
   origaction.setChannel("SIP/"+Extension);//呼叫连接通道,呼叫时,先呼通自己
   origaction.setExten("556"+phone);//要插入通话的分机
   origaction.setPriority(1);//优先级
   origaction.setTimeout(20000);//呼叫等待(毫秒)
   origaction.setAsync(true);//设置异步才会有结果返回
   ManagerResponse response =BaseUtil.getConnection().sendAction(origaction);  
   return response;
 }
 /**
  * 获取分机状态
  * @param Extension 分机号
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 public static ManagerResponse getExtension(String Extension) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
  if(BaseUtil.getConnection()==null){
   BaseUtil.getAsteriskJava();
  }
  ExtensionStateAction extaction=new ExtensionStateAction(Extension,"default");
  
  String Actionid=BaseController.getactionId("Extension", Extension);
  extaction.setActionId(Actionid);
  ManagerResponse response =BaseUtil.getConnection().sendAction(extaction);
  return response;
 }
 /**
  * 获取分机通话的录音文件
  * @param Extension 需要录音的分机号
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 public static ManagerResponse getMonitor(String Extension) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
  if(BaseUtil.getConnection()==null){
   BaseUtil.getAsteriskJava();
  }
  MonitorAction MonitorAction=new MonitorAction();   
  MonitorAction.setActionId(BaseController.getactionId("Monitor", Extension));
  MonitorAction.setMix(false);//获取false为1个语音文件,true为两个
  MonitorAction.setChannel(BaseUtil.getChannel(Extension));
  MonitorAction.setFile("分机号:"+Extension+" 时间:"+ToolUtils.formatDate(new Date(), "yyyymmddhhmmss")+" 监听录音");//语音文件的名称
  //MonitorAction.setFormat("");//录音文件编码格式
  ManagerResponse response =BaseUtil.getConnection().sendAction(MonitorAction);
  return response;
 }
 /**
  * 获取所有的sip话机信息连接情况
  * @param Actionid 唯一标识
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 public static ManagerResponse getSIPpeers(String Actionid) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
  if(BaseUtil.getConnection()==null){
   BaseUtil.getAsteriskJava();
  }
  SipPeersAction sippeers=new SipPeersAction();   
  sippeers.setActionId(Actionid);
  ManagerResponse response =BaseUtil.getConnection().sendAction(sippeers);
  return response;
 }
 /**
  * 重定向通话到指定的分机,外线
  * @param outextension 进行指定的本机号
  * @param inextension 指定的分机号,外线
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 public static ManagerResponse getRedirect(String outextension,String inextension) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
  if(BaseUtil.getConnection()==null){
   BaseUtil.getAsteriskJava();
  }
  MapCacheUtil mapcache=MapCacheUtil.getInstance();
  String channel=BaseUtil.getChannel(outextension);//获取本机的通道
  RedirectAction redrec=new RedirectAction();
  redrec.setActionId(BaseController.getactionId("Redirect", inextension));
  redrec.setChannel(String.valueOf(mapcache.get(channel)));//要转移的通道
  redrec.setContext("from-internal");
  redrec.setExten(inextension);//要转入的分机
  redrec.setPriority(1);//优先级
  ManagerResponse response=BaseUtil.getConnection().sendAction(redrec);
  return response;
 }
 /**
  * 通话保留
  * @param Extension
  * @param channel 对方通道
  * @param channel2 自己通道
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 public static ManagerResponse getPark(String Extension) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
  if(BaseUtil.getConnection()==null){
   BaseUtil.getAsteriskJava();
  }
  MapCacheUtil mapcache=MapCacheUtil.getInstance();
  ParkAction park=new ParkAction();
  String channel=BaseUtil.getChannel(Extension);
  park.setActionId(BaseController.getactionId("Park", Extension));
  park.setChannel(String.valueOf(mapcache.get(channel)));//要保留的通道
  park.setChannel2(channel);//时间结束要返回的通道  
  park.setTimeout(60*60*1000);//保留通话的时间
  ManagerResponse response=BaseUtil.getConnection().sendAction(park);
  return response;
 }
 /**
  * 通话接回
  * @param Extension
  * @param channel 对方通道
  * @param channel2 自己通道
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 public static ManagerResponse getBackPark(String Extension,String parked) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
  if(BaseUtil.getConnection()==null){
   BaseUtil.getAsteriskJava();
  }
  OriginateAction origaction=new OriginateAction();
  origaction.setContext("from-internal");//路由器
  String action=BaseController.getactionId("Originate", Extension);
  origaction.setActionId(action);//呼叫action id
  origaction.setChannel("SIP/"+Extension);//呼叫连接通道,呼叫时,先呼通自己  
  origaction.setExten(parked);
  origaction.setPriority(1);//优先级设置1时,不会回拨,为什么
  origaction.setAsync(true);//设置异步才会有结果返回
  ManagerResponse response =BaseUtil.getConnection().sendAction(origaction);  
  return response;
 }
 /**
  * 桥接通话
  * @param Extension
  * @param channel 对方通道
  * @param channel2 自己通道
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 public static ManagerResponse getBridge(String Extension) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
  if(BaseUtil.getConnection()==null){
   BaseUtil.getAsteriskJava();
  }
  MapCacheUtil mapcache=MapCacheUtil.getInstance();
  BridgeAction bridge=new BridgeAction();
  String channel=BaseUtil.getChannel(Extension);
  bridge.setActionId(BaseController.getactionId("Bridge", Extension));
  bridge.setChannel1(String.valueOf(mapcache.get(channel)));
  bridge.setChannel2(channel);
  bridge.setTone(true);
  //park.setTimeout(30000);//保留通话的时间
  ManagerResponse response=BaseUtil.getConnection().sendAction(bridge);
  return response;
 }
 /**
  * 获取分机示忙示闲状态值
  * @param Extension
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 public static ManagerResponse getdatabasednd(String Extension) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
  if(BaseUtil.getConnection()==null){
   BaseUtil.getAsteriskJava();
  }
  DbGetAction db=new DbGetAction();
  db.setActionId(BaseController.getactionId("DND", Extension));
  db.setFamily("DND");
  db.setKey(Extension);
  ManagerResponse response=BaseUtil.getConnection().sendAction(db);
  return response;
 }
 /**
  * 设置分机示闲状态值
  * @param Extension
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 public static ManagerResponse deldatabasednd(String Extension) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
  if(BaseUtil.getConnection()==null){
   BaseUtil.getAsteriskJava();
  }
  DbDelAction db=new DbDelAction();
  db.setActionId(BaseController.getactionId("DND", Extension));
  db.setFamily("DND");
  db.setKey(Extension);
  ManagerResponse response=BaseUtil.getConnection().sendAction(db);
  return response;
 }
 /**
  * 设置分机示忙状态值
  * @param Extension
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 public static ManagerResponse setdatabasednd(String Extension) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
  if(BaseUtil.getConnection()==null){
   BaseUtil.getAsteriskJava();
  }
  DbPutAction db=new DbPutAction();
  db.setActionId(BaseController.getactionId("DND", Extension));
  db.setFamily("DND");
  db.setKey(Extension);
  db.setVal("yes");
  ManagerResponse response=BaseUtil.getConnection().sendAction(db);
  return response;
 }

时间: 2024-08-05 23:46:01

asterisk-java ami4 一些基本功能的例子的相关文章

Java 守护线程(Daemon) 例子

Java  守护线程(Daemon) 示例 本文由 TonySpark 翻译自 Javarevisited.转载请参见文章末尾的要求. 当我们在Java中创建一个线程,缺省状态下它是一个User线程,如果该线程运行,JVM不会终结该程序.当一个线被标记为守护线程,JVM不会等待其结束,只要所有用户(User)线程都结束,JVM将终结程序及相关联的守护线程. Java中可以用 Thread.setDaemon(true) 来创建一个守护线程.咱们看一个Java中有关守护线程的例子. 1 publi

Java 基本数据类型 sizeof 功能

Java 基本数据类型 sizeof 功能 来源 https://blog.csdn.net/ithomer/article/details/7310008 Java基本数据类型int     32bitshort   16bitlong    64bitbyte    8bitchar    16bitfloat   32bitdouble  64bitboolean 1bit,This data type represents one bit of information, but its

JAVA UI 拖拽功能

java GUI拖拽功能是很实用也相对高级一些的功能. 有一小部分的GUI控件支持他们有dragEnabled属性.这些JComponent包括:javax.swing.JColorChooserjavax.swing.JFileChooserjavax.swing.JListjavax.swing.JTablejavax.swing.JTreejavax.swing.text.JTextComponent 大部分的控件不支持没有这个属性,尤其是常用的jpanel和jframe. 一种简单的方法

java操作xml的一个小例子

最近两天公司事比较多,这两天自己主要跟xml打交道,今天更一下用java操作xml的一个小例子. 原来自己操作xml一直用这个包:xstream-1.4.2.jar.然后用注解的方式,很方便,自己只要定义好bean的层次结构就可以了,第三方包会自动生成和解析xml. 但是今天发现,这个包有两个问题: 一个是比较复杂(我是菜鸟,求别喷,我真的觉得他挺复杂的..).比如说你要想处理既有属性又有值的xml就比较麻烦.(@XStreamConverter注解这种方法报这个错:com.thoughtwor

Java UDP实现聊天功能代码

我以前经常写的是基于TCP的网络编程,由于TCP建立连接鼻血要经过三次握手连接,服务器端需要阻塞式等待客户端的连接.而UDP则是可以直接向目的地址的目的端口上发送数据包,由于它只负责发送出去就好,不管对方是否正确接受到与否,所以当网络性能不好时它容易出现丢包的问题.(注意:UDP是基于数据报为单位进行传输的,而TCP是一种基于流进行传输的) 但是UDP很好的模拟了我们呢平时聊天的方式,可以很好的实现连续多次发送和接受,也就是简单的QQ聊天的功能. 现在来简要介绍Java中有关UDP编程相关的类:

Java基础:多功能小窗口,swing,io,net综合应用

概念原理的理解,不代表能熟练应用. 如果将多个知识点关联并应用起来,这能加快我们对知识的掌握. 作为一个初学者,最快速的上手方式应该就是学以致用了. 下面做了一个小窗口,主要是为了综合应用java的一些基础功能,界面稍显随意. 界面: 具体功能以及代码: 1 /*具体实现功能: 2 * 1.根据地址栏的路径打开文本文件,显示在文本区域. 3 * 2.根据地址栏的网址获取网页源代码,显示在文本区域. 4 * 3.通过打开按钮打开本地文本文件,显示在文本区域. 5 * 4.通过保存按钮,把文本区域内

java中生成流水号的一个例子(使用关系型数据库)

在实际的开发中,可能会有根据一定的规则生成流水号的需求(比如根据根据公司编码和日期生成4位流水号)我们可以把公司和日期联合起来作为一个业务编码,把这个业务编码和序列的值存储到数据库中,每次需要生成流水号的时候根据公司和日期联合起来生成的这个业务编码去数据库里边去查,若有记录的话就把记录的话就把对应的序列值返回,然后序列的值加一,若根据对应的业务编码查询不到相应的记录就把流水号对应的最小的值返回,然后在数据库中插入相应的记录 这种场景使用类似BerkeleyDB这种K,Value对数据库来实现是最

java 多线程——quartz 定时调度的例子

java 多线程 目录: Java 多线程——基础知识 Java 多线程 —— synchronized关键字 java 多线程——一个定时调度的例子 java 多线程——quartz 定时调度的例子 java 多线程—— 线程等待与唤醒 概述 第1部分 配置 第2部分 代码示例 第1部分 配置 有关quartz的api文档地址:Quartz Enterprise Job Scheduler 1.8.6 API 主要接口目录: 重点看下Job,Scheduler,Trigger,JobDetai

Java的四种引用源代码例子

Java的四种引用源代码例子 不解释,直接上代码,千言万语顶不住一行代码. package com.apkkids.javalanguage; import java.lang.ref.PhantomReference; import java.lang.ref.Reference; import java.lang.ref.ReferenceQueue; import java.lang.ref.SoftReference; import java.lang.ref.WeakReference