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

📄 athandler.java

📁 发送短信 接收短信 多种接口com/net/modem 开发库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// SMSLib for Java v3
// A Java API library for sending and receiving SMS via a GSM modem
// or other supported gateways.
// Web Site: http://www.smslib.org
//
// Copyright (C) 2002-2009, Thanasis Delenikas, Athens/GREECE.
// SMSLib is distributed under the terms of the Apache License version 2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.smslib.modem.athandler;

import java.io.IOException;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.smslib.GatewayException;
import org.smslib.TimeoutException;
import org.smslib.AGateway.AsyncEvents;
import org.smslib.AGateway.Protocols;
import org.smslib.InboundMessage.MessageClasses;
import org.smslib.modem.AModemDriver;
import org.smslib.modem.CNMIDetector;
import org.smslib.modem.ModemGateway;

/**
 * Generic modem AT handler.
 */
public class ATHandler extends AATHandler
{
	protected AModemDriver modemDriver;

	protected CNMIDetector cnmiDetector;

	public AModemDriver getModemDriver()
	{
		return this.modemDriver;
	}

	public ATHandler(ModemGateway myGateway)
	{
		super(myGateway);
		this.modemDriver = myGateway.getModemDriver();
		this.cnmiDetector = null;
		this.terminators = new String[13];
		this.terminators[0] = "OK\\s";
		this.terminators[1] = "\\s*[\\p{ASCII}]*\\s+OK\\s";
		this.terminators[2] = "(ERROR|NO CARRIER|NO DIALTONE)\\s";
		this.terminators[3] = "ERROR:\\s*\\d+\\s";
		this.terminators[4] = "\\+CM[ES]\\s+ERROR:\\s*\\d+\\s";
		this.terminators[5] = "\\+CPIN:\\s*READY\\s";
		this.terminators[6] = "\\+CPIN:\\s*SIM\\s*BUSY\\s";
		this.terminators[7] = "\\+CPIN:\\s*SIM\\s*PIN\\s";
		this.terminators[8] = "\\+CPIN:\\s*SIM\\s*PIN2\\s";
		this.terminators[9] = "\\+CMTI:\\s*\\p{Punct}[\\p{ASCII}]+\\p{Punct}\\p{Punct}\\s*\\d+\\s";
		this.terminators[10] = "\\+CDSI:\\s*\\p{Punct}[\\p{ASCII}]+\\p{Punct}\\p{Punct}\\s*\\d+\\s";
		this.terminators[11] = "RING\\s";
		this.terminators[12] = "\\+CLIP:\\s*\\p{Punct}[\\p{ASCII}]*\\p{Punct}\\p{Punct}\\s*\\d+[\\p{ASCII}]*\\s";
		this.unsolicitedResponses = new String[4];
		this.unsolicitedResponses[0] = "+CMTI";
		this.unsolicitedResponses[1] = "+CDSI";
		this.unsolicitedResponses[2] = "RING";
		this.unsolicitedResponses[3] = "+CLIP";
	}

	@Override
	public void sync() throws IOException, InterruptedException
	{
		getModemDriver().write("ATZ\r");
		Thread.sleep(getGateway().getService().getSettings().AT_WAIT);
	}

	@Override
	public void reset() throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		getModemDriver().write("\u001b");
		Thread.sleep(getGateway().getService().getSettings().AT_WAIT);
		getModemDriver().write("+++");
		Thread.sleep(getGateway().getService().getSettings().AT_WAIT);
		getModemDriver().write("ATZ");
		Thread.sleep(getGateway().getService().getSettings().AT_WAIT);
		getModemDriver().clearBuffer();
	}

	@Override
	public void echoOff() throws IOException, InterruptedException
	{
		getModemDriver().write("ATE0\r");
		Thread.sleep(getGateway().getService().getSettings().AT_WAIT);
		getModemDriver().clearBuffer();
	}

	@Override
	public void init() throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		getModemDriver().write("AT+CLIP=1\r");
		getModemDriver().getResponse();
		getModemDriver().write("AT+COPS=0\r");
		getModemDriver().getResponse();
	}

	@Override
	public void done() throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		// No default behavior.
	}

	@Override
	public boolean isAlive() throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		getModemDriver().write("AT\r");
		getModemDriver().getResponse();
		return (getModemDriver().isOk());
	}

	@Override
	public String getSimStatus() throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		getModemDriver().write("AT+CPIN?\r");
		return (getModemDriver().getResponse());
	}

	@Override
	public boolean enterPin(String pin) throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		getModemDriver().write("AT+CPIN=\"_1_\"\r".replaceAll("_1_", pin));
		getModemDriver().getResponse();
		return (getModemDriver().isOk());
	}

	@Override
	public boolean setVerboseErrors() throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		getModemDriver().write("AT+CMEE=1\r");
		getModemDriver().getResponse();
		return (getModemDriver().isOk());
	}

	@Override
	public boolean setPduProtocol() throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		getModemDriver().write("AT+CMGF=0\r");
		getModemDriver().getResponse();
		return (getModemDriver().isOk());
	}

	@Override
	public boolean setTextProtocol() throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		getModemDriver().write("AT+CMGF=1\r");
		getModemDriver().getResponse();
		if (getModemDriver().isOk())
		{
			getModemDriver().write("AT+CSCS=\"8859-1\"\r");
			getModemDriver().getResponse();
			return (getModemDriver().isOk());
		}
		return false;
	}

	@Override
	public boolean setIndications() throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		int RETRIES = 3;
		int count = 0;
		while (count < RETRIES)
		{
			getModemDriver().write("AT+CNMI=?\r");
			try
			{
				this.cnmiDetector = new CNMIDetector(getModemDriver().getResponse(), getGateway());
				getModemDriver().write(this.cnmiDetector.getATCommand());
				getModemDriver().getResponse();
				return (getModemDriver().isOk());
			}
			catch (Exception e)
			{
				count++;
				getGateway().getService().getLogger().logWarn("Retrying the detection of CNMI, modem busy?", null, getGateway().getGatewayId());
				Thread.sleep(getGateway().getService().getSettings().AT_WAIT_CNMI);
			}
		}
		getGateway().getService().getLogger().logWarn("CNMI detection failed, proceeding with defaults.", null, getGateway().getGatewayId());
		return false;
	}

	@Override
	public CNMIDetector getIndications()
	{
		return this.cnmiDetector;
	}

	@Override
	public String getManufacturer() throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		getModemDriver().write("AT+CGMI\r");
		return (getModemDriver().getResponse());
	}

	@Override
	public String getModel() throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		getModemDriver().write("AT+CGMM\r");
		return (getModemDriver().getResponse());
	}

	@Override
	public String getSerialNo() throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		getModemDriver().write("AT+CGSN\r");
		return (getModemDriver().getResponse());
	}

	@Override
	public String getImsi() throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		getModemDriver().write("AT+CIMI\r");
		return (getModemDriver().getResponse());
	}

	@Override
	public String getSwVersion() throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		getModemDriver().write("AT+CGMR\r");
		return (getModemDriver().getResponse());
	}

	@Override
	public String getBatteryLevel() throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		getModemDriver().write("AT+CBC\r");
		return (getModemDriver().getResponse());
	}

	@Override
	public String getSignalLevel() throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		getModemDriver().write("AT+CSQ\r");
		return (getModemDriver().getResponse());
	}

	@Override
	public boolean switchStorageLocation(String mem) throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		if (mem.equalsIgnoreCase("--")) return true;
		getModemDriver().write("AT+CPMS=\"" + mem + "\"\r");
		getModemDriver().getResponse();
		return (getModemDriver().isOk());
	}

	@Override

⌨️ 快捷键说明

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