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

📄 modemgateway.java

📁 发送短信 接收短信 多种接口com/net/modem 开发库
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
// 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;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.ajwcc.pduUtils.gsm3040.Pdu;
import org.ajwcc.pduUtils.gsm3040.PduParser;
import org.ajwcc.pduUtils.gsm3040.PduUtils;
import org.ajwcc.pduUtils.gsm3040.SmsDeliveryPdu;
import org.ajwcc.pduUtils.gsm3040.SmsStatusReportPdu;
import org.smslib.AGateway;
import org.smslib.Contact;
import org.smslib.GatewayException;
import org.smslib.InboundBinaryMessage;
import org.smslib.InboundEncryptedMessage;
import org.smslib.InboundMessage;
import org.smslib.OutboundMessage;
import org.smslib.Phonebook;
import org.smslib.StatusReportMessage;
import org.smslib.TimeoutException;
import org.smslib.UnknownMessage;
import org.smslib.InboundMessage.MessageClasses;
import org.smslib.OutboundMessage.MessageStatuses;
import org.smslib.modem.athandler.AATHandler;

/**
 * Class representing GSM modems or phones. Extends AGateway with modem specific
 * operations.
 */
public class ModemGateway extends AGateway
{
	/**
	 * Class representing different types of GSM modems / phones.
	 */
	public enum ModemTypes
	{
		/**
		 * Serially connected modem. These modems are connected via a serial
		 * port, either physical or emulated (i.e. USB, IrDA, etc).
		 */
		SERIAL,
		/**
		 * IP connected modem.
		 */
		IP
	}

	public enum IPProtocols
	{
		TEXT, BINARY
	}

	private AModemDriver driver;

	private AATHandler atHandler;

	private String modemDevice;

	private int modemParms;

	private IPProtocols ipProtocol;

	private String manufacturer;

	private String model;

	private String simPin, simPin2;

	private String customInitString;

	private String smscNumber;

	private int outMpRefNo;

	private List<List<InboundMessage>> mpMsgList;

	public ModemGateway(ModemTypes myType, String id, String myModemDevice, int myModemParms, String myManufacturer, String myModel)
	{
		super(id);
		init(myType, myModemDevice, myModemParms, myManufacturer, myModel, null);
	}

	public ModemGateway(String id, String myModemDevice, int myModemParms, String myManufacturer, String myModel, AModemDriver myDriver)
	{
		super(id);
		init(null, myModemDevice, myModemParms, myManufacturer, myModel, myDriver);
	}

	private void init(ModemTypes myType, String myModemDevice, int myModemParms, String myManufacturer, String myModel, AModemDriver myDriver)
	{
		setModemDevice(myModemDevice);
		setModemParms(myModemParms);
		setIpProtocol(IPProtocols.TEXT);
		this.manufacturer = myManufacturer;
		this.model = myModel;
		setAttributes(AGateway.GatewayAttributes.SEND | AGateway.GatewayAttributes.RECEIVE | AGateway.GatewayAttributes.BIGMESSAGES | AGateway.GatewayAttributes.WAPSI | AGateway.GatewayAttributes.PORTADDRESSING | AGateway.GatewayAttributes.FLASHSMS | AGateway.GatewayAttributes.DELIVERYREPORTS);
		if (myDriver != null) setDriver(myDriver);
		else
		{
			if (myType == ModemTypes.SERIAL) setDriver(new SerialModemDriver(this, getModemDevice() + ":" + getModemParms()));
			else setDriver(new IPModemDriver(this, getModemDevice() + ":" + getModemParms()));
		}
		setAtHandler(AATHandler.load(this, this.manufacturer, this.model));
		setSimPin("");
		setSimPin2("");
		setSmscNumber("");
		setCustomInitString("");
		this.outMpRefNo = new Random().nextInt();
		if (this.outMpRefNo < 0) this.outMpRefNo *= -1;
		this.outMpRefNo %= 65536;
		this.mpMsgList = new ArrayList<List<InboundMessage>>();
	}

	public void setIpProtocol(IPProtocols myIpProtocol)
	{
		this.ipProtocol = myIpProtocol;
	}

	public IPProtocols getIpProtocol()
	{
		return this.ipProtocol;
	}

	@Override
	public void startGateway() throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		getService().getLogger().logInfo("Starting gateway, using " + getATHandler().getDescription() + " AT Handler.", null, getGatewayId());
		getDriver().connect();
		super.startGateway();
		getService().getLogger().logInfo("Gateway started.", null, getGatewayId());
	}

	@Override
	public void stopGateway() throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		getService().getLogger().logInfo("Stopping gateway...", null, getGatewayId());
		getATHandler().done();
		super.stopGateway();
		getDriver().disconnect();
		getService().getLogger().logInfo("Gateway stopped.", null, getGatewayId());
	}

	@Override
	public void readMessages(Collection<InboundMessage> msgList, MessageClasses msgClass) throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		if (getStatus() != GatewayStatuses.STARTED) return;
		synchronized (getDriver().getSYNCCommander())
		{
			if (getProtocol() == Protocols.PDU) readMessagesPDU(msgList, msgClass, 0);
			else if (getProtocol() == Protocols.TEXT) readMessagesTEXT(msgList, msgClass, 0);
		}
	}

	@Override
	public InboundMessage readMessage(String memLoc, int memIndex) throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		Collection<InboundMessage> msgList;
		if (getStatus() != GatewayStatuses.STARTED) return null;
		synchronized (getDriver().getSYNCCommander())
		{
			msgList = new ArrayList<InboundMessage>();
			readMessages(msgList, MessageClasses.ALL);
			for (InboundMessage msg : msgList)
				if ((msg.getMemIndex() == memIndex) && (msg.getMemLocation().equals(memLoc))) return msg;
			return null;
		}
	}

	@Override
	public boolean sendMessage(OutboundMessage msg) throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		if (getStatus() != GatewayStatuses.STARTED) return false;
		synchronized (getDriver().getSYNCCommander())
		{
			if (getProtocol() == Protocols.PDU) return sendMessagePDU(msg);
			else if (getProtocol() == Protocols.TEXT) return sendMessageTEXT(msg);
			else return false;
		}
	}

	@Override
	public boolean deleteMessage(InboundMessage msg) throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		if (getStatus() != GatewayStatuses.STARTED) return false;
		synchronized (getDriver().getSYNCCommander())
		{
			if (msg.getMemIndex() >= 0) return deleteMessage(msg.getMemIndex(), msg.getMemLocation());
			else if ((msg.getMemIndex() == -1) && (msg.getMpMemIndex().length() != 0))
			{
				StringTokenizer tokens = new StringTokenizer(msg.getMpMemIndex(), ",");
				while (tokens.hasMoreTokens())
					deleteMessage(Integer.parseInt(tokens.nextToken()), msg.getMemLocation());
			}
			return true;
		}
	}

	@Override
	public int readPhonebook(Phonebook phonebook) throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		int count = 0;
		String locations, location, entries, entry;
		locations = getATHandler().readPhonebookLocations();
		if (locations.length() > 0)
		{
			StringTokenizer tokens = new StringTokenizer(locations, ",");
			while (tokens.hasMoreTokens())
			{
				location = tokens.nextToken().replaceAll("\"", "");
				entries = getATHandler().readPhonebook(location);
				if ((entries == null) || (entries.trim().length() == 0)) continue;
				BufferedReader reader = new BufferedReader(new StringReader(entries));
				entry = reader.readLine().trim();
				while (!entry.equalsIgnoreCase("OK"))
				{
					entry = entry.replaceAll("\\s*\\+CPBR:\\s*", "");
					entry = entry.replaceAll("\"\"", "\" \"");
					entry = entry.replaceAll("\"", "");
					StringTokenizer tokens4 = new StringTokenizer(entry, ",");
					String index = tokens4.nextToken();
					index = (index == null ? "" : index.trim());
					String phone = tokens4.nextToken();
					phone = (phone == null ? "" : phone.trim());
					// TODO: Parse number type.
					String type = tokens4.nextToken();
					type = (type == null ? "" : type.trim());
					// TODO: *end*
					String name = tokens4.nextToken();
					name = (name == null ? "" : name.trim());
					phonebook.getContacts().add(new Contact(name, phone, location, Integer.parseInt(index)));
					count++;
					entry = reader.readLine().trim();
				}
				reader.close();
			}
		}
		return count;
	}

	private boolean deleteMessage(int memIndex, String memLocation) throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		return getATHandler().deleteMessage(memIndex, memLocation);
	}

	private boolean sendMessageTEXT(OutboundMessage msg) throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		int refNo;
		boolean ok = false;
		refNo = getATHandler().sendMessage(0, "", msg.getRecipient(), msg.getText());
		if (refNo >= 0)
		{
			msg.setGatewayId(getGatewayId());
			msg.setRefNo("" + refNo);
			msg.setDispatchDate(new Date());
			msg.setMessageStatus(MessageStatuses.SENT);
			incOutboundMessageCount();
			ok = true;
		}
		else
		{
			msg.setRefNo(null);
			msg.setDispatchDate(null);
			msg.setMessageStatus(MessageStatuses.FAILED);
		}
		return ok;
	}

	private void readMessagesTEXT(Collection<InboundMessage> msgList, MessageClasses msgClass, int myLimit) throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		int i, j, memIndex;
		int limit = (myLimit < 0 ? 0 : myLimit);
		String response, line, msgText, originator, dateStr, refNo;
		BufferedReader reader;
		StringTokenizer tokens;
		InboundMessage msg;
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		for (int ml = 0; ml < (getATHandler().getStorageLocations().length() / 2); ml++)
		{
			if (getATHandler().switchStorageLocation(getATHandler().getStorageLocations().substring((ml * 2), (ml * 2) + 2)))
			{
				response = getATHandler().listMessages(msgClass);
				response = response.replaceAll("\\s+OK\\s+", "\nOK");
				reader = new BufferedReader(new StringReader(response));
				for (;;)
				{
					line = reader.readLine();
					if (line == null) break;
					line = line.trim();
					if (line.length() > 0) break;
				}
				while (true)
				{
					if (line == null) break;
					if (line.length() <= 0 || line.equalsIgnoreCase("OK")) break;
					i = line.indexOf(':');
					j = line.indexOf(',');
					memIndex = Integer.parseInt(line.substring(i + 1, j).trim());
					tokens = new StringTokenizer(line, ",");
					tokens.nextToken();
					tokens.nextToken();
					if (Character.isDigit(tokens.nextToken().trim().charAt(0)))
					{
						line = line.replaceAll(",,", ", ,");
						tokens = new StringTokenizer(line, ",");
						tokens.nextToken();
						tokens.nextToken();
						tokens.nextToken();
						refNo = tokens.nextToken();
						tokens.nextToken();
						dateStr = tokens.nextToken().replaceAll("\"", "");
						cal1.set(Calendar.YEAR, 2000 + Integer.parseInt(dateStr.substring(0, 2)));
						cal1.set(Calendar.MONTH, Integer.parseInt(dateStr.substring(3, 5)) - 1);
						cal1.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dateStr.substring(6, 8)));
						dateStr = tokens.nextToken().replaceAll("\"", "");
						cal1.set(Calendar.HOUR_OF_DAY, Integer.parseInt(dateStr.substring(0, 2)));
						cal1.set(Calendar.MINUTE, Integer.parseInt(dateStr.substring(3, 5)));
						cal1.set(Calendar.SECOND, Integer.parseInt(dateStr.substring(6, 8)));
						dateStr = tokens.nextToken().replaceAll("\"", "");
						cal2.set(Calendar.YEAR, 2000 + Integer.parseInt(dateStr.substring(0, 2)));
						cal2.set(Calendar.MONTH, Integer.parseInt(dateStr.substring(3, 5)) - 1);
						cal2.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dateStr.substring(6, 8)));
						dateStr = tokens.nextToken().replaceAll("\"", "");
						cal2.set(Calendar.HOUR_OF_DAY, Integer.parseInt(dateStr.substring(0, 2)));
						cal2.set(Calendar.MINUTE, Integer.parseInt(dateStr.substring(3, 5)));
						cal2.set(Calendar.SECOND, Integer.parseInt(dateStr.substring(6, 8)));

⌨️ 快捷键说明

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