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)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
You can see a list of possible alien commands by sending the 'help' command to the alien reader. However, not all of these commands are implemented in rifidi. See this [[Sirit_INfinity510#Supported Commands | List of supported commands]].
+
This class shows how to create a program to talk to the Sirit INfinity 510 reader.
  
 
<pre>
 
<pre>
 
/*
 
/*
  *  SiritClientRAPID.java
+
  *  SimpleSiritClient.java
 
  *
 
  *
  *  Created: 07.05.2009
+
  *  Created: 27.05.2009
 
  *  Project: RiFidi SiritClient
 
  *  Project: RiFidi SiritClient
 
  *  http://www.rifidi.org
 
  *  http://www.rifidi.org
Line 12: Line 12:
 
  *  License: Lesser GNU Public License (LGPL)
 
  *  License: Lesser GNU Public License (LGPL)
 
  *  http://www.opensource.org/licenses/lgpl-license.html
 
  *  http://www.opensource.org/licenses/lgpl-license.html
  * Author: Stefan Fahrnbauer - stefan@pramari.com
+
  * Author: Stefan Fahrnbauer - stefan@pramari.com
 
  */
 
  */
 
package sandbox;
 
package sandbox;
 
/**
 
* @author Stefan Fahrnbauer - stefan@pramari.com
 
*
 
*/
 
 
import com.sirit.data.DataManager;
 
import com.sirit.driver.IEventListener;
 
import com.sirit.mapping.EventInfo;
 
import com.sirit.mapping.InfoManager;
 
import com.sirit.mapping.ReaderManager;
 
import com.sirit.mapping.SetupManager;
 
import com.sirit.mapping.TagManager;
 
import com.sirit.mapping.TagResponse;
 
import com.sirit.mapping.TagResponseList;
 
  
 
import java.io.BufferedReader;
 
import java.io.BufferedReader;
 +
import java.io.IOException;
 
import java.io.InputStreamReader;
 
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
 
 
import java.io.PrintWriter;
 
import java.io.PrintWriter;
 
import java.net.Socket;
 
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 {
  
public class SiritClientRAPID implements IEventListener {
+
String ipAddress = "192.168.11.130";
 +
Socket sock;
 +
int port = 50007;
 +
BufferedReader reader;
 +
PrintWriter writer;
  
/*
+
Future<String> future;
* IP Address of the reader
+
*/
+
private String ipAddress = "192.168.11.130";
+
  
 
/**
 
/**
* Constructor
+
* @param args
 
*/
 
*/
public SiritClientRAPID() {
+
public static void main(String[] args) {
 +
SimpleSiritClient client = new SimpleSiritClient();
 +
client.go();
 
}
 
}
  
 
/**
 
/**
* Constructor - with IP Address
+
* executes the application
*
+
* @param ipAddr
+
*            IP Address for the reader
+
 
*/
 
*/
public SiritClientRAPID(String ipAddress) {
+
public void go() {
super();
+
this.ipAddress = ipAddress;
+
}
+
  
/**
+
setUpNetworking();
* Executes the application
+
*/
+
public void run() {
+
try {
+
long startTime = System.currentTimeMillis();
+
  
// Open a connection to the reader
+
String command = "";
DataManager dataManager = new DataManager(
+
DataManager.ConnectionTypes.SOCKET, ipAddress, 0);
+
dataManager.open();
+
System.out.println("Connection Opened");
+
  
// Get the reader's name
+
// Login to reader
InfoManager infoManager = new InfoManager(dataManager);
+
command = "reader.login(login=admin,pwd=readeradmin)";
String v = infoManager.getName();
+
processCommand(command);
System.out.println("Name: " + v);
+
infoManager = null;
+
  
// Login as administrator
+
// let the reader scan for tags for 3 seconds
ReaderManager readerManager = new ReaderManager(dataManager);
+
command = "tag.db.scan_tags(3000)";
if (!readerManager.login("admin", "readeradmin"))
+
processCommand(command);
throw new Exception("Login attempt failed: "
+
+ readerManager.getLastErrorMessage());
+
v = readerManager.whoAmI();
+
System.out.println("Login: " + v);
+
  
// Open an event channel and get it's ID
+
tearDown();
String id = dataManager.getEventChannel(this);
+
}
System.out.println("Event Channel ID: " + id);
+
  
// Register for event.tag.report
+
/**
if (!readerManager.eventsRegister(id, "event.tag.report"))
+
* initializes the connection
throw new Exception("Failure to register for event: "
+
*/
+ readerManager.getLastErrorMessage());
+
public void setUpNetworking() {
System.out.println("Registered for event.tag.report");
+
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();
 +
}
 +
}
  
// Set operating mode to active
+
/**
SetupManager setupManager = new SetupManager(dataManager);
+
* closes the connection
setupManager
+
*/
.setOperatingMode(SetupManager.OPERATING_MODE_TYPES.ACTIVE);
+
public void tearDown() {
System.out.println("Operating Mode: Active for 5 seconds");
+
try {
 +
sock.close();
 +
} catch (IOException ex) {
 +
ex.printStackTrace();
 +
}
 +
}
  
// Sleep while handling tag events
+
/**
Thread.sleep(5000);
+
* process command, get response and write to log
 +
*/
 +
public void processCommand(String command) {
  
// Unregister for event.tag.report
+
// create callable to read response
if (!readerManager.eventsUnregister(id, "event.tag.report"))
+
ExecutorService pool = Executors.newFixedThreadPool(2);
throw new Exception("Failure to unregister for event: "
+
// Callable for Commandchannel
+ readerManager.getLastErrorMessage());
+
Callable<String> callable = new IncomingCallable();
System.out.println("Unregistered for event.tag.report");
+
future = pool.submit(callable);
  
// Set operating mode to standby
+
// send command to reader
setupManager
+
sendCommand(command);
.setOperatingMode(SetupManager.OPERATING_MODE_TYPES.STANDBY);
+
System.out.println("Operating Mode: Standby");
+
  
// List the TagDB
+
// write to log window
System.out.println("Detected Tags:");
+
System.out.println(">>" + command + "\r\n");
TagManager tagManager = new TagManager(dataManager);
+
TagResponseList tagList = tagManager.dbGet("", "");
+
  
TagResponse tagResponse;
+
// receive and write response
for (int tagResponseID = 0; tagResponseID < tagList.getCount(); tagResponseID++) {
+
System.out.println(getLastResponse());
tagResponse = tagList.getTagResponse(tagResponseID);
+
System.out.println("TagID: " + tagResponse.getTagId());
+
System.out.println("\tAntenna: " + tagResponse.getAntenna());
+
System.out.println("\tCount: " + tagResponse.getRepeat());
+
System.out.println("\t1st Time: " + tagResponse.getFirst());
+
System.out.println("\tLast Time: " + tagResponse.getLast());
+
 
+
}
+
 
+
// Close the connection
+
setupManager = null;
+
readerManager = null;
+
dataManager.close();
+
System.out.println("Connection Closed");
+
 
+
// Output the time to execute application
+
long endTime = System.currentTimeMillis();
+
long t = endTime - startTime - 500;
+
System.out.println("Estimated Time: " + t + "(ms)");
+
} catch (Exception e) {
+
System.out.println("Error: " + e.getMessage());
+
}
+
 
}
 
}
  
 
 
/**
 
/**
* Main method for this demo application
+
* sends command to reader
*
+
* @param args
+
*            Command line arguments first argument -IP Address of the
+
*            reader (optional)
+
 
*/
 
*/
public static void main(String[] args) {
+
public void sendCommand(String command) {
// Create SiritClient object
+
// send command to reader
SiritClientRAPID scApp;
+
writer.print(command + "\r\n");
if (args.length > 0)
+
writer.flush();
scApp = new SiritClientRAPID(args[0]);
+
else
+
scApp = new SiritClientRAPID();
+
 
+
// Execute the application
+
scApp.run();
+
System.out.println("Exiting");
+
System.exit(0);
+
 
+
 
}
 
}
  
 
/**
 
/**
* Event Handler
+
* waits for and returns the reader's response
 
*/
 
*/
public void EventFound(Object sender, EventInfo eventInfo) {
+
public String getLastResponse() {
String tagID = eventInfo
+
String response = "";
.getParameter(EventInfo.EVENT_TAG_ARRIVE_PARAMS.TAG_ID);
+
try {
System.out.println("Tag ID: " + tagID);
+
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;
 +
}
 
}
 
}
  
 
}
 
}
 
</pre>
 
</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