RfidAction.java 4.41 KB
package org.streamparty.remoterfid;

import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RfidAction {

    private SerialPort opened;
    private InputStream istream;
    private OutputStream ostream;

    private static final Logger logger = LoggerFactory.getLogger(RfidAction.class);

    private static RfidAction thisElement = null;

    public static RfidAction getInstance() {
	return thisElement;
    }

    private RfidAction(CommPortIdentifier portId) {
	try {
	    logger.debug("Opening port " + portId.getName());
	    opened = (SerialPort) portId.open("Stream cashier", 100);
	    opened.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
	    istream = opened.getInputStream();
	    ostream = opened.getOutputStream();
	} catch (PortInUseException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();

	} catch (UnsupportedCommOperationException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
    }

    public String listenPort() {
	// Tyhjennetn mahdollinen bufferi
	logger.info("Tyhjennetn bufferi");
	try {
	    while (istream.available() != 0) {
		readChar();
	    }
	} catch (IOException e) {

	}
	logger.info("Starting to read");
	String tag = "";
	// logger.debug((int)'\n');
	try {
	    logger.info("Starting read loop");

	    JButton close = new JButton("Close");
	    close.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
		    System.exit(0);
		}
	    });

	    JDialog readingDiag = new JDialog();
	    JPanel readingLabel = new JPanel(new GridLayout(1, 1));
	    readingLabel.add(new JLabel("Insert tag"));
	    readingDiag.add(readingLabel);
	    readingDiag.add(close);	
	    // readingDiag.setAlwaysOnTop(true);
	    readingDiag.setSize(200, 200);
	    readingDiag.setVisible(true);

	    // JButton okLabelButton = new JButton("Ok");
	    /*
	     * okLabelButton.addActionListener(new ActionListener(){ public void
	     * actionPerformed(ActionEvent arg0) {
	     * ((JDialog)((JPanel)((JButton)arg0
	     * .getSource()).getParent()).getParent()).dispose(); }});
	     */

	    while (true) {
		if (opened.isCTS()) {
		    ostream.write('U');
		    logger.info("Wrote U to reader");

		    Thread.sleep(300);

		    if (istream.available() < 1) {
			logger.info("istream size < 1");
			continue;
		    }
		    String rresponse = Integer.toHexString(istream.read());
		    logger.info("rresponse: " + rresponse);
		    if (rresponse.equals("86") || rresponse.toUpperCase().equals("A6")) {
			logger.info("Found valid code");
			List<String> hexVals = new ArrayList<String>();
			Collections.reverse(hexVals);
			while (istream.available() > 0) {
			    String val = Integer.toHexString(istream.read());
			    if (val.length() < 2) {
				val = "0" + val;
			    }
			    hexVals.add(val.toUpperCase());

			}
			hexVals.add("00");
			Collections.reverse(hexVals);
			Iterator<String> iter = hexVals.iterator();

			tag = "";

			while (iter.hasNext()) {
			    tag += iter.next();
			}
			readingDiag.dispose();
			break;

		    }
		} else {

		    // logger.info("CTS not open. waiting...");
		    // Thread.sleep(50);
		}
	    }
	} catch (IOException e) {
	    logger.warn("Tag reading error: ", e);
	} catch (InterruptedException e) {
	    logger.info("Tag reading interrupted ", e);
	}
	logger.debug("Read tag " + tag);
	return tag.trim();

    }

    private int readChar() {
	int merkki = -1;
	try {
	    if (istream.available() > 0) {
		merkki = istream.read();
	    }

	} catch (IOException e) {
	    logger.debug("Char reading error", e);
	}

	return merkki;
    }

    public static RfidAction init(CommPortIdentifier portId) {
	logger.info("Initializing rfid reader for port: " + portId.getName());
	thisElement = new RfidAction(portId);
	return thisElement;
    }

}