Difference between revisions of "Sirit Example Client"
From RifidiWiki
| Line 1: | Line 1: | ||
| − | |||
| This class shows how to create a program to talk to the Sirit INfinity 510 reader. | This class shows how to create a program to talk to the Sirit INfinity 510 reader. | ||
| − | + | <pre> | |
| /* | /* | ||
|   *  SimpleSiritClient.java |   *  SimpleSiritClient.java | ||
| Line 33: | Line 32: | ||
| public class SimpleSiritClient { | public class SimpleSiritClient { | ||
| − | 	String ipAddress =  | + | 	String ipAddress = "192.168.11.130"; | 
| 	Socket sock; | 	Socket sock; | ||
| 	int port = 50007; | 	int port = 50007; | ||
| Line 39: | Line 38: | ||
| 	PrintWriter writer; | 	PrintWriter writer; | ||
| − | 	Future | + | 	Future<String> future; | 
| 	/** | 	/** | ||
| Line 56: | Line 55: | ||
| 		setUpNetworking(); | 		setUpNetworking(); | ||
| − | 		String command =  | + | 		String command = ""; | 
| 		// Login to reader | 		// Login to reader | ||
| − | 		command =  | + | 		command = "reader.login(login=admin,pwd=readeradmin)"; | 
| 		processCommand(command); | 		processCommand(command); | ||
| 		// let the reader scan for tags for 3 seconds | 		// let the reader scan for tags for 3 seconds | ||
| − | 		command =  | + | 		command = "tag.db.scan_tags(3000)"; | 
| 		processCommand(command); | 		processCommand(command); | ||
| Line 103: | Line 102: | ||
| 		ExecutorService pool = Executors.newFixedThreadPool(2); | 		ExecutorService pool = Executors.newFixedThreadPool(2); | ||
| 		// Callable for Commandchannel | 		// Callable for Commandchannel | ||
| − | 		Callable | + | 		Callable<String> callable = new IncomingCallable(); | 
| 		future = pool.submit(callable); | 		future = pool.submit(callable); | ||
| Line 110: | Line 109: | ||
| 		// write to log window | 		// write to log window | ||
| − | 		System.out.println( | + | 		System.out.println(">>" + command + "\r\n"); | 
| 		// receive and write response | 		// receive and write response | ||
| Line 121: | Line 120: | ||
| 	public void sendCommand(String command) { | 	public void sendCommand(String command) { | ||
| 		// send command to reader | 		// send command to reader | ||
| − | 		writer.print(command +  | + | 		writer.print(command + "\r\n"); | 
| 		writer.flush(); | 		writer.flush(); | ||
| 	} | 	} | ||
| Line 129: | Line 128: | ||
| 	 */ | 	 */ | ||
| 	public String getLastResponse() { | 	public String getLastResponse() { | ||
| − | 		String response =  | + | 		String response = ""; | 
| 		try { | 		try { | ||
| 			response = future.get(); | 			response = future.get(); | ||
| Line 138: | Line 137: | ||
| 	} | 	} | ||
| − | 	public class IncomingCallable implements Callable | + | 	public class IncomingCallable implements Callable<String> { | 
| 		PrintWriter writer; | 		PrintWriter writer; | ||
| 		String command; | 		String command; | ||
| Line 144: | Line 143: | ||
| 		public String call() { | 		public String call() { | ||
| 			// get response | 			// get response | ||
| − | 			String message =  | + | 			String message = ""; | 
| − | 			String lastResponse =  | + | 			String lastResponse = ""; | 
| 			try { | 			try { | ||
| − | 				while (lastResponse.indexOf( | + | 				while (lastResponse.indexOf("\r\n\r\n") == -1) { | 
| 					message = reader.readLine(); | 					message = reader.readLine(); | ||
| − | 					message = message.replace( | + | 					message = message.replace("\r", "").replace("\n", "") | 
| − | 							+  | + | 							+ "\r\n"; | 
| 					lastResponse = lastResponse.concat(message); | 					lastResponse = lastResponse.concat(message); | ||
| 				} | 				} | ||
| Line 161: | Line 160: | ||
| } | } | ||
| − | + | </pre> | |
Latest revision as of 19:43, 26 November 2010
This class shows how to create a program to talk to the Sirit INfinity 510 reader.
/*
 *  SimpleSiritClient.java
 *
 *  Created:	27.05.2009
 *  Project:	RiFidi SiritClient
 *  				http://www.rifidi.org
 *  				http://rifidi.sourceforge.net
 *  Copyright:	Pramari LLC and the Rifidi Project
 *  License:	Lesser GNU Public License (LGPL)
 *  				http://www.opensource.org/licenses/lgpl-license.html
 *	Author:		Stefan Fahrnbauer - stefan@pramari.com
 */
package sandbox;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
 * @author Stefan Fahrnbauer - stefan@pramari.com
 * 
 */
public class SimpleSiritClient {
	String ipAddress = "192.168.11.130";
	Socket sock;
	int port = 50007;
	BufferedReader reader;
	PrintWriter writer;
	Future<String> future;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SimpleSiritClient client = new SimpleSiritClient();
		client.go();
	}
	/**
	 * executes the application
	 */
	public void go() {
		setUpNetworking();
		String command = "";
		// Login to reader
		command = "reader.login(login=admin,pwd=readeradmin)";
		processCommand(command);
		// let the reader scan for tags for 3 seconds
		command = "tag.db.scan_tags(3000)";
		processCommand(command);
		tearDown();
	}
	/**
	 * initializes the connection
	 */
	public void setUpNetworking() {
		try {
			// connection for submitting commands
			sock = new Socket(this.ipAddress, this.port);
			reader = new BufferedReader(new InputStreamReader(sock
					.getInputStream()));
			writer = new PrintWriter(sock.getOutputStream());
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
	/**
	 * closes the connection
	 */
	public void tearDown() {
		try {
			sock.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
	/**
	 * process command, get response and write to log
	 */
	public void processCommand(String command) {
		// create callable to read response
		ExecutorService pool = Executors.newFixedThreadPool(2);
		// Callable for Commandchannel
		Callable<String> callable = new IncomingCallable();
		future = pool.submit(callable);
		// send command to reader
		sendCommand(command);
		// write to log window
		System.out.println(">>" + command + "\r\n");
		// receive and write response
		System.out.println(getLastResponse());
	}
	/**
	 * sends command to reader
	 */
	public void sendCommand(String command) {
		// send command to reader
		writer.print(command + "\r\n");
		writer.flush();
	}
	/**
	 * waits for and returns the reader's response
	 */
	public String getLastResponse() {
		String response = "";
		try {
			response = future.get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return response;
	}
	public class IncomingCallable implements Callable<String> {
		PrintWriter writer;
		String command;
		public String call() {
			// get response
			String message = "";
			String lastResponse = "";
			try {
				while (lastResponse.indexOf("\r\n\r\n") == -1) {
					message = reader.readLine();
					message = message.replace("\r", "").replace("\n", "")
							+ "\r\n";
					lastResponse = lastResponse.concat(message);
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			return lastResponse;
		}
	}
}

