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

📄 cservice.java

📁 gsm modem 发送短信 闪信 WAP PUSH开发包
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
//	jSMSEngine API.
//	An open-source API package for sending and receiving SMS via a GSM device.
//	Copyright (C) 2002-2006, Thanasis Delenikas, Athens/GREECE
//		Web Site: http://www.jsmsengine.org
//
//	jSMSEngine is distributed under the GPL license.
//
//	This program is free software; you can redistribute it and/or
//	modify it under the terms of the GNU General Public License
//	version 2 as published by the Free Software Foundation
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program; if not, write to the Free Software
//	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//

package org.jsmsengine;

import java.io.*;
import java.util.*;
import java.util.logging.*;
import java.text.*;
import java.lang.reflect.*;

/**
	This class provides all the functionality of jSMSEngine API to the developer.
	<br><br>
	The class CService provides all the interface routines to jSMSEngine. It
	is responsible for initialization of the communication with the GSM device,
	reading and sending messages, setting the phonebook.
	<br><br>
	The sequence of actions that need to be done are:
	<ul>
		<li>Call connect() to connect with the GSM device.</li>
		<li>Call sendMessage(), or readMessages() to send or receive messages
				from the device. Call deleteMessage() to delete a message from
				the device's memory.</li>
		<li>Call refreshDeviceInfo() to get updated GSM device specific information.</li>
		<li>Call disconnect() to disconnect from the GSM device.</li>
	</ul>
	<br>
*/
public class CService
{
	/**
		Internal Software Name / Version / Release Date
	*/
	public static final String _name = "jSMSEngine API";
	public static final String _version = "2.0.9";
	public static final String _reldate = "March 21, 2006";

	/**
		Some constants
	*/
	private static final int ASYNC_RECV_INTERVAL = 30 * 1000;
	private static final int KEEP_ALIVE_INTERVAL = 15 * 1000;

	/**
		Logging facilities.
	*/
	private static Logger log = Logger.getLogger("org.jsmsengine");

	/**
		Receive modes: Synchronous and Ascynchronous.
	*/
	public static final int RECEIVE_MODE_SYNC = 1;
	/**
		Receive modes: Synchronous and Ascynchronous.
	*/
	public static final int RECEIVE_MODE_ASYNC = 2;

	/**
		Default value for information that is not reported by the GSM device. 
	*/
	public static final String VALUE_NOT_REPORTED = "* N/A *";

	/**
		The SMSC number (if specifically given).
	*/
	private String smscNumber;

	/**
		The PIN number.
	*/
	private String simPin;

	/**
		Receive Mode: Synchronous or Asynchronous.
	*/
	private int receiveMode;

	/**
		AT Commands' Handler.
	*/
	private CATHandler atHandler;

	private CSerialDriver serialDriver;
	private boolean connected;
	private CPhoneBook phoneBook;
	private CDeviceInfo deviceInfo;

	private CKeepAliveThread keepAliveThread;
	private CReceiveThread receiveThread;
	private CSmsMessageListener messageHandler;

	/**
		Default constructor of the class.

		@param	port	the serial port where the GSM device is connected (e.g. "com1"). 
		@param	baud	the connection speed (i.e. 9600, 19200 etc).
		@param	gsmDeviceManufacturer	The manufacturer of your device, i.e. Nokia, Siemens, etc.
		@param	gsmDeviceModel	The model of your device, i.e. 6310i, C55, V600 etc.

		<br><br>
		Notes:
		<ul>
			<li>The manufacturer / model combination is used for accessing the correct AT handler for
					your device. If there is no specific handler developed, jSMSEngine will fall back and
					use the generic AT handler. This may or may not work, depending on the
					peculiarities of your device. If you wish to create a custom handler, please look
					at the specific section of the documentation pages.</li>
			<li>Use one of the standard values for baud. Most GSM phones work well
					at 9600 or 19200. Most dedicated GSM modems may work well up to
					115200. The connection speed is not that important to the speed
					at which jSMSEngine processes messages.</li>
		</ul>
	*/
	public CService(String port, int baud, String gsmDeviceManufacturer, String gsmDeviceModel)
	{
		smscNumber = null;
		simPin = null;

		connected = false;
		serialDriver = new CSerialDriver(port, baud, log);
		phoneBook = new CPhoneBook();
		deviceInfo = new CDeviceInfo();

		if ((System.getProperty("jsmsengine.debug") != null) && (System.getProperty("jsmsengine.debug").equalsIgnoreCase("true")))
		{
			log.setLevel(Level.FINEST);
			log.log(Level.INFO, "jSMSEngine:" + _name + " / " + _version + " / " + _reldate);
			log.log(Level.INFO, "   Using port: " + port + " @ " + baud + " baud.");
			log.log(Level.INFO, "	JRE Version: " + System.getProperty("java.version"));
			log.log(Level.INFO, "	JRE Impl Version: " + System.getProperty("java.vm.version"));
			//log.log(Level.INFO, "	Class Path: " + System.getProperty("java.class.path"));
			//log.log(Level.INFO, "	Library Path: " + System.getProperty("java.library.path"));
			log.log(Level.INFO, "	O/S: " + System.getProperty("os.name") + " / " + System.getProperty("os.arch") + " / " + System.getProperty("os.version"));
		}
		else log.setLevel(Level.SEVERE);

//
// Alex 'Kazuma' Garbagnati changes starts here...
//
        //
        // This is the base Handler.
        //  Note: this could be a private static member of the class
        String BASE_HANDLER = "org.jsmsengine.CATHandler";


        //
        // This is the array that contains the handlers to initialize and the
        //  description array (just to sinmplyfy logging)
        String[] handlerClassNames = { null, null, BASE_HANDLER };
        String[] handlerDescriptions = { null, null, "Generic" };


        //
        // First step: based on the values provided through the
        //      gsmDeviceManufacturer and gsmDeviceModel parameters, the
        //      array will be filled. The first entry will contain the
        //      handler name with manifacturer and device names, the
        //      second entry will contain the manifacturer name only and
        //      the third name (always present) contain the default class.
        //  Note: if there is no manifacturer, the model is ignored.
        StringBuffer handlerClassName = new StringBuffer(BASE_HANDLER);
        if (gsmDeviceManufacturer != null && !gsmDeviceManufacturer.equals("")) {

            handlerClassName.append("_").append(gsmDeviceManufacturer);
            handlerClassNames[1] = handlerClassName.toString();
            handlerDescriptions[1] = gsmDeviceManufacturer + " (Generic)";
            if (gsmDeviceModel != null && !gsmDeviceModel.equals("")) {
                handlerClassName.append("_").append(gsmDeviceModel);
                handlerClassNames[0] = handlerClassName.toString();
                handlerDescriptions[0] = gsmDeviceManufacturer + " " + gsmDeviceModel;
            }
        }


        //
        // Get the correct Handler class. First will try the first entry. If
        //      the Class.forName() method will throw a ClassNotFoundException
        //      then it will try the second entry. If this one will also throw
        //      the same Exception then the system will try to get the last
        //      entry. A ClassNotFoundException at this point it has to be
        //      considered a Critical Error.
        Class handlerClass = null;
        for (int i=0; i<3; i++) {
            try {
                // try to get the Class only if the entry is not null
                if (handlerClassNames[i] != null) {
                    handlerClass = Class.forName(handlerClassNames[i].toString());

                    // if we get to this line, it means that we have a Class. Then log
                    //      the handler and get out of the loop.
                    log.log(Level.INFO, "jSMSEngine: Using " + handlerDescriptions[i] + " AT handler.");
                    break;
                }
            }
            catch (ClassNotFoundException cnfE) {

                // We should consider this a critical error *only* if we get this
                //      exception on the last entry, that should be the default
                //      handler class.
                if (i == 2) {
                    // TODO: HANDLE THIS ERROR
                    log.severe("CANNOT INITIALIZE HANDLER (" + cnfE.getMessage() + ")");
                }
                // else we should try the next entry!
            }
        }


        //
        // If we are here, we have a valid class. At this point get the constructor
        //      and instantiate the new handler object.
        try {

            // extract the contructor (the one with the @CSerialDriver,Logger" signature)
            Class[] constructorParameters = { CSerialDriver.class, Logger.class };
            Constructor handlerConstructor = handlerClass.getConstructor(constructorParameters);

            // prepare the parameters and instantiate the handler class
            Object[] handlerParameters = {serialDriver, log};
            atHandler = (CATHandler)handlerConstructor.newInstance(handlerParameters);
        }
        // this should NEVER Happen. If it does happen, it means something has gone
        //  very, very... but very bad !
        catch (Exception e) {
            // TODO: HANDLE THIS ERROR(s)
            log.severe("CANNOT INITIALIZE HANDLER (" + e.getMessage() + ")");
        }

//
// Alex 'Kazuma' Garbagnati changes ends here...
//

/*
		// Original code.
		// Replaced with reflection-enabled code for selection of appropriate AT handler.
		// Which AT Handler should I use???
		if (gsmDeviceManufacturer.equalsIgnoreCase("Nokia"))
		{
			atHandler = new CATHandler_Nokia(serialDriver, log);
			log.log(Level.INFO, "jSMSEngine: Using Nokia(Generic) AT handler.");
		}
		else if (gsmDeviceManufacturer.equalsIgnoreCase("Wavecom"))
		{
			atHandler = new CATHandler_Wavecom(serialDriver, log);
			log.log(Level.INFO, "jSMSEngine: Using Wavecom(Generic) AT handler.");
		}
		else if (gsmDeviceManufacturer.equalsIgnoreCase("Siemens"))
		{
			atHandler = new CATHandler_Siemens(serialDriver, log);
			log.log(Level.INFO, "jSMSEngine: Using Siemens AT(Generic) handler.");
		}
		else if (gsmDeviceManufacturer.equalsIgnoreCase("SonyEricsson"))
		{
			atHandler = new CATHandler_SonyEricsson(serialDriver, log);
			log.log(Level.INFO, "jSMSEngine: Using SonyEricsson(Generic) AT handler.");
		}
		else
		{
			atHandler = new CATHandler(serialDriver, log);
			log.log(Level.INFO, "jSMSEngine: Using generic AT handler.");
		}
*/
		
		receiveMode = RECEIVE_MODE_SYNC;
		receiveThread = null;
	}

	/**
		Returns TRUE if the API is connected to the GSM device.

		@return  TRUE if the API is connected to the GSM device.
	*/
	public boolean getConnected() { return connected; }

	/**
		Returns a CDeviceInfo object that holds information about the GSM
		device in use.

		@return  a CDeviceInfo object.
		@see	CDeviceInfo
	*/
	public CDeviceInfo getDeviceInfo() { return deviceInfo; }

	/**
		Sets the Short Message Service Center (SMSC) number. Please use international format.
		If you don't want to set the SMSC and use the one defined in your GSM device, use an
		empty string parameter. Another way to do the same, is to pass a null parameter. Some
		phones may prefer one way or the other - please test your phone.

		@param	smscNumber	the SMSC number.
	*/
	public void setSmscNumber(String smscNumber) { this.smscNumber = smscNumber; }

	/**
		Returns the Short Message Service Center (SMSC) number you have previously
		defined with setSmscNumber().

		@return  the SMSC number.
	*/
	public String getSmscNumber() { return smscNumber; }

	/**
		Sets the SIM pin number. This is used if and when the GSM device asks for it. If you set it to
		null, then the API does not give any PIN to the device (in order to avoid locking it up), and
		returns ERR_SIM_PIN_ERROR.

		@param	simPin	the SIM pin number.
	*/

⌨️ 快捷键说明

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