Difference between revisions of "Sirit Example Client RAPID"

From RifidiWiki

Jump to: navigation, search
m (Reverted edits by Amesycyxa (Talk); changed back to last version by Stefan)
 
Line 1: Line 1:
=[http://ynejynezex.co.cc Under Construction! Please Visit Reserve Page. Page Will Be Available Shortly]=
 
 
This class shows how to create a program to talk to the Sirit INfinity 510 reader with the use of Sirit's RAPID API.
 
This class shows how to create a program to talk to the Sirit INfinity 510 reader with the use of Sirit's RAPID API.
  
<pre>
+
<pre>
 
/*
 
/*
 
  *  SiritClientRAPID.java
 
  *  SiritClientRAPID.java
Line 47: Line 46:
 
* IP Address of the reader
 
* IP Address of the reader
 
*/
 
*/
private String ipAddress = &quot;192.168.11.130&quot;;
+
private String ipAddress = "192.168.11.130";
  
 
/**
 
/**
Line 77: Line 76:
 
DataManager.ConnectionTypes.SOCKET, ipAddress, 0);
 
DataManager.ConnectionTypes.SOCKET, ipAddress, 0);
 
dataManager.open();
 
dataManager.open();
System.out.println(&quot;Connection Opened&quot;);
+
System.out.println("Connection Opened");
  
 
// Get the reader's name
 
// Get the reader's name
 
InfoManager infoManager = new InfoManager(dataManager);
 
InfoManager infoManager = new InfoManager(dataManager);
 
String v = infoManager.getName();
 
String v = infoManager.getName();
System.out.println(&quot;Name: &quot; + v);
+
System.out.println("Name: " + v);
 
infoManager = null;
 
infoManager = null;
  
 
// Login as administrator
 
// Login as administrator
 
ReaderManager readerManager = new ReaderManager(dataManager);
 
ReaderManager readerManager = new ReaderManager(dataManager);
if (!readerManager.login(&quot;admin&quot;, &quot;readeradmin&quot;))
+
if (!readerManager.login("admin", "readeradmin"))
throw new Exception(&quot;Login attempt failed: &quot;
+
throw new Exception("Login attempt failed: "
 
+ readerManager.getLastErrorMessage());
 
+ readerManager.getLastErrorMessage());
 
v = readerManager.whoAmI();
 
v = readerManager.whoAmI();
System.out.println(&quot;Login: &quot; + v);
+
System.out.println("Login: " + v);
  
 
// Open an event channel and get it's ID
 
// Open an event channel and get it's ID
 
String id = dataManager.getEventChannel(this);
 
String id = dataManager.getEventChannel(this);
System.out.println(&quot;Event Channel ID: &quot; + id);
+
System.out.println("Event Channel ID: " + id);
  
 
// Register for event.tag.report
 
// Register for event.tag.report
if (!readerManager.eventsRegister(id, &quot;event.tag.report&quot;))
+
if (!readerManager.eventsRegister(id, "event.tag.report"))
throw new Exception(&quot;Failure to register for event: &quot;
+
throw new Exception("Failure to register for event: "
 
+ readerManager.getLastErrorMessage());
 
+ readerManager.getLastErrorMessage());
System.out.println(&quot;Registered for event.tag.report&quot;);
+
System.out.println("Registered for event.tag.report");
  
 
// Set operating mode to active
 
// Set operating mode to active
Line 107: Line 106:
 
setupManager
 
setupManager
 
.setOperatingMode(SetupManager.OPERATING_MODE_TYPES.ACTIVE);
 
.setOperatingMode(SetupManager.OPERATING_MODE_TYPES.ACTIVE);
System.out.println(&quot;Operating Mode: Active for 5 seconds&quot;);
+
System.out.println("Operating Mode: Active for 5 seconds");
  
 
// Sleep while handling tag events
 
// Sleep while handling tag events
Line 113: Line 112:
  
 
// Unregister for event.tag.report
 
// Unregister for event.tag.report
if (!readerManager.eventsUnregister(id, &quot;event.tag.report&quot;))
+
if (!readerManager.eventsUnregister(id, "event.tag.report"))
throw new Exception(&quot;Failure to unregister for event: &quot;
+
throw new Exception("Failure to unregister for event: "
 
+ readerManager.getLastErrorMessage());
 
+ readerManager.getLastErrorMessage());
System.out.println(&quot;Unregistered for event.tag.report&quot;);
+
System.out.println("Unregistered for event.tag.report");
  
 
// Set operating mode to standby
 
// Set operating mode to standby
 
setupManager
 
setupManager
 
.setOperatingMode(SetupManager.OPERATING_MODE_TYPES.STANDBY);
 
.setOperatingMode(SetupManager.OPERATING_MODE_TYPES.STANDBY);
System.out.println(&quot;Operating Mode: Standby&quot;);
+
System.out.println("Operating Mode: Standby");
  
 
// List the TagDB
 
// List the TagDB
System.out.println(&quot;Detected Tags:&quot;);
+
System.out.println("Detected Tags:");
 
TagManager tagManager = new TagManager(dataManager);
 
TagManager tagManager = new TagManager(dataManager);
TagResponseList tagList = tagManager.dbGet(&quot;&quot;, &quot;&quot;);
+
TagResponseList tagList = tagManager.dbGet("", "");
  
 
TagResponse tagResponse;
 
TagResponse tagResponse;
for (int tagResponseID = 0; tagResponseID &lt; tagList.getCount(); tagResponseID++) {
+
for (int tagResponseID = 0; tagResponseID < tagList.getCount(); tagResponseID++) {
 
tagResponse = tagList.getTagResponse(tagResponseID);
 
tagResponse = tagList.getTagResponse(tagResponseID);
System.out.println(&quot;TagID: &quot; + tagResponse.getTagId());
+
System.out.println("TagID: " + tagResponse.getTagId());
System.out.println(&quot;\tAntenna: &quot; + tagResponse.getAntenna());
+
System.out.println("\tAntenna: " + tagResponse.getAntenna());
System.out.println(&quot;\tCount: &quot; + tagResponse.getRepeat());
+
System.out.println("\tCount: " + tagResponse.getRepeat());
System.out.println(&quot;\t1st Time: &quot; + tagResponse.getFirst());
+
System.out.println("\t1st Time: " + tagResponse.getFirst());
System.out.println(&quot;\tLast Time: &quot; + tagResponse.getLast());
+
System.out.println("\tLast Time: " + tagResponse.getLast());
  
 
}
 
}
Line 143: Line 142:
 
readerManager = null;
 
readerManager = null;
 
dataManager.close();
 
dataManager.close();
System.out.println(&quot;Connection Closed&quot;);
+
System.out.println("Connection Closed");
  
 
// Output the time to execute application
 
// Output the time to execute application
 
long endTime = System.currentTimeMillis();
 
long endTime = System.currentTimeMillis();
 
long t = endTime - startTime - 500;
 
long t = endTime - startTime - 500;
System.out.println(&quot;Estimated Time: &quot; + t + &quot;(ms)&quot;);
+
System.out.println("Estimated Time: " + t + "(ms)");
 
} catch (Exception e) {
 
} catch (Exception e) {
System.out.println(&quot;Error: &quot; + e.getMessage());
+
System.out.println("Error: " + e.getMessage());
 
}
 
}
 
}
 
}
Line 165: Line 164:
 
// Create SiritClient object
 
// Create SiritClient object
 
SiritClientRAPID scApp;
 
SiritClientRAPID scApp;
if (args.length &gt; 0)
+
if (args.length > 0)
 
scApp = new SiritClientRAPID(args[0]);
 
scApp = new SiritClientRAPID(args[0]);
 
else
 
else
Line 172: Line 171:
 
// Execute the application
 
// Execute the application
 
scApp.run();
 
scApp.run();
System.out.println(&quot;Exiting&quot;);
+
System.out.println("Exiting");
 
System.exit(0);
 
System.exit(0);
  
Line 183: Line 182:
 
String tagID = eventInfo
 
String tagID = eventInfo
 
.getParameter(EventInfo.EVENT_TAG_ARRIVE_PARAMS.TAG_ID);
 
.getParameter(EventInfo.EVENT_TAG_ARRIVE_PARAMS.TAG_ID);
System.out.println(&quot;Tag ID: &quot; + tagID);
+
System.out.println("Tag ID: " + tagID);
 
}
 
}
  
 
}
 
}
&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 with the use of Sirit's RAPID API.

/*
 *  SiritClientRAPID.java
 *
 *  Created:	07.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;

/**
 * This class shows how to create a program to talk to the Sirit INfinity 510 reader
 * with the use of Sirit's RAPID API.
 *
 * @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.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;


public class SiritClientRAPID implements IEventListener {

	/*
	 * IP Address of the reader
	 */
	private String ipAddress = "192.168.11.130";

	/**
	 * Constructor
	 */
	public SiritClientRAPID() {
	}

	/**
	 * Constructor - with IP Address
	 * 
	 * @param ipAddr
	 *            IP Address for the reader
	 */
	public SiritClientRAPID(String ipAddress) {
		super();
		this.ipAddress = ipAddress;
	}

	/**
	 * Executes the application
	 */
	public void run() {
		try {
			long startTime = System.currentTimeMillis();

			// Open a connection to the reader
			DataManager dataManager = new DataManager(
					DataManager.ConnectionTypes.SOCKET, ipAddress, 0);
			dataManager.open();
			System.out.println("Connection Opened");

			// Get the reader's name
			InfoManager infoManager = new InfoManager(dataManager);
			String v = infoManager.getName();
			System.out.println("Name: " + v);
			infoManager = null;

			// Login as administrator
			ReaderManager readerManager = new ReaderManager(dataManager);
			if (!readerManager.login("admin", "readeradmin"))
				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
			String id = dataManager.getEventChannel(this);
			System.out.println("Event Channel ID: " + id);

			// Register for event.tag.report
			if (!readerManager.eventsRegister(id, "event.tag.report"))
				throw new Exception("Failure to register for event: "
						+ readerManager.getLastErrorMessage());
			System.out.println("Registered for event.tag.report");

			// Set operating mode to active
			SetupManager setupManager = new SetupManager(dataManager);
			setupManager
					.setOperatingMode(SetupManager.OPERATING_MODE_TYPES.ACTIVE);
			System.out.println("Operating Mode: Active for 5 seconds");

			// Sleep while handling tag events
			Thread.sleep(5000);

			// Unregister for event.tag.report
			if (!readerManager.eventsUnregister(id, "event.tag.report"))
				throw new Exception("Failure to unregister for event: "
						+ readerManager.getLastErrorMessage());
			System.out.println("Unregistered for event.tag.report");

			// Set operating mode to standby
			setupManager
					.setOperatingMode(SetupManager.OPERATING_MODE_TYPES.STANDBY);
			System.out.println("Operating Mode: Standby");

			// List the TagDB
			System.out.println("Detected Tags:");
			TagManager tagManager = new TagManager(dataManager);
			TagResponseList tagList = tagManager.dbGet("", "");

			TagResponse tagResponse;
			for (int tagResponseID = 0; tagResponseID < tagList.getCount(); tagResponseID++) {
				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
	 * 
	 * @param args
	 *            Command line arguments first argument -IP Address of the
	 *            reader (optional)
	 */
	public static void main(String[] args) {
		// Create SiritClient object
		SiritClientRAPID scApp;
		if (args.length > 0)
			scApp = new SiritClientRAPID(args[0]);
		else
			scApp = new SiritClientRAPID();

		// Execute the application
		scApp.run();
		System.out.println("Exiting");
		System.exit(0);

	}

	/**
	 * Event Handler
	 */
	public void EventFound(Object sender, EventInfo eventInfo) {
		String tagID = eventInfo
				.getParameter(EventInfo.EVENT_TAG_ARRIVE_PARAMS.TAG_ID);
		System.out.println("Tag ID: " + tagID);
	}

}