Difference between revisions of "Using the Engine API"
From RifidiWiki
Line 1: | Line 1: | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
[[Category:developerDoc]][[Category:EmulatorDoc]] | [[Category:developerDoc]][[Category:EmulatorDoc]] | ||
− | This page describes how to create an instance of the Rifidi engine, create readers, and add and remove tags from that engine all programatically via RMI. It is a tutorial for creating a | + | This page describes how to create an instance of the Rifidi engine, create readers, and add and remove tags from that engine all programatically via RMI. It is a tutorial for creating a "hello world" Rifidi application just to show you how the engine works. In production code, however, you shouldn't run the program from the activator. |
=Download the source code= | =Download the source code= | ||
In order to use the Engine API, you will first need to download the core rifidi plugins from svn. See [[Rifidi:Source_Code|Running Rifidi from Source]] for instructions on how to do this | In order to use the Engine API, you will first need to download the core rifidi plugins from svn. See [[Rifidi:Source_Code|Running Rifidi from Source]] for instructions on how to do this | ||
Line 30: | Line 22: | ||
## Give the class a name and click finish | ## Give the class a name and click finish | ||
=Write the Hello World Application= | =Write the Hello World Application= | ||
− | + | <pre>package testproj; | |
import java.io.BufferedReader; | import java.io.BufferedReader; | ||
Line 73: | Line 65: | ||
/** The IP address of the virtual reader */ | /** The IP address of the virtual reader */ | ||
− | String READER_IP = | + | String READER_IP = "127.0.0.1"; |
/** The port of the virtual reader */ | /** The port of the virtual reader */ | ||
int READER_PORT = 20000; | int READER_PORT = 20000; | ||
− | String READER_NAME = | + | String READER_NAME = "alien_test"; |
// The GeneralReaderPropertyHolder is the object that defines a new | // The GeneralReaderPropertyHolder is the object that defines a new | ||
Line 82: | Line 74: | ||
GeneralReaderPropertyHolder readerProperties = new GeneralReaderPropertyHolder(); | GeneralReaderPropertyHolder readerProperties = new GeneralReaderPropertyHolder(); | ||
readerProperties | readerProperties | ||
− | .setReaderClassName( | + | .setReaderClassName("org.rifidi.emulator.reader.alien.module.AlienReaderModule"); |
readerProperties.setNumAntennas(2); | readerProperties.setNumAntennas(2); | ||
readerProperties.setReaderName(READER_NAME); | readerProperties.setReaderName(READER_NAME); | ||
− | readerProperties.setProperty( | + | readerProperties.setProperty("inet_address", READER_IP + ":" |
+ READER_PORT); | + READER_PORT); | ||
− | readerProperties.setProperty( | + | readerProperties.setProperty("heartbeat_address", READER_IP |
− | + | + | + ":54321"); |
readerProperties.setNumGPIs(4); | readerProperties.setNumGPIs(4); | ||
readerProperties.setNumGPOs(8); | readerProperties.setNumGPOs(8); | ||
Line 107: | Line 99: | ||
// uses the TagFactory to generate some new tags | // uses the TagFactory to generate some new tags | ||
− | ArrayList | + | ArrayList<RifidiTag> tags = TagFactory.generateTags(tagPattern); |
// Set the Unique IDs for each of the tags. This is *not* the EPC. | // Set the Unique IDs for each of the tags. This is *not* the EPC. | ||
Line 118: | Line 110: | ||
// add the tags to the virtual reader | // add the tags to the virtual reader | ||
− | readerManager.addTags(READER_NAME, 0, new HashSet | + | readerManager.addTags(READER_NAME, 0, new HashSet<RifidiTag>(tags)); |
// sleep for 500 miliseconds | // sleep for 500 miliseconds | ||
Line 124: | Line 116: | ||
/* | /* | ||
− | * The next bit of code is | + | * The next bit of code is "client" code. It is equivalent to |
* connecting to an alien reader over telnet and seding the 'get | * connecting to an alien reader over telnet and seding the 'get | ||
* taglist' command. It is only shown here to demonstrate that the | * taglist' command. It is only shown here to demonstrate that the | ||
Line 140: | Line 132: | ||
// send username and password | // send username and password | ||
System.out.println(readFromReader(in)); | System.out.println(readFromReader(in)); | ||
− | out.write( | + | out.write("alien\n"); |
out.flush(); | out.flush(); | ||
System.out.println(readFromReader(in)); | System.out.println(readFromReader(in)); | ||
Thread.sleep(500); | Thread.sleep(500); | ||
− | out.write( | + | out.write("password\n"); |
out.flush(); | out.flush(); | ||
System.out.println(readFromReader(in)); | System.out.println(readFromReader(in)); | ||
// send get taglist command | // send get taglist command | ||
− | out.write( | + | out.write("t\n"); |
out.flush(); | out.flush(); | ||
String returnVal = readFromReader(in); | String returnVal = readFromReader(in); | ||
Line 191: | Line 183: | ||
} | } | ||
− | + | </pre> | |
=Edit the Activator= | =Edit the Activator= | ||
Line 198: | Line 190: | ||
Although this is a bad idea for production code, for the sake of simplicity, the start() method in the activator for this project starts the hello world class. This is the activator for the bundle: | Although this is a bad idea for production code, for the sake of simplicity, the start() method in the activator for this project starts the hello world class. This is the activator for the bundle: | ||
− | + | <pre>package testproj; | |
import org.osgi.framework.BundleActivator; | import org.osgi.framework.BundleActivator; | ||
Line 215: | Line 207: | ||
*/ | */ | ||
public void start(BundleContext context) throws Exception { | public void start(BundleContext context) throws Exception { | ||
− | Thread t = new Thread(new TestRunner(), | + | Thread t = new Thread(new TestRunner(), "Rifidi Hello World Thread"); |
t.start(); | t.start(); | ||
} | } | ||
Line 245: | Line 237: | ||
− | + | </pre> | |
=Run the program= | =Run the program= | ||
To run this program from Eclipse, you need to create a new Run Configuration. | To run this program from Eclipse, you need to create a new Run Configuration. | ||
− | # Click Run- | + | # Click Run->Run Configurations |
# Double click on OSGi Framework on the tree on the left | # Double click on OSGi Framework on the tree on the left | ||
# Select the required bundles. The 'bundles' list displays a list of the bundles that will be activated when you run the program. You only want the bundles selected that are required. | # Select the required bundles. The 'bundles' list displays a list of the bundles that will be activated when you run the program. You only want the bundles selected that are required. | ||
Line 258: | Line 250: | ||
#Click 'Run' | #Click 'Run' | ||
− | You should see alot of output in the console, and at the bottom of the output, you should see the tags that you | + | You should see alot of output in the console, and at the bottom of the output, you should see the tags that you "client" got from the reader: |
− | + | <pre> | |
Tag:304A 37DA 04A4 4927 D1C8 0775, Disc:2008/11/19 12:09:20, Last:2008/11/19 12:09:21, Count:1, Ant:0, Proto:2 | Tag:304A 37DA 04A4 4927 D1C8 0775, Disc:2008/11/19 12:09:20, Last:2008/11/19 12:09:21, Count:1, Ant:0, Proto:2 | ||
Tag:3008 26FD CCAC 1E53 612D 56EB, Disc:2008/11/19 12:09:20, Last:2008/11/19 12:09:21, Count:1, Ant:0, Proto:2 | Tag:3008 26FD CCAC 1E53 612D 56EB, Disc:2008/11/19 12:09:20, Last:2008/11/19 12:09:21, Count:1, Ant:0, Proto:2 | ||
Line 265: | Line 257: | ||
Tag:307A 11F3 960A B58C 7454 B248, Disc:2008/11/19 12:09:20, Last:2008/11/19 12:09:21, Count:1, Ant:0, Proto:2 | Tag:307A 11F3 960A B58C 7454 B248, Disc:2008/11/19 12:09:20, Last:2008/11/19 12:09:21, Count:1, Ant:0, Proto:2 | ||
Tag:308C 5BD0 BA14 CB02 55D4 89D5, Disc:2008/11/19 12:09:20, Last:2008/11/19 12:09:21, Count:1, Ant:0, Proto:2 | Tag:308C 5BD0 BA14 CB02 55D4 89D5, Disc:2008/11/19 12:09:20, Last:2008/11/19 12:09:21, Count:1, Ant:0, Proto:2 | ||
− | + | </pre> |
Latest revision as of 19:43, 26 November 2010
This page describes how to create an instance of the Rifidi engine, create readers, and add and remove tags from that engine all programatically via RMI. It is a tutorial for creating a "hello world" Rifidi application just to show you how the engine works. In production code, however, you shouldn't run the program from the activator.
Contents
Download the source code
In order to use the Engine API, you will first need to download the core rifidi plugins from svn. See Running Rifidi from Source for instructions on how to do this
Create a new OSGi bundle
Because Rifidi is OSGi based, all code must be written inside an OSGi bundle. The thing you will need to create a new OSGi bundle for your code to run in. To do this, follow the same steps as in Create a new reader project. You do not need to add the project to svn.
For this example, you will want to declare dependencies on the following plugins:
- org.rifidi.emulator
- org.rifidi.services
- org.rifidi.tags
- org.rifidi.emulator.reader.alien
Create a program
- Create a new package.
- Right click on the package found in the src directory.
- Select new package
- Give the package a name and select Finish
- Create a new class
- Right Click on the new package
- Select new class
- Give the class a name and click finish
Write the Hello World Application
package testproj; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.util.ArrayList; import java.util.HashSet; import org.rifidi.emulator.manager.ReaderManager; import org.rifidi.emulator.reader.module.GeneralReaderPropertyHolder; import org.rifidi.services.annotations.Inject; import org.rifidi.services.registry.ServiceRegistry; import org.rifidi.tags.enums.TagGen; import org.rifidi.tags.factory.TagCreationPattern; import org.rifidi.tags.factory.TagFactory; import org.rifidi.tags.id.TagType; import org.rifidi.tags.impl.RifidiTag; /** * This class demonstrates how to use the Rifidi Engine RMI API to create a * virtual reader and add tags to it. * * @author Kyle Neumeier - kyle@pramari.com * */ public class RifidiClass { /** * The reader manager - the service that controls the virtual RFID readers */ private ReaderManager readerManager; public RifidiClass() { // tell the service registry to inject dependencies into this object ServiceRegistry.getInstance().service(this); } private void startReader() { try { /** The IP address of the virtual reader */ String READER_IP = "127.0.0.1"; /** The port of the virtual reader */ int READER_PORT = 20000; String READER_NAME = "alien_test"; // The GeneralReaderPropertyHolder is the object that defines a new // virtual reader to create GeneralReaderPropertyHolder readerProperties = new GeneralReaderPropertyHolder(); readerProperties .setReaderClassName("org.rifidi.emulator.reader.alien.module.AlienReaderModule"); readerProperties.setNumAntennas(2); readerProperties.setReaderName(READER_NAME); readerProperties.setProperty("inet_address", READER_IP + ":" + READER_PORT); readerProperties.setProperty("heartbeat_address", READER_IP + ":54321"); readerProperties.setNumGPIs(4); readerProperties.setNumGPOs(8); // create the virtual reader on the remote engine readerManager.createReader(readerProperties); // turn the reader on so that you can connect to it from a client // (e.g. telnet) readerManager.start(READER_NAME); // The TagCreationPattern is the object that defines how to create a // batch of tags TagCreationPattern tagPattern = new TagCreationPattern(); tagPattern.setNumberOfTags(5); tagPattern.setTagGeneration(TagGen.GEN2); tagPattern.setTagType(TagType.SGTIN96); // uses the TagFactory to generate some new tags ArrayList<RifidiTag> tags = TagFactory.generateTags(tagPattern); // Set the Unique IDs for each of the tags. This is *not* the EPC. // The UniqueID is the ID used internally to differentiate between // tags. long id = 1; for (RifidiTag tag : tags) { tag.setTagEntitiyID(id++); } // add the tags to the virtual reader readerManager.addTags(READER_NAME, 0, new HashSet<RifidiTag>(tags)); // sleep for 500 miliseconds Thread.sleep(500); /* * The next bit of code is "client" code. It is equivalent to * connecting to an alien reader over telnet and seding the 'get * taglist' command. It is only shown here to demonstrate that the * tags that reader has been turned on and tags were added * successfully. */ // create a new socket connection to the reader Socket connection = new Socket(READER_IP, READER_PORT); BufferedReader in = new BufferedReader(new InputStreamReader( connection.getInputStream())); PrintWriter out = new PrintWriter(connection.getOutputStream()); // send username and password System.out.println(readFromReader(in)); out.write("alien\n"); out.flush(); System.out.println(readFromReader(in)); Thread.sleep(500); out.write("password\n"); out.flush(); System.out.println(readFromReader(in)); // send get taglist command out.write("t\n"); out.flush(); String returnVal = readFromReader(in); // print tag list coming back from reader System.out.println(returnVal); } catch (Exception e) { e.printStackTrace(); } } /** * This method reads from the socket. * * @param inBuf * @return The string that was read * @throws IOException */ public String readFromReader(BufferedReader inBuf) throws IOException { StringBuffer buf = new StringBuffer(); int ch = inBuf.read(); while ((char) ch != '\0') { buf.append((char) ch); ch = inBuf.read(); } return buf.toString(); } /** * This method is called by the service registry to inject the ReaderManager * * @param readerManager */ @Inject public void setReaderManager(ReaderManager readerManager) { this.readerManager = readerManager; // start the program now that we know the readerManager has been // injected. startReader(); } }
Edit the Activator
The Activator is the class that the OSGi framework uses to start and stop the bundle that you wrote. The start() method is called when your bundle is activated, and the stop() method is called when the bundle is deactivated. For an introduction to OSGi see this article.
Although this is a bad idea for production code, for the sake of simplicity, the start() method in the activator for this project starts the hello world class. This is the activator for the bundle:
package testproj; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import testproj.RifidiClass; public class Activator implements BundleActivator { /* * (non-Javadoc) * * @see * org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext * ) */ public void start(BundleContext context) throws Exception { Thread t = new Thread(new TestRunner(), "Rifidi Hello World Thread"); t.start(); } /* * (non-Javadoc) * * @see * org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) */ public void stop(BundleContext context) throws Exception { } /** * Run the program in a separate thread so we don't occupy the OSGi starter * thread. * * @author Kyle Neumeier - kyle@pramari.com * */ private class TestRunner implements Runnable { @Override public void run() { new RifidiClass(); } } }
Run the program
To run this program from Eclipse, you need to create a new Run Configuration.
- Click Run->Run Configurations
- Double click on OSGi Framework on the tree on the left
- Select the required bundles. The 'bundles' list displays a list of the bundles that will be activated when you run the program. You only want the bundles selected that are required.
- Click 'Deselct All'
- Check the bundle that you just created.
- Check all the bundles that start with 'org.springframework...'
- Select 'Add Required Bundles'
- Click 'Run'
You should see alot of output in the console, and at the bottom of the output, you should see the tags that you "client" got from the reader:
Tag:304A 37DA 04A4 4927 D1C8 0775, Disc:2008/11/19 12:09:20, Last:2008/11/19 12:09:21, Count:1, Ant:0, Proto:2 Tag:3008 26FD CCAC 1E53 612D 56EB, Disc:2008/11/19 12:09:20, Last:2008/11/19 12:09:21, Count:1, Ant:0, Proto:2 Tag:302E 5243 7D73 9CEF 1E8A 08FA, Disc:2008/11/19 12:09:20, Last:2008/11/19 12:09:21, Count:1, Ant:0, Proto:2 Tag:307A 11F3 960A B58C 7454 B248, Disc:2008/11/19 12:09:20, Last:2008/11/19 12:09:21, Count:1, Ant:0, Proto:2 Tag:308C 5BD0 BA14 CB02 55D4 89D5, Disc:2008/11/19 12:09:20, Last:2008/11/19 12:09:21, Count:1, Ant:0, Proto:2