Difference between revisions of "Sirit Example Client RAPID"
From RifidiWiki
Line 1: | Line 1: | ||
− | |||
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> | |
/* | /* | ||
* SiritClientRAPID.java | * SiritClientRAPID.java | ||
Line 47: | Line 46: | ||
* IP Address of the reader | * IP Address of the reader | ||
*/ | */ | ||
− | private String ipAddress = | + | 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( | + | 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( | + | 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( | + | if (!readerManager.login("admin", "readeradmin")) |
− | throw new Exception( | + | throw new Exception("Login attempt failed: " |
+ readerManager.getLastErrorMessage()); | + readerManager.getLastErrorMessage()); | ||
v = readerManager.whoAmI(); | v = readerManager.whoAmI(); | ||
− | System.out.println( | + | 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( | + | System.out.println("Event Channel ID: " + id); |
// Register for event.tag.report | // Register for event.tag.report | ||
− | if (!readerManager.eventsRegister(id, | + | if (!readerManager.eventsRegister(id, "event.tag.report")) |
− | throw new Exception( | + | throw new Exception("Failure to register for event: " |
+ readerManager.getLastErrorMessage()); | + readerManager.getLastErrorMessage()); | ||
− | System.out.println( | + | 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( | + | 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, | + | if (!readerManager.eventsUnregister(id, "event.tag.report")) |
− | throw new Exception( | + | throw new Exception("Failure to unregister for event: " |
+ readerManager.getLastErrorMessage()); | + readerManager.getLastErrorMessage()); | ||
− | System.out.println( | + | 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( | + | System.out.println("Operating Mode: Standby"); |
// List the TagDB | // List the TagDB | ||
− | System.out.println( | + | System.out.println("Detected Tags:"); |
TagManager tagManager = new TagManager(dataManager); | TagManager tagManager = new TagManager(dataManager); | ||
− | TagResponseList tagList = tagManager.dbGet( | + | TagResponseList tagList = tagManager.dbGet("", ""); |
TagResponse tagResponse; | TagResponse tagResponse; | ||
− | for (int tagResponseID = 0; tagResponseID | + | for (int tagResponseID = 0; tagResponseID < tagList.getCount(); tagResponseID++) { |
tagResponse = tagList.getTagResponse(tagResponseID); | tagResponse = tagList.getTagResponse(tagResponseID); | ||
− | System.out.println( | + | System.out.println("TagID: " + tagResponse.getTagId()); |
− | System.out.println( | + | System.out.println("\tAntenna: " + tagResponse.getAntenna()); |
− | System.out.println( | + | System.out.println("\tCount: " + tagResponse.getRepeat()); |
− | System.out.println( | + | System.out.println("\t1st Time: " + tagResponse.getFirst()); |
− | System.out.println( | + | System.out.println("\tLast Time: " + tagResponse.getLast()); |
} | } | ||
Line 143: | Line 142: | ||
readerManager = null; | readerManager = null; | ||
dataManager.close(); | dataManager.close(); | ||
− | System.out.println( | + | 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( | + | System.out.println("Estimated Time: " + t + "(ms)"); |
} catch (Exception e) { | } catch (Exception e) { | ||
− | System.out.println( | + | System.out.println("Error: " + e.getMessage()); |
} | } | ||
} | } | ||
Line 165: | Line 164: | ||
// Create SiritClient object | // Create SiritClient object | ||
SiritClientRAPID scApp; | SiritClientRAPID scApp; | ||
− | if (args.length | + | 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( | + | 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( | + | System.out.println("Tag ID: " + tagID); |
} | } | ||
} | } | ||
− | + | </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); } }