📄 atd.java
字号:
package com.jzl.sirius.at.command;
import java.nio.ByteBuffer;
import com.jzl.sirius.at.ATCommand;
import com.jzl.sirius.at.ATConstants;
import com.jzl.sirius.at.util.StringMatch;
import com.jzl.sirius.at.util.StringUtils;
/**
*
* @author xujian
* @version 2.0
* @see com.jzl.sirius.at.ATCommand
*/
public class ATD extends ATCommand {
private ATDListener listener = null;
private StringMatch okMatch = new StringMatch(ATConstants.RESPONSE_OK);
private StringMatch noCarrierMatch = new StringMatch(ATConstants.RESPONSE_NO_CARRIER);
private StringMatch busyMatch = new StringMatch(ATConstants.RESPONSE_BUSY);
private StringMatch noAnswerMatch = new StringMatch(ATConstants.RESPONSE_NO_ANSWER);
private StringMatch errorMatch = new StringMatch(ATConstants.RESPONSE_ERROR);
private StringMatch cmeErrorMatch = new StringMatch(ATConstants.RESPONSE_CME_ERROR);
private StringMatch cmsErrorMatch = new StringMatch(ATConstants.RESPONSE_CMS_ERROR);
private StringMatch windMatch = new StringMatch(ATConstants.CRLF + "+WIND:");
private StringBuffer wind = new StringBuffer();
private boolean windFlag = false;
private StringMatch crlfMatch = new StringMatch(ATConstants.CRLF);
private boolean cmeFlag = false;
private boolean cmsFlag = false;
private StringBuffer cmeError = new StringBuffer();
private StringBuffer cmsError = new StringBuffer();
private StringBuffer command = new StringBuffer();
public ATD(String nb) {
command.append("ATD");
command.append(nb);
command.append(";");
command.append(ATConstants.CR);
}
public String buildCommand() {
return command.toString();
}
protected void decodeCmeError(StringBuffer sb) {
int errorCode = parseErrorCode(sb.toString(), ATConstants.RESPONSE_CME_ERROR, ATConstants.CRLF);
setCmeErrorCode(errorCode);
}
protected void decodeCmsError(StringBuffer sb) {
int errorCode = parseErrorCode(sb.toString(), ATConstants.RESPONSE_CMS_ERROR, ATConstants.CRLF);
setCmsErrorCode(errorCode);
}
private int parseErrorCode(String str, String startPattern, String endPattern) {
String error = StringUtils.substring(str, startPattern, endPattern);
try {
return Integer.parseInt(error.trim());
}
catch(Exception ex) {
}
return -1;
}
public boolean readResponse(ByteBuffer buffer) {
while(buffer.hasRemaining()) {
char ch = (char)buffer.get();
if (okMatch.match(ch)) {
setResultCode(ATConstants.OK);
return true;
}
if (errorMatch.match(ch)) {
setResultCode(ATConstants.ERROR);
return true;
}
if (noCarrierMatch.match(ch)) {
setResultCode(ATConstants.NO_CARRIER);
return true;
}
if (busyMatch.match(ch)) {
setResultCode(ATConstants.BUSY);
return true;
}
if (noAnswerMatch.match(ch)) {
setResultCode(ATConstants.NO_ANSWER);
return true;
}
boolean flag = windFlag;
if (flag) {
wind.append(ch);
if (crlfMatch.match(ch)) {
dispatchEvent();
windFlag = false;
wind = new StringBuffer();
}
}
else if (windMatch.match(ch)){
windFlag = true;
wind.append(ATConstants.CRLF + "+WIND:");
}
if (cmeFlag) {
cmeError.append(ch);
if (crlfMatch.match(ch)) {
decodeCmeError(cmeError);
return true;
}
}
else if (cmeErrorMatch.match(ch)) {
cmeFlag = true;
cmeError.append(ATConstants.RESPONSE_CME_ERROR);
}
if (cmsFlag) {
cmsError.append(ch);
if (crlfMatch.match(ch)) {
decodeCmsError(cmsError);
return true;
}
}
else if (cmsErrorMatch.match(ch)) {
cmsFlag = true;
cmsError.append(ATConstants.RESPONSE_CMS_ERROR);
}
}
return false;
}
private void dispatchEvent() {
try {
String str = StringUtils.substring(wind.toString(), "+WIND:", ATConstants.CRLF).trim();
String[] arry = str.split(",");
ATDEvent event = null;
if (arry.length == 1)
event = new ATDEvent(Integer.parseInt(arry[0].trim()));
else if (arry.length > 1)
event = new ATDEvent(Integer.parseInt(arry[0].trim()), Integer.parseInt(arry[1].trim()));
if (event != null && listener != null)
listener.event(event);
}
catch(Exception ex) {
}
}
public ATDListener getListener() {
return listener;
}
public void setListener(ATDListener listener) {
this.listener = listener;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -