/**
*
* Asterisk通道工具类
*
*
* @author sunflower
*
* 时间:2010-7-26 下午01:11:41
*
* Email:zhangxuehuaemail # gmail 点 com
*
*/
public class ChanUtil {
private ChanUtil() {
}
public static final String CHAN_LOCAL = "LOCAL";
public static final String CHAN_DAHDI = "DAHDI";
public static final String CHAN_ZAP = "ZAP";
public static final String CHAN_SIP = "SIP";
public static final String CHAN_IAX2 = "IAX2";
private static final Map<String, Integer> CHANNEL_TYPES = new HashMap<String, Integer>(
5);
static {
CHANNEL_TYPES.put(CHAN_LOCAL, 0);// 0
CHANNEL_TYPES.put(CHAN_DAHDI, 1);// 1
CHANNEL_TYPES.put(CHAN_ZAP, 1 << 1);// 2
CHANNEL_TYPES.put(CHAN_SIP, 1 << 2);// 4
CHANNEL_TYPES.put(CHAN_IAX2, 1 <<3);// 8
}
/**
* 判断通道类型是否被支持
*
* @param channelType
* 需要鉴定的通道类型
* @return 是true,否false
*/
public static final boolean contansChanType(String channelType) {
return CHANNEL_TYPES.containsKey(StringUtils.upperCase(channelType));
}
/**
* 是否是本地通道
*
* @param channel
* 需要鉴定的通道
* @return 是true,否false
*/
public static final boolean isLocalChan(String channel) {
return StringUtils.startsWithIgnoreCase(channel, CHAN_LOCAL);
}
/**
* 是否是dahdi通道
*
* @param channel
* 需要鉴定的通道
* @return 是true,否false
*/
public static final boolean isDahdiChan(String channel) {
return StringUtils.startsWithIgnoreCase(channel, CHAN_DAHDI);
}
/**
* 是否是zap通道
*
* @param channel
* 需要鉴定的通道
* @return 是true,否false
*/
public static final boolean isZapChan(String channel) {
return StringUtils.startsWithIgnoreCase(channel, CHAN_ZAP);
}
/**
* 是否是sip通道
*
* @param channel
* 需要鉴定的通道
* @return 是true,否false
*/
public static final boolean isSipChan(String channel) {
return StringUtils.startsWithIgnoreCase(channel, CHAN_SIP);
}
/**
* 是否是IAX2通道
*
* @param channel
* 需要鉴定的通道
* @return 是true,否false
*/
public static final boolean isIax2Chan(String channel) {
return StringUtils.startsWithIgnoreCase(channel, CHAN_IAX2);
}
/**
* 窄化通道,获得简单通道字符串 例如:
*<ul>
* <li>DAHDI/2-1处理后DAHDI/2</li>
* <li>SIP/102-d2dg3232ds处理后SIP/102</li>
* <li>Local/FMPR-100@from-internal-7a11,1处理后Local/FMPR-100</li>
* </ul>
*
* @param channel
* @return
*/
public static String narrowChan(String realChannel) {
if (StringUtils.isEmpty(realChannel)) {
return "";
}
if (StringUtils.contains(realChannel, "@")) { // 处理local通道
return StringUtils.substringBefore(realChannel, "@");
} else if (StringUtils.contains(realChannel, "-")) {
return StringUtils.substringBefore(realChannel, "-");
} else {
return realChannel;
}
}
/**
* 判断通道是否是FMPR通道
* <p>
* 该通道在Fellow
* me通道出现,Elastix1.3++,Local/FMPR-100@from-internal-7a11,1中100为实际被叫号码
* 而Local/FMGL-101@from-internal-7a11,1中101是100的fellow me
*
* @param channel
* 需要鉴定的通道
* @return true or false
*/
public static boolean isFMPRChan(String channel) {
return StringUtils.containsIgnoreCase(channel, "Local/FMPR");
}
/**
* 判断通道是否是FMGL通道
* <p>
* 该通道在Fellow
* me通道出现,Elastix1.3++,Local/FMPR-100@from-internal-7a11,1中100为实际被叫号码
* 而Local/FMGL-101@from-internal-7a11,1中101是100的fellow me
*
* @param channel
* 需要鉴定的通道
* @return true or false
*/
public static boolean isFMGLChan(String channel) {
return StringUtils.containsIgnoreCase(channel, "Local/FMGL");
}
/**
* 解析实际通道获取号码
* <p>
* 支持的通道样式
* <ul>
* <li>Local/100@from-internal-68d2,2</li>
* <li>Local/FMPR-100@from-internal-7a11,1</li>
* <li>SIP/101-b7604d00</li>
* </ul>
*
*
* @param channel
* 实际通道
* @return 支持的通道返回号码字符串,否返回UNKNOWN
*/
public static String getExtenFromChannel(String realChannel) {
if (StringUtils.isEmpty(realChannel)) {
return "";
}
// 解析内部通道
if (StringUtils.contains(realChannel, "@")) {
if (isFMGLChan(realChannel) || isFMPRChan(realChannel)) {
return StringUtils.substringBetween(realChannel, "-", "@");
}
// Local/100@from-internal-7a11,1
else {
return StringUtils.substringBetween(realChannel, "/", "@");
}
}
// 解析sip通道 例如SIP/100-0a208990
else if (isSipChan(realChannel)) {
return StringUtils.substringBetween(realChannel, "/", "-");
}
// 不支持的通道格式返回未知
else {
return "UNKNOWN";
}
}
public static class AstState {
/* from include/asterisk/channel.h */
/**
* Channel is down and available.
*/
public static final int AST_STATE_DOWN = 0;
/**
* Channel is down, but reserved.
*/
public static final int AST_STATE_RSRVD = 1;
/**
* Channel is off hook.
*/
public static final int AST_STATE_OFFHOOK = 2;
/**
* Digits (or equivalent) have been dialed.
*/
public static final int AST_STATE_DIALING = 3;
/**
* Line is ringing.
*/
public static final int AST_STATE_RING = 4;
/**
* Remote end is ringing.
*/
public static final int AST_STATE_RINGING = 5;
/**
* Line is up.
*/
public static final int AST_STATE_UP = 6;
/**
* Line is busy.
*/
public static final int AST_STATE_BUSY = 7;
/**
* Digits (or equivalent) have been dialed while offhook.
*/
public static final int AST_STATE_DIALING_OFFHOOK = 8;
}
}