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

📄 samplereaderio.java

📁 SUnRDIS网络应用平台,可以在该平台上开发RFID应用,系统.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. * */package com.mycompany.adapter.sample;import java.util.logging.*;import java.util.List;import java.util.ArrayList;import java.util.LinkedList;import java.util.Properties;import java.util.Collection;import java.util.Iterator;import java.io.EOFException;import java.io.IOException;import java.io.InputStreamReader;import java.io.BufferedReader;import java.net.URL;import java.net.HttpURLConnection;import java.net.*;import java.text.MessageFormat;import com.sun.autoid.adapter.DeviceAdapter;import com.sun.autoid.adapter.AbstractReaderAdapter;import com.sun.autoid.adapter.AbstractSocketIO;import com.sun.autoid.adapter.AbstractSocketReaderIO;import com.sun.autoid.adapter.AbstractReaderIO;import com.sun.autoid.adapter.TimeoutException;import com.sun.autoid.adapter.ConnectionResetException;import com.sun.autoid.adapter.NotSupportedException;import com.sun.autoid.adapter.OutOfRangeException;import com.sun.autoid.adapter.command.GetTagListCommand;import com.sun.autoid.adapter.Indicator;import com.sun.autoid.identity.*;import com.sun.autoid.event.IdentifierListEvent;import com.sun.autoid.event.EventConstants;import com.sun.autoid.event.EventUtil;import com.sun.autoid.pmlcore.pmlparser.*;import com.sun.autoid.pmlcore.pml.*;public class SampleReaderIO extends AbstractSocketReaderIO {        private SampleAdapter adapter;    private PmlParser pmlParser;        private boolean autoMode = false;        /**      * Creates a new instance of SampleReaderIO     * @param adapter       the SampleAdapter performing the IO     * @param hostname      the String specifying the name of the reader to contact     * @param port          the port number the reader listens on     * @param version       passed through to the ReaderRequest and ReaderResponse to identify protocol version     * @param properties    the Properties of the Reader Adapter     * @throws Exception     */    public SampleReaderIO(SampleAdapter adapter, String hostname, int port, int version, Properties properties) throws Exception {        super(adapter, hostname, port, version, properties);        this.adapter = adapter;	pmlParser = new PmlParser();    }        /**     * Performs the actual exchange of bytes with the reader     * @param oCmd           the command Object that needs to be executed     * @param getResponse   the boolean value indicating whether to wait for the response     * @throws TimeoutException if the reader did not reply within the timeout specified during the creation of the ReaderIO     * @throws ConnectionResetException if the connection to the reader has been lost     * @throws IOException for any other error condition     */    public Object tellReader(Object oCmd, boolean getResponse) throws TimeoutException, IOException, ConnectionResetException {        String data;        StringBuffer response = null;        String cmd = (String)oCmd;                logger.log(Level.FINEST, "Tell reader: {0}", cmd);        try {            send(cmd);              // Send command to reader        } catch (IOException ioe) {            String message = MessageFormat.format("Exception sending command to {0} Reader",                new Object[] { adapter.getDeviceID() });            logger.log(Level.WARNING, message, ioe);            throw ioe;        }        if (!getResponse)            return null;                response = new StringBuffer();        while (true) {			// Loop until success or error.  Ignore data            data = getLine();            if(data == null)                throw new EOFException("Unexpected: End of file from reader socket");            if (data.length() == 0)                return response.toString();			// success            response.append(data);            if(logger.isLoggable(Level.FINEST)) {                logger.log(Level.FINEST, "Command response from {0} Reader: {1}",                    new Object[] {adapter.getDeviceID(), data});            }            if (data.length() >= 5 && data.startsWith("Error")) {                String message = MessageFormat.format("Command error from {0} Reader: {1}",                    new Object[] {adapter.getDeviceID(), data});                logger.log(Level.WARNING, message);                throw new IOException(message);            }        }    }        /**     * Performs the reader specific initialization sequence     * @param properties the reader adapter Properties     * @throws TimeoutException if communication with the reader could not be established within the given timeout     * @throws IOException if communication with the reader failed to I/O problems     * @throws Exception for any other problem     */        public void initialize(Properties properties) throws IOException, TimeoutException, Exception {        tellReader("Reset", false);    }        /**     * Returns true if AutoMode is supported by this reader     */    public boolean isAutoModeSupported() {        return true;    }        /**     * Sets the reader into Automatic mode, where the reader constantly generates     * taglist results and communicates them back to the adapter.     * @param frequency                 specifies how often (in msecs) the reader shuld communicate back to the adapter     * @throws NotSupportedException    if the reader can not be set into Automatic mode     * @throws TimeoutException         if the reader did not reply within the timeout specified during the creation of     *                                  the ReaderIO     * @throws ConnectionResetException if the connection to the reader has been lost     * @throws IOException              for any other error condition     */    public void setAutoModeOn(long frequency) throws IOException, ConnectionResetException, TimeoutException, NotSupportedException {                logger.log(Level.FINEST, "setAutoModeOn(" + frequency + ")");        tellReader("Set Auto Mode " +frequency, true);        autoMode = true;    }            /**     * Turns off Automatic mode     * taglist results and communicates them back to the adapter.     * @throws TimeoutException         if the reader did not reply within the timeout specified during the creation of     *                                  the ReaderIO     * @throws ConnectionResetException if the connection to the reader has been lost     * @throws IOException              for any other error condition     */    public void setAutoModeOff() throws IOException, ConnectionResetException, TimeoutException {                logger.log(Level.FINEST, "setAutoModeOff()");        if (autoMode)            tellReader("Reset", true);        autoMode = false;    }        /**     * Provides a Collection of EMSListEvents     * @param scanLength the number of milliseconds reader should scan for tags     * @return the Collection of EMSListEvent elements     * @throws TimeoutException if the reader did not reply within the timeout specified during the creation of the ReaderIO     * @throws ConnectionResetException if the connection to the reader has been lost     * @throws IOException for any other error condition     */    public Collection getTagList(long scanLength) throws IOException, ConnectionResetException, TimeoutException {        logger.log(Level.FINE, "getTagList() invoked {0}", adapter.getDeviceID());        // issue the request        tellReader("Get Tag List", false);          // get response        return listenForTags();    }        /**     * Listens for the tag list     * @return the Collection of EMSListEvent elements     * @throws TimeoutException         if the reader did not reply within the timeout specified during the creation of     *                                  the ReaderIO     * @throws ConnectionResetException if the connection to the reader has been lost     * @throws IOException              for any other error condition     */     public Collection listenForTags() throws IOException, ConnectionResetException, TimeoutException {        StringBuffer response = new StringBuffer();        String data = null;        while (true) {			// Loop until success or error.  Ignore data            data = getLine();            if(data == null)                throw new EOFException("Unexpected: End of file from reader socket");            if (data.length() == 0)                continue;			            response.append(data);            if(logger.isLoggable(Level.FINEST)) {                logger.log(Level.FINEST, "Command response from {0} Reader: {1}",                    new Object[] {adapter.getDeviceID(), data});            }            if(data.equals("</pmlcore:Sensor>"))                break;            if (data.length() >= 5 && data.startsWith("Error")) {                String message = MessageFormat.format("Command error from {0} Reader: {1}",                    new Object[] {adapter.getDeviceID(), data});                 logger.log(Level.WARNING, message);                throw new IOException(message);            }        }         String pml = response.toString();        logger.finest(pml);        LinkedList resultList = new LinkedList();	try {            Sensor sensor = pmlParser.unmarshalPML(pml);            IdentifierListEvent event = (IdentifierListEvent)EventUtil.toEvent(pml, sensor);            numTagsRead += event.getTagList().size();            numReads++;            resultList.add(event);	}catch(Exception ex) {            String message = MessageFormat.format("Error processing getTagList(): {0}", new Object[] { adapter.getDeviceID() });            logger.log(Level.SEVERE,message, ex);	}        return resultList;    }          /**     * Programs the single tag in the reader field of action     * @param id the Identifier to assign to the tag     * @param param the Object parameter to be used and interpreted by the reader     * @return the EPC written to the tag     * @throws IOException if an error occurred     * @throws ConnectionResetException if the connection to the reader has been lost

⌨️ 快捷键说明

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