⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 at_cgact.java

📁 自己实现的基于串口通讯发送短消息的低层协议JAVA代码
💻 JAVA
字号:
package com.jzl.sirius.at.command;

import java.nio.CharBuffer;
import java.util.HashMap;
import java.util.Iterator;

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
 * @see com.jzl.sirius.at.command.ATCommandBase
 */
public class AT_CGACT extends ATCommandBase {

	private int commandType;

	private StringBuffer command = new StringBuffer();

    private HashMap<Integer, Integer> states = new HashMap<Integer, Integer>();
	private String supportedStates = "";

	/**
	 * 构造函数 提供"AT+CGACT?"的AT命令
	 */
	public AT_CGACT() {
		this(false);
	}

	/**
	 * 构造函数 提供"AT+CGACT=?"的AT命令或"AT+CGACT?"的AT命令
	 * 
	 * @param tested
	 *            当为true时提供"AT+CGACT=?"的AT命令,当为false时提供"AT+CGACT?"的AT命令。
	 */
	public AT_CGACT(boolean tested) {
        super(new StringMatch(ATConstants.CRLF + "+CGACT:"));
		commandType = tested ? COMMAND_TYPE_TEST : COMMAND_TYPE_READ;
		command.append(tested ? "AT+CGACT=?" : "AT+CGACT?");
		command.append(ATConstants.CR);
	}

	/**
	 * 构造函数 提供"AT+CGACT=int"的AT命令
	 * 
	 * @param intState
	 *            state of PDP context activation
	 */
	public AT_CGACT(int state) {
        commandType = COMMAND_TYPE_ACTION;
        command.append("AT+CGACT=");
        command.append(state);
        command.append(ATConstants.CR);
	}

	public AT_CGACT(int state, int cid1) {
        commandType = COMMAND_TYPE_ACTION;
        command.append("AT+CGACT=");
        command.append(state);
        command.append(",");
        command.append(cid1);
        command.append(ATConstants.CR);
	}

	public AT_CGACT(int state, int cid1, int cid2) {
        commandType = COMMAND_TYPE_ACTION;
        command.append("AT+CGACT=");
        command.append(state);
        command.append(",");
        command.append(cid1);
        command.append(",");
        command.append(cid2);
        command.append(ATConstants.CR);
	}

	public AT_CGACT(int state, int cid1, int cid2, int cid3) {
        commandType = COMMAND_TYPE_ACTION;
        command.append("AT+CGACT=");
        command.append(state);
        command.append(",");
        command.append(cid1);
        command.append(",");
        command.append(cid2);
        command.append(",");
        command.append(cid3);
        command.append(ATConstants.CR);
	}

	public AT_CGACT(int state, int cid1, int cid2, int cid3, int cid4) {
        commandType = COMMAND_TYPE_ACTION;
        command.append("AT+CGACT=");
        command.append(state);
        command.append(",");
        command.append(cid1);
        command.append(",");
        command.append(cid2);
        command.append(",");
        command.append(cid3);
        command.append(",");
        command.append(cid4);
        command.append(ATConstants.CR);
	}
    
    public String buildCommand() {
        return command.toString();
    }
    
    protected void decodeOkResponse(CharBuffer response) {
        String str = response.toString();
        try {
            if (commandType == COMMAND_TYPE_READ) {
                // format:<CR><LF>+CGACT: <cid>,<state><CR><LF><CR><LF>OK<CR><LF>
                decodeStates(str);
                
            } else if (commandType == COMMAND_TYPE_TEST) {
                // format:<CR><LF>+CGACT: (supported <state>s)<CR><LF><CR><LF>OK<CR><LF>
                supportedStates = StringUtils.substring(str, "+CGACT:", ATConstants.CRLF).trim();
            }
        }
        catch(Exception ex) {
            
        }
    }
    
    private void decodeStates(String str) {
        String startToken = ATConstants.CRLF + "+CGACT:";
        
        while(true) {
            int pos = str.indexOf(startToken);
            if (pos == -1)
                return;
            str = str.substring(pos + startToken.length());
            pos = str.indexOf(ATConstants.CRLF);
            if (pos == -1)
                return;
            
            String state = str.substring(0, pos);
            String[] arry = state.split(",");
            states.put(Integer.decode(arry[0].trim()), Integer.decode(arry[1].trim()));
            
            str = str.substring(pos);
        }
    }



	/**
	 * 取得设备state的值
	 * 
	 * @return 返回设备state的值
	 */
	public int getState(int cid) {
        Integer value = states.get(cid);
        if (value == null)
            return -1;
		return value.intValue();
	}

	/**
	 * 取得设备cid的值
	 * 
	 * @return 返回设备cid的值
	 */
	public int[] getCids() {
	    int[] cids = new int[states.keySet().size()];
        
        
        Iterator itr = states.keySet().iterator();
        int i = 0;
        while(itr.hasNext()) {
            cids[i++] = ((Integer)itr.next()).intValue();
        }
        
        return cids;
	}

	/**
	 * 取得设备支持的state的值
	 * 
	 * @return 返回设备支持的state的值
	 */
	public String getSupportedStates() {
		return supportedStates;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -