📄 atcommandbase.java
字号:
package com.jzl.sirius.at.command;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
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 abstract class ATCommandBase extends ATCommand {
private StringMatch okMatch = new StringMatch(ATConstants.RESPONSE_OK);
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 crlfMatch = new StringMatch(ATConstants.CRLF);
private StringMatch startMatch;
private boolean cmeFlag = false;
private boolean cmsFlag = false;
private StringBuffer cmeError = new StringBuffer();
private StringBuffer cmsError = new StringBuffer();
private CharBuffer response = CharBuffer.allocate(1024);
private boolean started = false;
public ATCommandBase() {
this(null);
}
public ATCommandBase(StringMatch startMatch) {
this.startMatch = startMatch;
started = (startMatch == null);
}
protected void decodeOkResponse(CharBuffer response) {
}
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 (started) {
response.append(ch);
}
else if (startMatch != null && startMatch.match(ch)) {
response.put(startMatch.getPattern());
started = true;
}
if (okMatch.match(ch)) {
setResultCode(ATConstants.OK);
response.flip();
decodeOkResponse(response);
return true;
}
if (errorMatch.match(ch)) {
setResultCode(ATConstants.ERROR);
return true;
}
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;
}
protected StringMatch getStartMatch() {
return startMatch;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -