Difference between revisions of "Sirit Example Client"

From RifidiWiki

Jump to: navigation, search
m (Reverted edits by Amesycyxa (Talk); changed back to last version by Stefan)
 
Line 1: Line 1:
=[http://ekipebu.co.cc UNDER COSTRUCTION, PLEASE SEE THIS POST IN RESERVE COPY]=
 
 
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>
+
<pre>
 
/*
 
/*
 
  *  SimpleSiritClient.java
 
  *  SimpleSiritClient.java
Line 33: Line 32:
 
public class SimpleSiritClient {
 
public class SimpleSiritClient {
  
String ipAddress = &quot;192.168.11.130&quot;;
+
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&lt;String&gt; future;
+
Future<String> future;
  
 
/**
 
/**
Line 56: Line 55:
 
setUpNetworking();
 
setUpNetworking();
  
String command = &quot;&quot;;
+
String command = "";
  
 
// Login to reader
 
// Login to reader
command = &quot;reader.login(login=admin,pwd=readeradmin)&quot;;
+
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 = &quot;tag.db.scan_tags(3000)&quot;;
+
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&lt;String&gt; callable = new IncomingCallable();
+
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(&quot;&gt;&gt;&quot; + command + &quot;\r\n&quot;);
+
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 + &quot;\r\n&quot;);
+
writer.print(command + "\r\n");
 
writer.flush();
 
writer.flush();
 
}
 
}
Line 129: Line 128:
 
*/
 
*/
 
public String getLastResponse() {
 
public String getLastResponse() {
String response = &quot;&quot;;
+
String response = "";
 
try {
 
try {
 
response = future.get();
 
response = future.get();
Line 138: Line 137:
 
}
 
}
  
public class IncomingCallable implements Callable&lt;String&gt; {
+
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 = &quot;&quot;;
+
String message = "";
String lastResponse = &quot;&quot;;
+
String lastResponse = "";
 
try {
 
try {
while (lastResponse.indexOf(&quot;\r\n\r\n&quot;) == -1) {
+
while (lastResponse.indexOf("\r\n\r\n") == -1) {
 
message = reader.readLine();
 
message = reader.readLine();
message = message.replace(&quot;\r&quot;, &quot;&quot;).replace(&quot;\n&quot;, &quot;&quot;)
+
message = message.replace("\r", "").replace("\n", "")
+ &quot;\r\n&quot;;
+
+ "\r\n";
 
lastResponse = lastResponse.concat(message);
 
lastResponse = lastResponse.concat(message);
 
}
 
}
Line 161: Line 160:
  
 
}
 
}
&lt;/pre&gt;
+
</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;
		}
	}

}
Personal tools